Skip to content

Commit 34bb3ce

Browse files
committed
update README.md
1 parent b6b117a commit 34bb3ce

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

README.md

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# ChatGPT Java API
44
[![Maven Central](https://img.shields.io/maven-central/v/com.cjcrafter/openai?color=blue&label=Download)](https://central.sonatype.com/namespace/com.cjcrafter)
55
[![](https://img.shields.io/badge/-docs%20-blueviolet?logo=Kotlin&colorA=gray)](https://openai.cjcrafter.com/)
6-
[![](https://img.shields.io/badge/-examples%20-orange?logo=Read+The+Docs&colorA=gray)](https://github.com/CJCrafter/ChatGPT-Java-API/wiki)
6+
[![](https://img.shields.io/badge/-examples%20-orange?logo=Read+The+Docs&colorA=gray)](https://github.com/CJCrafter/ChatGPT-Java-API/tree/master/examples/src/main)
77
[![](https://img.shields.io/github/discussions/CJCrafter/ChatGPT-Java-API)](https://github.com/CJCrafter/ChatGPT-Java-API/discussions)
88
[![License](https://img.shields.io/github/license/CJCrafter/ChatGPT-Java-API)](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/LICENSE)
99

@@ -12,80 +12,87 @@ A community-maintained easy-to-use Java/Kotlin OpenAI API for ChatGPT, Text Comp
1212

1313
## Features
1414
* [Completions](https://platform.openai.com/docs/api-reference/completions)
15+
* Streaming support via `OpenAI#streamCompletion`
1516
* [Chat Completions](https://platform.openai.com/docs/api-reference/chat)
16-
* [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference) support via `AzureOpenAI` class
17+
* Streaming support via `OpenAI#streamChatCompletion`
18+
* Functions support, check out the [java examples](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/examples/src/main/java/chat/StreamChatCompletionFunction.java#L49) and [kotlin examples](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/examples/src/main/kotlin/chat/StreamChatCompletionFunction.kt#L37)
19+
* [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference) support via `AzureOpenAI` class
1720

1821
## Installation
1922
For Kotlin DSL (`build.gradle.kts`), add this to your dependencies block:
2023
```kotlin
2124
dependencies {
22-
implementation("com.cjcrafter:openai:1.3.1")
25+
implementation("com.cjcrafter:openai:2.0.0")
2326
}
2427
```
2528
For Maven projects, add this to your `pom.xml` file in the `<dependencies>` block:
2629
```xml
2730
<dependency>
2831
<groupId>com.cjcrafter</groupId>
2932
<artifactId>openai</artifactId>
30-
<version>1.3.1</version>
33+
<version>2.0.0</version>
3134
</dependency>
3235
```
33-
See the [maven repository](https://central.sonatype.com/artifact/com.cjcrafter/openai/1.3.1) for gradle/ant/etc.
36+
See the [maven repository](https://central.sonatype.com/artifact/com.cjcrafter/openai/2.0.0) for gradle/ant/etc.
3437

3538

3639
## Working Example
37-
This is a basic working example. To see more features in action (async calls, streaming)
38-
see the [java examples](https://github.com/CJCrafter/ChatGPT-Java-API/wiki/Java)
39-
and [kotlin examples](https://github.com/CJCrafter/ChatGPT-Java-API/wiki/Kotlin)
40+
This is a simple working example of the ChatGPT API in Java:
4041
```java
41-
public class JavaChatTest {
42-
43-
public static void main(String[] args) throws OpenAIError {
44-
Scanner scan = new Scanner(System.in);
45-
46-
// This is the prompt that the bot will refer back to for every message.
47-
ChatMessage prompt = ChatMessage.toSystemMessage("You are ChatGPT, a helpful chat bot.");
48-
49-
// Use a mutable (modifiable) list! Always! You should be reusing the
50-
// ChatRequest variable, so in order for a conversation to continue
51-
// you need to be able to modify the list.
52-
List<ChatMessage> messages = new ArrayList<>(List.of(prompt));
53-
54-
// ChatRequest is the request we send to OpenAI API. You can modify the
55-
// model, temperature, maxTokens, etc. This should be saved, so you can
56-
// reuse it for a conversation.
57-
ChatRequest request = ChatRequest.builder()
58-
.model("gpt-3.5-turbo")
59-
.messages(messages).build();
60-
61-
// Loads the API key from the .env file in the root directory.
62-
// You should never put your API keys in code, keep your key safe!
63-
String key = Dotenv.load().get("OPENAI_TOKEN");
64-
OpenAI openai = new OpenAI(key);
65-
66-
// The conversation lasts until the user quits the program
67-
while (true) {
68-
69-
// Prompt the user to enter a response
70-
System.out.println("Enter text below:\n\n");
71-
String input = scan.nextLine();
72-
73-
// Add the newest user message to the conversation
74-
messages.add(ChatMessage.toUserMessage(input));
75-
76-
// Use the OpenAI API to generate a response to the current
77-
// conversation. Print the resulting message.
78-
ChatResponse response = openai.createChatCompletion(request);
79-
System.out.println("\n" + response.get(0).getMessage().getContent());
80-
81-
// Save the generated message to the conversational memory. It is
82-
// crucial to save this message, otherwise future requests will be
83-
// confused that there was no response.
84-
messages.add(response.get(0).getMessage());
85-
}
42+
43+
import com.cjcrafter.openai.OpenAI;
44+
import com.cjcrafter.openai.chat.ChatMessage;
45+
import com.cjcrafter.openai.chat.ChatRequest;
46+
import com.cjcrafter.openai.chat.ChatResponse;
47+
import io.github.cdimascio.dotenv.Dotenv;
48+
49+
import java.util.ArrayList;
50+
import java.util.List;
51+
import java.util.Scanner;
52+
53+
/**
54+
* In this Java example, we will be using the Chat API to create a simple chatbot.
55+
*/
56+
public class ChatCompletion {
57+
58+
public static void main(String[] args) {
59+
60+
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
61+
// dependency. Then you can add a .env file in your project directory.
62+
String key = Dotenv.load().get("OPENAI_TOKEN");
63+
OpenAI openai = OpenAI.builder()
64+
.apiKey(key)
65+
.build();
66+
67+
List<ChatMessage> messages = new ArrayList<>();
68+
messages.add(ChatMessage.toSystemMessage("Help the user with their problem."));
69+
70+
// Here you can change the model's settings, add tools, and more.
71+
ChatRequest request = ChatRequest.builder()
72+
.model("gpt-3.5-turbo")
73+
.messages(messages)
74+
.build();
75+
76+
Scanner scan = new Scanner(System.in);
77+
while (true) {
78+
System.out.println("What are you having trouble with?");
79+
String input = scan.nextLine();
80+
81+
messages.add(ChatMessage.toUserMessage(input));
82+
ChatResponse response = openai.createChatCompletion(request);
83+
84+
System.out.println("Generating Response...");
85+
System.out.println(response.get(0).getMessage().getContent());
86+
87+
// Make sure to add the response to the messages list!
88+
messages.add(response.get(0).getMessage());
8689
}
90+
}
8791
}
8892
```
93+
94+
For more examples, check out [examples](https://github.com/CJCrafter/ChatGPT-Java-API/tree/master/examples/src/main).
95+
8996
> **Note**: OpenAI recommends using environment variables for your API token
9097
([Read more](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety)).
9198

0 commit comments

Comments
 (0)