当前位置:   article > 正文

springboot整合Chat Generative Pre-trained Transformer_transformer spring boot

transformer spring boot

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

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

先睹为快
QA问答
在这里插入图片描述在这里插入图片描述在这里插入图片描述
图像绘画:一艘宇宙飞船遨游在浩瀚星空
在这里插入图片描述在这里插入图片描述

oK现在开始实战++
1、创建一个基于springboot的maven项目并引入依赖
1.1 idea新建一个maven项目
在这里插入图片描述
1.2 修改配置文件pom.xml

   <dependency>
      <groupId>com.theokanning.openai-gpt3-java</groupId>
       <artifactId>client</artifactId>
       <version>0.10.0</version>
   </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

1.3 将apikeys配置在application
在这里插入图片描述
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());
        });

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

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();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.2 执行测试代码,查看控制台结果
在这里插入图片描述

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

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

2.5 运行测试方法,点击控制台打印的图片地址,查看生成的图片
在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/943556
推荐阅读
相关标签
  

闽ICP备14008679号