Getting Started

This section offers quick guidance on how to get started with using Spring AI.

API Keys

To use OpenAI or Azure OpenAI, you need to generate an API key.

OpenAI

Create an account at OpenAI signup page and generate the token on the API Keys page.

The Spring AI project defines a configuration property named spring.ai.openai.api-key that you should set to the value of the API Key obtained from openai.com.

Exporting an environment variable is one way to set that configuration property:

export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>

Azure OpenAI

Obtain your Azure OpenAI endpoint and api-key from the Azure OpenAI Service section on the Azure Portal.

The Spring AI project defines a configuration property named spring.ai.azure.openai.api-key that you should set to the value of the API Key obtained from Azure. There is also a configuration property named spring.ai.azure.openai.endpoint that you should set to the endpoint URL obtained when provisioning your model in Azure.

Exporting environment variables is one way to set these configuration properties:

export SPRING_AI_AZURE_OPENAI_API_KEY=<INSERT KEY HERE>
export SPRING_AI_AZURE_OPENAI_ENDPOINT=<INSERT ENDPOINT URL HERE>

Dependencies

The Spring AI project provides artifacts in the Spring Milestone Repository. You need to add configuration to add a reference to the Spring Milestone repository in your build file. For example, in Maven, add the following repository definition:

  <repositories>
    <repository>
      <id>spring-snapshots</id>
      <name>Spring Snapshots</name>
      <url>https://repo.spring.io/snapshot</url>
      <releases>
        <enabled>false</enabled>
      </releases>
    </repository>
  </repositories>

Add the Spring Boot Starter, depending on whether you use Azure Open AI or Open AI:

  • Azure OpenAI

    <dependency>
        <groupId>org.springframework.experimental.ai</groupId>
        <artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
        <version>0.7.1-SNAPSHOT</version>
    </dependency>
  • OpenAI

    <dependency>
        <groupId>org.springframework.experimental.ai</groupId>
        <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
        <version>0.7.1-SNAPSHOT</version>
    </dependency>

Spring CLI

The Spring CLI makes it easy to create new applications with code in your terminal window. Think of it as the 'create-react-app' of Spring for those familiar with the JavaScript ecosystem.

Download the latest Spring CLI Release and follow the instructions to add spring to your PATH.

Create a simple AI application:

  • For OpenAI

spring boot new ai
  • For Azure OpenAI

spring boot new ai-azure

You can also add the same simple AI application to your current project by using:

  • For OpenAI

spring boot add ai
  • For Azure OpenAI

spring boot add ai-azure

There is a project catalog available for Azure OpenAI that covers more functionality. Add the catalog by running the the following command:

spring project-catalog add ai-azure

Now you have the following projects that you can use to create a new project by using the spring boot new command or add to your existing project by using the spring boot add command.

spring project list

├──────────────────────────┼───────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────┤
│ai-azure-hello-world      │AI - Hello,    │https://github.com/rd-1-2022/ai-azure-openai-helloworld             │ai-azure│[java-17,      │
│                          │World          │                                                                    │        │boot-3.1.x, ai,│
│                          │               │                                                                    │        │azure, web]    │
├──────────────────────────┼───────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────┤
│ai-azure-prompt-templating│AI - Prompt    │https://github.com/rd-1-2022/ai-azure-prompt-template               │ai-azure│[java-17,      │
│                          │Templating     │                                                                    │        │boot-3.1.x, ai,│
│                          │               │                                                                    │        │azure, prompt  │
│                          │               │                                                                    │        │templating]    │
├──────────────────────────┼───────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────┤
│ai-azure-prompt-roles     │AI - Prompt    │https://github.com/rd-1-2022/ai-azure-openai-prompt-roles           │ai-azure│[java-17,      │
│                          │Templating     │                                                                    │        │boot-3.1.x, ai,│
│                          │               │                                                                    │        │azure, prompt  │
│                          │               │                                                                    │        │roles]         │
├──────────────────────────┼───────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────┤
│ai-azure-stuff-prompt     │AI - Stuff the │https://github.com/rd-1-2022/ai-azure-stuff-prompt                  │ai-azure│[java-17,      │
│                          │prompt         │                                                                    │        │boot-3.1.x, ai,│
│                          │               │                                                                    │        │azure, prompt  │
│                          │               │                                                                    │        │stuff]         │
├──────────────────────────┼───────────────┼────────────────────────────────────────────────────────────────────┼────────┼───────────────┤
│ai-azure-prompt-rag       │AI - Retrieval │https://github.com/rd-1-2022/ai-azure-retrieval-augmented-generation│ai-azure│[java-17,      │
│                          │Augmented      │                                                                    │        │boot-3.1.x, ai,│
│                          │Generation     │                                                                    │        │azure,         │
│                          │               │                                                                    │        │retrieval]     │
└──────────────────────────┴───────────────┴────────────────────────────────────────────────────────────────────┴────────┴───────────────┘