Commit Graph

383 Commits

Author SHA1 Message Date
dafriz
250d2afbb6 Add Meta Llama 3.1, 3.2 instruct model enums
This commit adds support for new Meta Llama 3.1 and 3.2 instruct models
to the LlamaChatBedrockApi enum. It includes model IDs for:

Llama 3.1: 8B, 70B, 405B
Llama 3.2: 1B, 3B, 11B, 90B

These additions allow users to specify the latest Llama models when
using the Bedrock API. The commit also updates the class Javadoc with
a link to AWS documentation for model IDs and bumps the @since version
to 1.0.0.
2024-10-03 16:24:56 -04:00
Soby Chacko
ef3d43ca9c Use OpenAIAsyncClient for streaming in AzureOpenAiChatModel
- Switch to OpenAIAsyncClient for streaming operations
- Modify AzureOpenAiChatModel constructor to accept OpenAIClientBuilder
- Update getChatCompletionsStream to use non-blocking async client
- Refactor related classes and tests to support OpenAIClientBuilder
- Revise AzureOpenAiAutoConfiguration to provide OpenAIClientBuilder
- Add AzureOpenAiChatClientTest to verify streaming functionality
- Adjust existing tests for compatibility with OpenAIClientBuilder

Resolves https://github.com/spring-projects/spring-ai/issues/981

This change improves support for asynchronous streaming operations
in the AzureOpenAiChatModel, addressing potential issues in reactive environments.
2024-10-03 15:55:38 -04:00
Mark Pollack
bb88e2f2de Add retry support to VertexAI embedding and chat models
Resolves https://github.com/spring-projects/spring-ai/issues/832

Introduces retry functionality to VertexAI embedding and
chat models, enhancing their resilience against transient failures.

It also corrects a typo in the VertexAiEmbeddingConnectionDetails
class name.

Key changes:

* Add RetryTemplate to VertexAiTextEmbeddingModel and VertexAiGeminiChatModel
* Introduce spring-ai-retry dependency
* Refactor code to support retry logic
* Update auto-configuration classes to incorporate retry functionality
* Fix typo in VertexAiEmbeddingConnectionDetails class name

remove extraneous commented out code

Add missing copyright headers, author etc.
2024-10-02 14:38:06 -04:00
Christian Tzolov
6fc76b7f9b Refactor advisor architecture in Spring AI
This commit introduces a major overhaul of the advisor system in Spring AI,
improving modularity, type safety, and consistency

Core Changes:
- Replace RequestAdvisor and ResponseAdvisor with CallAroundAdvisor and StreamAroundAdvisor
- Introduce AdvisedRequest and AdvisedResponse classes for better encapsulation
- Deprecate RequestResponseAdvisor in favor of new advisor types
- Remove AdvisorObservableHelper class

Advisor Implementation Updates:
- Update AbstractChatMemoryAdvisor, MessageChatMemoryAdvisor, PromptChatMemoryAdvisor,
QuestionAnswerAdvisor, SafeGuardAroundAdvisor, SimpleLoggerAdvisor, and
VectorStoreChatMemoryAdvisor to implement new advisor interfaces
- Remove CacheAroundAdvisor (functionality likely moved elsewhere)
- Make CallAroundAdvisor and StreamAroundAdvisor extend Ordered interface

Client and Chain Management:
- Modify DefaultChatClient to use new advisor chain approach
- Refactor DefaultAroundAdvisorChain for better ordering and observation
- Implement builder pattern for advisor chain construction in DefaultChatClient
- Separate call and stream advisors in DefaultAroundAdvisorChain

Observation and Context Handling:
- Update observation conventions and context handling in advisors
- Add order field to AdvisorObservationContext
- Modify DefaultAdvisorObservationConvention to include order in high cardinality key values

Testing and Integration:
- Refactor ChatClientAdvisorTests and add new AdvisorsTests
- Update integration tests to reflect new advisor structure
- Enhance AdvisorsTests to verify correct advisor execution order

New Features:
- Generalize the Protect From Blocking functionality across all advisors
- Add (experimental) Re2 advisor to enhance reasoning capabilities of LLMs
- Add disabled Re2 test in OpenAiChatClientIT

Documentation:
- Add Advisors documentation
- Enhance advisors documentation with order explanation and Re2 example

Advisor Ordering:
- Introduce Advisor constants for precedence ordering
- Update AbstractChatMemoryAdvisor to use new precedence constant
- Improve advisor ordering and management in DefaultAroundAdvisorChain.Builder
- Remove redundant reordering logic from DefaultAroundAdvisorChain

