chatgpt java環(huán)境調(diào)用源碼實現(xiàn)demo
chatgpt java環(huán)境調(diào)用源碼
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、調(diào)用接口
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實現(xiàn)調(diào)用ChatGPT
1、導(dǎo)入依賴
<dependency> <groupId>com.theokanning.openai-gpt3-java</groupId> <artifactId>client</artifactId> <version>0.10.0</version> </dependency>
2、demo
(前提有賬號token,本人余額不多了T-T,還可以玩幾次,但是網(wǎng)絡(luò)服務(wù)器問題容易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結(jié)束):\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、總結(jié):
上下文承接時間是9s作用,猜測服務(wù)器有session超時過期設(shè)置。
老是超時或者被攔截,看臉。
有更好的操作望分享
到此這篇關(guān)于chatgpt java環(huán)境調(diào)用源碼實現(xiàn)的文章就介紹到這了,更多相關(guān)Java實現(xiàn)調(diào)用ChatGPT內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Lambda List轉(zhuǎn)Map代碼實例
這篇文章主要介紹了Java Lambda List轉(zhuǎn)Map代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03全解史上最快的JOSN解析庫alibaba Fastjson
這篇文章主要介紹了史上最快的JOSN解析庫alibaba Fastjson,對FastJson感興趣的同學(xué),一定要看一下2021-04-04mybatis關(guān)聯(lián)關(guān)系映射的實現(xiàn)
MyBatis的關(guān)聯(lián)關(guān)系映射在復(fù)雜數(shù)據(jù)模型中至關(guān)重要,使開發(fā)人員能夠以最靈活的方式滿足不同項目的需求,本文就來介紹一下mybatis關(guān)聯(lián)關(guān)系映射的實現(xiàn),感興趣的可以了解一下2023-09-09Mybatis?大數(shù)據(jù)量批量寫優(yōu)化的案例詳解
這篇文章主要介紹了Mybatis?大數(shù)據(jù)量批量寫優(yōu)化的示例代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05Java連接 JDBC基礎(chǔ)知識(操作數(shù)據(jù)庫:增刪改查)
這篇文章主要介紹了Java連接 JDBC基礎(chǔ)知識,包括操作數(shù)據(jù)庫之增刪改查操作,需要的朋友可以參考下2021-04-04