Commit Graph

71 Commits

Author SHA1 Message Date
Mark Pollack
67a8896422 Next development version 2024-11-20 18:03:30 -05:00
Mark Pollack
33c05c399c Release version 1.0.0-M4 2024-11-20 18:02:47 -05:00
Ilayaperumal Gopinathan
3b95dfd87f Fix checkstyle errors 2024-11-19 23:29:36 +00:00
Ilayaperumal Gopinathan
3812d2e6d8 Remove explicit ChatOptions for the models using FunctionCallingOptions
- Since FunctionCallingOptions already extends ChatOptions,
   removing explicit usage of ChatOptions in the Model Options classes
2024-11-19 18:02:54 -05:00
Christian Tzolov
0eacc9193b feat(tools): Add tool call history to ToolContext
- Add TOOL_CALL_HISTORY constant to store tool call history
- Extend ToolContext with getToolConversationHistory method
- Include tool call history in tool context during function execution
- Add test coverage for tool call history verification

Resolves #1202
2024-11-19 22:50:38 +00:00
Soby Chacko
edc70031dc Remove Boot dependency from core anthropic and watson modules
Fixes: #1591
2024-11-19 17:05:41 -05:00
jitokim
d8583271c7 fix callback merging in AzureOpenAiChatModel constructor
and minor fixes

Signed-off-by: jitokim <pigberger70@gmail.com>
2024-11-19 09:12:51 +01:00
Soby Chacko
f5b00a027c Various checkstyle fixes 2024-11-18 19:17:09 -05:00
jitokim
fb2e7528d0 Fix Anthropic issue using unsubscribed response body in exception message
Signed-off-by: jitokim <pigberger70@gmail.com>
2024-11-18 16:44:21 -05:00
Christian Tzolov
0ca91b2ed9 fix: Resolve various javadoc and checkstyle issues 2024-11-16 11:06:14 +01:00
Christian Tzolov
fb0d99dc37 Revamp function callback builder API
Introduces a simplified, type-safe builder pattern for function callbacks to
improve developer experience and code reliability. The new hierarchical API
separates concerns between direct function invocation and method reflection,
while providing better compile-time safety.

This change deprecates the older FunctionCallbackWrapper in favor of a more
intuitive FunctionCallback.Builder that better handles generic types via
ParameterizedTypeReference. It also adds automatic function description
generation as a fallback when none is provided, though explicit descriptions
are still recommended.

The update standardizes function callback handling across all AI model
implementations (OpenAI, Ollama, Minimax, etc.) and improves response
handling with configurable converters.

Core API Enhancements:

- New Builder Interface: Replaced FunctionCallbackWrapper.builder() with
   FunctionCallback.builder(), introducing a hierarchical approach that improves
   customization and type safety.
- Specialized Builders: Introduced FunctionInvokerBuilder for direct Function/BiFunction
   implementations and MethodInvokerBuilder for reflection-based invocations.
- Generic Type Support: Added ParameterizedTypeReference for better handling of generic parameters.
- Unified Method Definition: Merged method() and argumentTypes() into a single method() call
   for simplicity and type safety.
- Automatic Descriptions: Implemented auto-generation of function descriptions, with warnings
   to encourage explicit descriptions.
- Configurable Response Converters: Enhanced response handling with support for custom
   converters, reducing unnecessary JSON conversions.

Architecture Improvements:

- Established common Builder interface for shared properties
- Separated function object handling from constructor
- Added method-specific configuration (name, arg types, target)
- Added JSON schema generation support for ResolvableType
- Moved to standardized schema types across AI providers
- Set OPEN_API_SCHEMA as default for Vertex AI Gemini

Builder Pattern Standardization:

- Standardized builder method ordering across implementations
- Moved function() call after description() for consistency
- Improved function callback configuration with unified patterns
- Enhanced error handling and validation in DefaultFunctionCallbackBuilder

Deprecations:

- FunctionCallbackWrapper.Builder replaced by DefaultFunctionCallbackBuilder
- Removed CustomizedTypeReference in favor of ParameterizedTypeReference
- Deprecated older ChatClient API methods for function handling

Testing & Documentation:

- Updated all AI model implementations (OpenAI, Ollama, Minimax, Moonshot, ZhiPuAI)
- Added comprehensive integration tests for static/instance methods
- Added integration tests for auto-generated descriptions
- Updated documentation to reflect new builder pattern usage
- Added Kotlin extension for inputType() support

Co-authored-by: Sébastien Deleuze <sebastien.deleuze@broadcom.com>
2024-11-15 15:32:59 -05:00
Soby Chacko
4de95b7708 Checkstyle fix 2024-11-11 10:50:31 -05:00
Christian Tzolov
5f6b892eda Add function calling support to invoke methods with dynamic arguments and return values
This change enables more flexible integration between Spring AI and LLM function
calling capabilities while maintaining type safety and ease of use.

- Add new MethodFunctionCallback class to support method invocation via reflection
 - Supports both static and non-static method calls
 - Handles multiple parameter types including primitives, objects, collections
 - Supports empty parameters and empty response
 - Auto-generates JSON schema from method parameters
 - Special handling for ToolContext parameters
 - Builder pattern for easy configuration

