Skip to content

Commit 8ecab31

Browse files
committed
Fix tool abstraction, add runs api
1 parent 94b0711 commit 8ecab31

37 files changed

+807
-100
lines changed

examples/src/main/java/assistant/Assistant.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.cjcrafter.openai.OpenAI;
44
import com.cjcrafter.openai.assistants.CreateAssistantRequest;
5+
import com.cjcrafter.openai.assistants.ModifyAssistantRequest;
56
import io.github.cdimascio.dotenv.Dotenv;
67

78
import java.util.Scanner;
@@ -74,4 +75,43 @@ public static void create() {
7475
System.out.println("Request: " + request);
7576
System.out.println("Response: " + openai.getAssistants().create(request));
7677
}
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+
}
77117
}

examples/src/main/java/chat/StreamChatCompletionFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void main(String[] args) {
4646
ChatRequest request = ChatRequest.builder()
4747
.model("gpt-3.5-turbo")
4848
.messages(messages)
49-
.addTool(FunctionTool.builder()
49+
.addTool(Function.builder()
5050
.name("solve_math_problem")
5151
.description("Returns the result of a math problem as a double")
5252
.addStringParameter("equation", "The math problem for you to solve", true)
@@ -98,7 +98,7 @@ public static ChatMessage handleToolCall(ToolCall call, List<Tool> validTools) {
9898
// at tool calls (And you probably aren't very good at prompt
9999
// engineering yet!). OpenAI will often "Hallucinate" arguments.
100100
try {
101-
if (call.getType() != ToolType.FUNCTION)
101+
if (call.getType() != Tool.Type.FUNCTION)
102102
throw new HallucinationException("Unknown tool call type: " + call.getType());
103103

104104
FunctionCall function = call.getFunction();

examples/src/main/kotlin/assistant/Assistant.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import io.github.cdimascio.dotenv.dotenv
77

88
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
99
// dependency. Then you can add a .env file in your project directory.
10-
val openai = openAI { apiKey(dotenv()["OPENAI_TOKEN"]) }
10+
private val openai = openAI { apiKey(dotenv()["OPENAI_TOKEN"]) }
1111

1212
fun main() {
1313
do {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package assistant
2+
3+
import com.cjcrafter.openai.openAI
4+
import com.cjcrafter.openai.threads.create
5+
import com.cjcrafter.openai.threads.message.ThreadUser
6+
import io.github.cdimascio.dotenv.dotenv
7+
8+
fun main() {
9+
// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
10+
// dependency. Then you can add a .env file in your project directory.
11+
val openai = openAI { apiKey(dotenv()["OPENAI_TOKEN"]) }
12+
13+
// Ask the user to choose an assistant
14+
val assistants = openai.assistants.list()
15+
assistants.data.forEachIndexed { index, assistant -> println("$index. $assistant") }
16+
val choice = readln().toInt()
17+
val assistant = assistants.data[choice]
18+
19+
val thread = openai.threads.create()
20+
openai.threads.messages(thread).create {
21+
role(ThreadUser.USER)
22+
content("Hi! I am using the threads API right now through Java!")
23+
}
24+
25+
var run = openai.threads.runs(thread).create {
26+
assistant(assistant)
27+
//instructions("You can override instructions, model, etc.")
28+
}
29+
30+
// This is a known limitation in OpenAI, and they are working to address
31+
// this so that we can easily stream a response without nonsense like this.
32+
while (!run.status.isDone) {
33+
Thread.sleep(2500)
34+
run = openai.threads.runs(thread).retrieve(run)
35+
}
36+
37+
println(run)
38+
39+
// Cleanup... Optional
40+
//openai.threads.delete(thread)
41+
}

examples/src/main/kotlin/chat/StreamChatCompletionFunction.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.cjcrafter.openai.chat.ChatMessage.Companion.toSystemMessage
55
import com.cjcrafter.openai.chat.ChatMessage.Companion.toUserMessage
66
import com.cjcrafter.openai.chat.tool.Tool
77
import com.cjcrafter.openai.chat.tool.ToolCall
8-
import com.cjcrafter.openai.chat.tool.ToolType
98
import com.cjcrafter.openai.exception.HallucinationException
109
import com.cjcrafter.openai.openAI
1110
import io.github.cdimascio.dotenv.dotenv
@@ -85,7 +84,7 @@ fun handleToolCall(call: ToolCall, validTools: List<Tool>?): ChatMessage {
8584
// at tool calls (And you probably aren't very good at prompt
8685
// engineering yet!). OpenAI will often "Hallucinate" arguments.
8786
return try {
88-
if (call.type !== ToolType.FUNCTION)
87+
if (call.type !== Tool.Type.FUNCTION)
8988
throw HallucinationException("Unknown tool call type: " + call.type)
9089

9190
val function = call.function

src/main/kotlin/com/cjcrafter/openai/OpenAIImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,14 @@ open class OpenAIImpl @ApiStatus.Internal constructor(
137137

138138
private var threads0: ThreadHandlerImpl? = null
139139
override val threads: ThreadHandler
140-
get() = threads0 ?: ThreadHandlerImpl(requestHelper, ASSISTANTS_ENDPOINT).also { threads0 = it }
140+
get() = threads0 ?: ThreadHandlerImpl(requestHelper, THREADS_ENDPOINT).also { threads0 = it }
141141

142142
companion object {
143143
const val COMPLETIONS_ENDPOINT = "v1/completions"
144144
const val CHAT_ENDPOINT = "v1/chat/completions"
145145
const val EMBEDDINGS_ENDPOINT = "v1/embeddings"
146146
const val FILES_ENDPOINT = "v1/files"
147147
const val ASSISTANTS_ENDPOINT = "v1/assistants"
148+
const val THREADS_ENDPOINT = "v1/threads"
148149
}
149150
}

src/main/kotlin/com/cjcrafter/openai/assistants/AbstractAssistantBuilder.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cjcrafter.openai.assistants
22

3-
import com.cjcrafter.openai.chat.tool.AbstractTool
43
import com.cjcrafter.openai.chat.tool.Tool
54
import com.cjcrafter.openai.files.FileObject
65
import com.cjcrafter.openai.util.OpenAIDslMarker
@@ -129,11 +128,11 @@ abstract class AbstractAssistantBuilder<T> protected constructor() {
129128
*
130129
* @throws IllegalStateException if there are already 128 tools
131130
*/
132-
fun addTool(tool: AbstractTool) = apply {
131+
fun addTool(tool: Tool) = apply {
133132
if (tools == null) tools = mutableListOf()
134133
if (tools!!.size > 128)
135134
throw IllegalStateException("cannot have more than 128 tools")
136-
tools!!.add(tool.toTool())
135+
tools!!.add(tool)
137136
}
138137

139138
/**

src/main/kotlin/com/cjcrafter/openai/chat/ChatRequest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.cjcrafter.openai.chat
22

3-
import com.cjcrafter.openai.chat.tool.AbstractTool
4-
import com.cjcrafter.openai.chat.tool.FunctionTool
53
import com.cjcrafter.openai.chat.tool.Tool
64
import com.cjcrafter.openai.chat.tool.ToolChoice
75
import com.cjcrafter.openai.util.OpenAIDslMarker
@@ -86,9 +84,9 @@ data class ChatRequest @JvmOverloads internal constructor(
8684
*
8785
* @param tool
8886
*/
89-
fun addTool(tool: AbstractTool) = apply {
87+
fun addTool(tool: Tool) = apply {
9088
if (tools == null) tools = mutableListOf()
91-
tools!!.add(tool.toTool())
89+
tools!!.add(tool)
9290
}
9391

9492
/**
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.cjcrafter.openai.chat
22

3-
import com.cjcrafter.openai.chat.tool.FunctionTool
3+
import com.cjcrafter.openai.chat.tool.Function
4+
import com.cjcrafter.openai.chat.tool.Tool
45

56
/**
67
* Creates a [ChatRequest] using the [ChatRequest.Builder] using Kotlin DSL.
78
*/
89
fun chatRequest(block: ChatRequest.Builder.() -> Unit) = ChatRequest.builder().apply(block).build()
910

1011
/**
11-
* Adds a [FunctionTool] to the [ChatRequest] using Kotlin DSL.
12+
* Adds a [Tool.FunctionTool] to the [ChatRequest] using Kotlin DSL.
1213
*/
13-
fun ChatRequest.Builder.function(block: FunctionTool.Builder.() -> Unit) = addTool(FunctionTool.builder().apply(block).build())
14+
fun ChatRequest.Builder.function(block: Function.Builder.() -> Unit) = addTool(Function.builder().apply(block).build())

src/main/kotlin/com/cjcrafter/openai/chat/tool/AbstractTool.kt

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

0 commit comments

Comments
 (0)