Il s'agit d'une bibliothèque "non officielle" ou "maintenue par la communauté". S'il y a des bugs, les amis sont les bienvenus pour signaler et apprendre les uns des autres.
La libération et la réimpression originales indiquent la source!
Adresse du document: https://chatgpt-java.unfbx.com/
Spark Mockup Java SDK: https://github.com/grt1228/sparkdesk-java
À l'anglais doc -> anglais doc
| Ensemble, discutez de Chatgpt-Java, SDK Project Project Project Product Development Exchange | Échec du groupe Suivez la récupération officielle du compte: Chatgpt-java |
|---|---|
![]() |
Le SDK officiel de Java Client de Chatgpt OpenAI, SDK, qui peut être rapidement connecté à des projets. Prend en charge toutes les interfaces OpenAI officielles et prend en charge l'informatique de jetons. Document de référence: tokens_readme.md .
| Assistant | Dall-e-3 | Finetunejob | Tts | ChatCompletionwithPicture |
|---|---|---|---|---|
| Base de connaissances personnalisée, assistant d'IA | AI génère des images | Travail de réglage fin | Texte à la voix | ChatCompletion avec des photos supplémentaires |
| Tiktoken | Chat | Achèvement | Images | Discours au texte | Enquête au solde |
|---|---|---|---|---|---|
| Calcul de jeton | Modèle de dialogue GPT-3.5, 4.0 | Dialogue GPT-3.0 | Modèle d'image | Voix au texte, traduction vocale | Enquête au solde |
| Incorporer | Fichiers | Modérations | Affiner | Modèles |
|---|---|---|---|---|
| Encombrer | Modèle de formation personnalisé | Revue de texte, identification des mots sensibles | Ajustement fin | Recherche de la recherche de modèle |
Prise en charge de la sortie de streaming:
| Méthode d'implémentation de sortie de streaming | Mini-programme | Androïde | ios | H5 |
|---|---|---|---|---|
| Référence SSE: OpenAISseeventsourcelistener | Non pris en charge | soutien | soutien | soutien |
| Référence WebSocket: OpenAwebsocketVentsourceListEner | soutien | soutien | soutien | soutien |
Sur la base du projet de démonstration développé par ce SDK, Spring Boot est intégré pour implémenter le mode de dialogue CAHTGPT.
Ce projet prend en charge la sortie par défaut et la sortie de streaming . Référence complète du cas de test SDK:
| Cas de test SDK | Cas de test de calcul des jetons | Cas d'appel d'appel complet |
|---|---|---|
| Openaiclienttest et openaistreamclienttest | Référence du calcul des jetons: tiktokenstest | OpenaiclientFunctiontest |
<dependency>
<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.0.14-beta1</version>
</dependency>
Plus d'exemple SDK Référence: OpenASistReamClientTest
public class Test {
public static void main(String[] args) {
OpenAiStreamClient client = OpenAiStreamClient.builder()
.apiKey(Arrays.asList("sk-********","sk-********"))
//自定义key的获取策略:默认KeyRandomStrategy
//.keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
//自己做了代理就传代理地址,没有可不不传
// .apiHost("https://自己代理的服务器地址/")
.build();
//聊天模型:gpt-3.5
ConsoleEventSourceListener eventSourceListener = new ConsoleEventSourceListener();
Message message = Message.builder().role(Message.Role.USER).content("你好啊我的伙伴!").build();
ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();
client.streamChatCompletion(chatCompletion, eventSourceListener);
CountDownLatch countDownLatch = new CountDownLatch(1);
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
//国内访问需要做代理,国外服务器不需要
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
//!!!!千万别再生产或者测试环境打开BODY级别日志!!!!
//!!!生产或者测试环境建议设置为这三种级别:NONE,BASIC,HEADERS,!!!
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.proxy(proxy)//自定义代理
.addInterceptor(httpLoggingInterceptor)//自定义日志
.connectTimeout(30, TimeUnit.SECONDS)//自定义超时时间
.writeTimeout(30, TimeUnit.SECONDS)//自定义超时时间
.readTimeout(30, TimeUnit.SECONDS)//自定义超时时间
.build();
OpenAiStreamClient client = OpenAiStreamClient.builder()
.apiKey(Arrays.asList("sk-********","sk-********"))
//自定义key的获取策略:默认KeyRandomStrategy
//.keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
.okHttpClient(okHttpClient)
//自己做了代理就传代理地址,没有可不不传
// .apiHost("https://自己代理的服务器地址/")
.build();
}
}
Journal de sortie (le texte est une sortie continue):
23:03:59.158 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI建立sse连接...
23:03:59.160 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "n", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.172 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "n", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.251 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u5fc3", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.313 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u60c5", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.380 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u8212", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.439 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u7545", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.532 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "uff0c", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.579 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u5fc3", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.641 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u65f7", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.673 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u795e", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.751 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u6021", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.782 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:{"id": "cmpl-6pIHnOOJiiUEVMesXwxzzcSQFoZHj", "object": "text_completion", "created": 1677683039, "choices": [{"text": "u3002", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
23:03:59.815 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据:[DONE]
23:03:59.815 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据结束了
23:03:59.815 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI关闭sse连接...
Plus d'exemple SDK Référence: OpenaiclientTest
public class Test {
public static void main(String[] args) {
OpenAiClient openAiClient = OpenAiClient.builder()
.apiKey(Arrays.asList("sk-********","sk-********"))
//自定义key的获取策略:默认KeyRandomStrategy
//.keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
//自己做了代理就传代理地址,没有可不不传
// .apiHost("https://自己代理的服务器地址/")
.build();
//聊天模型:gpt-3.5
Message message = Message.builder().role(Message.Role.USER).content("你好啊我的伙伴!").build();
ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();
ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
chatCompletionResponse.getChoices().forEach(e -> {
System.out.println(e.getMessage());
});
}
}
public class Test {
public static void main(String[] args) {
//国内访问需要做代理,国外服务器不需要
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
//!!!!千万别再生产或者测试环境打开BODY级别日志!!!!
//!!!生产或者测试环境建议设置为这三种级别:NONE,BASIC,HEADERS,!!!
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.proxy(proxy)//自定义代理
.addInterceptor(httpLoggingInterceptor)//自定义日志输出
.addInterceptor(new OpenAiResponseInterceptor())//自定义返回值拦截
.connectTimeout(10, TimeUnit.SECONDS)//自定义超时时间
.writeTimeout(30, TimeUnit.SECONDS)//自定义超时时间
.readTimeout(30, TimeUnit.SECONDS)//自定义超时时间
.build();
//构建客户端
OpenAiClient openAiClient = OpenAiClient.builder()
.apiKey(Arrays.asList("sk-********","sk-********"))
//自定义key的获取策略:默认KeyRandomStrategy
//.keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
.okHttpClient(okHttpClient)
//自己做了代理就传代理地址,没有可不不传
// .apiHost("https://自己代理的服务器地址/")
.build();
//聊天模型:gpt-3.5
Message message = Message.builder().role(Message.Role.USER).content("你好啊我的伙伴!").build();
ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();
ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
chatCompletionResponse.getChoices().forEach(e -> {
System.out.println(e.getMessage());
});
}
}
Télécharger le code source dans le package
| Q | UN |
|---|---|
| Comment obtenir un dialogue continu? | Problèmes: # 8 |
| Comment implémenter la sortie de streaming? | Mettre à niveau la version 1.0.2, Code source de référence: OpenAistReamClientTest |
| Comment intégrer le Springboot pour implémenter l'interface API de sortie de streaming? | Reportez-vous à un autre projet: ChatGpt-Steam-Output |
| La dernière version de GPT-3.5-Turbo prend-elle en charge? | La mise à niveau 1.0.3 prend déjà en charge ChatCompletion, les cas de test de référence: OpenAstReamClientTest et OpenAstReamClientTest |
| La dernière version du langage à la traduction du texte et du langage est-elle prise en charge? | Mise à niveau 1.0.3 prend déjà en charge les cas de test de référence Whisper: OpenAstreamClientTest et OpenAStreamClientTest |
Vous pouvez vérifier cette solution lors de votre visite au niveau national: noobnooc / noobnooc # 9
Basé sur cette collection de cas de développement SDK : collection de cas SDK SDK ChatGpt-Java
Debout sur les épaules du géant:
Si le projet vous est utile, vous pouvez choisir de m'inviter à prendre une tasse de thé au lait


Protocole open source: Licence