diff --git a/backend/agents/client/client.go b/backend/agents/client/client.go index 4186e54..14743b2 100644 --- a/backend/agents/client/client.go +++ b/backend/agents/client/client.go @@ -160,23 +160,6 @@ func (client AgentClient) Request(req *AgentRequestBody) (AgentResponse, error) return AgentResponse{}, errors.New("Unsupported. We currently only accept 1 choice from AI.") } - msg := agentResponse.Choices[0].Message - - if len(msg.Content) > 0 { - client.Log.Debugf("Content: %s", msg.Content) - } - - if msg.ToolCalls != nil && len(*msg.ToolCalls) > 0 { - client.Log.Debugf("Tool Call: %s", (*msg.ToolCalls)[0].Function.Name) - - prettyJson, err := json.MarshalIndent((*msg.ToolCalls)[0].Function.Arguments, "", " ") - if err != nil { - return AgentResponse{}, err - } - - client.Log.Debugf("Arguments: %s", string(prettyJson)) - } - req.Chat.AddAiResponse(agentResponse.Choices[0].Message) return agentResponse, nil @@ -184,12 +167,17 @@ func (client AgentClient) Request(req *AgentRequestBody) (AgentResponse, error) func (client *AgentClient) ToolLoop(info ToolHandlerInfo, req *AgentRequestBody) error { for { - err := client.Process(info, req) + response, err := client.Request(req) if err != nil { return err } - _, err = client.Request(req) + if response.Choices[0].FinishReason == "stop" { + client.Log.Debug("Agent is finished") + return nil + } + + err = client.Process(info, req) if err != nil { return err } @@ -227,7 +215,7 @@ func (client *AgentClient) Process(info ToolHandlerInfo, req *AgentRequestBody) client.Reply = toolCall.Function.Arguments } - client.Log.Debugf("Response: %s", toolResponse.Content) + client.Log.Debug("Tool call response", "toolCall", toolCall.Function.Name, "response", toolResponse.Content) req.Chat.AddToolResponse(toolResponse) } @@ -238,8 +226,11 @@ func (client *AgentClient) Process(info ToolHandlerInfo, req *AgentRequestBody) func (client *AgentClient) RunAgent(userId uuid.UUID, imageId uuid.UUID, imageName string, imageData []byte) error { var tools any err := json.Unmarshal([]byte(client.Options.JsonTools), &tools) + if err != nil { + panic(err) + } - toolChoice := "any" + toolChoice := "auto" request := AgentRequestBody{ Tools: &tools, @@ -258,11 +249,6 @@ func (client *AgentClient) RunAgent(userId uuid.UUID, imageId uuid.UUID, imageNa request.Chat.AddSystem(client.Options.SystemPrompt) request.Chat.AddImage(imageName, imageData, client.Options.Query) - _, err = client.Request(&request) - if err != nil { - return err - } - toolHandlerInfo := ToolHandlerInfo{ ImageId: imageId, ImageName: imageName,