Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c3d8955

Browse files
rename examples to avoid clashing names
1 parent e461e67 commit c3d8955

15 files changed

+93
-8
lines changed

‎examples/src/main/java/assistant/Assistant.java renamed to ‎examples/src/main/java/assistant/AssistantExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import java.util.Scanner;
99

10-
public class Assistant {
10+
public class AssistantExample {
1111

1212
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
1313
// dependency. Then you can add a .env file in your project directory.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package assistant;
2+
3+
import com.cjcrafter.openai.OpenAI;
4+
import com.cjcrafter.openai.assistants.Assistant;
5+
import com.cjcrafter.openai.assistants.ListAssistantResponse;
6+
import com.cjcrafter.openai.threads.Thread;
7+
import com.cjcrafter.openai.threads.message.*;
8+
import com.cjcrafter.openai.threads.runs.CreateRunRequest;
9+
import com.cjcrafter.openai.threads.runs.MessageCreationDetails;
10+
import com.cjcrafter.openai.threads.runs.Run;
11+
import com.cjcrafter.openai.threads.runs.RunStep;
12+
import io.github.cdimascio.dotenv.Dotenv;
13+
14+
import java.util.Scanner;
15+
16+
public class ThreadExample {
17+
18+
public static void main(String[] args) throws InterruptedException {
19+
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
20+
// dependency. Then you can add a .env file in your project directory.
21+
OpenAI openai = OpenAI.builder()
22+
.apiKey(Dotenv.load().get("OPENAI_TOKEN"))
23+
.build();
24+
25+
// Ask the user to choose an assistant
26+
ListAssistantResponse assistants = openai.assistants().list();
27+
for (int i = 0; i < assistants.getData().size(); i++) {
28+
Assistant assistant = assistants.getData().get(i);
29+
System.out.println(i + ". " + assistant);
30+
}
31+
32+
Scanner scan = new Scanner(System.in);
33+
int choice = Integer.parseInt(scan.nextLine());
34+
Assistant assistant = assistants.getData().get(choice);
35+
36+
// We have to create a new thread. We'll save this thread, so we can
37+
// add user messages and get responses later.
38+
Thread thread = openai.threads().create();
39+
40+
while (true) {
41+
42+
// Handle user input
43+
System.out.println("Type your input below: ");
44+
String input = scan.nextLine();
45+
openai.threads().messages(thread).create(CreateThreadMessageRequest.builder()
46+
.role(ThreadUser.USER)
47+
.content(input)
48+
.build());
49+
50+
// After adding a message to the thread, we have to "run" the thread
51+
Run run = openai.threads().runs(thread).create(CreateRunRequest.builder()
52+
.assistant(assistant)
53+
.build());
54+
55+
// This is a known limitation in OpenAI, and they are working to
56+
// address this so that we can easily stream a response without
57+
// nonsense like this.
58+
while (!run.getStatus().isTerminal()) {
59+
java.lang.Thread.sleep(1000);
60+
run = openai.threads().runs(thread).retrieve(run);
61+
}
62+
63+
// Once the run stops, we want to retrieve the steps of the run.
64+
// this includes message outputs, function calls, code
65+
// interpreters, etc.
66+
for (RunStep step : openai.threads().runs(thread).steps(run).list().getData()) {
67+
if (step.getType() != RunStep.Type.MESSAGE_CREATION) {
68+
System.out.println("Assistant made step: " + step.getType());
69+
continue;
70+
}
71+
72+
// This cast is safe since we checked the type above
73+
MessageCreationDetails details = (MessageCreationDetails) step.getStepDetails();
74+
ThreadMessage message = openai.threads().messages(thread).retrieve(details.getMessageCreation().getMessageId());
75+
for (ThreadMessageContent content : message.getContent()) {
76+
if (content.getType() != ThreadMessageContent.Type.TEXT) {
77+
System.err.println("Unhandled message content type: " + content.getType());
78+
System.err.println("This will never occur since this Assistant doesn't use images.");
79+
System.exit(-1);
80+
}
81+
82+
System.out.println(((TextContent) content).getText().getValue());
83+
}
84+
}
85+
}
86+
}
87+
}

‎examples/src/main/java/chat/ChatCompletion.java renamed to ‎examples/src/main/java/chat/ChatCompletionExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* In this Java example, we will be using the Chat API to create a simple chatbot.
1616
*/
17-
public class ChatCompletion {
17+
public class ChatCompletionExample {
1818

1919
public static void main(String[] args) {
2020

‎examples/src/main/java/chat/StreamChatCompletion.java renamed to ‎examples/src/main/java/chat/StreamChatCompletionExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Instead of waiting for the full response to generate, we will "stream" tokens
1616
* 1 by 1 as they are generated.
1717
*/
18-
public class StreamChatCompletion {
18+
public class StreamChatCompletionExample {
1919

2020
public static void main(String[] args) {
2121

‎examples/src/main/java/chat/StreamChatCompletionFunction.java renamed to ‎examples/src/main/java/chat/StreamChatCompletionFunctionExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* 1 by 1 as they are generated. We will also add a Math tool so that the chatbot
2424
* can solve math problems with a math parser.
2525
*/
26-
public class StreamChatCompletionFunction {
26+
public class StreamChatCompletionFunctionExample {
2727

2828
public static void main(String[] args) {
2929

‎examples/src/main/java/completion/Completion.java renamed to ‎examples/src/main/java/completion/CompletionExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* In this Java example, we will be using the Legacy Completion API to generate
99
* a response.
1010
*/
11-
public class Completion {
11+
public class CompletionExample {
1212

1313
public static void main(String[] args) {
1414

‎examples/src/main/kotlin/assistant/Thread.kt renamed to ‎examples/src/main/kotlin/assistant/ThreadExample.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package assistant
33
import com.cjcrafter.openai.chat.tool.CodeInterpreterToolCall
44
import com.cjcrafter.openai.chat.tool.FunctionToolCall
55
import com.cjcrafter.openai.chat.tool.RetrievalToolCall
6-
import com.cjcrafter.openai.chat.tool.Tool
76
import com.cjcrafter.openai.openAI
87
import com.cjcrafter.openai.threads.create
98
import com.cjcrafter.openai.threads.message.ImageContent
@@ -51,7 +50,7 @@ fun main() {
5150
// This is a known limitation in OpenAI, and they are working to address
5251
// this so that we can easily stream a response without nonsense like this.
5352
while (!run.status.isTerminal) {
54-
Thread.sleep(2500)
53+
java.lang.Thread.sleep(1000)
5554
run = openai.threads.runs(thread).retrieve(run)
5655
}
5756

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /