Skip to content

Commit

Permalink
fix(runs): run steps deserialization (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam authored Nov 25, 2023
1 parent 5554118 commit 8111678
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.test.assertTrue
class TestAssistants : TestOpenAI() {

@Test
fun listAssistants() = test {
fun assistants() = test {
val request = assistantRequest {
name = "Math Tutor"
tools = listOf(AssistantTool.CodeInterpreter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package com.aallam.openai.client

import com.aallam.openai.api.assistant.AssistantTool
import com.aallam.openai.api.assistant.assistantRequest
import com.aallam.openai.api.core.PaginatedList
import com.aallam.openai.api.core.Status
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.api.run.RunRequest
import com.aallam.openai.api.run.RunStep
import com.aallam.openai.api.run.RunStepDetails
import com.aallam.openai.api.run.ThreadRunRequest
import com.aallam.openai.client.internal.JsonLenient
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down Expand Up @@ -50,4 +54,74 @@ class TestRuns : TestOpenAI() {
val runs = openAI.runSteps(threadId = run.threadId, runId = run.id)
assertEquals(0, runs.size)
}

@Test
fun runSteps() = test {
val json = """
{
"object": "list",
"data": [
{
"id": "step_dVK6IlRuFv1z2d8GtB13a8Ff",
"object": "thread.run.step",
"created_at": 1700903454,
"run_id": "run_Sc9g8odBSrMHXFajgIRD1vx6",
"assistant_id": "asst_1UUuuAqyn7mctRo1bf95YV4G",
"thread_id": "thread_R6mlKFGLXAz71vb05JsEPeCf",
"type": "message_creation",
"status": "completed",
"cancelled_at": null,
"completed_at": 1700903455,
"expires_at": null,
"failed_at": null,
"last_error": null,
"step_details": {
"type": "message_creation",
"message_creation": {
"message_id": "msg_h9QzSO20zNuonZhXcwNpbnoR"
}
}
},
{
"id": "step_kDMRs0mJcg2bN7KzrcxorXI2",
"object": "thread.run.step",
"created_at": 1700903443,
"run_id": "run_Sc9g8odBSrMHXFajgIRD1vx6",
"assistant_id": "asst_1UUuuAqyn7mctRo1bf95YV4G",
"thread_id": "thread_R6mlKFGLXAz71vb05JsEPeCf",
"type": "tool_calls",
"status": "completed",
"cancelled_at": null,
"completed_at": 1700903454,
"expires_at": null,
"failed_at": null,
"last_error": null,
"step_details": {
"type": "tool_calls",
"tool_calls": [
{
"id": "call_MeXdlXsLiVDZGmjgtdzDDoKs",
"type": "code_interpreter",
"code_interpreter": {
"input": "from sympy import symbols, Eq, solve\r\n\r\n# Define the variable\r\nx = symbols('x')\r\n\r\n# Define the equation\r\nequation = Eq(3 * x + 11, 14)\r\n\r\n# Solve the equation\r\nsolution = solve(equation, x)\r\nsolution",
"outputs": [
{
"type": "logs",
"logs": "[1]"
}
]
}
}
]
}
}
],
"first_id": "step_dVK6IlRuFv1z2d8GtB13a8Ff",
"last_id": "step_kDMRs0mJcg2bN7KzrcxorXI2",
"has_more": false
}
""".trimIndent()
val decoded = JsonLenient.decodeFromString<PaginatedList<RunStep>>(json)
println(decoded)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@BetaOpenAI
@Serializable
public sealed interface RunStep {
/**
* The identifier of the run step, which can be referenced in API endpoints.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package com.aallam.openai.api.run

import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.file.FileId
import com.aallam.openai.api.message.MessageId
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
* A run step object.
*/
@BetaOpenAI
@Serializable
public sealed interface RunStepDetails

/**
Expand All @@ -32,7 +34,7 @@ public data class MessageCreation(
/**
* The ID of the message that was created by this run step.
*/
@SerialName("message") public val message: String,
@SerialName("message_id") public val messageId: MessageId,
)

/**
Expand Down Expand Up @@ -133,7 +135,7 @@ public sealed interface CodeInterpreterToolCallOutput {
/**
* The text output from the Code Interpreter tool call.
*/
@SerialName("text") public val text: String,
@SerialName("text") public val text: String? = null,
) : CodeInterpreterToolCallOutput

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import com.aallam.openai.api.core.Status
import com.aallam.openai.api.message.MessageContent
import com.aallam.openai.api.message.MessageRequest
import com.aallam.openai.api.model.ModelId
import com.aallam.openai.api.run.MessageCreationStep
import com.aallam.openai.api.run.RunRequest
import com.aallam.openai.api.run.ToolCallStep
import com.aallam.openai.api.run.ToolCallsStep
import com.aallam.openai.client.OpenAI
import kotlinx.coroutines.delay

Expand Down Expand Up @@ -57,6 +60,10 @@ suspend fun assistants(openAI: OpenAI) {
val retrievedRun = openAI.getRun(threadId = thread.id, runId = run.id)
} while (retrievedRun.status != Status.Completed)

// 5.1 Check run steps
val runSteps = openAI.runSteps(threadId = run.threadId, runId = run.id)
println("\nRun steps: ${runSteps.size}")

// 6. Display the assistant's response
val assistantMessages = openAI.messages(thread.id)
println("\nThe assistant's response:")
Expand Down

0 comments on commit 8111678

Please sign in to comment.