springboot整合Chat Generative Pre-trained Transformer

什么是Chat Generative Pre-trained Transformer
Chat Generative Pre-trained Transformer,是以人工智能驱动的聊天机器人程序 ,已经更新多个版本,很多大厂也都在接入其API。

整合难度
难度一颗星,基本上就是给官方API发请求,然后获取响应即可。

先睹为快
QA问答
springboot整合Chat Generative Pre-trained Transformer_第1张图片springboot整合Chat Generative Pre-trained Transformer_第2张图片springboot整合Chat Generative Pre-trained Transformer_第3张图片
图像绘画:一艘宇宙飞船遨游在浩瀚星空
springboot整合Chat Generative Pre-trained Transformer_第4张图片springboot整合Chat Generative Pre-trained Transformer_第5张图片

oK现在开始实战++
1、创建一个基于springboot的maven项目并引入依赖
1.1 idea新建一个maven项目
springboot整合Chat Generative Pre-trained Transformer_第6张图片
1.2 修改配置文件pom.xml

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

1.3 将apikeys配置在application
springboot整合Chat Generative Pre-trained Transformer_第7张图片
1.4 编写测试类

@SpringBootTest
class GptDemoApplicationTests {

    @Value("${api.keys}")
    private String apiKeys;

    @Test
    void contextLoads() {
        OpenAiService service = new OpenAiService(apiKeys);
        System.out.println("\nCreating Image...");
        CreateImageRequest request = CreateImageRequest.builder()
                .prompt("A spaceship travels in the vast starry sky")
                .build();

        System.out.println("\nImage is located at:");
        System.out.println(service.createImage(request).getData().get(0).getUrl());
    }

    /**
     * qa
     */
    @Test
    void qa(){
        OpenAiService service = new OpenAiService(apiKeys,3000);
        String ques = "地球为什么是圆的?";
        System.err.println(ques);
        System.out.println("\nCreating completion...");
        CompletionRequest completionRequest = CompletionRequest.builder()
                .model("text-davinci-003")
                .prompt(ques)
                .user("testing")
                .maxTokens(1000)
                .topP(1.0)
                .n(1)
                .temperature(0.0)
                .frequencyPenalty(0.0)
                .presencePenalty(0.0)
                .build();
        service.createCompletion(completionRequest).getChoices().forEach(obj ->{
            System.err.println(obj.getText());
        });

    }
}

2、简单测试下是否能够运行
2.1 首先测试QA是否正常运行

OpenAiService service = new OpenAiService(apiKeys,3000);
        String ques = "地球为什么是圆的?";
        System.err.println(ques);
        System.out.println("\nCreating completion...");
        CompletionRequest completionRequest = CompletionRequest.builder()
                .model("text-davinci-003")
                .prompt(ques)
                .user("testing")
                .maxTokens(1000)
                .topP(1.0)
                .n(1)
                .temperature(0.0)
                .frequencyPenalty(0.0)
                .presencePenalty(0.0)
                .build();

2.2 执行测试代码,查看控制台结果
springboot整合Chat Generative Pre-trained Transformer_第8张图片

2.3 测试生成一张图片,生成 一艘宇宙飞船遨游在浩瀚星空 的图片

     CreateImageRequest request = CreateImageRequest.builder()
                .prompt("A spaceship travels in the vast starry sky")
                .build();

2.5 运行测试方法,点击控制台打印的图片地址,查看生成的图片
springboot整合Chat Generative Pre-trained Transformer_第9张图片
springboot整合Chat Generative Pre-trained Transformer_第10张图片

你可能感兴趣的:(人工智能,人工智能,自然语言处理)