spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/index.html
Apache License 2.0
3.35k stars 866 forks source link

VectorStoreChatMemoryAdvisor is not working correctly with PGvector #800

Closed MarkKorenevskiy closed 2 months ago

MarkKorenevskiy commented 6 months ago

Bug description When using PGvector as vector store for VectorStoreChatMemoryAdvisor to save conversation history, there is org.postgresql.util.PSQLException "ERROR: syntax error at or near "conversationId"". The problem happens because in adviseRequest method in VectorStoreChatMemoryAdvisor single quotes added around DOCUMENT_METADATA_CONVERSATION_ID, which causes an error when parse it in JDBCTemplate.

var searchRequest = SearchRequest.query(request.userText())
            .withTopK(this.doGetChatMemoryRetrieveSize(context))
            .withFilterExpression(
                    "'" + DOCUMENT_METADATA_CONVERSATION_ID + "'=='" + this.doGetConversationId(context) + "'");

Environment SpringAI - 1.0.0-M1 SpringBoot - 3.2.6 Java - 21

Steps to reproduce

  1. Create simple application with org.springframework.ai:spring-ai-openai-spring-boot-starter and org.springframework.ai:spring-ai-pgvector-store-spring-boot-starter dependencies
  2. Configure PGvector as instructed in [https://docs.spring.io/spring-ai/reference/api/vectordbs/pgvector.html#_run_postgres_pgvector_db_locally](PGvector docs)
  3. Create new ChatClient bean with VectorStoreChatMemoryAdvisor default advisor and provide advisor with autowired vector store
  4. Try to prompt created chatClient. When VectorStoreChatMemoryAdvisor tries to do similarity search, exception will be thrown.

Expected behavior Return conversation history if any present in PGvector

Custom solution I extended from VectorStoreChatMemoryAdvisor, copiedadviseRequest method and manually removed this single quotes around DOCUMENT_METADATA_CONVERSATION_ID. It completely worked for me.

alwyngo commented 3 months ago

var searchRequest = SearchRequest.query(request.userText()) .withTopK(this.doGetChatMemoryRetrieveSize(context)) .withFilterExpression( " + DOCUMENT_METADATA_CONVERSATION_ID + "=='" + this.doGetConversationId(context) + "'");

this works, remove the ' around DOCUMENT_METADATA_CONVERSATION_ID

LiveNathan commented 3 months ago

copiedadviseRequest method

@MarkKorenevskiy Could you please share the entire method here? I'm trying to do the same fix, but there are some private fields in VectorStoreChatMemoryAdvisor so I'm wondering how you got access to those.