Using OpenAI ChatGPT API in Spring Boot

1. Let’s start

Let's take a look at how to call the OpenAI ChatGPT API in Spring Boot.

We will create a Spring Boot application that will generate responses to prompts by calling the OpenAI ChatGPT API.

2、OpenAI ChatGPT API

Before we get into the details, let’s discuss the OpenAI ChatGPT API that we will be using in this tutorial. We will call the Create Chat Completion API to generate the response to the prompt.

2.1 API parameters and authentication

Let’s take a look at the mandatory request parameters of the API:

  • model: This is the version of the model we will send the request to. This model is available in several versions. We will use the gpt-3.5-turbo model, which is the latest public version of this model;
  • message: The message is a prompt to the model. Every message requires two fields: role and content. The role field specifies the sender of the message. In the request it will be "user" and in the response it will be "assistant". The content field is the actual message.

In order to authenticate with the API, we will generate an OpenAI API key. We will set this key in the Authorization header when calling the API.

An example request in cURL format looks like this:

$ curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-3.5-turbo",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Additionally, the API accepts a number of optional parameters to modify the response.

Next, we'll focus on a simple request, but let's look at some optional parameters that can help tailor the response:

  • n: can be specified if we want to increase the number of responses generated. The default value is 1;
  • temperature: Controls the randomness of the response. The default value is 1 (most random);
  • max_tokens: Used to limit the maximum number of tokens in the response. The default value is infinity, which means the response will be as long as the model can generate. In general, it is best to set this value to a reasonable number to avoid generating long responses and incurring high costs.

2.2 API Response

The API response will be a JSON object with some metadata and select fields. The select field will be an array of objects. Each object has a text field that contains the response to the prompt.

The number of objects in the selection array will be equal to the optional n parameter in the request. If the n argument is not specified, the options array will contain a single object.

Specific code:

{
    
    
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    
    
    "index": 0,
    "message": {
    
    
      "role": "assistant",
      "content": "\n\n 来啦,老弟……"
    },
    "finish_reason": "stop"
  }],
  "usage": {
    
    
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

The used field in the response will contain the prompt and the number of tokens used in the response. This is used to calculate the cost of API calls.

3. Specific cases

We will create a Spring Boot application using the OpenAI ChatGPT API.

To do this, we will create a Spring Boot Rest API that accepts a prompt as a request parameter, passes it to the OpenAI ChatGPT API, and returns the response as the response body.

3.1 Add dependencies

First, we create a Spring Boot project. We need the Spring Boot Starter Web dependencies for this project:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3.2 DTO

Next, we create a DTO corresponding to the request parameters of the OpenAI ChatGPT API:

public class ChatRequest {
    
    

    private String model;
    private List<Message> messages;
    private int n;
    private double temperature;

    public ChatRequest(String model, String prompt) {
    
    
        this.model = model;
        
        this.messages = new ArrayList<>();
        this.messages.add(new Message("user", prompt));
    }

    // getters and setters
}

Continue to define the Message class:

public class Message {
    
    

    private String role;
    private String content;

    // constructor, getters and setters
}

Then we create a DTO for the response:

public class ChatResponse {
    
    

    private List<Choice> choices;

    // constructors, getters and setters
    
    public static class Choice {
    
    

        private int index;
        private Message message;

        // constructors, getters and setters
    }
}

3.3 Controller

We create a controller that will accept the prompt as a request parameter and return the response as the response body:

@RestController
public class ChatController {
    
    
    
    @Qualifier("openaiRestTemplate")
    @Autowired
    private RestTemplate restTemplate;
    
    @Value("${openai.model}")
    private String model;
    
    @Value("${openai.api.url}")
    private String apiUrl;
    
    @GetMapping("/chat")
    public String chat(@RequestParam String prompt) {
    
    
        // create a request
        ChatRequest request = new ChatRequest(model, prompt);
        
        // call the API
        ChatResponse response = restTemplate.postForObject(apiUrl, request, ChatResponse.class);
        
        if (response == null || response.getChoices() == null || response.getChoices().isEmpty()) {
    
    
            return "No response";
        }
        
        // return the first response
        return response.getChoices().get(0).getMessage().getContent();
    }
}

Let’s analyze some important parts of the code:

  • We use the @Qualifier annotation to inject the RestTemplate bean we will create in the next section;
  • Using the RestTemplate bean, we call the OpenAI ChatGPT API using the postForObject() method. The postForObject() method takes the URL, request object and response class as parameters;
  • Finally, we read the select list of replies and return the first reply.

3.4 RestTemplate

We define a custom RestTemplate bean that will use the OpenAI API key for authentication:

@Configuration
public class OpenAIRestTemplateConfig {
    
    

    @Value("${openai.api.key}")
    private String openaiApiKey;

    @Bean
    @Qualifier("openaiRestTemplate")
    public RestTemplate openaiRestTemplate() {
    
    
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add((request, body, execution) -> {
    
    
            request.getHeaders().add("Authorization", "Bearer " + openaiApiKey);
            return execution.execute(request, body);
        });
        return restTemplate;
    }
}

3.5 Properties

Provide the properties of the API in the application.properties file:

openai.model=gpt-3.5-turbo
openai.api.url=https://api.openai.com/v1/chat/completions
openai.api.key=your-api-key

Then, you can run the program.

4. Summary

We explored the OpenAI ChatGPT API to generate responses to prompts. We created a Spring Boot application that calls an API to generate responses to prompts.

Supongo que te gusta

Origin blog.csdn.net/qq_41340258/article/details/132384321
Recomendado
Clasificación