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 {