Refactor tool output format
This commit is contained in:
@@ -2,11 +2,10 @@ package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
// callTools executes each tool call in the message concurrently and returns
|
||||
@@ -41,26 +40,31 @@ func (c *Client) callTools(ctx context.Context, msg Message) []Message {
|
||||
slog.Any("error", err),
|
||||
slog.String("name", tc.Function.Name),
|
||||
)
|
||||
result = fmt.Sprintf(
|
||||
`{"ok": false, "error": %q}`, err,
|
||||
)
|
||||
} else {
|
||||
c.log.InfoContext(
|
||||
ctx,
|
||||
"called tool",
|
||||
slog.String("name", tc.Function.Name),
|
||||
)
|
||||
results[idx] = Message{
|
||||
Role: RoleTool,
|
||||
ContentParts: []ContentPart{{
|
||||
Type: ContentTypeText,
|
||||
Text: err.Error(),
|
||||
}},
|
||||
Name: tc.Function.Name,
|
||||
ToolCallID: tc.ID,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Content cannot be empty.
|
||||
if strings.TrimSpace(result) == "" {
|
||||
result = `{"ok": true, "result": null}`
|
||||
}
|
||||
c.log.InfoContext(
|
||||
ctx,
|
||||
"called tool",
|
||||
slog.String("name", tc.Function.Name),
|
||||
)
|
||||
|
||||
formatted := result.FormatResult()
|
||||
|
||||
results[idx] = Message{
|
||||
Role: RoleTool,
|
||||
ContentParts: []ContentPart{{
|
||||
Type: ContentTypeText, Text: result,
|
||||
Type: ContentTypeText,
|
||||
Text: formatted,
|
||||
}},
|
||||
Name: tc.Function.Name,
|
||||
ToolCallID: tc.ID,
|
||||
|
||||
Reference in New Issue
Block a user