1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
package agent
import (
"encoding/json"
"gf-lt/models"
"strings"
)
// PWAgent: is AgenterA type agent (enclosed with tool chaining)
// sysprompt explain tools and how to plan for execution
type PWAgent struct {
*AgentClient
sysprompt string
}
// NewPWAgent creates a PWAgent with the given client and system prompt
func NewPWAgent(client *AgentClient, sysprompt string) *PWAgent {
return &PWAgent{AgentClient: client, sysprompt: sysprompt}
}
// SetTools sets the tools available to the agent
func (a *PWAgent) SetTools(tools []models.Tool) {
a.tools = tools
}
func (a *PWAgent) ProcessTask(task string) []byte {
req, err := a.FormFirstMsg(a.sysprompt, task)
if err != nil {
a.Log().Error("PWAgent failed to process the request", "error", err)
return []byte("PWAgent failed to process the request; err: " + err.Error())
}
toolCallLimit := 10
for i := 0; i < toolCallLimit; i++ {
resp, err := a.LLMRequest(req)
if err != nil {
a.Log().Error("failed to process the request", "error", err)
return []byte("failed to process the request; err: " + err.Error())
}
execTool, toolCallID, hasToolCall := findToolCall(resp)
if !hasToolCall {
return resp
}
a.setToolCallOnLastMessage(resp, toolCallID)
toolResp := string(execTool())
req, err = a.FormMsgWithToolCallID(toolResp, toolCallID)
if err != nil {
a.Log().Error("failed to form next message", "error", err)
return []byte("failed to form next message; err: " + err.Error())
}
}
return nil
}
func (a *PWAgent) setToolCallOnLastMessage(resp []byte, toolCallID string) {
if toolCallID == "" {
return
}
var genericResp map[string]interface{}
if err := json.Unmarshal(resp, &genericResp); err != nil {
return
}
var name string
var args map[string]string
if choices, ok := genericResp["choices"].([]interface{}); ok && len(choices) > 0 {
if firstChoice, ok := choices[0].(map[string]interface{}); ok {
if message, ok := firstChoice["message"].(map[string]interface{}); ok {
if toolCalls, ok := message["tool_calls"].([]interface{}); ok && len(toolCalls) > 0 {
if tc, ok := toolCalls[0].(map[string]interface{}); ok {
if fn, ok := tc["function"].(map[string]interface{}); ok {
name, _ = fn["name"].(string)
argsStr, _ := fn["arguments"].(string)
json.Unmarshal([]byte(argsStr), &args)
}
}
}
}
}
}
if name == "" {
content, _ := genericResp["content"].(string)
name = extractToolNameFromText(content)
}
lastIdx := len(a.chatBody.Messages) - 1
if lastIdx >= 0 {
a.chatBody.Messages[lastIdx].ToolCallID = toolCallID
if name != "" {
argsJSON, _ := json.Marshal(args)
a.chatBody.Messages[lastIdx].ToolCall = &models.ToolCall{
ID: toolCallID,
Name: name,
Args: string(argsJSON),
}
}
}
}
func extractToolNameFromText(text string) string {
jsStr := toolCallRE.FindString(text)
if jsStr == "" {
return ""
}
jsStr = strings.TrimSpace(jsStr)
jsStr = strings.TrimPrefix(jsStr, "__tool_call__")
jsStr = strings.TrimSuffix(jsStr, "__tool_call__")
jsStr = strings.TrimSpace(jsStr)
start := strings.Index(jsStr, "{")
end := strings.LastIndex(jsStr, "}")
if start == -1 || end == -1 || end <= start {
return ""
}
jsStr = jsStr[start : end+1]
var fc models.FuncCall
if err := json.Unmarshal([]byte(jsStr), &fc); err != nil {
return ""
}
return fc.Name
}
|