it’s an “unofficial" or "community-maintained” library. This is an unofficial community-maintained library. Bug reports are welcome and let's learn from each other.
Original publication must be credited if reprinted!
Open source license: LICENSE
| Let's discuss issues related to chatgpt-java, SDK problem consultation, project and product development communication | If the group is invalid, please follow the official account to restore: chatgpt-java |
|---|---|
![]() |
![]() |
ChatGPT Java Client
The Java SDK for OpenAI's official API allows for quick integration into projects. It supports all of OpenAI's official interfaces, as well as token calculations. Reference documentation: Tokens_README.md.
| TikToken | Chat | Completions | Images | Speech To Text | Balance Inquiry |
|---|---|---|---|---|---|
| Token Calculation | GPT-3.5, 4.0 Dialogue Model | GPT-3.0 Dialogue | Image Model | Speech-to-Text, Speech Translation | Balance Inquiry |
| Embeddings | Files | Moderations | Fine-tune | Models |
|---|---|---|---|---|
| Embeddings | Custom Training Models | Text Review, Sensitive Word Identification | Fine-tune | Model Retrieval |
Supports streaming output: Streaming output implementation | Mini Program | Android | iOS | H5 ---|---|---|---|--- SSE Reference:OpenAISSEEventSourceListener | Not Supported | Supported| Supported | Supported WebSocket Reference:OpenAIWebSocketEventSourceListener | Supported| Supported | Supported | Supported
creditGrants method supports the latest GPT-4 model. Refer to the ChatCompletion.Model to build the message body and pass it into the model. Thanks to the group members for providing the balance interface address and @PlexPt for providing the model parameters.This project supports both default output and streaming output. For a complete SDK test case, see:
SDK Test Cases | TikToken Test Cases | Full Function Call Test Cases ---|---|---| OpenAiClientTest and OpenAiStreamClientTest | Token calculation reference: TikTokensTest | OpenAiClientFunctionTest
<dependency>
<groupId>com.unfbx</groupId>
<artifactId>chatgpt-java</artifactId>
<version>1.0.14</version>
</dependency>
More SDK examples can be found at: OpenAiStreamClientTest
public class Test {
public static void main(String[] args) {
OpenAiStreamClient client = OpenAiStreamClient.builder()
.apiKey(Arrays.asList("sk-********","sk-********"))
// Custom key acquisition strategy: default KeyRandomStrategy
//.keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
// If you have a proxy, pass the proxy address, otherwise you can skip it
// .apiHost("https://自己代理的服务器地址/")
.build();
// Chat model: 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) {
// If accessing the service from China, a proxy needs to be configured. No proxy is needed for accessing the service from overseas servers.
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890));
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new OpenAILogger());
// !!! Do not enable BODY level logging in production or test environment !!!
// !!! It is recommended to set the logging level to one of these three levels in production or test environment: NONE, BASIC, HEADERS !!!
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.proxy(proxy) // custom proxy
.addInterceptor(httpLoggingInterceptor) // custom logger
.connectTimeout(30, TimeUnit.SECONDS) // custom connection timeout
.writeTimeout(30, TimeUnit.SECONDS) // custom write timeout
.readTimeout(30, TimeUnit.SECONDS) // custom read timeout
.build();
OpenAiStreamClient client = OpenAiStreamClient.builder()
.apiKey(Arrays.asList("sk-********","sk-********"))
// custom key acquisition strategy: default is KeyRandomStrategy
//.keyStrategy(new KeyRandomStrategy())
.keyStrategy(new FirstKeyStrategy())
.okHttpClient(okHttpClient) // custom OkHttpClient
// if a proxy is used, pass the proxy address, if not, do not pass
// .apiHost("https://your.proxy.server.address/")
.build();
}
}
Output log (text is continuously output): ``` 23:03:59.158 [省略无效信息] INFO com.unfbx.chatgpt.sse.ConsoleEventSourceListener - OpenAI建立sse连接... 23:03:59.160 [省略无效信息] INFO com.unfbx.chatgpt.sse.Conso
$ claude mcp add chatgpt-java \
-- python -m otcore.mcp_server <graph>