java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java调用ChatGPT

chatgpt java环境调用源码实现demo

作者:TD程序员

这篇文章主要介绍了chatgpt java环境调用源码实现demo,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

chatgpt java环境调用源码

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</version> </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,还可以玩几次,但是网络服务器问题容易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 == chatGPTProperties.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() + ")";
    }
}

3、测试

4、总结:

上下文承接时间是9s作用,猜测服务器有session超时过期设置。
老是超时或者被拦截,看脸。
有更好的操作望分享

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

您可能感兴趣的文章:
阅读全文