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.
This commit is contained in:
Steve Dudenhoeffer 2025-03-17 00:18:32 -04:00
parent 7f5e34e437
commit 2737a5b2be
2 changed files with 15 additions and 22 deletions

View File

@ -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 there are tool calls, then we need to add those to the conversation
if c.response != nil { if c.response != nil {
for _, call := range c.response.Calls { res.Conversation = append(res.Conversation, *c.response)
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,
})
}
}
} }
// if there are tool results, then we need to add those to the conversation // if there are tool results, then we need to add those to the conversation

View File

@ -25,27 +25,29 @@ func (r ResponseChoice) toRaw() map[string]any {
calls = append(calls, call.toRaw()) calls = append(calls, call.toRaw())
} }
res["calls"] = calls res["tool_calls"] = calls
return res return res
} }
func (r ResponseChoice) toChatCompletionMessages() []openai.ChatCompletionMessage { 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 { for _, call := range r.Calls {
res = append(res, call.toChatCompletionMessages()...) res.ToolCalls = append(res.ToolCalls, openai.ToolCall{
} ID: call.ID,
Type: openai.ToolTypeFunction,
if r.Refusal != "" || r.Content != "" { Function: openai.FunctionCall{
res = append(res, openai.ChatCompletionMessage{ Name: call.FunctionCall.Name,
Role: openai.ChatMessageRoleAssistant, Arguments: call.FunctionCall.Arguments,
Content: r.Content, },
Refusal: r.Refusal,
}) })
} }
return []openai.ChatCompletionMessage{res}
return res
} }
func (r ResponseChoice) toInput() []Input { func (r ResponseChoice) toInput() []Input {