Spring AI with Open Ai

Integrating OpenAI ChatGPT with Spring AI

With the advent of AI-driven technologies, Spring AI now supports OpenAI's ChatGPT, a powerful language model that has significantly advanced the capabilities of text generation in software applications. In this blog, we will delve into the process of integrating ChatGPT with Spring AI, explaining key concepts and providing practical examples to help you get started.

Getting Started: Obtaining an OpenAI API Key

The first step towards integrating ChatGPT into your Spring application is obtaining an API key from OpenAI, which serves as your secret key for accessing the API. Here’s how to do it:

  1. Visit the OpenAI website and sign up for an account.
  2. Once your account is set up, navigate to the API section and select the 'API Keys' page.
  3. Click on 'Create new key'. This will generate a new API key that you can use in your application.
  4. Copy this key and secure it. You will need to add this key to your Spring application’s environment variables.

Prerequisites

Before integrating ChatGPT into your Spring application, you must first set up an OpenAI API account. Start by creating an account on the OpenAI signup page and generate an API key on the API Keys page. This key will be used in your application to authenticate API requests.

Setting Up Your Environment

Configure your Spring AI project by exporting the API key as an environment variable:

export SPRING_AI_OPENAI_API_KEY=<INSERT KEY HERE>

Dependency Management

To manage dependencies effectively, add the Spring AI Bill of Materials (BOM) and relevant repositories to your build system. This ensures consistent versions across your project.

Enabling Auto-configuration

Spring Boot simplifies the integration of OpenAI ChatGPT through auto-configuration. Add the spring-ai-openai-spring-boot-starter dependency to your Maven or Gradle configuration:


// Maven
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

// Gradle
dependencies {
    implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}

Configuration and Usage

Several properties can be configured to customize the behavior of the ChatGPT client. For instance, you can set retry policies, connection settings, and chat-specific options like model choice and response format.

You can write either application.properties or, application.yml as per your choice. Here are some examples of how to configure these settings:

application.properties


# OpenAI API key
openai.api.key=your-api-key-here

# Model choice (e.g., gpt-3.5-turbo, gpt-4)
openai.model=gpt-3.5-turbo

# Maximum number of tokens in the response
openai.max-tokens=2048

# Temperature setting for response creativity (0.0 to 1.0)
openai.temperature=0.7

# Retry policies
openai.retry.max-attempts=3
openai.retry.backoff-period=2000

# Connection settings
openai.connection.timeout=5000
openai.read-timeout=5000

# Proxy settings (if needed)
openai.proxy.host=proxy.example.com
openai.proxy.port=8080

# Custom headers (if needed)
openai.headers.X-Custom-Header=custom-value

application.yml


openai:
  api:
    key: your-api-key-here
  model: gpt-3.5-turbo
  max-tokens: 2048
  temperature: 0.7
  retry:
    max-attempts: 3
    backoff-period: 2000
  connection:
    timeout: 5000
    read-timeout: 5000
  proxy:
    host: proxy.example.com
    port: 8080
  headers:
    X-Custom-Header: custom-value

Advanced Features: Function Calling with Example

OpenAI ChatGPT also supports function calling, allowing the model to generate JSON outputs that can invoke registered Java functions. This bridges the gap between the AI's text generation capabilities and practical application functionalities.

Example Implementation

Below is an example of how to set up a simple Spring Boot controller to utilise the ChatGPT client:


@RestController
public class ChatController {
    private final OpenAiChatClient chatClient;

    @Autowired
    public ChatController(OpenAiChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @GetMapping("/ai/generate")
    public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", chatClient.call(message));
    }
}

Conclusion

Integrating OpenAI's ChatGPT with Spring AI enhances your applications by enabling sophisticated AI-driven text generation capabilities. By following the steps outlined above, you can successfully set up and customise the OpenAI Chat client within your Spring AI environment, paving the way for innovative applications that leverage cutting-edge AI technology.

For a deeper dive into more advanced configurations and features, refer to the full documentation provided by Spring AI and OpenAI.

Also Read:  Spring Boot with Ollama

Comments

Popular Posts on Code Katha

Java Interview Questions for 10 Years Experience

Sql Interview Questions for 10 Years Experience

Spring Boot Interview Questions for 10 Years Experience

Java interview questions - Must to know concepts

Visual Studio Code setup for Java and Spring with GitHub Copilot

Spring Data JPA

Data Structures & Algorithms Tutorial with Coding Interview Questions

Java interview questions for 5 years experience

Spring AI with Ollama

Elasticsearch Java Spring Boot