README
ΒΆ
Server Agent in CLI Mode with StreamCompletion
This example demonstrates how to use a Server Agent in CLI mode using the StreamCompletion method, which provides the same functionality as the Crew Agent's streaming method.
Key Features
- Dual Mode: The same agent can be used in both CLI and HTTP server modes
- StreamCompletion: Uses the crew-style
StreamCompletionmethod for CLI interactions - Tool Execution: Supports tool calls with automatic confirmation in CLI mode
- Streaming: Real-time streaming responses
- Consistent API: Same interface as Crew Agent for easy code migration
What's New
The Server Agent now has a StreamCompletion method that mirrors the Crew Agent's functionality:
agent.StreamCompletion(question, func(chunk string, finishReason string) error {
if chunk != "" {
fmt.Print(chunk)
}
return nil
})
This method handles:
- Context compression (if configured)
- Tool call detection and execution
- RAG context integration (if configured)
- Streaming response generation
CLI vs Server Mode
CLI Mode (this example)
- Uses
StreamCompletionmethod - Interactive console-based interface
- Auto-confirms tool execution by default
- Can set custom confirmation prompts with
SetConfirmationPromptFunction
Server Mode (sample 49-server-agent-stream)
- Uses
StartServermethod - HTTP API with SSE streaming
- Web-based confirmation via
/operation/validateendpoint - Separate endpoints for all operations
Usage
Prerequisites
Make sure you have a model server running:
# Using Docker Model Runner or Ollama
docker model pull ai/qwen2.5:1.5B-F16
# or
docker model pull hf.co/menlo/jan-nano-gguf:q4_k_m
Run the Example
cd samples/72-server-agent-cli-stream
go mod tidy
go run main.go
Try These Commands
π§ You: Hello! Say hello to Alice
π€ Bob: [executes say_hello tool and responds]
π§ You: What is 42 plus 58?
π€ Bob: [executes calculate_sum tool and responds]
π§ You: What time is it?
π€ Bob: [executes get_current_time tool and responds]
Customization
Custom Confirmation Prompt
By default, tool calls are auto-confirmed in CLI mode. To add manual confirmation:
agent.SetConfirmationPromptFunction(func(functionName string, arguments string) tools.ConfirmationResponse {
fmt.Printf("\nβ οΈ Execute %s? (y/n): ", functionName)
response, _ := input.ReadInput()
if response == "y" {
return tools.Confirmed
}
return tools.Denied
})
Add RAG Support
ragAgent, _ := rag.NewAgent(ctx, ragConfig, modelConfig, embeddingConfig)
agent.SetRagAgent(ragAgent)
agent.SetSimilarityLimit(0.7)
agent.SetMaxSimilarities(3)
Add Context Compression
compressorAgent, _ := compressor.NewAgent(ctx, compressorConfig, modelConfig)
agent.SetCompressorAgent(compressorAgent)
agent.SetContextSizeLimit(8000)
Benefits of Using Server Agent in CLI Mode
- Code Reusability: Same agent code works in both CLI and HTTP modes
- Testing: Test your server agent logic in CLI before deploying as HTTP service
- Consistency: Same behavior across crew agents and server agents
- Flexibility: Easy to switch between interactive and server modes
Related Examples
- 49-server-agent-stream: Server agent in HTTP mode
- 50-server-agent-with-tools: Server agent with tools (HTTP mode)
- 54-server-agent-tools-rag-compress: Full-featured server agent (HTTP mode)
- 55-crew-agent: Crew agent with StreamCompletion
- 69-simple-crew-agent: Simple crew agent example
Architecture
ServerAgent
βββ CLI Mode (this example)
β βββ StreamCompletion()
β βββ Compress context if needed
β βββ Detect and execute tool calls
β βββ Add RAG context
β βββ Stream response
β
βββ HTTP Mode (sample 49)
βββ StartServer()
βββ handleCompletion()
βββ Compress context if needed
βββ Detect and execute tool calls
βββ Add RAG context
βββ Stream response via SSE
Both modes share the same underlying logic but differ in:
- Input/output handling
- Tool confirmation mechanism
- Response streaming method
Documentation
ΒΆ
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.