|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + "trpc.group/trpc-go/trpc-agent-go/agent/llmagent" |
| 13 | + "trpc.group/trpc-go/trpc-agent-go/event" |
| 14 | + "trpc.group/trpc-go/trpc-agent-go/model" |
| 15 | + "trpc.group/trpc-go/trpc-agent-go/model/openai" |
| 16 | + "trpc.group/trpc-go/trpc-agent-go/runner" |
| 17 | + "trpc.group/trpc-go/trpc-agent-go/tool" |
| 18 | + "trpc.group/trpc-go/trpc-agent-go/tool/email" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + streaming = flag.Bool("streaming", true, "Enable streaming mode for responses") |
| 23 | + modelName = flag.String("model", "deepseek-chat", "Name of the model to use") |
| 24 | +) |
| 25 | + |
| 26 | +func main() { |
| 27 | + // Parse command line flags. |
| 28 | + flag.Parse() |
| 29 | + |
| 30 | + fmt.Printf("🚀 Send Email Chat Demo\n") |
| 31 | + fmt.Printf("Model: %s\n", *modelName) |
| 32 | + fmt.Printf("Streaming: %t\n", *streaming) |
| 33 | + fmt.Printf("Type 'exit' to end the conversation\n") |
| 34 | + fmt.Printf("Available tools: send_email\n") |
| 35 | + fmt.Println(strings.Repeat("=", 50)) |
| 36 | + |
| 37 | + // Create and run the chat. |
| 38 | + chat := &emailChat{ |
| 39 | + modelName: *modelName, |
| 40 | + streaming: *streaming, |
| 41 | + } |
| 42 | + |
| 43 | + if err := chat.run(); err != nil { |
| 44 | + log.Fatal("Chat failed: %v", err) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +type emailChat struct { |
| 49 | + modelName string |
| 50 | + runner runner.Runner |
| 51 | + userID string |
| 52 | + sessionID string |
| 53 | + streaming bool |
| 54 | +} |
| 55 | + |
| 56 | +// run starts the interactive chat session. |
| 57 | +func (c *emailChat) run() error { |
| 58 | + ctx := context.Background() |
| 59 | + |
| 60 | + // Setup the runner. |
| 61 | + if err := c.setup(ctx); err != nil { |
| 62 | + return fmt.Errorf("setup failed: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Start interactive chat. |
| 66 | + return c.startChat(ctx) |
| 67 | +} |
| 68 | + |
| 69 | +// setup creates the runner with LLM agent and send email tool. |
| 70 | +func (c *emailChat) setup(ctx context.Context) error { |
| 71 | + // Create OpenAI model. |
| 72 | + modelInstance := openai.New(c.modelName) |
| 73 | + |
| 74 | + // Create email tool. |
| 75 | + // For basic usage: |
| 76 | + emailTool, err := email.NewToolSet() |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("create file tool set: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + // Create LLM agent with email tool. |
| 82 | + genConfig := model.GenerationConfig{ |
| 83 | + MaxTokens: intPtr(2000), |
| 84 | + Temperature: floatPtr(0.7), |
| 85 | + Stream: c.streaming, // Enable streaming |
| 86 | + } |
| 87 | + |
| 88 | + agentName := "email-assistant" |
| 89 | + llmAgent := llmagent.New( |
| 90 | + agentName, |
| 91 | + llmagent.WithModel(modelInstance), |
| 92 | + llmagent.WithDescription("A helpful AI assistant with access to email sending capabilities"), |
| 93 | + llmagent.WithInstruction("Use the email tool to send emails. ask user to provide account credentials"), |
| 94 | + llmagent.WithGenerationConfig(genConfig), |
| 95 | + llmagent.WithToolSets([]tool.ToolSet{emailTool}), |
| 96 | + ) |
| 97 | + |
| 98 | + // Create runner. |
| 99 | + appName := "email-agent" |
| 100 | + c.runner = runner.NewRunner( |
| 101 | + appName, |
| 102 | + llmAgent, |
| 103 | + ) |
| 104 | + |
| 105 | + // Setup identifiers. |
| 106 | + c.userID = "user" |
| 107 | + c.sessionID = fmt.Sprintf("email-session-%d", time.Now().Unix()) |
| 108 | + |
| 109 | + fmt.Printf("✅ Email chat ready! Session: %s\n\n", c.sessionID) |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// startChat runs the interactive conversation loop. |
| 114 | +func (c *emailChat) startChat(ctx context.Context) error { |
| 115 | + scanner := bufio.NewScanner(os.Stdin) |
| 116 | + |
| 117 | + // Print welcome message with examples. |
| 118 | + fmt.Println("💡 Try asking questions like:") |
| 119 | + fmt. Println( " - send an email to [email protected] user:your_email password:your_password subject:subject content:content") |
| 120 | + fmt.Println() |
| 121 | + |
| 122 | + for { |
| 123 | + fmt.Print("👤 You: ") |
| 124 | + if !scanner.Scan() { |
| 125 | + break |
| 126 | + } |
| 127 | + |
| 128 | + userInput := strings.TrimSpace(scanner.Text()) |
| 129 | + if userInput == "" { |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + // Handle exit command. |
| 134 | + if strings.ToLower(userInput) == "exit" { |
| 135 | + fmt.Println("👋 Goodbye!") |
| 136 | + return nil |
| 137 | + } |
| 138 | + |
| 139 | + // Process the user message. |
| 140 | + if err := c.processMessage(ctx, userInput); err != nil { |
| 141 | + fmt.Printf("❌ Error: %v\n", err) |
| 142 | + } |
| 143 | + |
| 144 | + fmt.Println() // Add spacing between turns |
| 145 | + } |
| 146 | + |
| 147 | + if err := scanner.Err(); err != nil { |
| 148 | + return fmt.Errorf("input scanner error: %w", err) |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +// processMessage handles a single message exchange. |
| 155 | +func (c *emailChat) processMessage(ctx context.Context, userMessage string) error { |
| 156 | + message := model.NewUserMessage(userMessage) |
| 157 | + // Run the agent through the runner. |
| 158 | + eventChan, err := c.runner.Run(ctx, c.userID, c.sessionID, message) |
| 159 | + if err != nil { |
| 160 | + return fmt.Errorf("failed to run agent: %w", err) |
| 161 | + } |
| 162 | + // Process streaming response. |
| 163 | + return c.processResponse(eventChan) |
| 164 | +} |
| 165 | + |
| 166 | +// processResponse handles the response with email tool visualization. |
| 167 | +func (c *emailChat) processResponse(eventChan <-chan *event.Event) error { |
| 168 | + fmt.Print("🤖 Assistant: ") |
| 169 | + |
| 170 | + var ( |
| 171 | + fullContent string |
| 172 | + toolCallsDetected bool |
| 173 | + assistantStarted bool |
| 174 | + ) |
| 175 | + |
| 176 | + for event := range eventChan { |
| 177 | + |
| 178 | + // Handle errors. |
| 179 | + if event.Error != nil { |
| 180 | + fmt.Printf("\n❌ Error: %s\n", event.Error.Message) |
| 181 | + continue |
| 182 | + } |
| 183 | + |
| 184 | + // Detect and display tool calls. |
| 185 | + if len(event.Response.Choices) > 0 && len(event.Response.Choices[0].Message.ToolCalls) > 0 { |
| 186 | + toolCallsDetected = true |
| 187 | + if assistantStarted { |
| 188 | + fmt.Printf("\n") |
| 189 | + } |
| 190 | + fmt.Printf("🔍 email initiated:\n") |
| 191 | + for _, toolCall := range event.Response.Choices[0].Message.ToolCalls { |
| 192 | + fmt.Printf(" • %s (ID: %s)\n", toolCall.Function.Name, toolCall.ID) |
| 193 | + if len(toolCall.Function.Arguments) > 0 { |
| 194 | + fmt.Printf(" Query: %s\n", string(toolCall.Function.Arguments)) |
| 195 | + } |
| 196 | + } |
| 197 | + fmt.Printf("\n🔄 send email...\n") |
| 198 | + } |
| 199 | + |
| 200 | + // Detect tool responses. |
| 201 | + if event.Response != nil && len(event.Response.Choices) > 0 { |
| 202 | + hasToolResponse := false |
| 203 | + for _, choice := range event.Response.Choices { |
| 204 | + if choice.Message.Role == model.RoleTool && choice.Message.ToolID != "" { |
| 205 | + fmt.Printf("✅ send email results (ID: %s): %s\n", |
| 206 | + choice.Message.ToolID, |
| 207 | + strings.TrimSpace(choice.Message.Content)) |
| 208 | + hasToolResponse = true |
| 209 | + } |
| 210 | + } |
| 211 | + if hasToolResponse { |
| 212 | + continue |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + // Process content from choices. |
| 217 | + if len(event.Response.Choices) > 0 { |
| 218 | + choice := event.Response.Choices[0] |
| 219 | + |
| 220 | + if !assistantStarted { |
| 221 | + if toolCallsDetected { |
| 222 | + fmt.Printf("\n🤖 Assistant: ") |
| 223 | + } |
| 224 | + assistantStarted = true |
| 225 | + } |
| 226 | + |
| 227 | + // Handle content based on streaming mode. |
| 228 | + var content string |
| 229 | + if c.streaming { |
| 230 | + // Streaming mode: use delta content. |
| 231 | + content = choice.Delta.Content |
| 232 | + } else { |
| 233 | + // Non-streaming mode: use full message content. |
| 234 | + content = choice.Message.Content |
| 235 | + } |
| 236 | + |
| 237 | + if content != "" { |
| 238 | + fmt.Print(content) |
| 239 | + fullContent += content |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + // Check if this is the final event. |
| 244 | + if event.Done { |
| 245 | + fmt.Printf("\n") |
| 246 | + break |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + return nil |
| 251 | +} |
| 252 | + |
| 253 | +// intPtr returns a pointer to the given int. |
| 254 | +func intPtr(i int) *int { |
| 255 | + return &i |
| 256 | +} |
| 257 | + |
| 258 | +// floatPtr returns a pointer to the given float64. |
| 259 | +func floatPtr(f float64) *float64 { |
| 260 | + return &f |
| 261 | +} |
0 commit comments