chatgpt java環(huán)境調用源碼實現demo
chatgpt java環(huán)境調用源碼
1、啟動環(huán)境
開發(fā)工具:
jdk1.8
maven3.5.0
命令行工具:
curl
php
2、創(chuàng)建工程
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環(huán)境調用源碼實現的文章就介紹到這了,更多相關Java實現調用ChatGPT內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java源碼解析阻塞隊列ArrayBlockingQueue介紹
今天小編就為大家分享一篇關于Java源碼解析阻塞隊列ArrayBlockingQueue介紹,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01

