From 2737a5b2be93df899e2a9ed4f7272649d0d62489 Mon Sep 17 00:00:00 2001 From: Steve Dudenhoeffer Date: Mon, 17 Mar 2025 00:18:32 -0400 Subject: [PATCH] Refactor response handling for clarity and consistency. Simplified how responses and tool calls are appended to conversations. Adjusted structure in message formatting to better align with tool call requirements, ensuring consistent data representation. --- context.go | 11 +---------- response.go | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/context.go b/context.go index 04a2b27..bb5620a 100644 --- a/context.go +++ b/context.go @@ -28,16 +28,7 @@ func (c *Context) ToNewRequest(toolResults ...ToolCallResponse) Request { // if there are tool calls, then we need to add those to the conversation if c.response != nil { - for _, call := range c.response.Calls { - res.Conversation = append(res.Conversation, call) - - if c.response.Content != "" || c.response.Refusal != "" { - res.Conversation = append(res.Conversation, Message{ - Role: RoleAssistant, - Text: c.response.Content, - }) - } - } + res.Conversation = append(res.Conversation, *c.response) } // if there are tool results, then we need to add those to the conversation diff --git a/response.go b/response.go index ef88bfa..85160c3 100644 --- a/response.go +++ b/response.go @@ -25,27 +25,29 @@ func (r ResponseChoice) toRaw() map[string]any { calls = append(calls, call.toRaw()) } - res["calls"] = calls + res["tool_calls"] = calls return res } func (r ResponseChoice) toChatCompletionMessages() []openai.ChatCompletionMessage { - var res []openai.ChatCompletionMessage + var res = openai.ChatCompletionMessage{ + Role: openai.ChatMessageRoleAssistant, + Content: r.Content, + Refusal: r.Refusal, + } for _, call := range r.Calls { - res = append(res, call.toChatCompletionMessages()...) - } - - if r.Refusal != "" || r.Content != "" { - res = append(res, openai.ChatCompletionMessage{ - Role: openai.ChatMessageRoleAssistant, - Content: r.Content, - Refusal: r.Refusal, + res.ToolCalls = append(res.ToolCalls, openai.ToolCall{ + ID: call.ID, + Type: openai.ToolTypeFunction, + Function: openai.FunctionCall{ + Name: call.FunctionCall.Name, + Arguments: call.FunctionCall.Arguments, + }, }) } - - return res + return []openai.ChatCompletionMessage{res} } func (r ResponseChoice) toInput() []Input {