You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit enhances the documentation for working with multiple chat models in Spring AI by:
- Add introduction explaining common scenarios where multiple chat models are useful.
- Restructuring the content into clear subsections:
- Multiple ChatClients with a Single Model Type
- ChatClients for Different Model Types
- Multiple OpenAI-Compatible API Endpoints
Signed-off-by: Mark Pollack <mark.pollack@broadcom.com>
In this simple example, the user input sets the contents of the user message.
48
48
The `call()` method sends a request to the AI model, and the `content()` method returns the AI model's response as a `String`.
49
49
50
-
=== Create a ChatClient programmatically
50
+
=== Working with Multiple Chat Models
51
51
52
-
You can disable the `ChatClient.Builder` autoconfiguration by setting the property `spring.ai.chat.client.enabled=false`.
53
-
This is useful if multiple chat models are used together.
54
-
Then, create a `ChatClient.Builder` instance programmatically for every `ChatModel` you need:
52
+
There are several scenarios where you might need to work with multiple chat models in a single application:
53
+
54
+
* Using different models for different types of tasks (e.g., a powerful model for complex reasoning and a faster, cheaper model for simpler tasks)
55
+
* Implementing fallback mechanisms when one model service is unavailable
56
+
* A/B testing different models or configurations
57
+
* Providing users with a choice of models based on their preferences
58
+
* Combining specialized models (one for code generation, another for creative content, etc.)
59
+
60
+
By default, Spring AI autoconfigures a single `ChatClient.Builder` bean. However, you may need to work with multiple chat models in your application. Here's how to handle this scenario:
61
+
62
+
In all cases, you need to disable the `ChatClient.Builder` autoconfiguration by setting the property `spring.ai.chat.client.enabled=false`.
63
+
64
+
This allows you to create multiple `ChatClient` instances manually.
65
+
66
+
==== Multiple ChatClients with a Single Model Type
67
+
68
+
This section covers a common use case where you need to create multiple ChatClient instances that all use the same underlying model type but with different configurations.
69
+
70
+
[source,java]
71
+
----
72
+
// Create ChatClient instances programmatically
73
+
ChatModel myChatModel = ... // already autoconfigured by Spring Boot
// or create a ChatClient with the default builder settings:
155
+
The `OpenAiApi` and `OpenAiChatModel` classes provide a `mutate()` method that allows you to create variations of existing instances with different properties. This is particularly useful when you need to work with multiple OpenAI-compatible APIs.
The `ChatClient` fluent API allows you to create a prompt in three distinct ways using an overloaded `prompt` method to initiate the fluent API:
@@ -407,72 +554,6 @@ In this configuration, the `MessageChatMemoryAdvisor` will be executed first, ad
407
554
408
555
xref:ROOT:api/retrieval-augmented-generation.adoc#_questionansweradvisor[Learn about Question Answer Advisor]
409
556
410
-
TIP: Refer to the xref:api/chat-memory.adoc[Chat Memory] documentation for more information on how to use the `ChatMemory` interface to manage conversation history in combination with the advisors.
411
-
412
-
The following advisor implementations use the `ChatMemory` interface to advice the prompt with conversation history which differ in the details of how the memory is added to the prompt
413
-
414
-
* `MessageChatMemoryAdvisor` : Memory is retrieved and added as a collection of messages to the prompt
415
-
* `PromptChatMemoryAdvisor` : Memory is retrieved and added into the prompt's system text.
416
-
* `VectorStoreChatMemoryAdvisor` : The constructor `VectorStoreChatMemoryAdvisor(VectorStore vectorStore, String defaultConversationId, int chatHistoryWindowSize, int order)` This constructor allows you to:
417
-
418
-
. Specify the VectorStore instance used for managing and querying documents.
419
-
. Set a default conversation ID to be used if none is provided in the context.
420
-
. Define the window size for chat history retrieval in terms of token size.
421
-
. Provide system text advice used for the chat advisor system.
422
-
. Set the order of precedence for this advisor in the chain.
423
-
424
-
425
-
The `VectorStoreChatMemoryAdvisor.builder()` method lets you specify the default conversation ID, the chat history window size, and the order of the chat history to be retrieved.
426
-
427
-
A sample `@Service` implementation that uses several advisors is shown below.
0 commit comments