Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assistants v2 #352

Merged
merged 6 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Fixed
- **chat**: enhance flow cancel capability (#333)
- **config**: request options headers override previous values (#353)

## 3.7.2
> Published 28 Apr 2024
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.assistant.Assistant
import com.aallam.openai.api.assistant.AssistantId
import com.aallam.openai.api.assistant.AssistantRequest
import com.aallam.openai.api.core.DeleteResponse
import com.aallam.openai.api.core.ListResponse
import com.aallam.openai.api.core.RequestOptions
import com.aallam.openai.api.core.SortOrder
import com.aallam.openai.api.core.*
import com.aallam.openai.api.exception.OpenAIAPIException
import com.aallam.openai.api.file.FileId
import com.aallam.openai.client.Assistants
Expand All @@ -30,7 +27,7 @@ internal class AssistantsApi(val requester: HttpRequester) : Assistants {
url(path = ApiPath.Assistants)
setBody(request)
contentType(ContentType.Application.Json)
beta("assistants", requestOptions?.betaVersion ?: 1)
beta("assistants", 2)
requestOptions(requestOptions)
}.body()
}
Expand All @@ -42,7 +39,7 @@ internal class AssistantsApi(val requester: HttpRequester) : Assistants {
return requester.perform<HttpResponse> {
it.get {
url(path = "${ApiPath.Assistants}/${id.id}")
beta("assistants", requestOptions?.betaVersion ?: 1)
beta("assistants", 2)
requestOptions(requestOptions)
}
}.body()
Expand All @@ -63,7 +60,7 @@ internal class AssistantsApi(val requester: HttpRequester) : Assistants {
url(path = "${ApiPath.Assistants}/${id.id}")
setBody(request)
contentType(ContentType.Application.Json)
beta("assistants", 1)
beta("assistants", 2)
requestOptions(requestOptions)
}.body()
}
Expand All @@ -74,7 +71,7 @@ internal class AssistantsApi(val requester: HttpRequester) : Assistants {
val response = requester.perform<HttpResponse> {
it.delete {
url(path = "${ApiPath.Assistants}/${id.id}")
beta("assistants", requestOptions?.betaVersion ?: 1)
beta("assistants", 2)
requestOptions(requestOptions)
}
}
Expand All @@ -101,7 +98,7 @@ internal class AssistantsApi(val requester: HttpRequester) : Assistants {
after?.let { parameter("after", it.id) }
before?.let { parameter("before", it.id) }
}
beta("assistants", requestOptions?.betaVersion ?: 1)
beta("assistants", 2)
requestOptions(requestOptions)
}.body()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.aallam.openai.client.internal.api
import com.aallam.openai.api.core.PaginatedList
import com.aallam.openai.api.core.RequestOptions
import com.aallam.openai.api.core.SortOrder
import com.aallam.openai.api.file.FileId
import com.aallam.openai.api.message.Message
import com.aallam.openai.api.message.MessageId
import com.aallam.openai.api.message.MessageRequest
Expand All @@ -28,8 +27,10 @@ internal class MessagesApi(val requester: HttpRequester) : Messages {
url(path = "${ApiPath.Threads}/${threadId.id}/messages")
setBody(request)
contentType(ContentType.Application.Json)
beta("assistants", 1)
requestOptions(requestOptions)
beta("assistants", 2)
requestOptions(
requestOptions
)
}.body()
}
}
Expand All @@ -38,8 +39,10 @@ internal class MessagesApi(val requester: HttpRequester) : Messages {
return requester.perform {
it.get {
url(path = "${ApiPath.Threads}/${threadId.id}/messages/${messageId.id}")
beta("assistants", 1)
requestOptions(requestOptions)
beta("assistants", 2)
requestOptions(
requestOptions
)
}.body()
}
}
Expand All @@ -57,7 +60,7 @@ internal class MessagesApi(val requester: HttpRequester) : Messages {
setBody(mapOf("metadata" to meta))
contentType(ContentType.Application.Json)
}
beta("assistants", 1)
beta("assistants", 2)
requestOptions(requestOptions)
}.body()
}
Expand All @@ -79,7 +82,7 @@ internal class MessagesApi(val requester: HttpRequester) : Messages {
before?.let { value -> parameter("before", value.id) }
after?.let { value -> parameter("after", value.id) }
}
beta("assistants", 1)
beta("assistants", 2)
requestOptions(requestOptions)
}.body()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class ThreadsApi(val requester: HttpRequester) : Threads {
setBody(req)
contentType(ContentType.Application.Json)
}
beta("assistants", requestOptions?.betaVersion ?: 1)
beta("assistants", 2)
requestOptions(requestOptions)
}.body()
}
Expand All @@ -43,7 +43,7 @@ internal class ThreadsApi(val requester: HttpRequester) : Threads {
return requester.perform<HttpResponse> {
it.get {
url(path = "${ApiPath.Threads}/${id.id}")
beta("assistants", requestOptions?.betaVersion ?: 1)
beta("assistants", 2)
requestOptions(requestOptions)
}
}.body()
Expand All @@ -67,7 +67,7 @@ internal class ThreadsApi(val requester: HttpRequester) : Threads {
url(path = "${ApiPath.Threads}/${id.id}")
setBody(request)
contentType(ContentType.Application.Json)
beta("assistants", requestOptions?.betaVersion ?: 1)
beta("assistants", 2)
requestOptions(requestOptions)
}.body()
}
Expand All @@ -78,7 +78,7 @@ internal class ThreadsApi(val requester: HttpRequester) : Threads {
val response = requester.perform<HttpResponse> {
it.delete {
url(path = "${ApiPath.Threads}/${id.id}")
beta("assistants", requestOptions?.betaVersion ?: 1)
beta("assistants", 2)
requestOptions(requestOptions)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import io.ktor.client.request.*
*/
internal fun HttpRequestBuilder.requestOptions(requestOptions: RequestOptions? = null) {
if (requestOptions == null) return
requestOptions.headers.forEach { (key, value) -> headers.append(key, value) }
requestOptions.headers.forEach { (key, value) ->
if (headers.contains(key)) headers.remove(key)
headers[key] = value
}
requestOptions.urlParameters.forEach { (key, value) -> url.parameters.append(key, value) }
requestOptions.timeout?.let { timeout ->
timeout {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,6 @@ class TestAssistants : TestOpenAI() {

@Test
fun codeInterpreter() = test {
val request = assistantRequest {
name = "Math Tutor"
tools = listOf(AssistantTool.CodeInterpreter)
model = ModelId("gpt-4")
}
val assistant = openAI.assistant(request)
assertEquals(request.name, assistant.name)
assertEquals(request.tools, assistant.tools)
assertEquals(request.model, assistant.model)

val getAssistant = openAI.assistant(assistant.id)
assertEquals(getAssistant, assistant)

val assistants = openAI.assistants()
assertTrue { assistants.isNotEmpty() }

val updated = assistantRequest { name = "Super Math Tutor" }
val updatedAssistant = openAI.assistant(assistant.id, updated)
assertEquals(updated.name, updatedAssistant.name)

openAI.delete(updatedAssistant.id)

val fileGetAfterDelete = openAI.assistant(updatedAssistant.id)
assertNull(fileGetAfterDelete)
}

@Test
fun codeInterpreterV2() = test {
val request = assistantRequest {
name = "Math Tutor"
tools = listOf(AssistantTool.CodeInterpreter)
Expand All @@ -51,9 +23,6 @@ class TestAssistants : TestOpenAI() {
}
val assistant = openAI.assistant(
request = request,
requestOptions = RequestOptions(
betaVersion = 2,
)
)
assertEquals(request.name, assistant.name)
assertEquals(request.tools, assistant.tools)
Expand All @@ -62,17 +31,10 @@ class TestAssistants : TestOpenAI() {

val getAssistant = openAI.assistant(
assistant.id,
requestOptions = RequestOptions(
betaVersion = 2,
)
)
assertEquals(getAssistant, assistant)

val assistants = openAI.assistants(
requestOptions = RequestOptions(
betaVersion = 2,
)
)
val assistants = openAI.assistants()
assertTrue { assistants.isNotEmpty() }

val updated = assistantRequest {
Expand All @@ -82,25 +44,16 @@ class TestAssistants : TestOpenAI() {
val updatedAssistant = openAI.assistant(
assistant.id,
updated,
requestOptions = RequestOptions(
betaVersion = 2,
)
)
assertEquals(updated.name, updatedAssistant.name)
assertEquals(request.responseFormat, assistant.responseFormat)

openAI.delete(
updatedAssistant.id,
requestOptions = RequestOptions(
betaVersion = 2,
),
)

val fileGetAfterDelete = openAI.assistant(
updatedAssistant.id,
requestOptions = RequestOptions(
betaVersion = 2,
)
)
assertNull(fileGetAfterDelete)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,8 @@ class TestThreads : TestOpenAI() {
fun threads() = test {
val thread = openAI.thread()

val retrieved = openAI.thread(thread.id)
assertEquals(thread, retrieved)

val metadata = mapOf("modified" to "true", "user" to "aallam")
val updated = openAI.thread(id = thread.id, metadata = metadata)
assertEquals(metadata, updated.metadata)

val deleted = openAI.delete(thread.id)
assertEquals(true, deleted)
}

@Test
fun threadsV2() = test {
val thread = openAI.thread(
requestOptions = RequestOptions(
betaVersion = 2
)
)

val retrieved = openAI.thread(
thread.id,
requestOptions = RequestOptions(
betaVersion = 2
)
)
// thread creating request receives null tool_resource,
// but retrieved one always has code interpreter with empty file_ids
Expand All @@ -50,17 +28,11 @@ class TestThreads : TestOpenAI() {
val updated = openAI.thread(
id = thread.id,
metadata = metadata,
requestOptions = RequestOptions(
betaVersion = 2
)
)
assertEquals(metadata, updated.metadata)

val deleted = openAI.delete(
thread.id,
requestOptions = RequestOptions(
betaVersion = 2
)
)
assertEquals(true, deleted)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,31 @@ class TestRequestOptions {
client.models(requestOptions = requestOptions)
assertEquals(requestHeaders?.get(key), requestOptions.headers[key])
}

@Test
fun testOverride() = runTest {
val key = "OpenAI-Beta"
val requestOptions = RequestOptions(
headers = mapOf(key to "assistants=v0"),
)
var requestHeaders: Map<String, String>? = null
val client = OpenAI(
token = token,
headers = mapOf(key to "assistants=v3"),
httpClientConfig = {
install("RequestInterceptor") {
requestPipeline.intercept(HttpRequestPipeline.Before) {
requestHeaders = context.headers.entries().associate { it.key to it.value.first() }
}
}
}
)

try {
client.assistants(requestOptions = requestOptions)
} catch (e: Exception) {
// skip
}
assertEquals(requestHeaders?.get(key), requestOptions.headers[key])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ public data class RequestOptions(
public val headers: Map<String, String> = emptyMap(),
public val urlParameters: Map<String, String> = emptyMap(),
public val timeout: Timeout? = null,
public val betaVersion: Int? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ public data class Message(
*/
@SerialName("run_id") val runId: RunId? = null,

/**
* A list of file IDs that the assistant should use. Useful for tools like retrieval
* and code_interpreter that can access files. A maximum of 10 files can be attached to a message.
*/
@SerialName("file_ids") val fileIds: List<FileId>,

/**
* Set of 16 key-value pairs that can be attached to an object. This can be useful for storing
* additional information about the object in a structured format. Keys can be a maximum of 64 characters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ public class MessageRequest(
*/
@SerialName("content") public val content: String,

/**
* A list of File IDs that the message should use.
* There can be a maximum of 10 files attached to a message.
* Useful for tools like retrieval and code interpreter that can access and use files.
*/
@SerialName("file_ids") public val fileIds: List<FileId>? = null,

/**
* Set of 16 key-value pairs that can be attached to an object.
* This can be useful for storing additional information about the object in a structured format.
Expand Down Expand Up @@ -59,13 +52,6 @@ public class MessageRequestBuilder {
*/
public var content: String? = null

/**
* A list of File IDs that the message should use.
* There can be a maximum of 10 files attached to a message.
* Useful for tools like retrieval and code interpreter that can access and use files.
*/
public var fileIds: List<FileId>? = null

/**
* Set of 16 key-value pairs that can be attached to an object.
* This can be useful for storing additional information about the object in a structured format.
Expand All @@ -76,7 +62,6 @@ public class MessageRequestBuilder {
public fun build(): MessageRequest = MessageRequest(
role = requireNotNull(role) { "role is required" },
content = requireNotNull(content) { "content is required" },
fileIds = fileIds,
metadata = metadata
)
}