开发者

chatgpt java环境调用源码实现demo

开发者 https://www.devze.com 2023-02-19 10:24 出处:网络 作者: TD程序员
目录chatgpt Java环境调用源码1、启动环境2、创建工程3、编译工程4、引入依赖5、调用接口扩展:Java实现调用ChatGPT1、导入依赖2、demo3、测试4、总结:chatgpt jwww.devze.comav编程a环境调用源码
目录
  • chatgpt Java环境调用源码
    • 1、启动环境
    • 2、创建工程
    • 3、编译工程
    • 4、引入依赖
    • 5、调用接口
  • 扩展:Java实现调用ChatGPT
    • 1、导入依赖
    • 2、demo
    • 3、测试
    • 4、总结:

chatgpt jwww.devze.comav编程a环境调用源码

1、启动环境

开发工具:

jdk1.8

maven3.5.0

命令行工具:

curl

php

2、创建工程

mvn archetype:generate -DgroupId=com.example.gpt -DartifactId=gpt-demo

3、编译工程

mvn compile

4、引入依赖

<dependency>

<groupId>com.alibaba</groupId> <artifactId>chatgpt</artifactId> <version>1.0.0</vers编程客栈ion> </dependency>

5、调用接口

String url = "http://localhost:8080/chatgpt/api/v1.0/user/sendmessage";
ChatGPT chatGPT = ChatGPT.create();
byte[] message = "Hello, world!";
try {
chatGPT.sendMessage(url, message);
} catch (Exception e) {
e.printStackTrace();
}

扩展:Java实现调用ChatGPT

1、导入依赖

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

2、demo

(前提有账号token,本人余额不多了T-T,还可以玩几次,但是网络服务器开发者_C入门问题容易timeout或者429拦截)

import com.theokanning.openai.OpenAiService;
import com.theokanning.openai.completion.CompletionChoice;
import com.theokanning.openai.completion.CompletionRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Demo {
    public static final String token = "私我";
    public static final String model = "text-davinci-003";
    public static final String retries = "20";

    public static void main(String[] args) {
        ChatGPTProperties chatGPTProperties = new ChatGPTProperties();
        chatGPTProperties.setModel(model);
        chatGPTProperties.setToken(token);
        chatGPTProperties.setRetries(Integer.valueOf(retries));

        OpenAiService openAiService = new OpenAiService(token);

        boolean flag = true;
        while (flag) {
            System.out.println("Q(exit结束):\t");
            Scanner scanner = new Scanner(System.in);
            String promote = scanner.nextLine();
            if ("exit".equals(promote)) {
                flag = false;
            } else {
                getReq(openAiService, chatGPTProperties, promote);
            }
        }
    }

    public static void getReq(OpenAiService openAiService, ChatGPTProperties chatGPTProperties, String promote) {
        System.out.println("please wait a fell seconds...");
        List<CompletionChoice> choices = new ArrayList();
        int i = 0;
        Random random = new Random();
        CompletionRequest completionRequest = CompletionRequest.builder()
                .model(model)
                .prompt(promote)
                .user("DEFAULT USER")
                .temperature(0.9D)
                .topP(1.0D)
                .maxTokens(4000)
                .build();
        while (i < chatGPTProperties.getRetries()) {
            try {
                if (i > 0) {
                    Thread.sleep((long) (500 + random.nextInt(500)));
                }
//                System.out.println("loading");
                System.out.println("第" + (i + 1) + "次请求");
                choices = openAiService.createCompletion(completionRequest).getChoices();
                break;
            } catch (Exception var4) {
                System.out.println(new StringBuilder().append("answer failed ").append(i + 1).append(" times, the error message is: ").append(var4.getMessage()).toString());
                if (i == chatG编程客栈PTProperties.getRetries() - 1) {
                    System.out.println((i + 1) + "次请求失败");
                    throw new RuntimeException(var4);
                }
                ++i;
            }
        }
        for (CompletionChoice choice : choices) {
            String text = choice.getText();
            System.out.println(text);
        }
    }


}

class ChatGPTProperties {
    private String token;
    private String model = "text-davinci-003";
    private Integer retries = 5;

    public ChatGPTProperties() {
    }

    public String getToken() {
        return this.token;
    }

    public String getModel() {
        return this.model;
    }

    public Integer getRetries() {
        return this.retries;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public void setRetries(Integer retries) {
        this.retries = retries;
    }

//    protected boolean canEqual(Object other) {
//        return other instanceof io.github.asleepyfish.config.ChatGPTProperties;
//    }

    public String toString() {
        return "ChatGPTProperties(token=" + this.getToken() + ", model=" + this.getModel() + ", retries=" + this.getRetries() + ")";
android    }
}

3、测试

chatgpt java环境调用源码实现demo

4、总结:

上下文承接时间是9s作用,猜测服务器有session超时过期设置。

老是超时或者被拦截,看脸。

有更好的操作望分享

到此这篇关于chatgpt java环境调用源码实现的文章就介绍到这了,更多相关Java实现调用ChatGPT内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号