{"id":13543236,"url":"https://github.com/Grt1228/chatgpt-java","last_synced_at":"2025-04-02T12:31:53.348Z","repository":{"id":65808092,"uuid":"600362783","full_name":"Grt1228/chatgpt-java","owner":"Grt1228","description":"ChatGPT Java SDK支持流式输出、Gpt插件、联网。支持OpenAI官方所有接口。ChatGPT的Java客户端。OpenAI GPT-3.5-Turb GPT-4  Api Client for Java","archived":false,"fork":false,"pushed_at":"2024-08-08T03:04:22.000Z","size":466,"stargazers_count":3426,"open_issues_count":72,"forks_count":819,"subscribers_count":38,"default_branch":"develop","last_synced_at":"2025-03-27T02:01:57.190Z","etag":null,"topics":["chatgpt","chatgpt-java","gpt-35-turbo","gpt-4","gpt-plugins","java","openai-api","openai-chatgpt","openai-images","openai-whisper","tiktoken-java","whisper"],"latest_commit_sha":null,"homepage":"https://chatgpt-java.unfbx.com","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Grt1228.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-11T08:54:54.000Z","updated_at":"2025-03-27T01:29:29.000Z","dependencies_parsed_at":"2024-05-07T16:28:40.997Z","dependency_job_id":"f5eb9639-f771-4d2d-8952-f1ef0d938915","html_url":"https://github.com/Grt1228/chatgpt-java","commit_stats":{"total_commits":226,"total_committers":10,"mean_commits":22.6,"dds":0.6283185840707964,"last_synced_commit":"ae761b89450812fab4cd7bba6626f9874705ca40"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Grt1228%2Fchatgpt-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Grt1228%2Fchatgpt-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Grt1228%2Fchatgpt-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Grt1228%2Fchatgpt-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Grt1228","download_url":"https://codeload.github.com/Grt1228/chatgpt-java/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246815734,"owners_count":20838495,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatgpt","chatgpt-java","gpt-35-turbo","gpt-4","gpt-plugins","java","openai-api","openai-chatgpt","openai-images","openai-whisper","tiktoken-java","whisper"],"created_at":"2024-08-01T11:00:27.139Z","updated_at":"2025-04-02T12:31:48.331Z","avatar_url":"https://github.com/Grt1228.png","language":"Java","readme":"\nit’s an “unofficial\" or \"community-maintained” library.\n这是一个非官方的社区维护的库。有bug欢迎朋友们指出，互相学习。\n\u003e **原创发布转载注明出处！**\n\n\u003e 文档地址：https://chatgpt-java.unfbx.com/\n\n\u003e 星火大模型Java SDK: https://github.com/Grt1228/SparkDesk-Java \n\nTo English Doc -\u003e [English Doc](README_EN.md)\n\n\n### 💬 联系我，ChatGPT相关学习交流咨询全部免费。\n\n一起探讨chatgpt-java，SDK问题咨询\u003cbr/\u003e项目产品开发交流 | 群失效关注公众号恢复：chatgpt-java |\n---|---\n\u003cimg src=\"https://user-images.githubusercontent.com/27008803/225246389-7b452214-f3fe-4a70-bd3e-832a0ed34288.jpg\" width=\"210\" height=\"300\" alt=\"二维码\" /\u003e  | \u003cimg src=\"https://g-photo.oss-cn-shanghai.aliyuncs.com/hd15.jpg\" width=\"210\" height=\"210\" alt=\"二维码\" /\u003e | \n\u003c!--\u003cimg src=\"https://user-images.githubusercontent.com/27008803/225246581-15e90f78-5438-4637-8e7d-14c68ca13b59.jpg\" width=\"210\" height=\"300\" alt=\"二维码\" /\u003e--\u003e\n\n- [📖 项目简介](#-项目简介)\n- [🚩 特性](#-特性)\n- [📑 更新日志](#-更新日志)\n- [🚀 快速开始](#-快速开始)\n  - [方式一](#方式一)\n    - [1、导入pom依赖](#1导入pom依赖)\n    - [2、流式客户端使用示例：](#2流式客户端使用示例)\n      - [默认OkHttpClient](#默认okhttpclient)\n      - [自定义OkHttpClient客户端使用示例：](#自定义okhttpclient客户端使用示例)\n    - [3、默认客户端使用示例（支持全部API）：](#3默认客户端使用示例支持全部api)\n      - [默认OkHttpClient](#默认okhttpclient-1)\n      - [自定义OkHttpClient客户端使用示例：](#自定义okhttpclient客户端使用示例-1)\n  - [方式二（下载源码直接运行）](#方式二下载源码直接运行)\n- [❔ QA](#-qa)\n- [📌 国内访问解决方案](#-国内访问解决方案)\n- [📋 开发案例收集](#-开发案例收集)\n- [🌟 Star History](#-star-history)\n- [🙏 鸣谢](#-鸣谢)\n- [☕ 打赏](#-打赏)\n\n# 📖 项目简介\n\n**ChatGPT的Java客户端**\nOpenAI官方Api的Java SDK，可以快速接入项目使用。支持OpenAI官方全部接口，**同时支持Tokens计算。参考文档：[Tokens_README.md](https://github.com/Grt1228/chatgpt-java/blob/main/Tokens_README.md)**。\n\n| Assistant | Dall-e-3 | FineTuneJob | TTS | ChatCompletionWithPicture |\n| --- | --- | --- | --- | --- |\n| 自定义知识库、AI助理 | AI生成图片 | 微调job | 文本转语音 | 附加图片的ChatCompletion |\n\n| TikToken | Chat | Completions | Images | Speech To Text | 余额查询 |\n| --- | --- | --- | --- | --- | --- |\n| Token计算 | GPT-3.5、4.0对话模型 | GPT-3.0对话 | 图片模型 | 语音转文字，语音翻译 | 余额查询\n\n\n| Embeddings | Files | Moderations | Fine-tune | Models |\n| --- | --- | --- | --- | --- |\n| 嵌入 | 自定义训练模型 | 文本审核，敏感词鉴别 | 微调 | 模型检索相关 |\n\n支持流式输出：\n流式输出实现方式 | 小程序 | 安卓 | ios | H5 \n---|---|---|---|---\nSSE参考：[OpenAISSEEventSourceListener](https://github.com/Grt1228/chatgpt-steam-output/blob/main/src/main/java/com/unfbx/chatgptsteamoutput/listener/OpenAISSEEventSourceListener.java) | 不支持| 支持| 支持 | 支持\nWebSocket参考：[OpenAIWebSocketEventSourceListener](https://github.com/Grt1228/chatgpt-steam-output/blob/main/src/main/java/com/unfbx/chatgptsteamoutput/listener/OpenAIWebSocketEventSourceListener.java) | 支持| 支持| 支持| 支持\n\n基于本SDK开发的Demo项目，整合Spring Boot 实现CahtGPT对话模式，思路可以参考：\n**https://github.com/Grt1228/chatgpt-steam-output**\n\n# 🚩 特性\n- 支持附加图片的ChatCompletion、指定返回数据格式、Tool Call、Dall-e-3生成图片、FineTuneJob、文本转语音TTS。\n- 支持GPT插件模式 参考实现[PluginTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/PluginTest.java)\n- 支持当key异常（失效、过期、封禁）时，自定义动态处理key 参考实现[DynamicKeyOpenAiAuthInterceptor](https://github.com/Grt1228/chatgpt-java/blob/main/src/main/java/com/unfbx/chatgpt/interceptor/DynamicKeyOpenAiAuthInterceptor.java)\n- 支持当key异常时的告警处理（钉钉、飞书、email、企业微信等等需要自定义开发）参考实现[DynamicKeyOpenAiAuthInterceptor](https://github.com/Grt1228/chatgpt-java/blob/main/src/main/java/com/unfbx/chatgpt/interceptor/DynamicKeyOpenAiAuthInterceptor.java)\n- 支持多种Tokens计算方式\n- 支持自定义OkhttpClient\n- 支持自定义多Apikey\n- 支持自定义ApiKey的获取策略\n- 支持余额查询\n- 支持个人账户信息查询\n- 支持GPT3、GPT3.5、GPT4.0、GPT3.5—0614、GPT4.0—0614...\n- 支持全部OpenAI的Api\n\n# 📑 更新日志\n- [x] 1.1.6   ChatCompletion接口支持logprobs参数设置，Assistant相关接口支持流式输出\n- [x] 1.1.5   Bug Fix\n- [x] 1.1.3   支持Assistant、Run、Thread、Message Api。测试案例[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/v1_1_3/OpenAiClientTest.java)\n- [x] 1.1.2-beta0   支持附加图片的ChatCompletion、指定返回数据格式、Tool Call、Dall-e-3生成图片、FineTuneJob、文本转语音TTS。官方文档示例：[chatgpt-java.unfbx.com](https://chatgpt-java.unfbx.com/docs/category/-%E6%A0%B8%E5%BF%83%E5%8A%9F%E8%83%BD) 。测试案例[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/v1_1_2/OpenAiClientTest.java)\n- [x] 1.1.1-beta0   function call改成GPT插件模式调用更加简单的调用方式.参考实现[PluginTest](https://github.com/Grt1228/chatgpt-java/blob/develop/src/test/java/com/unfbx/chatgpt/PluginTest.java)\n- [x] 1.0.15  moderations接口更新，代码优化，序列化修复。\n- [x] 1.0.14  升级支持最新版Gpt-3.5—0614、Gpt-4.0—0614等模型, 支持function-calling完整使用案例参考：[OpenAiClientFunctionTest](https://github.com/Grt1228/chatgpt-java/blob/1.0.14/src/test/java/com/unfbx/chatgpt/OpenAiClientFunctionTest.java) \n- [x] 1.0.13  支持当key异常（失效、过期、封禁）时，自定义动态处理key，参考实现[DynamicKeyOpenAiAuthInterceptor](https://github.com/Grt1228/chatgpt-java/blob/main/src/main/java/com/unfbx/chatgpt/interceptor/DynamicKeyOpenAiAuthInterceptor.java) ，支持key异常时的告警处理（钉钉、飞书、email、企业微信等等需要自定义开发）\n- [x] 1.0.12  tokens计算优化、删除模型接口修改、语音接口更新支持官方最新参数\n- [x] 1.0.11  增加新的余额查询接口参考：[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java) 和[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) ,修复tokens计算慢的问题，\n- [x] 1.0.10  支持tokens计算：[TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java) ，更多详细的资料参考文档：[Tokens_README.md](https://github.com/Grt1228/chatgpt-java/blob/main/Tokens_README.md)\n- [x] 1.0.9   支持自定义key使用策略参考：[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java) 和[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) ，弃用ChatGPTClient，优化Moderation接口\n- [x] 1.0.8   修改OpenAiClient和OpenAiStreamClient的自定义相关实现，超时设置，代理设置，自定义拦截器设置改为通过自定义OkHttpClient实现，将OkHttpClient交由用户自定义控制更加合理，可以实现更多的参数自定义。支持多Api Keys配置。\n- [x] 1.0.7   修复反序列化报错Bug：https://github.com/Grt1228/chatgpt-java/issues/79 ，Image SDK枚举值bug：https://github.com/Grt1228/chatgpt-java/issues/76 ，感谢两位朋友指出：[@CCc3120](https://github.com/CCc3120) 、[@seven-cm](https://github.com/seven-cm)\n- [x] 1.0.6   支持余额查询参考：[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java) 和[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) creditGrants方法,支持最新GPT-4模型，参考：[ChatCompletion.Model](https://github.com/Grt1228/chatgpt-java/blob/main/src/main/java/com/unfbx/chatgpt/entity/chat/ChatCompletion.java/)构建消息体传入模型即可。感谢群友提供的余额接口地址以及[@PlexPt](https://github.com/PlexPt) 提供的模型参数\n- [x] 1.0.5   支持自定义Api Host，使用Builder构建。参考下面的快速开始部分代码。\n- [x] 1.0.4   官方最新的ChatGPT Stream模式下的Api返回值改动。\n- [x] 1.0.3   支持最新的GPT-3.5-Turbo模型和Whisper-1模型，支持语音功能转文字，语音翻译。OpenAiClient和OpenAiStreamClient支持Builder构造，支持代理。\n- [x] 1.0.2   支持Stream流式输出，参考：OpenAiStreamClient\n- [x] 1.0.1   支持自定义超时时间，自定义OkHttpClient拦截器，参考：OpenAiClient构造函数\n- [x] 1.0.0   支持所有的OpenAI官方接口\n\n# 🚀 快速开始\n本项目支持**默认输出**和**流式输出**。完整SDK测试案例参考：\n\nSDK测试案例 | Tokens计算测试案例 | 完整Function-Call调用案例\n---| --- | ---\n[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java) 和[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) | Tokens计算参考：[TikTokensTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/TikTokensTest.java) | [OpenAiClientFunctionTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientFunctionTest.java)\n\n## 方式一\n\n### 1、导入pom依赖\n```\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.unfbx\u003c/groupId\u003e\n    \u003cartifactId\u003echatgpt-java\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.14-beta1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n### 2、流式客户端使用示例：\n更多SDK示例参考：[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java) \n#### 默认OkHttpClient\n```\npublic class Test {\n    public static void main(String[] args) {\n        OpenAiStreamClient client = OpenAiStreamClient.builder()\n                .apiKey(Arrays.asList(\"sk-********\",\"sk-********\"))\n                //自定义key的获取策略：默认KeyRandomStrategy\n                //.keyStrategy(new KeyRandomStrategy())\n                .keyStrategy(new FirstKeyStrategy())\n                //自己做了代理就传代理地址，没有可不不传\n//                .apiHost(\"https://自己代理的服务器地址/\")\n                .build();\n        //聊天模型：gpt-3.5\n        ConsoleEventSourceListener eventSourceListener = new ConsoleEventSourceListener();\n        Message message = Message.builder().role(Message.Role.USER).content(\"你好啊我的伙伴！\").build();\n        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();\n        client.streamChatCompletion(chatCompletion, eventSourceListener);\n        CountDownLatch countDownLatch = new CountDownLatch(1);\n        try {\n            countDownLatch.await();\n        } catch (InterruptedException e) {\n            e.printStackTrace();\n        }\n    }\n}\n```\n#### 自定义OkHttpClient客户端使用示例：\n```\npublic class Test {\n    public static void main(String[] args) {\n        //国内访问需要做代理，国外服务器不需要\n        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(\"127.0.0.1\", 7890));\n        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());\n        //！！！！千万别再生产或者测试环境打开BODY级别日志！！！！\n        //！！！生产或者测试环境建议设置为这三种级别：NONE,BASIC,HEADERS,！！！\n        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);\n        OkHttpClient okHttpClient = new OkHttpClient\n                .Builder()\n                .proxy(proxy)//自定义代理\n                .addInterceptor(httpLoggingInterceptor)//自定义日志\n                .connectTimeout(30, TimeUnit.SECONDS)//自定义超时时间\n                .writeTimeout(30, TimeUnit.SECONDS)//自定义超时时间\n                .readTimeout(30, TimeUnit.SECONDS)//自定义超时时间\n                .build();\n        OpenAiStreamClient client = OpenAiStreamClient.builder()\n                .apiKey(Arrays.asList(\"sk-********\",\"sk-********\"))\n                //自定义key的获取策略：默认KeyRandomStrategy\n                //.keyStrategy(new KeyRandomStrategy())\n                .keyStrategy(new FirstKeyStrategy())\n                .okHttpClient(okHttpClient)\n                //自己做了代理就传代理地址，没有可不不传\n//                .apiHost(\"https://自己代理的服务器地址/\")\n                .build();\n    }\n}\n```\n输出日志（text是持续输出的）：\n```\n23:03:59.158 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI建立sse连接...\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23: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\"}\n23:03:59.815 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据：[DONE]\n23:03:59.815 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI返回数据结束了\n23:03:59.815 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI关闭sse连接...\n```\n### 3、默认客户端使用示例（支持全部API）：\n更多SDK示例参考：[OpenAiClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java) \n#### 默认OkHttpClient\n```\npublic class Test {\n    public static void main(String[] args) {\n        OpenAiClient openAiClient = OpenAiClient.builder()\n                .apiKey(Arrays.asList(\"sk-********\",\"sk-********\"))\n                //自定义key的获取策略：默认KeyRandomStrategy\n                //.keyStrategy(new KeyRandomStrategy())\n                .keyStrategy(new FirstKeyStrategy())\n                //自己做了代理就传代理地址，没有可不不传\n//                .apiHost(\"https://自己代理的服务器地址/\")\n                .build();\n                //聊天模型：gpt-3.5\n        Message message = Message.builder().role(Message.Role.USER).content(\"你好啊我的伙伴！\").build();\n        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();\n        ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);\n        chatCompletionResponse.getChoices().forEach(e -\u003e {\n            System.out.println(e.getMessage());\n        });\n    }\n}\n```\n#### 自定义OkHttpClient客户端使用示例：\n```\npublic class Test {\n    public static void main(String[] args) {\n        //国内访问需要做代理，国外服务器不需要\n        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(\"127.0.0.1\", 7890));\n        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());\n        //！！！！千万别再生产或者测试环境打开BODY级别日志！！！！\n        //！！！生产或者测试环境建议设置为这三种级别：NONE,BASIC,HEADERS,！！！\n        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);\n        OkHttpClient okHttpClient = new OkHttpClient\n                .Builder()\n                .proxy(proxy)//自定义代理\n                .addInterceptor(httpLoggingInterceptor)//自定义日志输出\n                .addInterceptor(new OpenAiResponseInterceptor())//自定义返回值拦截\n                .connectTimeout(10, TimeUnit.SECONDS)//自定义超时时间\n                .writeTimeout(30, TimeUnit.SECONDS)//自定义超时时间\n                .readTimeout(30, TimeUnit.SECONDS)//自定义超时时间\n                .build();\n        //构建客户端\n        OpenAiClient openAiClient = OpenAiClient.builder()\n                .apiKey(Arrays.asList(\"sk-********\",\"sk-********\"))\n                //自定义key的获取策略：默认KeyRandomStrategy\n                //.keyStrategy(new KeyRandomStrategy())\n                .keyStrategy(new FirstKeyStrategy())\n                .okHttpClient(okHttpClient)\n                //自己做了代理就传代理地址，没有可不不传\n//                .apiHost(\"https://自己代理的服务器地址/\")\n                .build();\n                //聊天模型：gpt-3.5\n        Message message = Message.builder().role(Message.Role.USER).content(\"你好啊我的伙伴！\").build();\n        ChatCompletion chatCompletion = ChatCompletion.builder().messages(Arrays.asList(message)).build();\n        ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);\n        chatCompletionResponse.getChoices().forEach(e -\u003e {\n            System.out.println(e.getMessage());\n        });\n    }\n}\n```\n## 方式二（下载源码直接运行）\n下载源码打包\n# ❔ QA\nQ | A\n---|---\n如何实现连续对话？ | issues：https://github.com/Grt1228/chatgpt-java/issues/8\n如何实现流式输出？ | 升级1.0.2版本，参考源码：[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java/)\n如何整合SpringBoot实现流式输出的Api接口？ | 参考另外一个项目：[chatgpt-steam-output](https://github.com/Grt1228/chatgpt-steam-output)\n最新版GPT-3.5-TURBO是否支持？ | 升级1.0.3 已经支持ChatCompletion, 参考测试案例：[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java/) 和[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java/)\n最新版语言转文字和语言翻译是否支持？ | 升级1.0.3 已经支持whisper参考测试案例：[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiStreamClientTest.java/) 和[OpenAiStreamClientTest](https://github.com/Grt1228/chatgpt-java/blob/main/src/test/java/com/unfbx/chatgpt/OpenAiClientTest.java/)\n\n# 📌 国内访问解决方案\n国内访问可以看下这个解决方案：**https://github.com/noobnooc/noobnooc/discussions/9**\n\n# 📋 开发案例收集\n**基于本SDK开发案例收集**：[chatgpt-java SDK案例征集](https://github.com/Grt1228/chatgpt-java/issues/87) \n# 🌟 Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=Grt1228/chatgpt-java\u0026type=Date)](https://star-history.com/#Grt1228/chatgpt-java\u0026Date)\n\n\n# 🙏 鸣谢\n站在巨人的肩膀：\n- OpenAi：https://openai.com/\n- [knuddelsgmbh](https://github.com/knuddelsgmbh) 的[jtokkit](https://github.com/knuddelsgmbh/jtokkit) 的开源计算算法。\n\n\n# ☕ 打赏\n如果项目对你有帮助，可以选择请我喝杯奶茶\n\n\u003cimg width=\"180\" alt=\"微信截图_20230405222411\" src=\"https://user-images.githubusercontent.com/27008803/230111508-3179cf30-e128-4b2e-9645-157266c491ce.png\"\u003e  \u003cimg width=\"164\" alt=\"微信截图_20230405222357\" src=\"https://user-images.githubusercontent.com/27008803/230111525-322f5036-d06d-46bb-94d1-db8ce9ed2adf.png\"\u003e\n\n开源协议：[LICENSE](https://github.com/Grt1228/chatgpt-java/blob/main/LICENSE) \n","funding_links":[],"categories":["A01_文本生成_文本对话","精选开源项目合集","人工智能"],"sub_categories":["大语言对话模型及数据","GTP编程语言客户端"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGrt1228%2Fchatgpt-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGrt1228%2Fchatgpt-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGrt1228%2Fchatgpt-java/lists"}