Skip to content

Commit de24e07

Browse files
committed
Update README with new coding example
1 parent c71c272 commit de24e07

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

README.md

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,55 @@ See the [maven repository](https://central.sonatype.com/artifact/com.cjcrafter/o
2323

2424

2525
# Working Example
26+
Check the wiki for more examples.
2627
```java
27-
import java.io.IOException;
28-
import java.util.List;
29-
import java.util.Scanner;
28+
public class JavaChatTest {
3029

31-
public class Main {
32-
public static void main(String[] args) throws IOException {
30+
public static void main(String[] args) throws OpenAIError {
3331
Scanner scan = new Scanner(System.in);
34-
String key = "sk-YOUR KEY HERE"; // TODO Add your open ai key here
35-
36-
// Create the initial prompt, we will reuse it later.
37-
String initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.";
38-
List<ChatBot.ChatMessage> messages = List.of(new ChatBot.ChatMessage("system", initialPrompt));
39-
ChatBot.ChatCompletionRequest request = new ChatBot.ChatCompletionRequest("gpt-3.5-turbo", messages);
40-
ChatBot bot = new ChatBot(key);
41-
42-
// ChatCompletionRequest copies the list, so let's modify the request's
43-
// copy of the list.
44-
messages = request.getMessages();
4532

33+
// This is the prompt that the bot will refer back to for every message.
34+
ChatMessage prompt = ChatMessage.toSystemMessage("You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.");
35+
36+
// Use a mutable (modifiable) list! Always! You should be reusing the
37+
// ChatRequest variable, so in order for a conversation to continue
38+
// you need to be able to modify the list.
39+
List<ChatMessage> messages = new ArrayList<>(List.of(prompt));
40+
41+
// ChatRequest is the request we send to OpenAI API. You can modify the
42+
// model, temperature, maxTokens, etc. This should be saved, so you can
43+
// reuse it for a conversation.
44+
ChatRequest request = ChatRequest.builder()
45+
.model("gpt-3.5-turbo")
46+
.messages(messages).build();
47+
48+
// Loads the API key from the .env file in the root directory.
49+
String key = Dotenv.load().get("OPENAI_TOKEN");
50+
OpenAI openai = new OpenAI(key);
51+
52+
// The conversation lasts until the user quits the program
4653
while (true) {
54+
55+
// Prompt the user to enter a response
4756
System.out.println("Enter text below:\n\n");
4857
String input = scan.nextLine();
4958

50-
// Generate a response, and print it to the user.
51-
messages.add(new ChatBot.ChatMessage("user", input));
52-
ChatBot.ChatCompletionResponse response = bot.generateResponse(request);
53-
System.out.println("\n" + response.getChoices().get(0).getMessage().getContent());
59+
// Add the newest user message to the conversation
60+
messages.add(ChatMessage.toUserMessage(input));
61+
62+
// Use the OpenAI API to generate a response to the current
63+
// conversation. Print the resulting message.
64+
ChatResponse response = openai.createChatCompletion(request);
65+
System.out.println("\n" + response.get(0).getMessage().getContent());
5466

55-
// Save the generated message to the bot's conversational memory
56-
messages.add(response.getChoices().get(0).getMessage());
67+
// Save the generated message to the conversational memory. It is
68+
// crucial to save this message, otherwise future requests will be
69+
// confused that there was no response.
70+
messages.add(response.get(0).getMessage());
5771
}
5872
}
5973
}
74+
6075
```
6176

6277
# Support

src/main/kotlin/com/cjcrafter/openai/completions/package-info.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/kotlin/com/cjcrafter/openai/exception/package-info.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)