Skip to content

Commit

Permalink
chore: add assistant function call test [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Nov 25, 2023
1 parent fccd758 commit e21ec0c
Showing 1 changed file with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package com.aallam.openai.client

import com.aallam.openai.api.assistant.AssistantTool
import com.aallam.openai.api.assistant.assistantRequest
import com.aallam.openai.api.chat.ToolCall
import com.aallam.openai.api.model.ModelId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
import com.aallam.openai.api.run.RequiredAction
import com.aallam.openai.api.run.Run
import com.aallam.openai.client.internal.JsonLenient
import kotlin.test.*

class TestAssistants : TestOpenAI() {

Expand Down Expand Up @@ -37,4 +38,91 @@ class TestAssistants : TestOpenAI() {
val fileGetAfterDelete = openAI.assistant(updatedAssistant.id)
assertNull(fileGetAfterDelete)
}

@Test
fun functionCall() {
val json = """
{
"id": "run_xxxxxx",
"object": "thread.run",
"created_at": 1700939516,
"assistant_id": "asst_xxxx",
"thread_id": "thread_xxxxx",
"status": "requires_action",
"started_at": 1700939516,
"expires_at": 1700940116,
"cancelled_at": null,
"failed_at": null,
"completed_at": null,
"required_action": {
"type": "submit_tool_outputs",
"submit_tool_outputs": {
"tool_calls": [
{
"id": "call_d0SQRcipynYAoc3k5wAkrTta",
"type": "function",
"function": {
"name": "executeAgentTask",
"arguments": "{\n \"taskType\": \"ADD\",\n \"taskDestination\": \"CALENDAR\",\n \"taskData\": \"18:00 - Wizyta u weterynarza\"\n}"
}
}
]
}
},
"last_error": null,
"model": "gpt-3.5-turbo",
"instructions": "Please address the user as Jane Doe. The user has a premium account.",
"tools": [
{
"type": "function",
"function": {
"name": "executeAgentTask",
"description": "Service for executing assistant tasks",
"parameters": {
"type": "object",
"properties": {
"taskType": {
"type": "string",
"enum": [
"ADD",
"DELETE",
"UPDATE",
"GET"
],
"description": "Task type: ADD, DELETE, UPDATE, GET"
},
"taskDestination": {
"type": "string",
"enum": [
"CALENDAR",
"NOTES",
"REMINDERS"
],
"description": "Task destination: CALENDAR, NOTES, REMINDERS"
},
"taskData": {
"type": "string",
"description": "Description of the task to be executed"
}
},
"required": [
"taskType",
"taskDestination",
"taskData"
]
}
}
}
],
"file_ids": [],
"metadata": {}
}
""".trimIndent()

val decoded = JsonLenient.decodeFromString<Run>(json)
assertIs<AssistantTool.FunctionTool>(decoded.tools?.first())
assertIs<RequiredAction.SubmitToolOutputs>(decoded.requiredAction)
val action = decoded.requiredAction as RequiredAction.SubmitToolOutputs
assertIs<ToolCall.Function>(action.toolOutputs.toolCalls.first())
}
}

0 comments on commit e21ec0c

Please sign in to comment.