Spring AI Integration via OpenTelemetry

Spring AI is a framework designed to simplify the integration of AI and machine learning capabilities into Spring applications. It provides a familiar Spring-based programming model for working with AI models, vector stores, and AI-powered features, making it easier to build intelligent applications within the Spring ecosystem.

Spring AI’s primary advantage is its seamless integration with the Spring framework, allowing developers to leverage Spring’s dependency injection, configuration management, and testing capabilities while building AI-powered applications.

Getting started

To use the Spring AI integration with Opik, you will need to have Spring AI and the required OpenTelemetry packages installed:

1<dependencies>
2 <!-- Spring AI -->
3 <dependency>
4 <groupId>org.springframework.ai</groupId>
5 <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
6 <version>0.8.0</version>
7 </dependency>
8
9 <!-- OpenTelemetry -->
10 <dependency>
11 <groupId>io.opentelemetry</groupId>
12 <artifactId>opentelemetry-api</artifactId>
13 <version>1.36.0</version>
14 </dependency>
15 <dependency>
16 <groupId>io.opentelemetry</groupId>
17 <artifactId>opentelemetry-sdk</artifactId>
18 <version>1.36.0</version>
19 </dependency>
20 <dependency>
21 <groupId>io.opentelemetry</groupId>
22 <artifactId>opentelemetry-exporter-otlp</artifactId>
23 <version>1.36.0</version>
24 </dependency>
25 <dependency>
26 <groupId>io.opentelemetry</groupId>
27 <artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
28 <version>1.36.0</version>
29 </dependency>
30</dependencies>

Environment configuration

Configure your environment variables based on your Opik deployment:

If you are using Opik Cloud, you will need to set the following environment variables:

$export OTEL_EXPORTER_OTLP_ENDPOINT=https://www.comet.com/opik/api/v1/private/otel
>export OTEL_EXPORTER_OTLP_HEADERS='Authorization=<your-api-key>,Comet-Workspace=default'

To log the traces to a specific project, you can add the projectName parameter to the OTEL_EXPORTER_OTLP_HEADERS environment variable:

$export OTEL_EXPORTER_OTLP_HEADERS='Authorization=<your-api-key>,Comet-Workspace=default,projectName=<your-project-name>'

You can also update the Comet-Workspace parameter to a different value if you would like to log the data to a different workspace.

Using Opik with Spring AI

Set up OpenTelemetry instrumentation for Spring AI in your application.properties:

1# OpenTelemetry configuration
2otel.traces.exporter=otlp
3otel.exporter.otlp.endpoint=${OTEL_EXPORTER_OTLP_ENDPOINT}
4otel.exporter.otlp.headers=${OTEL_EXPORTER_OTLP_HEADERS}

Your Spring AI code will now automatically send traces to Opik:

1@Service
2public class AiService {
3
4 private final AiClient aiClient;
5
6 public AiService(AiClient aiClient) {
7 this.aiClient = aiClient;
8 }
9
10 public String generateResponse(String prompt) {
11 return aiClient.generate(prompt);
12 }
13}

Further improvements

If you have any questions or suggestions for improving the Spring AI integration, please open an issue on our GitHub repository.