Commit Graph

275 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
40714c984d Make JsonReader thread-safe
Resolves #1365
2024-09-16 13:34:39 +02:00
Christian Tzolov
0041afac99 Set default advise stream response mode to on-finish 2024-09-11 12:09:42 +02:00
Laura Trotta
c658c7b52e GH-1341: Fixing bug in TokenCountBatchingStrategy
Fixes: https://github.com/spring-projects/spring-ai/issues/1341

* Instead of clearing the current batch, create a new instance each time it needs to be reset
* added author
2024-09-10 11:35:24 -04:00
Christian Tzolov
da5ad96d57 fix javadoc erros 2024-09-05 21:58:09 +02: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
fk
2c1f36c725 Fix problem with VectorStoreChatMemoryAdvisor using pgvector
- add integration test
2024-09-05 15:37:59 +02:00
Soby Chacko
73d0b301d6 Enhance TokenCountBatchingStrategy with reserve percentage
- Precompute document token counts before batching into List<List<Document>>
- Introduce configurable reserve percentage for max input token count

Resolves #1260
2024-09-04 17:01:04 -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
Mark Pollack
2f46f04983 disable javadoc linting 2024-08-23 12:55:47 -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
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
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
Mark Pollack
99d679f232 Add documentation for Observability 2024-08-21 17:26:03 -04:00
Christian Tzolov
3b014db165 Add ChatClient Advisor Observability support
- Add advisor context, convetions and documentation
- Add ObservableRequestResponseAdvisor wrapper.
- Add Tests

Resolves #1258
2024-08-21 14:28:24 -04:00
Mark Pollack
dad30f06d9 Add ChatOptions to PromptTemplate create methods
Enable ChatOptions configuration when creating Prompts from Templates,

Co-authored-by: zhangqian9158 <zhangqian9158@users.noreply.github.com>
2024-08-20 18:19:14 -04:00
Vivek Alpeshbhai Sonani
528dc0438a Unit tests for Generation class 2024-08-20 15:37:43 -04:00
rivkode
1d6cce2419 Add JSON array handling to JsonReader
Enhance JsonReader to detect and process both JSON objects and arrays at
the root level. This update introduces type checking for the JSON root node
using JsonNode and leverages the Stream API to handle JSON arrays.

- Refactor parsing logic to support both JSON arrays and objects
- Rename variable: resourceArray to arrayResource for clarity
- Convert for-loop to Stream API for processing JSON keys
- Add additional tests to ensure proper array handling
2024-08-20 15:06:19 -04:00
Mark Pollack
0f52ca510f Simply implementation of RelevancyEvaluator
Co-authored-by: Lee KyuHwun Lee-Kyuhwun@users.noreply.github.com
2024-08-20 14:46:27 -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
93fa2bf45a Add observability support to existing vector stores
Add observability support to:
 - Cassandra
 - Chroma
 - Elasticsearch
 - Milvus
 - Neo4j
 - OpenSearch
 - Qdrant
 - Redis
 - Typesense
 - Weaviate
 - Pinecone
 - Oracle
 - Gemifire
 - MongoDB
 - HanaDB

 Add autoconfiguration obsrvability for the above vector stores.
 Add integration tests for all vector stores.
2024-08-20 01:37:45 -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
3fa102e78f Prompt content and completion as span events
Signed-off-by: Thomas Vitale <ThomasVitale@users.noreply.github.com>
2024-08-20 00:59:43 -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
Christian Tzolov
bc3f9acb86 Add observability support to VectorStore
Implementation:
- Introduce AbstractObservationVectorStore with instrumentation for add, delete, and similaritySearch methods
- Create VectorStoreObservationContext to capture operation details
- Implement DefaultVectorStoreObservationConvention for naming and tagging
- Add VectorStoreObservationDocumentation for defining observation keys
- Create VectorStoreObservationAutoConfiguration for auto-configuring observations
- Add VectorStoreObservationProperties to control optional observation content filters
- Update VectorStore interface with getName() method
- Modify PgVectorStore and SimpleVectorStore to extend AbstractObservationVectorStore
- Add vector_store Spring AI kind

Filters:
- Implement VectorStoreQueryResponseObservationFilter
- Add VectorStoreDeleteRequestContentObservationFilter and VectorStoreAddRequestContentObservationFilter

Enhancements:
- Update PgVectorStoreAutoConfiguration to support observations
- Add observation support to PgVectorStore's Builder
- Add VectorStoreObservationContext.Operation enum with ADD, DELETE, and QUERY options

Tests:
- Add tests for VectorStore context, convention, and filters
- Add VectorStoreObservationAutoConfiguration tests
- Add PgVectorObservationIT

Resolves #1205
2024-08-16 10:33:50 -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
Christian Tzolov
66e4b8868f Add ObservationRegistry support to ChatClient
- Implement observable chat responses in DefaultChatClient
 - Add ChatClientObservationContext and related classes for metrics
 - Update ChatClient and builder methods to support ObservationRegistry
 - Enhance RequestResponseAdvisor with getName() method
 - Add ChatClient streaming observability support
 - Introduce ChatClientObservationDocumentation for metric key names
 - Create DefaultChatClientObservationConvention for implementing conventions
 - Add ChatClientInputContentObservationFilter for optional input content logging
 - Update ChatClientAutoConfiguration to include new observation components
 - Extend ChatClientBuilderProperties with observation configuration options
 - Add unit tests for new observation classes and configurations
 - Update AiOperationType and AiProvider enums with new values
 - Implement safeguards and warnings for sensitive data in observations

 Resolves #1206
2024-08-11 18:26:53 +02: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
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
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
Christian Tzolov
4cacbe8468 Refactor Ollama embedding model implementation
- Update OllamaEmbeddingModel to support batch embedding requests
- Rename EmbeddingRequest/Response to EmbeddingsRequest/Response
- Add truncate option to control input truncation
- Update documentation and tests for new embedding API
- Remove deprecated withModel and withDefaultOptions methods
- Adjust default values for various Ollama model options
- Include response medata with response model name

Resolves #1158
2024-08-04 11:21:45 +02:00
Soby Chacko
8348e92944 Minor code cleanup in EmbeddingOptions 2024-08-02 14:44:58 -04: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
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
6978e8f111 Streamline the VertexAI Gemini Function Calling
- Align with AbstractToolCallSupport
2024-07-30 10:46:38 +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
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
Mark Pollack
7775a76f51 Refactor SummaryMetadataEnricher for readability 2024-07-24 17:37:34 -04: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
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
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
Christian Tzolov
f0e555ea3c java format 2024-07-20 11:28:16 +02:00
Christian Tzolov
4392f1b72a Relax AbstractMessage parameter constrains. Remove jackson auto-module registrattion 2024-07-20 11:14:18 +02:00