Enable dynamic filter expressions for QuestionAnswerAdvisor

- Add FILTER_EXPRESSION advisor context parameter to update filter expressions per call/stream
 - Implement dynamic filter expression handling in QuestionAnswerAdvisor
 - Add unit tests for dynamic filter expression functionality
 - Update documentation with usage examples

 Resolves #887
This commit is contained in:
Christian Tzolov
2024-06-18 14:32:32 +02:00
parent 1fd43c84b3
commit 3cbda5acd8
3 changed files with 157 additions and 5 deletions

View File

@@ -183,8 +183,6 @@ After specifying the `stream` method on `ChatClient`, there are a few options fo
Creating a ChatClient with default system text in an `@Configuration` class simplifies runtime code.
By setting defaults, you only need to specify user text when calling `ChatClient`, eliminating the need to set system text for each request in your runtime code path.
=== Default System Text
In the following example, we will configure the system text to always reply in a pirate's voice.
@@ -346,6 +344,27 @@ ChatResponse response = ChatClient.builder(chatModel)
Is this example, the `SearchRequest.defaults()` will perform a similarity search over all documents in the Vector Database.
To restrict the types of documents that are searched, the `SearchRequest` takes a SQL like filter expression that is portable across all `VectorStores`.
==== Rutntime filter expressions
You can update the default `SearchRequest` filter expression at run time using the `FILTER_EXRESSION` advisor context parameter:
[source,java]
----
var chatClient = ChatClient.builder(chatModel)
.defaultSystem("Default system text.")
.defaultAdvisors(new QuestionAnswerAdvisor(vectorStore,
SearchRequest.defaults().withSimilarityThreshold(0.99d).withTopK(6)))
.build();
// and at runtime use the `FILTER_EXRESSION` advisor context parameter to update the filter expression
var content = chatClient.prompt()
.user("Please answer my question XYZ")
.advisors(a -> a.param(QuestionAnswerAdvisor.FILTER_EXRESSION, "type == 'Spring'"))
.call()
.content();
----
=== Chat Memory
The interface `ChatMemory` represents a storage for chat conversation history. It provides methods to add messages to a