This is an unofficial Java client library that helps to connect your Java applications with OpenAI API.
- Detailed javadocs
- The rest of API endpoints
- Tell us what you need
Maven:
<dependency>
<groupId>dev.ai4j</groupId>
<artifactId>openai4j</artifactId>
<version>0.10.0</version>
</dependency>
Gradle:
implementation 'dev.ai4j:openai4j:0.10.0'
Simple way:
String apiKey = System.getenv("OPENAI_API_KEY");
OpenAiClient client = OpenAiClient.builder()
.openAiApiKey(apiKey)
.build();
Customizable way:
String apiKey = System.getenv("OPENAI_API_KEY");
OpenAiClient client = OpenAiClient.builder()
.openAiApiKey(apiKey)
.callTimeout(ofSeconds(60))
.connectTimeout(ofSeconds(60))
.readTimeout(ofSeconds(60))
.writeTimeout(ofSeconds(60))
.proxy(HTTP, "XXX.XXX.XXX.XXX", 8080)
.logRequests()
.logResponses()
// other customizations coming soon!
.build();
Simple way:
String completion = client.completion("Write a poem about ChatGPT").execute();
Customizable way:
CompletionRequest request = CompletionRequest.builder()
.model(TEXT_DAVINCI_003)
.prompt("Write a poem about ChatGPT")
.temperature(0.9)
...
.build();
CompletionResponse response = client.completion(request).execute();
Simple way:
client.completion("Write a poem about ChatGPT")
.onResponse(response -> ...)
.onError(error -> ...)
.execute();
Customizable way:
CompletionRequest request = CompletionRequest.builder()
.model(TEXT_DAVINCI_003)
.prompt("Write a poem about ChatGPT")
.temperature(0.9)
...
.build();
client.completion(request)
.onResponse(response -> ...)
.onError(error -> ...)
.execute();
Simple way:
client.completion("Write a poem about ChatGPT")
.onPartialResponse(partialResponse -> ...)
.onComplete(() -> ...)
.onError(error -> ...)
.execute();
Customizable way:
CompletionRequest request = CompletionRequest.builder()
.model(TEXT_DAVINCI_003)
.prompt("Write a poem about ChatGPT")
.temperature(0.9)
...
.build();
client.completion(request)
.onPartialResponse(partialResponse -> ...)
.onComplete(() -> ...)
.onError(error -> ...)
.execute();
Simple way:
String completion = client.chatCompletion("Write a poem about ChatGPT").execute();
Customizable way:
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model(GPT_3_5_TURBO)
.addSystemMessage("You are a helpful assistant")
.addUserMessage("Write a poem about ChatGPT")
.temperature(0.9)
...
.build();
ChatCompletionResponse response = client.chatCompletions(request).execute();
Simple way:
client.chatCompletion("Write a poem about ChatGPT")
.onResponse(response -> ...)
.onError(error -> ...)
.execute();
Customizable way:
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model(GPT_3_5_TURBO)
.addSystemMessage("You are a helpful assistant")
.addUserMessage("Write a poem about ChatGPT")
.temperature(0.9)
...
.build();
client.chatCompletion(request)
.onResponse(response -> ...)
.onError(error -> ...)
.execute();
Simple way:
client.chatCompletion("Write a poem about ChatGPT")
.onPartialResponse(partialResponse -> ...)
.onComplete(() -> ...)
.onError(error -> ...)
.execute();
Customizable way:
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model(GPT_3_5_TURBO)
.addSystemMessage("You are a helpful assistant")
.addUserMessage("Write a poem about ChatGPT")
.temperature(0.9)
...
.build();
client.chatCompletion(request)
.onPartialResponse(partialResponse -> ...)
.onComplete(() -> ...)
.onError(error -> ...)
.execute();
Simple way:
List<Float> embedding = client.embedding("Write a poem about ChatGPT").execute();
Customizable way:
EmbeddingRequest request = EmbeddingRequest.builder()
.model(TEXT_EMBEDDING_ADA_002)
.input("Write a poem about ChatGPT", "Write a haiku about ChatGPT")
...
.build();
EmbeddingResponse embedding = client.embedding(request).execute();
Simple way:
client.embedding("Write a poem about ChatGPT")
.onResponse(response -> ...)
.onError(error -> ...)
.execute();
Customizable way:
EmbeddingRequest request = EmbeddingRequest.builder()
.model(TEXT_EMBEDDING_ADA_002)
.input("Write a poem about ChatGPT", "Write a haiku about ChatGPT")
...
.build();
client.embedding(request)
.onResponse(response -> ...)
.onError(error -> ...)
.execute();
Simple way:
ModerationResult moderationResult = client.moderation("Write a poem about ChatGPT").execute();
Customizable way:
ModerationRequest request = ModerationRequest.builder()
.input(INPUT)
.model(TEXT_MODERATION_STABLE)
.build();
ModerationResponse response = client.moderation(request).execute();
Simple way:
client.moderation("Write a poem about ChatGPT")
.onResponse(response -> ...)
.onError(error -> ...)
.execute();
Customizable way:
ModerationRequest request = ModerationRequest.builder()
.input(INPUT)
.model(TEXT_MODERATION_STABLE)
.build();
client.moderation(request)
.onResponse(response -> ...)
.onError(error -> ...)
.execute();
- How to get best results form AI: https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/
- Text version of above course: https://platform.openai.com/docs/guides/gpt-best-practices
- How to build software powered by OpenAI/ChatGPT: https://www.deeplearning.ai/short-courses/building-systems-with-chatgpt/
- Cookbook with examples of how to use OpenAI API: https://github.com/openai/openai-cookbook