Perpustakaan "tidak resmi" atau "diraih komunitas". Jika ada bug, teman dipersilakan untuk menunjukkan dan belajar dari satu sama lain.
Rilis asli dan cetak ulang menunjukkan sumbernya!
Alamat dokumen: https://chatgpt-java.unfbx.com/
Spark Mockup Java SDK: https://github.com/grt1228/sparkdesk-java
Untuk Dokter Bahasa Inggris -> Dokter Bahasa Inggris
| Bersama Diskusikan Chatgpt-Java, SDK Edisi Konsultasi Proyek Pengembangan Produk Exchange | Kegagalan Grup Ikuti Pemulihan Akun Resmi: Chatgpt-Java |
|---|---|
![]() |
Klien Java Chatgpt Openai API Java SDK resmi, yang dapat dengan cepat terhubung ke proyek. Mendukung semua antarmuka OpenAi resmi dan mendukung komputasi token. Dokumen Referensi: Tokens_readme.md .
| Asisten | Dall-E-3 | Finetunejob | Tts | Chatcompletionwithpicture |
|---|---|---|---|---|
| Basis Pengetahuan Kustom, Asisten AI | AI menghasilkan gambar | Pekerjaan yang menyempurnakan | Teks untuk disuarakan | Chatcompletion dengan gambar tambahan |
| Tiktoken | Mengobrol | Penyelesaian | Gambar | Pidato untuk teks | Penyelidikan Saldo |
|---|---|---|---|---|---|
| Perhitungan token | Model Dialog GPT-3.5, 4.0 | Dialog GPT-3.0 | Model gambar | Suara ke teks, terjemahan suara | Penyelidikan Saldo |
| Embeddings | File | Moderasi | Fine-tune | Model |
|---|---|---|---|---|
| Menanamkan | Model Pelatihan Kustom | Tinjauan Teks, Identifikasi Kata Sensitif | Penyesuaian yang baik | Pencarian Model Terkait |
Dukungan Output Streaming:
| Metode implementasi output streaming | Program mini | Android | iOS | H5 |
|---|---|---|---|---|
| Referensi SSE: OpenAisSeEventSourcelistener | Tidak didukung | mendukung | mendukung | mendukung |
| Referensi Websocket: OpenaiWebsocketEventSourcelistener | mendukung | mendukung | mendukung | mendukung |
Berdasarkan proyek demo yang dikembangkan oleh SDK ini, Spring Boot diintegrasikan untuk mengimplementasikan mode dialog Cahtgpt .
Proyek ini mendukung output default dan output streaming . Referensi Kasus Uji SDK Lengkap:
| Kasus uji SDK | Kasus uji perhitungan token | Casing panggilan fungsi lengkap |
|---|---|---|
| OpenaiClientTest dan OpenAistreamClientTest | Token Referensi Perhitungan: TikTokenstest | OpenaiClientFunctionTest |
<dependency>
<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.0.14-beta1</version>
</dependency>
Lebih banyak Contoh SDK Referensi: OpenAistreamClientTest
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();
}
}
Log Output (Teks adalah output kontinu):
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连接...
Lebih banyak Contoh SDK Referensi: 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());
});
}
}
Unduh kode sumber untuk paket
| Q | A |
|---|---|
| Bagaimana cara mencapai dialog berkelanjutan? | Masalah: #8 |
| Bagaimana cara mengimplementasikan output streaming? | Upgrade Versi 1.0.2, Kode Sumber Referensi: OpenAistreamClientTest |
| Bagaimana cara mengintegrasikan springboot untuk mengimplementasikan streaming output API antarmuka? | Lihat proyek lain: chatgpt-steam-output |
| Apakah versi terbaru dari dukungan GPT-3.5-turbo? | Tingkatkan 1.0.3 Sudah mendukung CHATCompletion, Kasus Tes Referensi: OpenAistreamClientTest dan OpenAistreamClientTest |
| Apakah versi terbaru dari bahasa teks dan terjemahan bahasa didukung? | Tingkatkan 1.0.3 Sudah mendukung Kasus Uji Referensi Whisper: OpenAistreamClientTest dan OpenAistreamClientTest |
Anda dapat memeriksa solusi ini saat mengunjungi di dalam negeri: NOOBNOOC/NOOBNOOC#9
Berdasarkan Koleksi Kasus Pengembangan SDK ini : Koleksi Kasus SDK Chatgpt-Java
Berdiri di pundak raksasa:
Jika proyek ini bermanfaat bagi Anda, Anda dapat memilih untuk mengundang saya untuk minum secangkir teh susu


Protokol Sumber Terbuka: Lisensi