准备工作

1、初始化一个springboot项目

参考地址:https://https://start.spring.io/

2、访问OPENAI官网获取API密钥

地址:https://platform.openai.com/account/api-keysount/api-keys

图片

3、通过OPENA开源SDK (OpenAI-Java)访问API

地址:https://github.com/TheoKanning/openai-java

集成ChatGPT

1、编写SpringBoot项目中的pom文件

<dependency>
  <groupId>com.theokanning.openai-gpt3-java</groupId>
  <artifactId>client</artifactId>
  <version>0.9.0</version>
</dependency>

2、初始化OpenAiService类

import com.theokanning.openai.OpenAiService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
* openai 配置类
*/
@Configuration
public class OpenAiConfiguration {

   @Value("${open.ai.key}")
   private String openAiKey;
   @Value("${open.ai.request.timeout}")
   private long timeout;
   
   @Bean
   public OpenAiService openAiService(){
       return new OpenAiService(openAiKey, Duration.ofSeconds(timeout));
   }
}

3、配置密钥、超时时间和使用的模型

server

3、编写访问业务类

import com.google.common.collect.Maps;
import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionRequest;
import com.theokanning.openai.completion.CompletionResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Slf4j
@Service
public class OpenAiChatBiz {

   @Value("${open.ai.model}")
   private String openAiModel;
   @Autowired
   private OpenAiService openAiService;
   /**
    * 聊天
    * @param prompt
    * @return
    */
   public String chat(String prompt){
       CompletionRequest completionRequest = CompletionRequest.builder()
               .prompt(prompt)
               .model(openAiModel)
               .echo(true)
               .temperature(0.7)
               .topP(1d)
               .frequencyPenalty(0d)
               .presencePenalty(0d)
               .maxTokens(1000)
               .build();
       CompletionResult completionResult = openAiService.createCompletion(completionRequest);
       String text = completionResult.getChoices().get(0).getText();
       return text;
   }
}

4、编写访问接口

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OpenAiChatApi {

   @Autowired
   private OpenAiChatBiz openAiChatBiz;

   @RequestMapping(path = "/chat/question",method = RequestMethod.GET)
   public String openAiChat(@RequestParam("question")String question){
       if(StringUtils.isBlank(question)){
           return "Please Input";
       }
       return openAiChatBiz.chat(question);
   }
}

效果展示

使用google的API Tester插件进行测试

图片

收录于合集 #springboot

 21

上一篇Spring Boot 应用可视化监控,一目了然!