feat(log): pretty logging agent responses and tool calls
This commit is contained in:
@@ -4,10 +4,11 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
type ResponseFormat struct {
|
||||
@@ -69,12 +70,14 @@ type AgentClient struct {
|
||||
|
||||
ToolHandler ToolsHandlers
|
||||
|
||||
Log *log.Logger
|
||||
|
||||
Do func(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
const OPENAI_API_KEY = "OPENAI_API_KEY"
|
||||
|
||||
func CreateAgentClient() (AgentClient, error) {
|
||||
func CreateAgentClient(log *log.Logger) (AgentClient, error) {
|
||||
apiKey := os.Getenv(OPENAI_API_KEY)
|
||||
|
||||
if len(apiKey) == 0 {
|
||||
@@ -89,6 +92,8 @@ func CreateAgentClient() (AgentClient, error) {
|
||||
return client.Do(req)
|
||||
},
|
||||
|
||||
Log: log,
|
||||
|
||||
ToolHandler: ToolsHandlers{
|
||||
handlers: map[string]ToolHandler{},
|
||||
},
|
||||
@@ -128,8 +133,6 @@ func (client AgentClient) Request(req *AgentRequestBody) (AgentResponse, error)
|
||||
return AgentResponse{}, err
|
||||
}
|
||||
|
||||
fmt.Println(string(response))
|
||||
|
||||
agentResponse := AgentResponse{}
|
||||
err = json.Unmarshal(response, &agentResponse)
|
||||
|
||||
@@ -141,6 +144,25 @@ func (client AgentClient) Request(req *AgentRequestBody) (AgentResponse, error)
|
||||
return AgentResponse{}, errors.New("Unsupported. We currently only accept 1 choice from AI.")
|
||||
}
|
||||
|
||||
client.Log.SetLevel(log.DebugLevel)
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user