java简单访问chatapt

java简单调用 gpt-3 api

引入依赖

  1. 进入官方推荐的 java sdk
    • 文档介绍使用openai可以使用api、client和service三种包,service最简单,本次只使用service。
  2. 找到依赖信息.
    implementation ‘com.theokanning.openai-gpt3-java::’
  3. 在maven库中找到所需版本,在浏览器搜索
    mvn com.theokanning.openai-gpt3-java
  4. 找到service包的所需配置代码
    • gradle
     implementation 'com.theokanning.openai-gpt3-java:service:0.10.0'
     
     //https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core  
     implementation 'com.fasterxml.jackson.core:jackson-core:2.13.4'
    
     //https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
     implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4.2'
    
    //https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
    implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.4'
    
    • maven
        <dependency>
           <groupId>com.theokanning.openai-gpt3-javagroupId>
           <artifactId>serviceartifactId>
           <version>0.10.0version>
        dependency> 
    
     
        <dependency>
           <groupId>com.fasterxml.jackson.coregroupId>
           <artifactId>jackson-coreartifactId>
           <version>2.13.4version>
        dependency>
     
        
        <dependency>
           <groupId>com.fasterxml.jackson.coregroupId>
           <artifactId>jackson-databindartifactId>
           <version>2.13.4.2version>
        dependency>
    
        
        <dependency>
           <groupId>com.fasterxml.jackson.coregroupId>
           <artifactId>jackson-annotationsartifactId>
           <version>2.13.4version>
        dependency>
    

创建项目

  1. 创建普通java项目,创建main方法。
public static void main(String[] args) {
   ObjectMapper mapper = defaultObjectMapper();
   // 主机地址,端口号
   Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", port));
   //        设置token(需自行申请),增加timeout字段,修改默认超时时间
   OkHttpClient client = defaultClient("your_token", Duration.ofSeconds(100))
                .newBuilder()
                .proxy(proxy)
                .build();
   Retrofit retrofit = defaultRetrofit(client, mapper);
   OpenAiApi api = retrofit.create(OpenAiApi.class);
   OpenAiService service = new OpenAiService(api);
        CompletionRequest completionRequest = CompletionRequest.builder()
      //   设置提示
                .prompt("Somebody once told me the world is gonna roll me")
//                把ada模型修改为davinci模型,davinci模型功能更强大
                .model("text-davinci-003")
                .echo(true)
//               增加maxTokens字段,默认tokens数不够用
                .maxTokens(1000)
                .build();
        System.out.println("--------");
        service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
        System.out.println("--------");
    }

  1. 运行,在控制台窗口中显示
   CompletionChoice(text=Somebody once told me the world is gonna roll me

    I ain't the sharpest tool in the shed

    She was looking kind of dumb with her finger and her thumb
    In the shape of an "L" on her forehead,index=0, logprobs=null, finish_reason=stop)

注意(下方地址都需要魔法上网):

  1. 需要CompletionRequest请求参数设置maxTokens()字段,默认token数只能显示一部分返回文本(token:英文字母≈1:3~4,汉字:token≈2:1)。
    文本转token数地址,其他参数详情.
  2. 须在调用的类OpenAiService里加上timeout参数即 Duration.ofSeconds(100)(Duration.ofSeconds( )的参数类型为long,单位为秒),否则可能会超时。
    OpenAiService service = new OpenAiService("your_token", Duration.ofSeconds(100));
    
  3. 选择模型.text-davinci-003模型更好,想要了解其它模型可以访问https://platform.openai.com/docs/models/gpt-3。

修改

 问题:connect timed out
 解决方案:配置代理
  1. 增加依赖包
  2. 增加配置代码

你可能感兴趣的:(java,gpt-3)