forked from trpc-group/trpc-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
290 lines (260 loc) · 7.68 KB
/
Copy pathmain.go
File metadata and controls
290 lines (260 loc) · 7.68 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// Tencent is pleased to support the open source community by making trpc-agent-go available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// trpc-agent-go is licensed under the Apache License Version 2.0.
//
//
// Package main demonstrates a minimal multi-turn chat powered by Runner.
// It focuses on core control flow with an in-memory session backend so the
// example stays self-contained and easy to run.
package main
import (
"bufio"
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/google/uuid"
"trpc.group/trpc-go/trpc-agent-go/agent"
"trpc.group/trpc-go/trpc-agent-go/agent/llmagent"
"trpc.group/trpc-go/trpc-agent-go/event"
"trpc.group/trpc-go/trpc-agent-go/model"
"trpc.group/trpc-go/trpc-agent-go/model/openai"
"trpc.group/trpc-go/trpc-agent-go/runner"
sessioninmemory "trpc.group/trpc-go/trpc-agent-go/session/inmemory"
"trpc.group/trpc-go/trpc-agent-go/tool"
"trpc.group/trpc-go/trpc-agent-go/tool/function"
)
var (
modelName = flag.String("model", "deepseek-v4-flash", "Name of the model to use")
streaming = flag.Bool("streaming", true, "Enable streaming mode for responses")
enableParallel = flag.Bool("enable-parallel", false, "Enable parallel tool execution (default: false, serial execution)")
variant = flag.String("variant", "openai", "Name of the variant to use when calling the OpenAI provider")
)
const (
appName = "runner-quickstart"
agentName = "chat-assistant"
)
func main() {
flag.Parse()
fmt.Printf("🚀 Runner quickstart: multi-turn chat with tools\n")
fmt.Printf("Model: %s\n", *modelName)
fmt.Printf("Streaming: %t\n", *streaming)
fmt.Printf("Parallel tools: %t\n", *enableParallel)
fmt.Printf("Session backend: in-memory (simple demo)\n")
fmt.Printf("Type '/exit' to end the conversation\n")
fmt.Printf("Available tools: calculator, current_time\n")
fmt.Println(strings.Repeat("=", 50))
chat := &multiTurnChat{
modelName: *modelName,
streaming: *streaming,
variant: *variant,
}
if err := chat.run(); err != nil {
log.Fatalf("chat failed: %v", err)
}
}
// multiTurnChat manages the conversation loop for the demo.
type multiTurnChat struct {
modelName string
streaming bool
runner runner.Runner
userID string
sessionID string
variant string
}
func (c *multiTurnChat) run() error {
ctx := context.Background()
if err := c.setup(ctx); err != nil {
return fmt.Errorf("setup failed: %w", err)
}
defer c.runner.Close()
return c.startChat(ctx)
}
// setup builds the runner with a model, tools, and the in-memory session store.
func (c *multiTurnChat) setup(_ context.Context) error {
modelInstance := openai.New(c.modelName, openai.WithVariant(openai.Variant(c.variant)))
sessionService := sessioninmemory.NewSessionService()
calculatorTool := function.NewFunctionTool(
c.calculate,
function.WithName("calculator"),
function.WithDescription("Perform basic mathematical calculations (add, subtract, multiply, divide, power)"),
)
timeTool := function.NewFunctionTool(
c.getCurrentTime,
function.WithName("current_time"),
function.WithDescription("Get the current time and date for a specific timezone"),
)
genConfig := model.GenerationConfig{
MaxTokens: intPtr(2000),
Temperature: floatPtr(0.7),
Stream: c.streaming,
}
llmAgent := llmagent.New(
agentName,
llmagent.WithModel(modelInstance),
llmagent.WithDescription("A helpful AI assistant with calculator and time tools."),
llmagent.WithInstruction("Use tools when helpful for calculations or time queries. Stay conversational."),
llmagent.WithGenerationConfig(genConfig),
llmagent.WithTools([]tool.Tool{calculatorTool, timeTool}),
llmagent.WithEnableParallelTools(*enableParallel),
)
c.runner = runner.NewRunner(
appName,
llmAgent,
runner.WithSessionService(sessionService),
)
c.userID = "demo-user"
c.sessionID = fmt.Sprintf("demo-session-%d", time.Now().Unix())
fmt.Printf("✅ Chat ready! Session: %s\n\n", c.sessionID)
return nil
}
func (c *multiTurnChat) startChat(ctx context.Context) error {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Print("👤 You: ")
if !scanner.Scan() {
break
}
userInput := strings.TrimSpace(scanner.Text())
if userInput == "" {
continue
}
if userInput == "/exit" {
fmt.Println("👋 Goodbye!")
return nil
}
if err := c.processMessage(ctx, userInput); err != nil {
fmt.Printf("❌ Error: %v\n", err)
}
fmt.Println()
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("input scanner error: %w", err)
}
return nil
}
func (c *multiTurnChat) processMessage(ctx context.Context, userMessage string) error {
message := model.NewUserMessage(userMessage)
requestID := uuid.New().String()
eventChan, err := c.runner.Run(ctx, c.userID, c.sessionID, message, agent.WithRequestID(requestID))
if err != nil {
return fmt.Errorf("failed to run agent: %w", err)
}
return c.processResponse(eventChan)
}
func (c *multiTurnChat) processResponse(eventChan <-chan *event.Event) error {
fmt.Print("🤖 Assistant: ")
var (
fullContent string
toolCallsDetected bool
assistantStarted bool
)
for evt := range eventChan {
if err := c.handleEvent(evt, &toolCallsDetected, &assistantStarted, &fullContent); err != nil {
return err
}
if evt.IsFinalResponse() {
fmt.Printf("\n")
break
}
}
return nil
}
func (c *multiTurnChat) handleEvent(
evt *event.Event,
toolCallsDetected *bool,
assistantStarted *bool,
fullContent *string,
) error {
if evt.Error != nil {
fmt.Printf("\n❌ Error: %s\n", evt.Error.Message)
return nil
}
if c.handleToolCalls(evt, toolCallsDetected, assistantStarted) {
return nil
}
if c.handleToolResponses(evt) {
return nil
}
c.handleContent(evt, toolCallsDetected, assistantStarted, fullContent)
return nil
}
func (c *multiTurnChat) handleToolCalls(
evt *event.Event,
toolCallsDetected *bool,
assistantStarted *bool,
) bool {
if evt.Response != nil && len(evt.Response.Choices) > 0 && len(evt.Response.Choices[0].Message.ToolCalls) > 0 {
*toolCallsDetected = true
if *assistantStarted {
fmt.Printf("\n")
}
fmt.Printf("🔧 Callable tool calls initiated:\n")
for _, toolCall := range evt.Response.Choices[0].Message.ToolCalls {
fmt.Printf(" • %s (ID: %s)\n", toolCall.Function.Name, toolCall.ID)
if len(toolCall.Function.Arguments) > 0 {
fmt.Printf(" Args: %s\n", string(toolCall.Function.Arguments))
}
}
fmt.Printf("\n🔄 Executing tools...\n")
return true
}
return false
}
func (c *multiTurnChat) handleToolResponses(evt *event.Event) bool {
if evt.Response == nil || len(evt.Response.Choices) == 0 {
return false
}
hasToolResponse := false
for _, choice := range evt.Response.Choices {
if choice.Message.Role == model.RoleTool && choice.Message.ToolID != "" {
fmt.Printf("✅ Callable tool response (ID: %s): %s\n",
choice.Message.ToolID,
strings.TrimSpace(choice.Message.Content))
hasToolResponse = true
}
}
return hasToolResponse
}
func (c *multiTurnChat) handleContent(
evt *event.Event,
toolCallsDetected *bool,
assistantStarted *bool,
fullContent *string,
) {
if evt.Response == nil || len(evt.Response.Choices) == 0 {
return
}
content := c.extractContent(evt.Response.Choices[0])
if content == "" {
return
}
c.displayContent(content, toolCallsDetected, assistantStarted, fullContent)
}
func (c *multiTurnChat) extractContent(choice model.Choice) string {
if c.streaming {
return choice.Delta.Content
}
return choice.Message.Content
}
func (c *multiTurnChat) displayContent(
content string,
toolCallsDetected *bool,
assistantStarted *bool,
fullContent *string,
) {
if !*assistantStarted {
if *toolCallsDetected {
fmt.Printf("\n🤖 Assistant: ")
}
*assistantStarted = true
}
fmt.Print(content)
*fullContent += content
}