This example demonstrates how to create an intelligent assistant that integrates multiple tools, including calculator, time tool, text processing tool, file operation tool, and web search tool.
- Basic Operations: Addition, subtraction, multiplication, division
- Scientific Functions: sqrt(square root), sin(sine), cos(cosine), abs(absolute value)
- Mathematical Constants: pi(pi), e(natural logarithm base)
- Examples:
Calculate 123 + 456 * 789Calculate sqrt(16)Calculate sin(30*pi/180)
- Current Time: Get current date and time
- Date Information: Get current date
- Day Information: Get current day of the week
- Timestamp: Get Unix timestamp
- Examples:
What time is it now?What day of the week is today?Get current timestamp
- Case Conversion: Convert to uppercase or lowercase
- Character Statistics: Count text length and word count
- Text Reversal: Reverse text content
- Examples:
Convert 'Hello World' to uppercaseCount characters in 'Hello World'Reverse text 'Hello World'
- Read Files: Read file contents
- Write Files: Create or write files
- List Directory: View directory contents
- Check Existence: Check if file exists
- Examples:
Read README.md fileCreate a test file in current directoryList all files in current directory
- Entity Search: Search for information about people, companies, locations, etc.
- Definition Queries: Find definitions of concepts and terms
- Historical Information: Find historical facts and data
- Examples:
Search for information about Steve JobsFind information about Tesla companyWhat is photosynthesis?
Make sure you have set up the relevant environment variables and dependencies:
# Set environment variables like API keys
export OPENAI_API_KEY="your-api-key"cd trpc.group/trpc-go/trpc-agent-go/examples/multi_tools
go run main.gogo run main.go -model="gpt-4"After the program starts, you can:
- Enter various questions and requests
- Observe how the assistant selects and uses different tools
- Enter
exitto quit the program
🚀 Multi-Tool Intelligent Assistant Demo
Model: deepseek-chat
Enter 'exit' to end the conversation
Available tools: calculator, time_tool, text_tool, file_tool, duckduckgo_search
============================================================
✅ Multi-tool intelligent assistant is ready! Session ID: multi-tool-session-1703123456
💡 Try asking these questions:
[Calculator] Calculate 123 + 456 * 789
[Calculator] Calculate the square root of pi
[Time] What time is it now?
[Time] What day of the week is today?
[Text] Convert 'Hello World' to uppercase
[Text] Count characters in 'Hello World'
[File] Read the README.md file
[File] Create a test file in the current directory
[Search] Search for information about Steve Jobs
[Search] Find information about Tesla company
👤 User: Calculate 100 + 200 * 3
🔧 Tool calls:
🧮 calculator (ID: call_abc123)
Arguments: {"expression":"100 + 200 * 3"}
⚡ Executing...
✅ Tool result (ID: call_abc123): {"expression":"100 + 200 * 3","result":700,"message":"Calculation result: 700"}
🤖 Assistant: According to mathematical operation rules, multiplication is performed first, then addition:
100 + 200 * 3 = 100 + 600 = 700
The calculation result is 700.
- File operation tools are restricted to current directory and subdirectories
- Prevent path traversal attacks
- Limit file reading content length
- English interface and prompts
- Clear tool call visualization
- Rich usage examples and help information
- Modular tool design
- Unified tool interface
- Easy to add new tools
To add new tools, follow these steps:
- Define Request and Response Structures
type myToolRequest struct {
Input string `json:"input" jsonschema:"description=Input description"`
}
type myToolResponse struct {
Output string `json:"output"`
Status string `json:"status"`
}- Implement Tool Function
func myToolHandler(req myToolRequest) myToolResponse {
// Implement tool logic
return myToolResponse{
Output: "Processing result",
Status: "Success",
}
}- Create Tool Instance
func createMyTool() tool.CallableTool {
return function.NewFunctionTool(
myToolHandler,
function.WithName("my_tool"),
function.WithDescription("Tool description"),
)
}- Register Tool
tools := []tool.Tool{
createMyTool(),
// Other tools...
}- API Limitations: Some tools may require network access or API keys
- Performance Considerations: Large file operations may affect performance
- Error Handling: Tool call failures will return error messages
- Security: File operations are protected by path restrictions
- Framework: trpc-agent-go
- Models: Supports various OpenAI-compatible models
- Tool System: Function calling-based tool invocation
- Streaming: Supports streaming responses and real-time interaction
This example follows the project's license terms.