top.luanyu:chatgpt

OpenAI spring boot starter


License
Apache-2.0

Documentation

ChatGPT

GitHub release Maven Central GitHub license

Java版本openai api的SDK,支持spring boot快速开始。

 

快速使用

一、引入依赖

<dependency>
    <groupId>cn.m9d2.chatgpt</groupId>
    <artifactId>chatgpt</artifactId>
    <version>{release-version}</version>
</dependency>

 

二、application.yml 配置

openai:
  api-key: "YOUR OWNER KEY"
  proxy:
    enable: true
    type: http
    hostname: 127.0.0.1
    port: 1087
  connect-timeout: 10000
  read-timeout: 30000

 

三、示例代码

@SpringBootApplication
public class SampleApplication implements ApplicationRunner {

    @Autowired
    private ChatService chatService;

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Completions completions = new Completions();
        List<Message> messages = new ArrayList<>();
        Message message = new Message();
        message.setContent("你好");
        message.setRole("user");
        messages.add(message);
        completions.setMessages(messages);
        CompletionsResponse response = chatService.completions(completions);
        for (CompletionsResponse.Choice choice : response.getChoices()) {
            System.out.print(choice.getMessage().getContent());
        }
    }
}

 

Spring boot示例项目

sample-chatgpt

鸣谢

本项目参考以下项目

Grt1228/chatgpt-java