From e21ec0cbc159640d155ed2391d1c2f55736eefdb Mon Sep 17 00:00:00 2001 From: Mouaad Aallam Date: Sat, 25 Nov 2023 23:57:39 +0100 Subject: [PATCH] chore: add assistant function call test [skip ci] --- .../aallam/openai/client/TestAssistants.kt | 96 ++++++++++++++++++- 1 file changed, 92 insertions(+), 4 deletions(-) diff --git a/openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestAssistants.kt b/openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestAssistants.kt index 77a5ca14..4c99a43e 100644 --- a/openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestAssistants.kt +++ b/openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestAssistants.kt @@ -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() { @@ -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(json) + assertIs(decoded.tools?.first()) + assertIs(decoded.requiredAction) + val action = decoded.requiredAction as RequiredAction.SubmitToolOutputs + assertIs(action.toolOutputs.toolCalls.first()) + } }