Skip to content

Commit 69cae8b

Browse files
authored
Merge pull request #35 from CJCrafter/assistants
Add assistants
2 parents f538803 + 46a4734 commit 69cae8b

File tree

162 files changed

+4246
-274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+4246
-274
lines changed

.gitattributes

100644100755
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
#
2-
# https://help.github.com/articles/dealing-with-line-endings/
3-
#
4-
# These are explicitly windows files and should use crlf
51
*.bat text eol=crlf
6-
2+
* text eol=lf

.github/FUNDING.yml

100644100755
File mode changed.

.github/workflows/dokka-publish.yml

100644100755
File mode changed.

.gitignore

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

build.gradle.kts

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import com.github.breadmoirai.githubreleaseplugin.GithubReleaseTask
22

33
group = "com.cjcrafter"
4-
version = "2.0.2"
4+
version = "2.0.2-SNAPSHOT"
55

66
plugins {
77
`java-library`

examples/build.gradle.kts

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies {
1414
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.3")
1515
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
1616

17-
implementation("ch.qos.logback:logback-classic:1.4.11")
17+
implementation("ch.qos.logback:logback-classic:1.4.12")
1818

1919
// https://mvnrepository.com/artifact/org.mariuszgromada.math/MathParser.org-mXparser
2020
// Used for tool tests
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package assistant;
2+
3+
import com.cjcrafter.openai.OpenAI;
4+
import com.cjcrafter.openai.assistants.CreateAssistantRequest;
5+
import com.cjcrafter.openai.assistants.ModifyAssistantRequest;
6+
import io.github.cdimascio.dotenv.Dotenv;
7+
8+
import java.util.Scanner;
9+
10+
public class AssistantExample {
11+
12+
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
13+
// dependency. Then you can add a .env file in your project directory.
14+
public static final OpenAI openai = OpenAI.builder()
15+
.apiKey(Dotenv.load().get("OPENAI_TOKEN"))
16+
.build();
17+
18+
public static final Scanner scan = new Scanner(System.in);
19+
20+
public static void main(String[] args) {
21+
22+
int input;
23+
do {
24+
System.out.println("1. Create");
25+
System.out.println("2. Retrieve");
26+
System.out.println("3. List");
27+
System.out.println("4. Delete");
28+
System.out.println("5. Modify");
29+
System.out.println("6. Exit");
30+
31+
System.out.print("Code: ");
32+
input = Integer.parseInt(scan.nextLine());
33+
switch (input) {
34+
case 1:
35+
create();
36+
break;
37+
case 2:
38+
retrieve();
39+
break;
40+
case 3:
41+
list();
42+
break;
43+
case 4:
44+
delete();
45+
break;
46+
case 5:
47+
modify();
48+
break;
49+
case 6:
50+
System.out.println("Goodbye!");
51+
break;
52+
default:
53+
System.out.println("Invalid code!");
54+
}
55+
} while (input != 6);
56+
}
57+
58+
public static void create() {
59+
System.out.print("Model: ");
60+
String model = scan.nextLine();
61+
System.out.print("Name: ");
62+
String name = scan.nextLine();
63+
System.out.print("Description: ");
64+
String description = scan.nextLine();
65+
System.out.print("Instructions: ");
66+
String instructions = scan.nextLine();
67+
68+
CreateAssistantRequest request = CreateAssistantRequest.builder()
69+
.model(model)
70+
.name(name)
71+
.description(description)
72+
.instructions(instructions)
73+
.build();
74+
75+
System.out.println("Request: " + request);
76+
System.out.println("Response: " + openai.getAssistants().create(request));
77+
}
78+
79+
public static void retrieve() {
80+
System.out.print("ID: ");
81+
String id = scan.nextLine();
82+
83+
System.out.println("Response: " + openai.getAssistants().retrieve(id));
84+
}
85+
86+
public static void list() {
87+
System.out.println("Response: " + openai.getAssistants().list());
88+
}
89+
90+
public static void delete() {
91+
System.out.print("ID: ");
92+
String id = scan.nextLine();
93+
94+
System.out.println("Response: " + openai.getAssistants().delete(id));
95+
}
96+
97+
98+
public static void modify() {
99+
System.out.print("ID: ");
100+
String id = scan.nextLine();
101+
System.out.print("Name: ");
102+
String name = scan.nextLine();
103+
System.out.print("Description: ");
104+
String description = scan.nextLine();
105+
System.out.print("Instructions: ");
106+
String instructions = scan.nextLine();
107+
108+
ModifyAssistantRequest request = ModifyAssistantRequest.builder()
109+
.name(name)
110+
.description(description)
111+
.instructions(instructions)
112+
.build();
113+
114+
System.out.println("Request: " + request);
115+
System.out.println("Response: " + openai.getAssistants().modify(id, request));
116+
}
117+
}
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+
}

0 commit comments

Comments
 (0)