These changes aim to provide a more flexible and powerful advisor system,
allowing for easier implementation of complex AI-driven interactions

Co-authored-by: Dariusz Jędrzejczyk <dariusz.jedrzejczyk@broadcom.com>
2024-10-01 15:08:21 -04:00
dafriz
f56ce20cad Add support for max_completion_tokens in OpenAI chat options request
An upper bound for the number of tokens that can be generated for a completion,
  including visible output tokens and reasoning tokens.
  Replaces max_tokens field which is now deprecated.
2024-10-01 11:05:51 +02:00
Christian Tzolov
110a520a40 Add Llama 3.2 models to OllamaModel enum
- Include LLAMA3_2 (3B) and LLAMA3_2_1B (1B) variants
2024-09-29 15:53:27 +02:00
Soby Chacko
e29d38d987 Remove unnecessary null checks in AzureOpenAiChatOptions.Builder
This is related to https://github.com/spring-projects/spring-ai/issues/889

This commit addresses a bug where certain fields were being made
indirectly mandatory due to Assert.notNull checks in the Builder's
with* methods. Specifically:

- Removed Assert.notNull checks from withResponseFormat, withSeed,
  withLogprobs, withTopLogprobs, and withEnhancements methods.

These checks were causing exceptions in AzureOpenAiChatModel.getDefaultOptions
when not all fields were set, leading to failures in methods like
ChatClient.create, even when using the AzureOpenAiChatModel constructor
with OpenAIClient.

The removal of these checks aligns with the @JsonInclude(Include.NON_NULL)
annotation on AzureOpenAiChatOptions, which already ignores null options.
This change maintains the intended flexibility while preventing unintended
mandatory requirements.
2024-09-27 13:22:44 -04:00
Anders Swanson
ccf190c77c Add OCI GenAI embedding model support
This commit introduces support for Oracle Cloud Infrastructure (OCI)
GenAI embedding models in Spring AI. It includes:

* New OCIEmbeddingModel class for interacting with OCI GenAI API
* Auto-configuration for easy setup and integration
* Properties for configuring OCI connection and embedding options
* Documentation updates explaining usage and configuration
* Integration tests to verify functionality

Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
2024-09-26 17:04:05 -04:00
Christian Tzolov
a55316c44d Remove name assertion from ToolResponseMessage validation
- Removed the assertion checking for a non-null 'name' in ToolResponseMessage
 - This change affects multiple AI model implementations:
   AzureOpenAiChatModel, MiniMaxChatModel, MistralAiChatModel,
   MoonshotChatModel, OpenAiChatModel, and ZhiPuAiChatModel

 Resolves #1410
2024-09-25 18:49:36 +02:00
dafriz
1673907db0 Add support for reasoning tokens in OpenAI usage data
This change introduces a new field for tracking reasoning tokens in the
OpenAI API response. It extends the Usage record to include
CompletionTokenDetails, allowing for more granular token usage
reporting. The OpenAiUsage class is updated to expose this new data,
and corresponding unit tests are added to verify the behavior.

This enhancement provides more detailed insights into token usage,
particularly for advanced AI models that separate reasoning from other
generation processes.
2024-09-24 17:18:20 -04:00
Soby Chacko
42dcb45f32 Align AzureOpenAiChatOptions with Azure ChatCompletionsOptions
Add missing options from Azure ChatCompletionsOptions to Spring AI
AzureOpenAiChatOptions. The following fields have been added:

- seed
- logprobs
- topLogprobs
- enhancements

This change ensures better alignment between the two option sets,
improving compatibility and feature parity.

Resolves https://github.com/spring-projects/spring-ai/issues/889
2024-09-24 16:35:37 -04:00
“claudio-code”
b468354dd3 Improve AnthropicChatModel handling of empty generations
- Add handling for cases where ChatCompletion has a stop reason but empty generations.
   Creates a Generation with empty content and metadata when this occurs.
 - Update AnthropicChatModelObservationIT to expect "end_turn" finish reason.
 - Update javadoc.
