# Observability for Spring AI (Java) with Opik

> Start here to integrate Opik into your Spring AI-based genai application for end-to-end LLM observability, unit testing, and optimization.

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.
The easiest way to start is to use the [OPIK SpringAI starter](https://github.com/comet-ml/opik-springai-demo) project.

### Prerequisites

Before running the demo application, ensure you have the following installed:

* **Java 21** or higher
* **Maven 3.6+** for dependency management and building
* **OpenAI API Key** (only for Opik Cloud) - Sign up at [OpenAI Platform](https://platform.openai.com/)
* **OPIK API Key** - Sign up at [Comet OPIK](https://www.comet.com/opik)

### Installation

#### 1. Clone the Repository

```bash
git clone git@github.com:comet-ml/opik-springai-demo.git
cd opik-springai-demo
```

#### 2. Verify Java Installation

```bash
java --version
```

Ensure you have Java 21 or higher installed.

#### 3. Verify Maven Installation

```bash
mvn --version
```

#### 4. Install Dependencies

```bash
mvn clean install
```

## Environment configuration

The application requires the following environment variables to be set:

#### Required Variables

* **OPENAI\_API\_KEY**: Your OpenAI API key
* **OTEL\_EXPORTER\_OTLP\_ENDPOINT**: OPIK OpenTelemetry endpoint
* **OTEL\_EXPORTER\_OTLP\_HEADERS**: Authorization headers for OPIK

Configure your environment variables based on your Opik deployment:

<Tabs>
  <Tab value="Opik Cloud" title="Opik Cloud">
    If you are using Opik Cloud, you will need to set the following
    environment variables:

    ```bash wordWrap
    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'
    ```

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

      ```bash wordWrap
      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.
    </Tip>
  </Tab>

  <Tab value="Enterprise deployment" title="Enterprise deployment">
    If you are using an Enterprise deployment of Opik, you will need to set the following
    environment variables:

    ```bash wordWrap
    export OTEL_EXPORTER_OTLP_ENDPOINT=https://<comet-deployment-url>/opik/api/v1/private/otel
    export OTEL_EXPORTER_OTLP_HEADERS='Authorization=<your-api-key>,Comet-Workspace=default'
    ```

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

      ```bash wordWrap
      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.
    </Tip>
  </Tab>

  <Tab value="Self-hosted instance" title="Self-hosted instance">
    If you are self-hosting Opik, you will need to set the following environment
    variables:

    ```bash
    export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:5173/api/v1/private/otel
    ```

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

      ```bash
      export OTEL_EXPORTER_OTLP_HEADERS='projectName=<your-project-name>'
      ```
    </Tip>
  </Tab>
</Tabs>

## Using Opik with Spring AI

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

```yaml
spring:
  application:
    name: spring-ai-opik-demo
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
      chat:
        options:
          model: gpt-4o
          temperature: 0.7
server:
  port: 8085