Commit Graph

196 Commits

Author SHA1 Message Date
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
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
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
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
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
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
Seongmin Kim
58c292ab87 Remove unnecessary if statement in test code
* Remove unnecessary if statement in 'toolFunctionCall' test code from OpenAiApiToolFunctionCallIT
* Reformat using java-format plugin
2024-08-19 15:40:41 -04:00
uzhuraul
c2b6e85608 Fix OpenAiAudioSpeechOptions speed description
formatting
2024-08-19 15:26:41 -04:00
Thomas Vitale
10e1e13fa2 Add observability support for Ollama
- Improve ITs to reuse a single container across tests
2024-08-16 14:29:23 -04:00
KAMOsama
3bef5c12bf Remove redundant withRateLimit call from OpenAiChatModel.
The withRateLimit call is redundant as rateLimit is already handled conditionally elsewhere. Its removal helps simplify the code and improves maintainability.
2024-08-16 11:52:27 -04:00
Johnny Lim
2d6117b796 Fix typos (#1223) 2024-08-15 10:27:12 -04:00
Christian Tzolov
d538e00643 Replace the Embedding format from List<Double> to float[]
- Adjust all affected classes including the Document.
 - Update docs.

Related to #405
2024-08-13 11:53:08 -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
Christian Tzolov
af25430054 Update OpenAI chat UML diagram 2024-08-09 10:05:39 +02:00
Christian Tzolov
2da161cfb1 Improve OpenAI structured outputs support
- Add `TEXT` to the supported ResponseFormat types
- Add JsonSchema record for structured output configuration
- Update OpenAI chat documentation with details on Structured Outputs
- Clarify usage of JSON_SCHEMA response format in properties and code examples
- Update Structured Output Converter docs to mention OpenAI Structured Outputs
2024-08-09 08:22:58 +02:00
Christian Tzolov
91afed5ae5 OpenAi: Add support for structured outputs and JSON schema
- Added support for OpenAI's structured outputs feature, which allows specifying a JSON schema for the model to match
- Introduced new record to configure the desired response format
- Added support for configuring the response format via application properties or the chat options builder
- Extend teh BeanOutputConverter to help generate JSON schema from a target domain object and convert the response.
- Added comprehensive tests to cover the new response format functionality

Resolves #1196
2024-08-08 17:00:18 -04:00
TarasVovk669
866b262cdd Add refusal field to ChatCompletionMessage and related classes
- Updated OpenAiChatModel, OpenAiApi, and OpenAiStreamFunctionCallingHelper to include the `refusal` field in metadata.
- Adjusted constructors and methods to handle the new `refusal` attribute.
- Modified related tests to account for the new `refusal` field.
- Add the refusal field value to the Spring AI AssistantMessage metadata

Resolves #1178
2024-08-08 19:19:09 +02:00
Dariusz Jędrzejczyk
478f180297 Add OpenAiChatModel stream observability
Integrated Micrometer's Observation into the OpenAiChatModel#stream reactive chain.

Included changes:
 - Added ability to aggregate streaming responses for use in Observation metadata.
 - Improved error handling and logging for chat response processing.
 - Updated unit tests to include new observation logic and subscribe to Flux responses.
 - Refined validation of observations in both normal and streaming chat operations.
 - Disabled retry for streaming which used RetryTemplate - should use .retryWhen operator as the next step.
 - Added an integration test.

Resolves #1190

Co-authored-by Christian Tzolov <ctzolov@vmware.com>
2024-08-08 13:05:28 +02:00
Thomas Vitale
08ccc10a16 Streamline EmbeddingOptions
* Add model and dimensions to option abstraction
* Use abstraction in Observations directly instead of dedicated implementation
* Clean-up the merge of runtime and default embedding options in OpenAI

Relates to #gh-1148

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-08 11:35:08 +02:00
Thomas Vitale
17ba1fc3ba Streamline ImageOptions
* Add style to option abstraction
* Use abstraction in Observations directly instead of dedicated implementation
* Clean-up the merge of runtime and default image options in OpenAI and Stability AI

Related to #1148

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-07 10:19:08 +02:00
Thomas Vitale
8784dee16f Update convention for chat finish reason in LLM observations
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-07 09:25:06 +02:00
Thomas Vitale
9a8a133d93 Fix NPE in OpenAiUsage
Fix gh-1152

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-04 11:29:06 +02:00
Christian Tzolov
3978e8ecc2 Enhance OpenAI Authentication and Configuration
- Add org-id and project-id properties with unified merging logic
- Update autoconfig and docs for all OpenAI models
- Introduce OpenAiChatOptions#httpHeaders option
- Add integration test for httpHeaders and update docs

Resolves #1141
2024-08-03 22:30:37 +02:00
Thomas Vitale
12cf38a669 Make MeterObservationHandler conditional on MeterRegistry bean
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-02 22:11:47 +02:00
Thomas Vitale
6bdb395686 Initial observability for Spring AI
* Observation APIs for chat, embedding and image models
* Conventions based on OpenTelemetry Semantic Conventions for GenAI
* Instrumentation for OpenAI chat, embedding, and image models
* Autoconfiguration for observability for OpenAI

Fixes gh-953

Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-01 22:25:43 +02:00
Christian Tzolov
a077e1b952 patch for 3f19c4a6e5 2024-08-01 17:40:28 +02:00
Christian Tzolov
3f19c4a6e5 Add OpenAI paralel funciton call option
Resolves #1143
2024-08-01 17:11:11 +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
14b5397ca0 Streamline the MistralAI Function Calling
Align with the parent AbstractToolCallSupport and remove the redundant code.
2024-07-30 10:43:39 +02:00
Soby Chacko
9aee4aac4c GH-876: stream.content() swallowing blank space
* Fixing an issue where calling stream.content() method swallows leading
  blank spaces.

Resolves https://github.com/spring-projects/spring-ai/issues/1089
Resolves https://github.com/spring-projects/spring-ai/issues/1089
2024-07-29 17:08:16 +02:00
Christian Tzolov
aa18a67321 Document how to use Mistral AI over Spring AI OpenAI API.
- Add a related integration test: MistralWithOpenAiChatModelIT

Resolves #1134
2024-07-29 16:08:08 +02:00
Mark Pollack
27354cd616 Fix possible npes in openai response processing 2024-07-26 11:44:44 -04: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
Seol_JY
698a531dae Refactor ContentFormatTransformer for improved readability
- Extract Method
- Replace explicit instanceof checks and casting with pattern matching
- Change var to explicit type
2024-07-24 17:19:49 -04:00
Soby Chacko
4d41080d45 Integration test for OpenAI streaming completion 2024-07-24 16:32:15 -04:00
Christian Tzolov
d53876a624 Add Ollama Function Call support
- extend the OllamaApi with Tool, ToolCalls and Function and add Tool Role.
- make OllamaChatModel extend the AbstractToolCallSupport.
- extend the OllamaChatModel to convert the Spring AI messages to OllamaApi messages, including Tools.
- OllamaOptions implements FunctionCallingOptions.
- add OllamaApiToolFunctionCallIT for testing function calling.
- patch the AbstractToolCallSupport#isToolCall to take set of stop resons.
- add FunctionCallbackInPromptIT and FunctionCallbackWrapperIT function calling auto-configuration tests.
- extend OllamAutoConfiguration to support function calling registration.
- add function call tests to OllamChatAutoConfigurationIT.
- add OllamaWithOpenAiChatModelIT to OpenAi that uses the OpenAI API to call Ollama.
- move buildToolCallConversation and handleToolCalls to parent AbstractToolCallSupport.
- update Docs. add Ollama function call docs.
- update Ollama diagrams.

Resolves #720
2024-07-24 16:28:06 -04:00
Mark Pollack
ee696f33dd Make chat and embedding paths configurable
* Updated docs
* Minor code cleanup
2024-07-24 15:19:01 -04:00
Thomas Vitale
6c88654404 Consolidate retry config for OpenAI
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-07-23 18:22:39 +02:00
Christian Tzolov
6270d627f1 ChatClient register functions with explicit input type
The Lambda functions do not retain the type information, so we need to provide the input type explicitly.

 Resolves #1052

 Co-authored-by: liuzhifei <2679431923@qq.com>
2024-07-23 15:01:05 +02:00
Piotr Olaszewski
0e97f9c579 Introduce AzureOpenAI transcription support
- Breaking changes: Classes from the org.springframework.ai.openai.metadata.audio.transcription package have been moved to the org.springframework.ai.audio.transcription package.
 - The AzureOpenAiAudioTranscriptionModel has been added to the auto-configuration.
 - The spring.ai.azure.openai.audio.transcription prefix was introduced for properties.
 - Introduces options properties which cover all of them (see: AzureOpenAiAudioTranscriptionOptions).
 - fix missing MutableResponseMetadata
 - add docs
 - adjust code to updated ResponseMetadata design
 - add test to AzureOpenAiAutoConfiguration
 - add missing AzureOpenAiAudioTranscriptionModel tests
2024-07-22 14:08:11 +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
dongfeng
92b06346fd Update enum to include OpenAI's GPT-4o-mini model 2024-07-19 13:05:04 +02:00
Christian Tzolov
edf943ec97 Adjust tests 2024-07-18 19:39:43 +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
Christian Tzolov
588082285a OpenAI ChatModel: handle null finish reasons responses 2024-07-18 15:04:21 +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