-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
129 lines (114 loc) · 3.45 KB
/
Copy pathexample_test.go
File metadata and controls
129 lines (114 loc) · 3.45 KB
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
127
128
129
package agentkit_test
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/ThiraSoft/agentkit"
"github.com/ThiraSoft/agentkit/llm"
)
// scripted stands in for a model in these examples: it asks for the clock
// tool, then answers with what the tool returned.
type scripted struct{}
func (scripted) Name() string { return "scripted" }
func (scripted) ModelName() string { return "scripted" }
func (s scripted) Chat(ctx context.Context, msgs []llm.Message, tools []llm.Tool) (*llm.Message, error) {
return s.Stream(ctx, msgs, tools, func(string) error { return nil })
}
func (scripted) Stream(_ context.Context, msgs []llm.Message, _ []llm.Tool, onChunk func(string) error) (*llm.Message, error) {
last := msgs[len(msgs)-1]
if last.Role != "tool" {
return &llm.Message{Role: "assistant", ToolCalls: []llm.ToolCall{{
ID: "1",
Type: "function",
Function: llm.FunctionCall{Name: "clock", Arguments: json.RawMessage(`{}`)},
}}}, nil
}
text := "It is " + last.Content + "."
if err := onChunk(text); err != nil {
return nil, err
}
return &llm.Message{Role: "assistant", Content: text}, nil
}
var clock = agentkit.Tool{
Name: "clock",
Description: "Tells the time.",
Parameters: llm.ToolParams{Type: "object", Properties: llm.ToolProperties{}},
Run: func(ctx context.Context, args json.RawMessage) (string, error) {
return "12:00", nil
},
}
func clockAgent() *agentkit.Agent {
agent, err := agentkit.New(context.Background(), agentkit.Config{
ProviderImpl: scripted{},
Tools: []agentkit.Tool{clock},
})
if err != nil {
log.Fatal(err)
}
return agent
}
// An agent with one Go tool. A real program sets Provider and Model (for
// instance "gemini" and "gemini-2.5-flash-lite") instead of ProviderImpl.
func Example() {
ctx := context.Background()
agent, err := agentkit.New(ctx, agentkit.Config{
ProviderImpl: scripted{},
Tools: []agentkit.Tool{clock},
})
if err != nil {
log.Fatal(err)
}
defer agent.Close()
conv := agent.NewConversation("Answer in one sentence.")
turn, err := conv.Send(ctx, "What time is it?", agentkit.Hooks{
OnText: func(chunk string) { fmt.Println("text:", chunk) },
})
if err != nil {
log.Fatal(err)
}
fmt.Println("steps:", turn.Steps)
fmt.Println("reply:", turn.Text)
// Output:
// text: It is 12:00.
// steps: 2
// reply: It is 12:00.
}
func ExampleHooks() {
agent := clockAgent()
defer agent.Close()
_, err := agent.NewConversation("").Send(context.Background(), "What time is it?", agentkit.Hooks{
OnToolCall: func(c agentkit.ToolCall) { fmt.Println("call:", c.Name, string(c.Arguments)) },
Approve: func(c agentkit.ToolCall) bool { return c.Name == "clock" },
OnToolResult: func(r agentkit.ToolResult) { fmt.Println("result:", r.Content) },
OnFinish: func() { fmt.Println("finished") },
})
if err != nil {
log.Fatal(err)
}
// Output:
// call: clock {}
// result: 12:00
// finished
}
func ExampleAgent_Tools() {
agent := clockAgent()
defer agent.Close()
fmt.Println(agent.Tools())
// Output: [clock]
}
func ExampleConversation_Messages() {
agent := clockAgent()
defer agent.Close()
conv := agent.NewConversation("Answer in one sentence.")
if _, err := conv.Send(context.Background(), "What time is it?", agentkit.Hooks{}); err != nil {
log.Fatal(err)
}
var roles []string
for _, m := range conv.Messages() {
roles = append(roles, m.Role)
}
fmt.Println(strings.Join(roles, " "))
// Output: system user assistant tool assistant
}