modelcall and chatclient rework
This commit is contained in:
committed by
Christian Tzolov
parent
2abf10dbf9
commit
c4bfb5709b
@@ -2,7 +2,7 @@
|
||||
|
||||
link:https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html[Amazon Bedrock] is a managed service that provides foundation models from various AI providers, available through a unified API.
|
||||
|
||||
Spring AI supports https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids-arns.html[all the Chat and Embedding AI models] available through Amazon Bedrock by implementing the Spring interfaces `ChatClient`, `StreamingChatClient`, and `EmbeddingClient`.
|
||||
Spring AI supports https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids-arns.html[all the Chat and Embedding AI models] available through Amazon Bedrock by implementing the Spring interfaces `ModelCall`, `StreamingChatClient`, and `EmbeddingClient`.
|
||||
|
||||
Additionally, Spring AI provides Spring Auto-Configurations and Boot Starters for all clients, making it easy to bootstrap and configure for the Bedrock models.
|
||||
|
||||
|
||||
@@ -132,7 +132,7 @@ TIP: In addition to the model specific https://github.com/spring-projects/spring
|
||||
|
||||
== Function Calling
|
||||
|
||||
You can register custom Java functions with the `AnthropicChatClient` and have the Anthropic Claude model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
You can register custom Java functions with the `AnthropicModelCall` and have the Anthropic Claude model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
This is a powerful technique to connect the LLM capabilities with external tools and APIs.
|
||||
Read more about xref:api/chat/functions/anthropic-chat-functions.adoc[Anthropic Function Calling].
|
||||
|
||||
@@ -194,7 +194,7 @@ spring.ai.anthropic.chat.options.max-tokens=450
|
||||
|
||||
TIP: replace the `api-key` with your Anthropic credentials.
|
||||
|
||||
This will create a `AnthropicChatClient` implementation that you can inject into your class.
|
||||
This will create a `AnthropicModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -224,7 +224,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/AnthropicChatClient.java[AnthropicChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Anthropic service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-anthropic/src/main/java/org/springframework/ai/anthropic/AnthropicChatClient.java[AnthropicChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Anthropic service.
|
||||
|
||||
Add the `spring-ai-anthropic` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
@@ -247,7 +247,7 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
Next, create a `AnthropicChatClient` and use it for text generations:
|
||||
Next, create a `AnthropicModelCall` and use it for text generations:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
|
||||
@@ -88,7 +88,7 @@ The prefix `spring.ai.azure.openai` is the property prefix to configure the conn
|
||||
| spring.ai.azure.openai.endpoint | The endpoint from the Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
|====
|
||||
|
||||
The prefix `spring.ai.azure.openai.chat` is the property prefix that configures the `ChatClient` implementation for Azure OpenAI.
|
||||
The prefix `spring.ai.azure.openai.chat` is the property prefix that configures the `ModelCall` implementation for Azure OpenAI.
|
||||
|
||||
[cols="3,5,3"]
|
||||
|====
|
||||
@@ -157,7 +157,7 @@ spring.ai.azure.openai.chat.options.temperature=0.7
|
||||
|
||||
TIP: replace the `api-key` and `endpoint` with your Azure OpenAI credentials.
|
||||
|
||||
This will create a `AzureOpenAiChatClient` implementation that you can inject into your class.
|
||||
This will create a `AzureOpenAiModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
|
||||
@@ -188,7 +188,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-azure-openai/src/main/java/org/springframework/ai/azure/openai/AzureOpenAiChatClient.java[AzureOpenAiChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the link:https://learn.microsoft.com/en-us/java/api/overview/azure/ai-openai-readme?view=azure-java-preview[Azure OpenAI Java Client].
|
||||
The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-azure-openai/src/main/java/org/springframework/ai/azure/openai/AzureOpenAiChatClient.java[AzureOpenAiChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the link:https://learn.microsoft.com/en-us/java/api/overview/azure/ai-openai-readme?view=azure-java-preview[Azure OpenAI Java Client].
|
||||
|
||||
To enable it, add the `spring-ai-azure-openai` dependency to your project's Maven `pom.xml` file:
|
||||
[source, xml]
|
||||
@@ -210,9 +210,9 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
TIP: The `spring-ai-azure-openai` dependency also provide the access to the `AzureOpenAiChatClient`. For more information about the `AzureOpenAiChatClient` refer to the link:../chat/azure-openai-chat.html[Azure OpenAI Chat] section.
|
||||
TIP: The `spring-ai-azure-openai` dependency also provide the access to the `AzureOpenAiModelCall`. For more information about the `AzureOpenAiModelCall` refer to the link:../chat/azure-openai-chat.html[Azure OpenAI Chat] section.
|
||||
|
||||
Next, create an `AzureOpenAiChatClient` instance and use it to generate text responses:
|
||||
Next, create an `AzureOpenAiModelCall` instance and use it to generate text responses:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
|
||||
@@ -138,7 +138,7 @@ spring.ai.bedrock.anthropic.chat.options.top-k=15
|
||||
|
||||
TIP: replace the `regions`, `access-key` and `secret-key` with your AWS credentials.
|
||||
|
||||
This will create a `BedrockAnthropicChatClient` implementation that you can inject into your class.
|
||||
This will create a `BedrockAnthropicModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -168,7 +168,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic/BedrockAnthropicChatClient.java[BedrockAnthropicChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Anthropic service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic/BedrockAnthropicChatClient.java[BedrockAnthropicChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Anthropic service.
|
||||
|
||||
Add the `spring-ai-bedrock` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
|
||||
@@ -179,7 +179,7 @@ spring.ai.bedrock.anthropic3.chat.options.top-k=15
|
||||
|
||||
TIP: replace the `regions`, `access-key` and `secret-key` with your AWS credentials.
|
||||
|
||||
This will create a `BedrockAnthropicChatClient` implementation that you can inject into your class.
|
||||
This will create a `BedrockAnthropicModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -209,7 +209,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/BedrockAnthropic3ChatClient.java[BedrockAnthropic3ChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Anthropic service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/anthropic3/BedrockAnthropic3ChatClient.java[BedrockAnthropic3ChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Anthropic service.
|
||||
|
||||
Add the `spring-ai-bedrock` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ spring.ai.bedrock.cohere.chat.options.temperature=0.8
|
||||
|
||||
TIP: replace the `regions`, `access-key` and `secret-key` with your AWS credentials.
|
||||
|
||||
This will create a `BedrockCohereChatClient` implementation that you can inject into your class.
|
||||
This will create a `BedrockCohereModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -160,7 +160,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/cohere/BedrockCohereChatClient.java[BedrockCohereChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Cohere service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/cohere/BedrockCohereChatClient.java[BedrockCohereChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Cohere service.
|
||||
|
||||
Add the `spring-ai-bedrock` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
|
||||
@@ -123,7 +123,7 @@ spring.ai.bedrock.jurassic2.chat.options.temperature=0.8
|
||||
|
||||
TIP: replace the `regions`, `access-key` and `secret-key` with your AWS credentials.
|
||||
|
||||
This will create a `BedrockAi21Jurassic2ChatClient` implementation that you can inject into your class.
|
||||
This will create a `BedrockAi21Jurassic2ModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -148,7 +148,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/jurassic2/BedrockAi21Jurassic2ChatClient.java[BedrockAi21Jurassic2ChatClient] implements the `ChatClient` uses the <<low-level-api>> to connect to the Bedrock Jurassic-2 service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/jurassic2/BedrockAi21Jurassic2ChatClient.java[BedrockAi21Jurassic2ChatClient] implements the `ModelCall` uses the <<low-level-api>> to connect to the Bedrock Jurassic-2 service.
|
||||
|
||||
Add the `spring-ai-bedrock` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ spring.ai.bedrock.llama.chat.options.temperature=0.8
|
||||
|
||||
TIP: replace the `regions`, `access-key` and `secret-key` with your AWS credentials.
|
||||
|
||||
This will create a `BedrockLlamaChatClient` implementation that you can inject into your class.
|
||||
This will create a `BedrockLlamaModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -158,7 +158,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/llama/BedrockLlamaChatClient.java[BedrockLlamaChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Anthropic service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/llama/BedrockLlamaChatClient.java[BedrockLlamaChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Anthropic service.
|
||||
|
||||
Add the `spring-ai-bedrock` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ spring.ai.bedrock.titan.chat.options.temperature=0.8
|
||||
|
||||
TIP: replace the `regions`, `access-key` and `secret-key` with your AWS credentials.
|
||||
|
||||
This will create a `BedrockTitanChatClient` implementation that you can inject into your class.
|
||||
This will create a `BedrockTitanModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -156,7 +156,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/titan/BedrockTitanChatClient.java[BedrockTitanChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Titanic service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/titan/BedrockTitanChatClient.java[BedrockTitanChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Bedrock Titanic service.
|
||||
|
||||
Add the `spring-ai-bedrock` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
= Anthropic Function Calling
|
||||
|
||||
You can register custom Java functions with the `AnthropicChatClient` and have the Anthropic models intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
You can register custom Java functions with the `AnthropicModelCall` and have the Anthropic models intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
This allows you to connect the LLM capabilities with external tools and APIs.
|
||||
The `claude-3-opus`, `claude-3-sonnet` and `claude-3-haiku` link:https://docs.anthropic.com/claude/docs/tool-use#tool-use-best-practices-and-limitations[models are trained to detect when a function should be called] and to respond with JSON that adheres to the function signature.
|
||||
|
||||
@@ -15,7 +15,7 @@ The `description` helps the model to understand when to call the function.
|
||||
As a developer, you need to implement a function that takes the function call arguments sent from the AI model, and respond with the result back to the model.
|
||||
Your function can in turn invoke other 3rd party services to provide the results.
|
||||
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ChatClient`.
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ModelCall`.
|
||||
|
||||
Under the hood, Spring wraps your POJO (the function) with the appropriate adapter code that enables interaction with the AI Model, saving you from writing tedious boilerplate code.
|
||||
The basis of the underlying infrastructure is the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallback.java[FunctionCallback.java] interface and the companion link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallbackWrapper.java[FunctionCallbackWrapper.java] utility class to simplify the implementation and registration of Java callback functions.
|
||||
@@ -70,7 +70,7 @@ We start with describing the most POJO friendly options.
|
||||
|
||||
In this approach you define `@Beans` in your application context as you would any other Spring managed object.
|
||||
|
||||
Internally, Spring AI `ChatClient` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
Internally, Spring AI `ModelCall` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
The name of the `@Bean` is passed as a `ChatOption`.
|
||||
|
||||
|
||||
@@ -136,7 +136,7 @@ static class Config {
|
||||
}
|
||||
----
|
||||
|
||||
It wraps the 3rd party `MockWeatherService` function and registers it as a `CurrentWeather` function with the `AnthropicChatClient`.
|
||||
It wraps the 3rd party `MockWeatherService` function and registers it as a `CurrentWeather` function with the `AnthropicModelCall`.
|
||||
It also provides a description (2) and an optional response converter (3) to convert the response into a text as expected by the model.
|
||||
|
||||
NOTE: By default, the response converter does a JSON serialization of the Response object.
|
||||
@@ -159,7 +159,7 @@ ChatResponse response = chatClient.call(new Prompt(List.of(userMessage),
|
||||
logger.info("Response: {}", response);
|
||||
----
|
||||
|
||||
// NOTE: You can can have multiple functions registered in your `ChatClient` but only those enabled in the prompt request will be considered for the function calling.
|
||||
// NOTE: You can can have multiple functions registered in your `ModelCall` but only those enabled in the prompt request will be considered for the function calling.
|
||||
|
||||
Above user question will trigger 3 calls to `CurrentWeather` function (one for each city) and produce the final response.
|
||||
|
||||
@@ -187,5 +187,5 @@ NOTE: The in-prompt registered functions are enabled by default for the duration
|
||||
|
||||
This approach allows to dynamically chose different functions to be called based on the user input.
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/anthropic/tool/FunctionCallWithPromptFunctionIT.java[FunctionCallWithPromptFunctionIT.java] integration test provides a complete example of how to register a function with the `AnthropicChatClient` and use it in a prompt request.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/anthropic/tool/FunctionCallWithPromptFunctionIT.java[FunctionCallWithPromptFunctionIT.java] integration test provides a complete example of how to register a function with the `AnthropicModelCall` and use it in a prompt request.
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Function calling lets developers create a description of a function in their code, then pass that description to a language model in a request. The response from the model includes the name of a function that matches the description and the arguments to call it with.
|
||||
|
||||
You can register custom Java functions with the `AzureOpenAiChatClient` and have the model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
You can register custom Java functions with the `AzureOpenAiModelCall` and have the model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
This allows you to connect the LLM capabilities with external tools and APIs.
|
||||
The Azure models are trained to detect when a function should be called and to respond with JSON that adheres to the function signature.
|
||||
|
||||
@@ -16,7 +16,7 @@ In general, the custom functions need to provide a function `name`, `description
|
||||
As a developer, you need to implement a function that takes the function call arguments sent from the AI model, and respond with the result back to the model.
|
||||
Your function can in turn invoke other 3rd party services to provide the results.
|
||||
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ChatClient`.
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ModelCall`.
|
||||
|
||||
Under the hood, Spring wraps your POJO (the function) with the appropriate adapter code that enables interaction with the AI Model, saving you from writing tedious boilerplate code.
|
||||
The basis of the underlying infrastructure is the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallback.java[FunctionCallback.java] interface and the companion link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallbackWrapper.java[FunctionCallbackWrapper.java] utility class to simplify the implementation and registration of Java callback functions.
|
||||
@@ -70,7 +70,7 @@ We start with describing the most POJO friendly options.
|
||||
|
||||
In this approach you define `@Beans` in your application context as you would any other Spring managed object.
|
||||
|
||||
Internally, Spring AI `ChatClient` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
Internally, Spring AI `ModelCall` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
The name of the `@Bean` is passed as a `ChatOption`.
|
||||
|
||||
|
||||
@@ -156,7 +156,7 @@ ChatResponse response = chatClient.call(new Prompt(List.of(userMessage),
|
||||
logger.info("Response: {}", response);
|
||||
----
|
||||
|
||||
// NOTE: You can can have multiple functions registered in your `ChatClient` but only those enabled in the prompt request will be considered for the function calling.
|
||||
// NOTE: You can can have multiple functions registered in your `ModelCall` but only those enabled in the prompt request will be considered for the function calling.
|
||||
|
||||
Above user question will trigger 3 calls to `CurrentWeather` function (one for each city) and the final response will be something like this:
|
||||
|
||||
@@ -194,5 +194,5 @@ NOTE: The in-prompt registered functions are enabled by default for the duration
|
||||
|
||||
This approach allows to dynamically chose different functions to be called based on the user input.
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/azure/tool/FunctionCallWithPromptFunctionIT.java[FunctionCallWithPromptFunctionIT.java] integration test provides a complete example of how to register a function with the `AzureOpenAiChatClient` and use it in a prompt request.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/azure/tool/FunctionCallWithPromptFunctionIT.java[FunctionCallWithPromptFunctionIT.java] integration test provides a complete example of how to register a function with the `AzureOpenAiModelCall` and use it in a prompt request.
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
= Mistral AI Function Calling
|
||||
|
||||
You can register custom Java functions with the `MistralAiChatClient` and have the Mistral AI models intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
You can register custom Java functions with the `MistralAiModelCall` and have the Mistral AI models intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
This allows you to connect the LLM capabilities with external tools and APIs.
|
||||
The `open-mixtral-8x22b`, `mistral_small_latest`, and `mistral_large_latest` models are trained to detect when a function should be called and to respond with JSON that adheres to the function signature.
|
||||
|
||||
@@ -15,7 +15,7 @@ The `description` helps the model to understand when to call the function.
|
||||
As a developer, you need to implement a function that takes the function call arguments sent from the AI model, and respond with the result back to the model.
|
||||
Your function can in turn invoke other 3rd party services to provide the results.
|
||||
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ChatClient`.
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ModelCall`.
|
||||
|
||||
Under the hood, Spring wraps your POJO (the function) with the appropriate adapter code that enables interaction with the AI Model, saving you from writing tedious boilerplate code.
|
||||
The basis of the underlying infrastructure is the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallback.java[FunctionCallback.java] interface and the companion link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallbackWrapper.java[FunctionCallbackWrapper.java] utility class to simplify the implementation and registration of Java callback functions.
|
||||
@@ -70,7 +70,7 @@ We start with describing the most POJO friendly options.
|
||||
|
||||
In this approach you define `@Beans` in your application context as you would any other Spring managed object.
|
||||
|
||||
Internally, Spring AI `ChatClient` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
Internally, Spring AI `ModelCall` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
The name of the `@Bean` is passed as a `ChatOption`.
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ static class Config {
|
||||
}
|
||||
----
|
||||
|
||||
It wraps the 3rd party `MockWeatherService` function and registers it as a `CurrentWeather` function with the `MistralAiChatClient`.
|
||||
It wraps the 3rd party `MockWeatherService` function and registers it as a `CurrentWeather` function with the `MistralAiModelCall`.
|
||||
It also provides a description (2) and an optional response converter (3) to convert the response into a text as expected by the model.
|
||||
|
||||
NOTE: By default, the response converter does a JSON serialization of the Response object.
|
||||
@@ -162,7 +162,7 @@ ChatResponse response = chatClient.call(new Prompt(List.of(userMessage),
|
||||
logger.info("Response: {}", response);
|
||||
----
|
||||
|
||||
// NOTE: You can can have multiple functions registered in your `ChatClient` but only those enabled in the prompt request will be considered for the function calling.
|
||||
// NOTE: You can can have multiple functions registered in your `ModelCall` but only those enabled in the prompt request will be considered for the function calling.
|
||||
|
||||
Above user question will trigger 3 calls to `CurrentWeather` function (one for each city) and produce the final response.
|
||||
|
||||
@@ -190,7 +190,7 @@ NOTE: The in-prompt registered functions are enabled by default for the duration
|
||||
|
||||
This approach allows to dynamically chose different functions to be called based on the user input.
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/mistralai/tool/PaymentStatusPromptIT.java[PaymentStatusPromptIT.java] integration test provides a complete example of how to register a function with the `MistralAiChatClient` and use it in a prompt request.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/mistralai/tool/PaymentStatusPromptIT.java[PaymentStatusPromptIT.java] integration test provides a complete example of how to register a function with the `MistralAiModelCall` and use it in a prompt request.
|
||||
|
||||
|
||||
== Appendices
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
= Function Calling
|
||||
|
||||
You can register custom Java functions with the `OpenAiChatClient` and have the OpenAI model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
You can register custom Java functions with the `OpenAiModelCall` and have the OpenAI model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
This allows you to connect the LLM capabilities with external tools and APIs.
|
||||
The OpenAI models are trained to detect when a function should be called and to respond with JSON that adheres to the function signature.
|
||||
|
||||
@@ -11,12 +11,12 @@ In general, the custom functions need to provide a function `name`, `descriptio
|
||||
|
||||
As a developer, you need to implement a function that takes the function call arguments sent from the AI model, and respond with the result back to the model. Your function can in turn invoke other 3rd party services to provide the results.
|
||||
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ChatClient`.
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ModelCall`.
|
||||
|
||||
Under the hood, Spring wraps your POJO (the function) with the appropriate adapter code that enables interaction with the AI Model, saving you from writing tedious boilerplate code.
|
||||
The basis of the underlying infrastructure is the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallback.java[FunctionCallback.java] interface and the companion link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallbackWrapper.java[FunctionCallbackWrapper.java] utility class to simplify the implementation and registration of Java callback functions.
|
||||
|
||||
// Additionally, the Auto-Configuration provides a way to auto-register any Function<I, O> beans definition as function calling candidates in the `ChatClient`.
|
||||
// Additionally, the Auto-Configuration provides a way to auto-register any Function<I, O> beans definition as function calling candidates in the `ModelCall`.
|
||||
|
||||
|
||||
== How it works
|
||||
@@ -71,7 +71,7 @@ We start with describing the most POJO friendly options.
|
||||
|
||||
In this approach you define `@Beans` in your application context as you would any other Spring managed object.
|
||||
|
||||
Internally, Spring AI `ChatClient` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
Internally, Spring AI `ModelCall` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
The name of the `@Bean` is passed as a `ChatOption`.
|
||||
|
||||
|
||||
@@ -136,7 +136,7 @@ static class Config {
|
||||
}
|
||||
----
|
||||
|
||||
It wraps the 3rd party `MockWeatherService` function and registers it as a `CurrentWeather` function with the `OpenAiChatClient`.
|
||||
It wraps the 3rd party `MockWeatherService` function and registers it as a `CurrentWeather` function with the `OpenAiModelCall`.
|
||||
It also provides a description (2) and an optional response converter (3) to convert the response into a text as expected by the model.
|
||||
|
||||
NOTE: By default, the response converter does a JSON serialization of the Response object.
|
||||
@@ -159,7 +159,7 @@ ChatResponse response = chatClient.call(new Prompt(List.of(userMessage),
|
||||
logger.info("Response: {}", response);
|
||||
----
|
||||
|
||||
// NOTE: You can can have multiple functions registered in your `ChatClient` but only those enabled in the prompt request will be considered for the function calling.
|
||||
// NOTE: You can can have multiple functions registered in your `ModelCall` but only those enabled in the prompt request will be considered for the function calling.
|
||||
|
||||
Above user question will trigger 3 calls to `CurrentWeather` function (one for each city) and the final response will be something like this:
|
||||
|
||||
@@ -197,11 +197,11 @@ NOTE: The in-prompt registered functions are enabled by default for the duration
|
||||
|
||||
This approach allows to dynamically chose different functions to be called based on the user input.
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/tool/FunctionCallbackInPromptIT.java[FunctionCallbackInPromptIT.java] integration test provides a complete example of how to register a function with the `OpenAiChatClient` and use it in a prompt request.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/openai/tool/FunctionCallbackInPromptIT.java[FunctionCallbackInPromptIT.java] integration test provides a complete example of how to register a function with the `OpenAiModelCall` and use it in a prompt request.
|
||||
//
|
||||
// === Register Functions with Default Options
|
||||
//
|
||||
// You can programmatically register functions with the `OpenAiChatClient` using the `OpenAiChatOptions#withFunctionCallbacks`:
|
||||
// You can programmatically register functions with the `OpenAiModelCall` using the `OpenAiChatOptions#withFunctionCallbacks`:
|
||||
//
|
||||
// [source,java]
|
||||
// ----
|
||||
@@ -215,7 +215,7 @@ The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot
|
||||
// new MockWeatherService()))) // function code
|
||||
// .build();
|
||||
//
|
||||
// OpenAiChatClient chatClient = new OpenAiChatClient(openaiApi, defaultOptions);
|
||||
// OpenAiModelCall chatClient = new OpenAiModelCall(openaiApi, defaultOptions);
|
||||
//
|
||||
// UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
|
||||
//
|
||||
@@ -223,7 +223,7 @@ The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot
|
||||
// OpenAiChatOptions.builder().withFunction("CurrentWeather").build())); // Enable the function
|
||||
// ----
|
||||
//
|
||||
// NOTE: Functions are registered when OpenAiChatClient is created, by you must enable in the Prompt the functions to be used in the request.
|
||||
// NOTE: Functions are registered when OpenAiModelCall is created, by you must enable in the Prompt the functions to be used in the request.
|
||||
|
||||
|
||||
== Appendices:
|
||||
|
||||
@@ -6,7 +6,7 @@ The parallel function calling is gone as well.
|
||||
|
||||
Function calling lets developers create a description of a function in their code, then pass that description to a language model in a request. The response from the model includes the name of a function that matches the description and the arguments to call it with.
|
||||
|
||||
You can register custom Java functions with the `VertexAiGeminiChatClient` and have the Gemini Pro model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
You can register custom Java functions with the `VertexAiGeminiModelCall` and have the Gemini Pro model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
|
||||
This allows you to connect the LLM capabilities with external tools and APIs.
|
||||
The VertexAI Gemini Pro model is trained to detect when a function should be called and to respond with JSON that adheres to the function signature.
|
||||
|
||||
@@ -18,12 +18,12 @@ In general, the custom functions need to provide a function `name`, `description
|
||||
As a developer, you need to implement a function that takes the function call arguments sent from the AI model, and respond with the result back to the model.
|
||||
Your function can in turn invoke other 3rd party services to provide the results.
|
||||
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ChatClient`.
|
||||
Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ModelCall`.
|
||||
|
||||
Under the hood, Spring wraps your POJO (the function) with the appropriate adapter code that enables interaction with the AI Model, saving you from writing tedious boilerplate code.
|
||||
The basis of the underlying infrastructure is the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallback.java[FunctionCallback.java] interface and the companion link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallbackWrapper.java[FunctionCallbackWrapper.java] utility class to simplify the implementation and registration of Java callback functions.
|
||||
|
||||
// Additionally, the Auto-Configuration provides a way to auto-register any Function<I, O> beans definition as function calling candidates in the `ChatClient`.
|
||||
// Additionally, the Auto-Configuration provides a way to auto-register any Function<I, O> beans definition as function calling candidates in the `ModelCall`.
|
||||
|
||||
== How it works
|
||||
|
||||
@@ -74,7 +74,7 @@ We start with describing the most POJO friendly options.
|
||||
|
||||
In this approach you define `@Beans` in your application context as you would any other Spring managed object.
|
||||
|
||||
Internally, Spring AI `ChatClient` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
Internally, Spring AI `ModelCall` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
|
||||
The name of the `@Bean` is passed as a `ChatOption`.
|
||||
|
||||
|
||||
@@ -139,7 +139,7 @@ static class Config {
|
||||
}
|
||||
----
|
||||
|
||||
It wraps the 3rd party `MockWeatherService` function and registers it as a `CurrentWeather` function with the `VertexAiGeminiChatClient`.
|
||||
It wraps the 3rd party `MockWeatherService` function and registers it as a `CurrentWeather` function with the `VertexAiGeminiModelCall`.
|
||||
It also provides a description (2) and sets the Schema type to Open API type (3).
|
||||
|
||||
NOTE: The default response converter does a JSON serialization of the Response object.
|
||||
@@ -162,7 +162,7 @@ ChatResponse response = chatClient.call(new Prompt(List.of(userMessage),
|
||||
logger.info("Response: {}", response);
|
||||
----
|
||||
|
||||
// NOTE: You can can have multiple functions registered in your `ChatClient` but only those enabled in the prompt request will be considered for the function calling.
|
||||
// NOTE: You can can have multiple functions registered in your `ModelCall` but only those enabled in the prompt request will be considered for the function calling.
|
||||
|
||||
Above user question will trigger 3 calls to `CurrentWeather` function (one for each city) and the final response will be something like this:
|
||||
|
||||
@@ -201,5 +201,5 @@ NOTE: The in-prompt registered functions are enabled by default for the duration
|
||||
|
||||
This approach allows to dynamically chose different functions to be called based on the user input.
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/gemini/tool/FunctionCallWithPromptFunctionIT.java[FunctionCallWithPromptFunctionIT.java] integration test provides a complete example of how to register a function with the `VertexAiGeminiChatClient` and use it in a prompt request.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/gemini/tool/FunctionCallWithPromptFunctionIT.java[FunctionCallWithPromptFunctionIT.java] integration test provides a complete example of how to register a function with the `VertexAiGeminiModelCall` and use it in a prompt request.
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ The prefix `spring.ai.mistralai.chat` is the property prefix that lets you confi
|
||||
| spring.ai.mistralai.chat.options.functionCallbacks | MistralAI Tool Function Callbacks to register with the ChatClient. | -
|
||||
|====
|
||||
|
||||
NOTE: You can override the common `spring.ai.mistralai.base-url` and `spring.ai.mistralai.api-key` for the `ChatClient` and `EmbeddingClient` implementations.
|
||||
NOTE: You can override the common `spring.ai.mistralai.base-url` and `spring.ai.mistralai.api-key` for the `ModelCall` and `EmbeddingClient` implementations.
|
||||
The `spring.ai.mistralai.chat.base-url` and `spring.ai.mistralai.chat.api-key` properties if set take precedence over the common properties.
|
||||
This is useful if you want to use different MistralAI accounts for different models and different model endpoints.
|
||||
|
||||
@@ -153,7 +153,7 @@ spring.ai.mistralai.chat.options.temperature=0.7
|
||||
|
||||
TIP: replace the `api-key` with your OpenAI credentials.
|
||||
|
||||
This will create a `MistralAiChatClient` implementation that you can inject into your class.
|
||||
This will create a `MistralAiModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -183,7 +183,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/MistralAiChatClient.java[MistralAiChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the MistralAI service.
|
||||
The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/MistralAiChatClient.java[MistralAiChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the MistralAI service.
|
||||
|
||||
Add the `spring-ai-mistral-ai` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
@@ -206,7 +206,7 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
Next, create a `MistralAiChatClient` and use it for text generations:
|
||||
Next, create a `MistralAiModelCall` and use it for text generations:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
= Ollama Chat
|
||||
|
||||
With https://ollama.ai/[Ollama] you can run various Large Language Models (LLMs) locally and generate text from them.
|
||||
Spring AI supports the Ollama text generation with `OllamaChatClient`.
|
||||
Spring AI supports the Ollama text generation with `OllamaModelCall`.
|
||||
|
||||
== Prerequisites
|
||||
|
||||
@@ -185,7 +185,7 @@ spring.ai.ollama.chat.options.temperature=0.7
|
||||
|
||||
TIP: replace the `base-url` with your Ollama server URL.
|
||||
|
||||
This will create a `OllamaChatClient` implementation that you can inject into your class.
|
||||
This will create a `OllamaModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -216,8 +216,8 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
If you don't want to use the Spring Boot auto-configuration, you can manually configure the `OllamaChatClient` in your application.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaChatClient.java[OllamaChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Ollama service.
|
||||
If you don't want to use the Spring Boot auto-configuration, you can manually configure the `OllamaModelCall` in your application.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-ollama/src/main/java/org/springframework/ai/ollama/OllamaChatClient.java[OllamaChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the Ollama service.
|
||||
|
||||
To use it add the `spring-ai-ollama` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
@@ -243,7 +243,7 @@ TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Man
|
||||
TIP: The `spring-ai-ollama` dependency provides access also to the `OllamaEmbeddingClient`.
|
||||
For more information about the `OllamaEmbeddingClient` refer to the link:../embeddings/ollama-embeddings.html[Ollama Embedding Client] section.
|
||||
|
||||
Next, create an `OllamaChatClient` instance and use it to text generations requests:
|
||||
Next, create an `OllamaModelCall` instance and use it to text generations requests:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@@ -274,7 +274,7 @@ image::ollama-chat-completion-api.jpg[OllamaApi Chat Completion API Diagram, 800
|
||||
|
||||
Here is a simple snippet showing how to use the API programmatically:
|
||||
|
||||
NOTE: The `OllamaApi` is low level api and is not recommended for direct use. Use the `OllamaChatClient` instead.
|
||||
NOTE: The `OllamaApi` is low level api and is not recommended for direct use. Use the `OllamaModelCall` instead.
|
||||
|
||||
[source,java]
|
||||
----
|
||||
|
||||
@@ -107,7 +107,7 @@ The prefix `spring.ai.openai.chat` is the property prefix that lets you configur
|
||||
| spring.ai.openai.chat.options.functions | List of functions, identified by their names, to enable for function calling in a single prompt requests. Functions with those names must exist in the functionCallbacks registry. | -
|
||||
|====
|
||||
|
||||
NOTE: You can override the common `spring.ai.openai.base-url` and `spring.ai.openai.api-key` for the `ChatClient` and `EmbeddingClient` implementations.
|
||||
NOTE: You can override the common `spring.ai.openai.base-url` and `spring.ai.openai.api-key` for the `ModelCall` and `EmbeddingClient` implementations.
|
||||
The `spring.ai.openai.chat.base-url` and `spring.ai.openai.chat.api-key` properties if set take precedence over the common properties.
|
||||
This is useful if you want to use different OpenAI accounts for different models and different model endpoints.
|
||||
|
||||
@@ -209,7 +209,7 @@ spring.ai.openai.chat.options.temperature=0.7
|
||||
|
||||
TIP: replace the `api-key` with your OpenAI credentials.
|
||||
|
||||
This will create a `OpenAiChatClient` implementation that you can inject into your class.
|
||||
This will create a `OpenAiModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -239,7 +239,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatClient.java[OpenAiChatClient] implements the `ChatClient` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the OpenAI service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatClient.java[OpenAiChatClient] implements the `ModelCall` and `StreamingChatClient` and uses the <<low-level-api>> to connect to the OpenAI service.
|
||||
|
||||
Add the `spring-ai-openai` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
@@ -262,7 +262,7 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
Next, create a `OpenAiChatClient` and use it for text generations:
|
||||
Next, create a `OpenAiModelCall` and use it for text generations:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
|
||||
@@ -151,7 +151,7 @@ spring.ai.vertex.ai.gemini.chat.options.temperature=0.5
|
||||
|
||||
TIP: replace the `api-key` with your VertexAI credentials.
|
||||
|
||||
This will create a `VertexAiGeminiChatClient` implementation that you can inject into your class.
|
||||
This will create a `VertexAiGeminiModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -181,7 +181,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/VertexAiGeminiChatClient.java[VertexAiGeminiChatClient] implements the `ChatClient` and uses the `VertexAI` to connect to the Vertex AI Gemini service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-vertex-ai-gemini/src/main/java/org/springframework/ai/vertexai/gemini/VertexAiGeminiChatClient.java[VertexAiGeminiChatClient] implements the `ModelCall` and uses the `VertexAI` to connect to the Vertex AI Gemini service.
|
||||
|
||||
Add the `spring-ai-vertex-ai-gemini` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
@@ -204,7 +204,7 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
Next, create a `VertexAiGeminiChatClient` and use it for text generations:
|
||||
Next, create a `VertexAiGeminiModelCall` and use it for text generations:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
|
||||
@@ -114,7 +114,7 @@ spring.ai.vertex.ai.chat.options.temperature=0.5
|
||||
|
||||
TIP: replace the `api-key` with your VertexAI credentials.
|
||||
|
||||
This will create a `VertexAiPaLm2ChatClient` implementation that you can inject into your class.
|
||||
This will create a `VertexAiPaLm2ModelCall` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the chat client for text generations.
|
||||
|
||||
[source,java]
|
||||
@@ -144,7 +144,7 @@ public class ChatController {
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/vertexai/paml2/VertexAiPaLm2ChatClient.java[VertexAiPaLm2ChatClient] implements the `ChatClient` and uses the <<low-level-api>> to connect to the VertexAI service.
|
||||
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/vertexai/paml2/VertexAiPaLm2ChatClient.java[VertexAiPaLm2ChatClient] implements the `ModelCall` and uses the <<low-level-api>> to connect to the VertexAI service.
|
||||
|
||||
Add the `spring-ai-vertex-ai-palm2` dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
@@ -167,7 +167,7 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
Next, create a `VertexAiPaLm2ChatClient` and use it for text generations:
|
||||
Next, create a `VertexAiPaLm2ModelCall` and use it for text generations:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
= watsonx.ai Chat
|
||||
|
||||
With https://dataplatform.cloud.ibm.com/docs/content/wsj/getting-started/overview-wx.html?context=wx&audience=wdp[watsonx.ai] you can run various Large Language Models (LLMs) locally and generate text from them.
|
||||
Spring AI supports the watsonx.ai text generation with `WatsonxAiChatClient`.
|
||||
Spring AI supports the watsonx.ai text generation with `WatsonxAiModelCall`.
|
||||
|
||||
|
||||
== Prerequisites
|
||||
|
||||
@@ -47,7 +47,7 @@ public interface StreamingChatClient extends StreamingModelClient<Prompt, ChatRe
|
||||
}
|
||||
----
|
||||
|
||||
The `stream` method takes a `Prompt` request similar to `ChatClient` but it streams the responses using the reactive Flux API.
|
||||
The `stream` method takes a `Prompt` request similar to `ModelCall` but it streams the responses using the reactive Flux API.
|
||||
|
||||
=== Prompt
|
||||
|
||||
@@ -184,7 +184,7 @@ public class Generation implements ModelResult<AssistantMessage> {
|
||||
|
||||
== Available Implementations
|
||||
|
||||
The `ChatClient` and `StreamingChatClient` implementations are provided for the following Model providers:
|
||||
The `ModelCall` and `StreamingChatClient` implementations are provided for the following Model providers:
|
||||
|
||||
image::spring-ai-chat-completions-clients.jpg[align="center", width="800px"]
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
NOTE: The `spring-ai-azure-openai` dependency also provide the access to the `AzureOpenAiEmbeddingClient`. For more information about the `AzureOpenAiChatClient` refer to the link:../embeddings/azure-openai-embeddings.html[Azure OpenAI Embeddings] section.
|
||||
NOTE: The `spring-ai-azure-openai` dependency also provide the access to the `AzureOpenAiEmbeddingClient`. For more information about the `AzureOpenAiModelCall` refer to the link:../embeddings/azure-openai-embeddings.html[Azure OpenAI Embeddings] section.
|
||||
|
||||
Next, create an `AzureOpenAiEmbeddingClient` instance and use it to compute the similarity between two input texts:
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ The prefix `spring.ai.mistralai.embedding` is property prefix that configures th
|
||||
| spring.ai.mistralai.embedding.options.encodingFormat | The format to return the embeddings in. Can be either float or base64. | -
|
||||
|====
|
||||
|
||||
NOTE: You can override the common `spring.ai.mistralai.base-url` and `spring.ai.mistralai.api-key` for the `ChatClient` and `EmbeddingClient` implementations.
|
||||
NOTE: You can override the common `spring.ai.mistralai.base-url` and `spring.ai.mistralai.api-key` for the `ModelCall` and `EmbeddingClient` implementations.
|
||||
The `spring.ai.mistralai.embedding.base-url` and `spring.ai.mistralai.embedding.api-key` properties if set take precedence over the common properties.
|
||||
Similarly, the `spring.ai.mistralai.embedding.base-url` and `spring.ai.mistralai.embedding.api-key` properties if set take precedence over the common properties.
|
||||
This is useful if you want to use different MistralAI accounts for different models and different model endpoints.
|
||||
@@ -175,8 +175,8 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
NOTE: The `spring-ai-mistral-ai` dependency provides access also to the `MistralAiChatClient`.
|
||||
For more information about the `MistralAiChatClient` refer to the link:../chat/mistralai-chat.html[MistralAI Chat Client] section.
|
||||
NOTE: The `spring-ai-mistral-ai` dependency provides access also to the `MistralAiModelCall`.
|
||||
For more information about the `MistralAiModelCall` refer to the link:../chat/mistralai-chat.html[MistralAI Chat Client] section.
|
||||
|
||||
Next, create an `MistralAiEmbeddingClient` instance and use it to compute the similarity between two input texts:
|
||||
|
||||
|
||||
@@ -177,8 +177,8 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
NOTE: The `spring-ai-ollama` dependency provides access also to the `OllamaChatClient`.
|
||||
For more information about the `OllamaChatClient` refer to the link:../chat/ollama-chat.html[Ollama Chat Client] section.
|
||||
NOTE: The `spring-ai-ollama` dependency provides access also to the `OllamaModelCall`.
|
||||
For more information about the `OllamaModelCall` refer to the link:../chat/ollama-chat.html[Ollama Chat Client] section.
|
||||
|
||||
Next, create an `OllamaEmbeddingClient` instance and use it to compute the similarity between two input texts:
|
||||
|
||||
|
||||
@@ -97,7 +97,7 @@ The prefix `spring.ai.openai.embedding` is property prefix that configures the `
|
||||
| spring.ai.openai.embedding.options.dimensions | The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models. | -
|
||||
|====
|
||||
|
||||
NOTE: You can override the common `spring.ai.openai.base-url` and `spring.ai.openai.api-key` for the `ChatClient` and `EmbeddingClient` implementations.
|
||||
NOTE: You can override the common `spring.ai.openai.base-url` and `spring.ai.openai.api-key` for the `ModelCall` and `EmbeddingClient` implementations.
|
||||
The `spring.ai.openai.embedding.base-url` and `spring.ai.openai.embedding.api-key` properties if set take precedence over the common properties.
|
||||
Similarly, the `spring.ai.openai.embedding.base-url` and `spring.ai.openai.embedding.api-key` properties if set take precedence over the common properties.
|
||||
This is useful if you want to use different OpenAI accounts for different models and different model endpoints.
|
||||
@@ -178,8 +178,8 @@ dependencies {
|
||||
|
||||
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
|
||||
|
||||
NOTE: The `spring-ai-openai` dependency provides access also to the `OpenAiChatClient`.
|
||||
For more information about the `OpenAiChatClient` refer to the link:../chat/openai-chat.html[OpenAI Chat Client] section.
|
||||
NOTE: The `spring-ai-openai` dependency provides access also to the `OpenAiModelCall`.
|
||||
For more information about the `OpenAiModelCall` refer to the link:../chat/openai-chat.html[OpenAI Chat Client] section.
|
||||
|
||||
Next, create an `OpenAiEmbeddingClient` instance and use it to compute the similarity between two input texts:
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ Another analogy is a SQL statement that contain placeholders for certain express
|
||||
|
||||
As Spring AI evolves, it will introduce higher levels of abstraction for interacting with AI models.
|
||||
The foundational classes described in this section can be likened to JDBC in terms of their role and functionality.
|
||||
The `ChatClient` class, for instance, is analogous to the core JDBC library in the JDK.
|
||||
The `ModelCall` class, for instance, is analogous to the core JDBC library in the JDK.
|
||||
Building upon this, Spring AI can provide helper classes similar to `JdbcTemplate`, Spring Data Repositories, and eventually, more advanced constructs like ChatEngines and Agents that consider past interactions with the model.
|
||||
|
||||
The structure of prompts has evolved over time within the AI field.
|
||||
@@ -24,7 +24,7 @@ OpenAI have introduced even more structure to prompts by categorizing multiple m
|
||||
|
||||
=== Prompt
|
||||
|
||||
It is common to use the `call` method of `ChatClient` that takes a `Prompt` instance and returns an `ChatResponse`.
|
||||
It is common to use the `call` method of `ModelCall` that takes a `Prompt` instance and returns an `ChatResponse`.
|
||||
|
||||
The Prompt class functions as a container for an organized series of Message objects, with each one forming a segment of the overall prompt.
|
||||
Every Message embodies a unique role within the prompt, differing in its content and intent.
|
||||
|
||||
@@ -226,7 +226,7 @@ public class CricketWorldCupRepository implements HanaVectorRepository<CricketWo
|
||||
}
|
||||
----
|
||||
|
||||
* Now, create a REST Controller class `CricketWorldCupHanaController`, and autowire `ChatClient` and `VectorStore` as dependencies
|
||||
* Now, create a REST Controller class `CricketWorldCupHanaController`, and autowire `ModelCall` and `VectorStore` as dependencies
|
||||
In this controller class, create the following REST endpoints:
|
||||
|
||||
- `/ai/hana-vector-store/cricket-world-cup/purge-embeddings` - to purge all the embeddings from the Vector Store
|
||||
@@ -238,7 +238,7 @@ In this controller class, create the following REST endpoints:
|
||||
package com.interviewpedia.spring.ai.hana;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.ChatClient;
|
||||
import org.springframework.ai.chat.ModelCall;
|
||||
import org.springframework.ai.chat.messages.UserMessage;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.chat.prompt.SystemPromptTemplate;
|
||||
|
||||
@@ -59,7 +59,7 @@ to
|
||||
* Moving the `prompt` and `messages` and `metadata` packages to subpackages of `org.sf.ai.chat`
|
||||
* New functionality is *text to image* clients. Classes are `OpenAiImageClient` and `StabilityAiImageClient`. See the integration tests for usage, docs are coming soon.
|
||||
* A new package `model` that contains interfaces and base classes to support creating AI Model Clients for any input/output data type combination. At the moment the chat and image model packages implement this. We will be updating the embedding package to this new model soon.
|
||||
* A new "portable options" design pattern. We wanted to provide as much portability in the `ChatClient` as possible across different chat based AI Models. There is a common set of generation options and then those that are specific to a model provider. A sort of "duck typing" approach is used. `ModelOptions` in the model package is a marker interface indicating implementations of this class will provide the options for a model. See `ImageOptions`, a subinterface that defines portable options across all text->image `ImageClient` implementations. Then `StabilityAiImageOptions` and `OpenAiImageOptions` provide the options specific to each model provider. All options classes are created via a fluent API builder all can be passed into the portable `ImageClient` API. These option data types are using in autoconfiguration/configuration properties for the `ImageClient` implementations.
|
||||
* A new "portable options" design pattern. We wanted to provide as much portability in the `ModelCall` as possible across different chat based AI Models. There is a common set of generation options and then those that are specific to a model provider. A sort of "duck typing" approach is used. `ModelOptions` in the model package is a marker interface indicating implementations of this class will provide the options for a model. See `ImageOptions`, a subinterface that defines portable options across all text->image `ImageClient` implementations. Then `StabilityAiImageOptions` and `OpenAiImageOptions` provide the options specific to each model provider. All options classes are created via a fluent API builder all can be passed into the portable `ImageClient` API. These option data types are using in autoconfiguration/configuration properties for the `ImageClient` implementations.
|
||||
|
||||
=== January 13, 2024 Update
|
||||
|
||||
@@ -79,7 +79,7 @@ Merge SimplePersistentVectorStore and InMemoryVectorStore into SimpleVectorStore
|
||||
|
||||
Refactor the Ollama client and related classes and package names
|
||||
|
||||
* Replace the org.springframework.ai.ollama.client.OllamaClient by org.springframework.ai.ollama.OllamaChatClient.
|
||||
* Replace the org.springframework.ai.ollama.client.OllamaClient by org.springframework.ai.ollama.OllamaModelCall.
|
||||
* The OllamaChatClient method signatures have changed.
|
||||
* Rename the org.springframework.ai.autoconfigure.ollama.OllamaProperties into org.springframework.ai.autoconfigure.ollama.OllamaChatProperties and change the suffix to: `spring.ai.ollama.chat`. Some of the properties have changed as well.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user