-
Notifications
You must be signed in to change notification settings - Fork 308
Add Spring AI integration documentation for Java SDK #4454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
40a5146
2dc8343
b0a6398
49df59c
8e5f4ea
b9edcf2
45fe400
658eb2f
6b743b2
a27d432
9811114
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,169 @@ | ||||||||
| --- | ||||||||
| id: spring-ai | ||||||||
| title: Spring AI integration - Java SDK | ||||||||
| sidebar_label: Spring AI | ||||||||
| slug: /develop/java/integrations/spring-ai | ||||||||
| toc_max_heading_level: 2 | ||||||||
| keywords: | ||||||||
| - ai | ||||||||
| - agents | ||||||||
| - spring ai | ||||||||
| - java | ||||||||
| - integrations | ||||||||
| tags: | ||||||||
| - Spring AI | ||||||||
| - Java SDK | ||||||||
| - Temporal SDKs | ||||||||
| - Integrations | ||||||||
| - AI Frameworks | ||||||||
| description: Build durable AI Workflows in Java with the Temporal Spring AI integration. | ||||||||
| --- | ||||||||
|
|
||||||||
| The [Temporal Spring AI integration](https://central.sonatype.com/artifact/io.temporal/temporal-spring-ai) lets you call [Spring AI](https://docs.spring.io/spring-ai/reference/) models, tools, vector stores, embeddings, and MCP servers from Temporal Workflows as durable Activities. Model calls and tool invocations are recorded in Workflow history, so they retry on failure and replay deterministically — without you having to change how you write Spring AI code. | ||||||||
|
donald-pinckney marked this conversation as resolved.
Outdated
|
||||||||
|
|
||||||||
| The integration is built on the Temporal Java SDK's [Plugin system](/develop/plugins-guide) and is distributed as the `io.temporal:temporal-spring-ai` module alongside the existing [Spring Boot integration](/develop/java/integrations/spring-boot-integration). | ||||||||
|
|
||||||||
| :::info | ||||||||
|
|
||||||||
| The Spring AI Integration is in Public Preview. Refer to the | ||||||||
| [Temporal product release stages guide](/evaluate/development-production-features/release-stages) for more information. | ||||||||
|
|
||||||||
| ::: | ||||||||
|
|
||||||||
| ## Compatibility | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This reads more like Prerequisites than compatibility? Let's be a bit more explicit that the user needs all these to get the integration to work |
||||||||
|
|
||||||||
| | Dependency | Minimum version | | ||||||||
| | ----------------- | --------------- | | ||||||||
| | Java | 17 | | ||||||||
| | Spring Boot | 3.x | | ||||||||
| | Spring AI | 1.1.0 | | ||||||||
| | Temporal Java SDK | 1.35.0 | | ||||||||
|
donald-pinckney marked this conversation as resolved.
|
||||||||
|
|
||||||||
| ## Add the dependency | ||||||||
|
|
||||||||
| Add `temporal-spring-ai` alongside `temporal-spring-boot-starter` and a Spring AI model starter (for example, `spring-ai-starter-model-openai`). | ||||||||
|
|
||||||||
| **[Apache Maven](https://maven.apache.org/):** | ||||||||
|
|
||||||||
| ```xml | ||||||||
| <dependency> | ||||||||
| <groupId>io.temporal</groupId> | ||||||||
| <artifactId>temporal-spring-ai</artifactId> | ||||||||
| <version>${temporal-sdk.version}</version> | ||||||||
| </dependency> | ||||||||
| ``` | ||||||||
|
|
||||||||
| **[Gradle Groovy DSL](https://gradle.org/):** | ||||||||
|
|
||||||||
| ```groovy | ||||||||
| implementation "io.temporal:temporal-spring-ai:${temporalSdkVersion}" | ||||||||
| ``` | ||||||||
|
|
||||||||
| When `temporal-spring-ai` is on the classpath, the `SpringAiPlugin` auto-registers `ChatModelActivity` with all Temporal Workers created by the Spring Boot integration. Optional Activities are auto-configured when their dependencies are present: | ||||||||
|
|
||||||||
| | Feature | Dependency | Registered Activity | | ||||||||
| | ------------ | --------------- | ------------------------ | | ||||||||
| | Vector store | `spring-ai-rag` | `VectorStoreActivity` | | ||||||||
| | Embeddings | `spring-ai-rag` | `EmbeddingModelActivity` | | ||||||||
| | MCP | `spring-ai-mcp` | `McpClientActivity` | | ||||||||
|
|
||||||||
| ## Call a chat model from a Workflow | ||||||||
|
|
||||||||
| Use `ActivityChatModel` as a Spring AI `ChatModel` inside a Workflow. Every call goes through a Temporal Activity, so model responses are durable and retried per your Activity options. | ||||||||
|
|
||||||||
| Wrap `ActivityChatModel` in a `TemporalChatClient` to build prompts and register tools: | ||||||||
|
|
||||||||
| ```java | ||||||||
|
donald-pinckney marked this conversation as resolved.
|
||||||||
| @WorkflowInit | ||||||||
| public MyWorkflowImpl(String goal) { | ||||||||
| ActivityChatModel chatModel = ActivityChatModel.forDefault(); | ||||||||
|
|
||||||||
| WeatherActivity weather = Workflow.newActivityStub( | ||||||||
| WeatherActivity.class, activityOptions); | ||||||||
|
|
||||||||
| this.chatClient = TemporalChatClient.builder(chatModel) | ||||||||
| .defaultSystem("You are a helpful assistant.") | ||||||||
| .defaultTools(weather, new TimestampTools()) | ||||||||
| .build(); | ||||||||
| } | ||||||||
|
|
||||||||
| @Override | ||||||||
| public String run(String goal) { | ||||||||
| return chatClient.prompt().user(goal).call().content(); | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| `ActivityChatModel.forDefault()` resolves to the default Spring AI `ChatModel` bean. To target a specific model in a multi-model application, pass its bean name to `ActivityChatModel.forModel("openai")`. | ||||||||
|
|
||||||||
| :::note | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
our prettier config would format the next line onto the :::note line if there is no space |
||||||||
| Streaming responses are not currently supported. | ||||||||
| ::: | ||||||||
|
|
||||||||
| ## Register tools | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we give a brief explanation of what it means to register tool and link to Spring AI docs? |
||||||||
|
|
||||||||
| Tools passed to `defaultTools()` are dispatched based on their type. The integration handles Temporal determinism for you when the tool is durable, and gives you control when it isn't. | ||||||||
|
|
||||||||
| ### Activity stubs | ||||||||
|
|
||||||||
| An interface annotated with both `@ActivityInterface` and Spring AI `@Tool` methods is auto-detected and executed as a Temporal Activity. Use this for external calls that need retries and timeouts. | ||||||||
|
|
||||||||
| ```java | ||||||||
| @ActivityInterface | ||||||||
| public interface WeatherActivity { | ||||||||
| @Tool(description = "Get weather for a city") | ||||||||
| @ActivityMethod | ||||||||
| String getWeather(String city); | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| ### Nexus service stubs | ||||||||
|
|
||||||||
| Nexus service stubs with `@Tool` methods are auto-detected and invoked as [Nexus operations](/develop/java/nexus), enabling cross-Namespace tool calls. | ||||||||
|
|
||||||||
| ### `@SideEffectTool` | ||||||||
|
|
||||||||
| Classes annotated with `@SideEffectTool` have each `@Tool` method wrapped in `Workflow.sideEffect()`. The result is recorded in history on first execution and replayed from history afterward. Use this for cheap, non-deterministic operations such as timestamps or UUIDs. | ||||||||
|
|
||||||||
| ```java | ||||||||
| @SideEffectTool | ||||||||
| public class TimestampTools { | ||||||||
| @Tool(description = "Get current time") | ||||||||
| public String now() { | ||||||||
| return Instant.now().toString(); | ||||||||
| } | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| ### Plain tools | ||||||||
|
|
||||||||
| Any class with `@Tool` methods that isn't an Activity stub, Nexus stub, or `@SideEffectTool` runs directly on the Workflow thread. Use this for inherently deterministic tools (such as updating in-memory agent state), or for orchestration of durable primitives as you need, e.g. calling multiple Activities, child Workflows, wait conditions, or other Temporal durable primitives. | ||||||||
|
|
||||||||
| ```java | ||||||||
| public class MyTools { | ||||||||
| @Tool(description = "Process data") | ||||||||
| public String process(String input) { | ||||||||
| SomeActivity act = Workflow.newActivityStub(SomeActivity.class, opts); | ||||||||
| return act.doWork(input); | ||||||||
| } | ||||||||
| } | ||||||||
| ``` | ||||||||
|
|
||||||||
| ## Use vector stores, embeddings, and MCP | ||||||||
|
|
||||||||
| When the corresponding Spring AI modules are on the classpath, the integration registers Activities for vector stores, embeddings, and MCP tool calls. Inject the matching Spring AI types into your Activities or Workflows and use them as you would in any Spring AI application — each operation is executed through a Temporal Activity. | ||||||||
|
|
||||||||
| You can also register these plugins explicitly, without relying on auto-configuration: | ||||||||
|
|
||||||||
| ```java | ||||||||
| new VectorStorePlugin(vectorStore); | ||||||||
| new EmbeddingModelPlugin(embeddingModel); | ||||||||
| new McpPlugin(); | ||||||||
| ``` | ||||||||
|
|
||||||||
| `ActivityMcpClient` wraps a Spring AI MCP client so that remote MCP tool calls become durable Activity executions. | ||||||||
|
|
||||||||
| ## Learn more | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
|
||||||||
| - [`temporal-spring-ai` README](https://github.com/temporalio/sdk-java/blob/master/temporal-spring-ai/README.md) — full reference for the module | ||||||||
| - [Spring Boot integration](/develop/java/integrations/spring-boot-integration) — required companion module | ||||||||
| - [Plugin system](/develop/plugins-guide) — how integrations are registered with Workers and Clients | ||||||||
Uh oh!
There was an error while loading. Please reload this page.