- Add comprehensive unit tests for MethodFunctionCallback
- Add integration tests for MethodFunctionCallback with both Anthropic and OpenAI clients
- Add jackson-module-jsonSchema dependency
- Modify FunctionCallback to check for empty tool context

Testing coverage includes:
- Static method invocation scenarios
- Non-static method calls with various parameter types
- Void return type methods
- Complex parameter types (enums, records, lists)
- Tool context handling
- Error cases and validation

Add MethodFunctionCallback reference docs
2024-11-10 22:58:40 -05:00
Soby Chacko
66f58d2d70 Change default build setting to disable Checkstyle enforcement
- Disable project-wide Checkstyle checks to unblock development
- Add documentation for enabling Checkstyle locally
- Fix remaining checkstyle violations in current codebase

Fixes #1669
2024-11-05 10:43:38 -05:00
jitokim
166e3d5f37 Fix incorrect logger class initialization
Signed-off-by: jitokim <pigberger70@gmail.com>
2024-11-04 12:53:59 +00:00
Soby Chacko
e72ab6ba25 Addressing more checkstyle violations
- Enable checkstyle on more modules and adressing violations
review
2024-10-31 01:04:41 -04:00
Soby Chacko
8e758dbd00 Introduce checkstyle plugin
- Based on https://github.com/spring-io/spring-javaformat
- In this iteration, checkstyles are only enabled for spring-ai-core
2024-10-24 16:43:59 -04:00
Christian Tzolov
a1980ec4dd Update Claude 3.5 Sonnet model version to 20241022
- Update Claude 3.5 Sonnet model version from 20240620 to 20241022 across:
  - AnthropicApi model definitions
  - Integration tests
  - Sample events JSON
  - Documentation pages