2024-09-24 10:42:22 +02:00
Christian Tzolov
e63dc6a457 Enhance ChatClientRequestSpec with sealed Prompt input
- When ChatClientRequestSpec#prompt(Prompt) is used, unseal the prompt instance.
   Convert the last message instance (if user message) into spec#user and spec#media
   and add the remaining messages (excluding the last) to the spec#messages.
   Add the prompt#options to the spec#options.
 - Improve DefaultChatClient to handle UserMessage media and content separately.
 - Update AbstractToolCallSupport to use new hasToolCalls() method.
 - Add hasToolCalls() method to AssistantMessage.
 - Enhance ChatClientTest with additional test cases for media handling.
 - Disable Groq and Nvidia integration tests due to rate limiting and credit requirements.
2024-09-24 10:19:08 +02:00
Christian Tzolov
9822eabb7f Fix Ollama ITs 2024-09-23 23:49:07 +02:00
Christian Tzolov
c98caf26d2 Minor test fixtures fixes 2024-09-23 23:03:56 +02:00
Christian Tzolov
501774925c Add proxy tool calls option to chat models
This commit introduces a new proxyToolCalls option for various chat
 models in the Spring AI project. When enabled, it allows the client to
 handle function calls externally instead of being processed internally
 by Spring AI.

 The change affects multiple chat model implementations, including:
  AnthropicChatModel
  AzureOpenAiChatModel
  MiniMaxChatModel
  MistralAiChatModel
  MoonshotChatModel
  OllamaChatModel
  OpenAiChatModel
  VertexAiGeminiChatModel
  ZhiPuAiChatModel

 The proxyToolCalls option is added to the respective chat options
 classes and integrated into the AbstractToolCallSupport class for
 consistent handling across different implementations.

 The proxyToolCalls option can be set either programmatically via
 the <ModelName>ChatOptions.builder().withProxyToolCalls() method
 or the spring.ai.<model-name>.chat.options.proxy-tool-calls
 application property.

 Documentation for the new option is also updated in the relevant
 Antora pages.

 Added ITs for proxy tool calls

 Remove ChatClientPromptRequestSpec and all ChatClient.prompt() overloads can how take advantage of the full fluent API.  Docs updated

 Resolves #1367
2024-09-23 16:22:58 -04:00
PabloSanchi
035036c7f2 Add support for watsonx.ai embedding model
This commit introduces support for the Watsonx.ai embedding model.
It includes:

- Watsonx embedding options class with tests
- Watsonx embedding model implementation
- Auto-configuration and properties for the embedding model
- Tests for the Watsonx embedding model
- Documentation for using the Watsonx embedding model

Also removed use of deprecated APIs in WatsonAIChatModel
2024-09-19 11:45:04 -04:00
Mark Pollack
db9cc62def Change enums of chatgpt o1 modesl to correct values 2024-09-19 10:42:26 -04:00
Johnny Lim
8b019c5fee Polish Javadoc for OpenAiApi.ChatModel 2024-09-17 18:32:05 -04:00
Mark Pollack
8bfb25da86 Fix streaming function call for MiniMax chat model
Sometimes, the MiniMax stream mode function calls might get split,
resulting in an empty tool call ID. This indicates that the previous
call is not finished, which is an unusual API design.

The issue occurs when tool calls return in a format like:
[{"id":"1","function":{"name":"a"}},{"id":"","function":{"arguments":"[1]"}}]

These need to be merged into:
[{"id":"1","name":"a","arguments":"[1]"}]

This commit addresses the merging process to handle split function calls.