- Upgrade Ollama container to 0.3.14 in tests
- Add llama3.2:1b model to Ollama tests
- Convert Ollama functionCallTest to parameterized test
2024-10-22 22:24:20 +02:00
Christian Tzolov
278a61fde4 Fix doc ref links 2024-10-21 10:24:28 +02:00
Mark Pollack
4c83fe8302 Guard against NPE in ZhiPu embedding model
- Update retry test to pass - needs investigation
2024-10-08 23:37:00 +02:00
Mark Pollack
4a892b5269 Release version 1.0.0-M3 2024-10-08 23:18:50 +02:00
Hyune-c
e12372171b Fix anthropicApi visibility in AnthropicChatModel 2024-10-06 22:41:23 +02:00
Thomas Vitale
3b1c68ac10 Improve optional values handling in chat model observations
Chat model observations support several key-value pairs, coming from the Spring AI abstractions.
Currently, whenever a value is not available (either because not configured by the user or not supported by the model provider), a span/metrics is generated anyway with value none.
That causes several issues, including an unneeded increase in time series, challenges in alerting/monitoring (especially for integer/double attributes that suddenly are populated with a string), and incompliance with the OpenTelemetry Semantic Conventions (according to which, attributes should be excluded altogether if there's no value).
This pull request changes the conventions for chat model observation to exclude the generation of span/metrics attributes for optional values which don't have any value.

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-10-05 23:55:16 +02:00
Christian Tzolov
9c10a08bef Add tool context support to chat options and enhance function calling
This commit adds support for tool context in various chat options classes across
  different AI model implementations and enhances function calling capabilities.

  The tool context allows passing additional contextual information to function callbacks.

 - Add toolContext field to chat options classes
 - Update builder classes to support setting toolContext
 - Enhance FunctionCallback interface to support context-aware function calls
 - Update AbstractFunctionCallback to implement BiFunction instead of Function
 - Modify FunctionCallbackWrapper to support both Function and BiFunction and
   to use the new SchemaType location
 - Add support for BiFunction in TypeResolverHelper
 - Update ChatClient interface and DefaultChatClient implementation to support
   new function calling methods with Function, BiFunction and FunctionCallback arguments
 - Refactor AbstractToolCallSupport to pass tool context to function execution
 - Update all affected <Model>ChatOptions with tool context support
 - Simplify OpenAiChatClientMultipleFunctionCallsIT test
 - Add tests for function calling with tool context
 - Add new test cases for function callbacks with context in various integration tests
 - Modify existing tests to incorporate new context-aware function calling capabilities
 - Add docs in in openai function calling

 Resolves #864, #1303, #991
2024-10-04 16:47:42 -04:00
Christian Tzolov
45c6622736 Fix support for FunctionCallingOptions across AI models
Add support for PortableFunctionCallingOptions across AI models

- Modify FunctionCallingOptions interface to extend ChatOptions for better integration
- Refactor option handling in chat models to accommodate both ChatOptions and FunctionCallingOptions
- Implement handling of FunctionCallingOptions in Anthropic, Azure OpenAI,
  MistralAI, Ollama, OpenAI, VertexAI Gemini, and other models
- Update existing function calling tests to use new FunctionCallingOptions.

Resolves #624
2024-10-04 15:50:59 -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
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
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
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
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
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
Johnny Lim
2d6117b796 Fix typos (#1223) 2024-08-15 10:27:12 -04:00
Thomas Vitale
bf84d5945e Streamline ChatOptions
* Surface more configuration APIs to ChatOptions
* Use abstraction in Observations directly instead of dedicated implementation
* Simplify metadata config in observations for defined models
* Improve merging of runtime and default options in OpenAI
* Fix missing option in Mistral AI

Relates to gh-1148

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-10 14:48:05 +02:00
kamosama
7c800f35b8 Fix a FunctionCallback inside a container that pollutes the global model's ChatOptions
The autoconfiguration adds the FunctionCallbacks directly to the model's ChatOptions,
 which results in the FunctionCallback being included in the request each time it is called.

 The modification registers the container's FunctionCallback directly to the model's functionCallbackRegister
 using the parent AbstractToolCallSupport constsructor.

 Replace the handleFunctionCallbackConfigurations by simplified runtimeFunctionCallbackConfigurations.

Co-authored-by Christian Tzolov <ctzolov@vmware.com>
2024-07-30 20:11:14 +02:00
Christian Tzolov
1c0c77b4eb Remove a redundant code line 2024-07-27 23:42:00 +02:00
Christian Tzolov
6363352b82 Improve Anthropic function calling
- factor out the common funciton calling logic form AnthropicChatModel to the AbstractToolCallSupport.
 - improve the AbstractToolCallSupport isToolCall to handle OpenAi and Anthropic.
 - fix an issue with the function calling streaming aggreagation leading to lost usage statistics.
 - small code improvements for OpenAiChatModel.
2024-07-25 18:13:26 +02:00
GR
5642521e74 fix: AbstractToolCallSupport wrong method name 2024-07-25 09:22:13 +02:00
Christian Tzolov
4392f1b72a Relax AbstractMessage parameter constrains. Remove jackson auto-module registrattion 2024-07-20 11:14:18 +02:00
Mark Pollack
5aafea872d Improve Message object hierarchy
- SystemMessage no longer has Media
- Add MediaContent interface
- Clear redundant code
2024-07-19 23:05:46 +02:00
Christian Tzolov
5dc9480eea Anthropic: make the beta features header value configurable
- add new spring.ai.anthropic.beta-version property.
  - add ITs.

  Resolves #1068
2024-07-18 20:54:28 +02:00
Christian Tzolov
018bb2c600 Ensure that ChatClient copies the input chatoptions
- introduce copy() method to the ChatOptions.
 - make sure that the DefaultChatClientRequestSpec takes a copy of the input chat options to prevent multation.
 - add a OpenAiChatClientMultipleFunctionCallsIT to reproduce the problem and verify the solution.

 Resolves #1064
2024-07-18 18:56:28 +02:00
Thomas Vitale
40d8671f3e Improve new function calling strategy
- Streamline the Generation constructors

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-07-18 14:18:45 +02:00
Mark Pollack
97f443d615 Update to ResponseMetadata design
* Remove inheritance from HashMap
* No more subclasses per model provider
* Builder class for ChatResponse
* Fix the AbstractResponseMetadata#AI_METADATA_STRING parameter order
* ChatResponseMetadata ignore Null values.
2024-07-18 12:48:28 +02:00
Sinsy
c9a3e66f90 fix: test name error (#1035)
* fix: test name error

* fix: test name error
2024-07-17 11:07:30 +02:00
Christian Tzolov
ee19ee1db9 Add high-level function calling support for Anthropic
- simplify and unify the AbstractToolCallSupport::executeFuncitons. Fix filed typos
 - remove old classes
2024-07-15 13:57:38 +02:00
Christian Tzolov
c70c20b79d Add VertexAI Embedding Model support
- add new spring-ai-vertex-ai-embedding project.
 - add VertexAiTextEmbeddingModel and VertexAiMultimodalEmbeddingMode with related options configuration classes.
 - add ITs
 - add auto-configuraiton and boot starters.
 - register to BOM.
 - add documentation.
 - add multimodal embedding documentation
 - extend the Embedding metdata so that it can keep references to the source document's data, Id, mediatype

 Resolves #1013
 Related to #1009
2024-07-10 15:36:13 -04:00
Christian Tzolov
d8be884cb1 Improve Anthropic Claude streaming handling
- add StreamEven API domain model for reliably parsing stream events.
 - add StreamHelper#mergeToolUseEvents to aggregate partial tool use jsons into a list of ContentBlocks.
 - add StreamHelper#eventToChatCompletionResponse to convert Flux<StreamEvents> into Flux<ChatCompletionResponse>.
 - Rename MediaContent -> ContentBlock, RequestMessage -> AnthropicMessage, ChatCompletion -> ChatCompletionResponse.
 - Improve tests and docs.
2024-07-02 23:36:25 +02:00
Mariusz Bernacki
f5b72242c0 Add Anthropic Claude streaming Function Calling support
Resolves #930
2024-07-02 10:28:31 +02:00