authored-by: mxsl-gr <mxsl-gr@users.noreply.github.com>
2024-09-17 14:56:23 -04:00
dafriz
f226b1a570 Fix typos in assert notNull in Bedrock model 2024-09-16 23:55:45 -04:00
PARK-afk
7fda93f7a4 docs: ChatModelDesciption (#1357)
Co-authored-by: 박준서 <junsepar@student.42seoul.kr>
2024-09-16 23:48:13 -04:00
박준서
40eaa9974e OpenAI: Add gpt-o1/o1-mini to chat model enum 2024-09-16 23:45:34 -04:00
Thomas Vitale
4b123a7516 Use Double instead of Float for portable ChatOptions
This change updates the type of portable chat options from Float to
Double. Affected options include:
- frequencyPenalty
- presencePenalty
- temperature
- topP

The motivation for this change is to simplify coding. In Java, Float
values require an "f" suffix (e.g., 0.5f), while Double values don't
need any suffix. This makes Double easier to type and reduces
potential errors from forgetting the "f" suffix.

APIs, tests, and documentation have been updated to reflect this
change.

Fixes gh-712

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-09-16 15:35:51 -04:00
Christian Tzolov
b22a577ebc Add message history ITs for few chat models 2024-09-10 12:08:57 +02:00
Christian Tzolov
b5c7975fac Add test for #1317
add integration test for https://github.com/spring-projects/spring-ai/pull/1317
2024-09-10 11:23:17 +02:00
Peter Dolukhanov
26c5674acb Fix where previous Assistant Messages were not being added to the parts list.
Results in: io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Unable to submit request because it must include at least one parts field, which describes the prompt input. Learn more: https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini
2024-09-10 11:18:36 +02:00
GR
b38cbe6386 Enhance MiniMax chat model compatibility and add tests
- Add web search mode response in choice.message for enhanced
compatibility
- Implement web search mode for stream mode
- Add comprehensive unit tests for new features

Related to #1292

feat: enhance the compatibility of the minimax model and tests, related issue #1292
2024-09-09 17:56:04 -04:00
inpink
897a4114f5 Fix Javadoc comments in OpenAiAudioApi and OpenAiApi
- Correct OpenAiAudioApi Javadoc: remove chat completion reference
- Add missing @param for webClientBuilder in OpenAiApi constructor
2024-09-09 17:48:05 -04:00
Christian Tzolov
28276d14a4 Refactor Advisor Interfaces and add AroundAdvisor support
Refactoring and enhancements to the advisor functionality.

 New Advisor interfaces in the org.springframework.ai.chat.client.advisor.api package:
 - Advisor: Base interface for all advisor types.
 - RequestAdvisor: For advising on request data before execution.
 - ResponseAdvisor: For advising on response data after execution, with enhanced streaming modes.
 - CallAroundAdvisor and StreamAroundAdvisor: For around advice on synchronous and streaming requests respectively.
 - AroundAdvisorChain and DefaultAroundAdvisorChain: To manage chaining of around advisors.

 Advisor Chain and Prompt Generation:
 - Added the DefaultAroundAdvisorChain class to manage the sequence of advisors applied around chat model methods.
 - Adjusted the prompt generation (toPrompt) to integrate with the refactored AdvisedRequest object.

 Refactoring and Updates:
 - Replaced the deprecated RequestResponseAdvisor interface with RequestAdvisor and ResponseAdvisor across the spring-ai-core and test modules.
 - Updated the DefaultChatClient and related classes to use the new Advisor interface, improving modularity and consistency.
 - Refactored DefaultAdvisorSpec and DefaultChatClientRequestSpec to handle the new Advisor type, and revised advisor lists and methods accordingly.
 - Enhanced the handling of streaming responses, introducing StreamResponseMode for better control during streaming scenarios.
2024-09-05 12:35:34 -04:00
Craig Walls
3cab5bd009 Autowire WebClient.Builder into OllamaApi 2024-09-04 14:11:21 -04:00
Christian Tzolov
bf8dabfb11 Improve stream advisor processing
* Fixes an issue with advisor name resolution
* Streamlines repeating code
* Add a new advisor strategy for ON_FINISH_REASON streaming responses, which is used by the Q&A advisor
* Improve observable instrumentation by passing the parent observation to the advisor observation
2024-09-03 21:41:36 -04:00
Mark Pollack
e1884d1d92 Next development version 2024-08-23 18:47:37 -04:00
Mark Pollack
43ad2bdb97 Release version 1.0.0-M2 2024-08-23 18:46:58 -04:00
GR
4fac212b0d Add web search capability to MiniMax model
Implement web search functionality for the MiniMax model.
Includes unit tests
This enhancement expands the model's ability to access and utilize current information from the internet.

Resolves #1245
2024-08-23 12:17:45 -04:00
GR
0927bd197d Fix MiniMax model function call implementation
Implement function call capability for MiniMax model and add unit tests based on new tool classes.
Address most scenarios, but note limitations in complex English contexts
with multiple function calls. Weather query example: may stop
prematurely when querying multiple locations due to single-location
parameter limit. This behavior stems from model performance constraints.

Streaming function calling is not passing tests, will be address seperately.

Resolves #1077

Implement function call capability for the Moonshot model. Include unit
tests to verify the new functionality. This feature addresses the
requirements outlined in issue #1058.
fix: MiniMax function call

review
2024-08-23 12:03:33 -04:00
GR
611c9493e3 Add function call support for Moonshot model
Implement function call capability for the Moonshot model. Include unit
tests to verify the new functionality.

Resolves #1058
2024-08-23 11:43:07 -04:00
GR
935e1a38ed Add ZhiPu model support for AbstractToolCallSupport
Implement ZhiPu function call to work with AbstractToolCallSupport
framework. Add unit tests for function call functionality.

Resolves #1078.
2024-08-23 11:12:21 -04:00
Szymon Ochnio
197fe8105c Fix Azure chat client image handling for byte arrays
Resolve issue with AzureOpenAiChatModel processing non-public images as
byte arrays. Implement handling for both URL strings and byte arrays,
converting latter to base64 encoded data URLs. Add test case for
resource-based media data. Update documentation with sample for
classpath resources.
2024-08-23 10:50:24 -04:00
Christian Tzolov
99a4fec214 Remove the reactive advisor observability support
-  Add Anthropic WebClient.Builder autoconfiguration to activate the OOTB WebClient obsercabilty support.
2024-08-23 09:57:49 -04:00
Mark Pollack
793052c2e9 Add googleSearchRetrieval as an option for VertexAIGeminiChatOptions
- Add test
2024-08-22 19:26:36 -04:00
KimMinjeong
bc55bc7196 Add responseMimeType option in VertexAiGeminiChatOptions
- Add test
2024-08-22 18:48:59 -04:00
Mohammed, Ahmed yousri salama (Canada)
189468127c Implement OpenAI Moderation model
This commit introduces the OpenAiModerationModel and related classes:

- Add OpenAiModerationModel for content moderation
- Create OpenAiModerationOptions for configuration
- Implement OpenAiModerationProperties for Spring Boot setup
- Add integration tests in OpenAiModerationModelIT
- Add documentation

Co-authored-by: hemeda3 <hemeda3@users.noreply.github.com>
2024-08-22 17:39:16 -04:00
GR
a3c0927c9b Add MiniMax chat option to mask sensitive information
This commit introduces a new boolean option 'maskSensitiveInfo' for
the MiniMax API model support in Spring AI. This feature allows users
to control whether sensitive information in the output is masked.

Relevant unit tests have been added to ensure proper functionality.

Resolves: #1216
2024-08-22 11:23:59 -04:00
Thomas Vitale
036093a42b Enhance vector store observability support
* Consolidate usage of “db.collection.name” attribute to track table name, collection name, index name, document name, or whatever concept a vector database uses to store data. Removed “db.index” that was use sometimes instead of “db.collection.name”. This usage is in line with the OpenTelemetry Semantic Conventions.
* Configure query response content to be included as a “span event” instead of a “span attribute” if the backend system supports that, similar to how we do for the model observations.
* Structure vector store observation attributes in dedicated enums, including one for the Spring AI Kinds to avoid hard-coding the same value in a lot of places. This follows the OpenTelemetry Semantic Conventions as much as possible. Also, adopt Spring usual non-null-by-default strategy as much as possible.
* Align vector store conventions to the chat model ones, and follow alphabetical order for values. This is particularly useful for the convention classes, for which the Micrometer performance of exporting telemetry data improves when key values are added already sorted to the context.
* Fix flaky test in Mistral AI.
* Improve Qdrant integration tests.

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-22 09:38:59 +02:00
wmz7year
32a4652aba Fix out of bounds exception in BedrockAnthropic3ChatModel
Handle the case where AnthropicChatResponse content is empty to prevent
an out of bounds exception. This ensures that the model behaves correctly
even when no content is returned in the response.
2024-08-20 14:55:47 -04:00
Soby Chacko
949f1ed4e8 Add Batching strategy for embedding documents
- When embedding documents, allow batching the documents using some criteria.
 - `BatchingStrategy` interface with a `TokenCountBatchingStrategy` implementation that uses
   the openai max input token size of 8191 as the default.
 - Add a default method in EmbeddingModel to embed document using this new batching strategy.
 - Change `MilvusVectorStore` to make use of this new batching API.
 - Adding unit tests for `TokenCountBatchingStrategy`.
 - Adding openai integration test to call the embed API that uses batching.

Resolves https://github.com/spring-projects/spring-ai/issues/1214

Other vector stores will be updated seperately
2024-08-20 14:08:06 -04:00
Thomas Vitale
3b7522b6c0 Model observability for Anthropic
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-20 01:13:43 -04:00
Thomas Vitale
80fe5e4897 Model observability for Mistral
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-20 00:36:31 -04:00
Christian Tzolov
46893b0a99 Add documentation for NVIDIA LLM API integration
Add NVIDIA LLM API - OpenAI Integration Test.
2024-08-19 21:53:20 +02:00