A CLI-based assistant that leverages Groq's LLM capabilities with tool augmentation for web search and calculations.
This project implements an AI assistant using Groq's large language models, enhanced with the ability to perform web searches using Google's Custom Search API and execute mathematical calculations. The system features intelligent query routing, tool detection, and conversation management.
-
Main Application (
main.py
):- Contains the conversation loop and CLI interface
- Implements the core
run_conversation
function for handling interactions - Includes routing logic to determine which tools to use
-
Tool System (
tools/
):tool_manager.py
- Central registry for tools with detection capabilitiesweb_search.py
- Google Search API integrationcalculate.py
- Safe mathematical expression evaluation
-
Utilities (
utils/
):logger.py
- Comprehensive logging system- Various utility functions
- Primary Model:
llama-3.3-70b-versatile
- Used for general responses - Routing Model:
llama-3.1-8b-instant
- Lightweight model for routing decisions
The system uses a smaller, faster model to determine which tools might be required:
- Analyzes user queries to detect calculation or web search needs
- Routes requests to appropriate tool handlers
- Improves both response time and reduces unnecessary API calls
- Route query (determine if tools are needed)
- Make initial API call to Groq with appropriate tools
- If tool call is detected in the response, execute the tool
- Make a second API call with the tool results
- Return the final response to the user
Multiple approaches for detecting tool execution requests:
- Official function calling API through
tool_calls
- Text pattern detection using regex for natural language tool requests
- Forced tool usage when routing suggests tools but model doesn't use them
Comprehensive logging with:
- Session-specific logs with timestamps
- Permanent log file
- Console output for key events
- Detailed recording of all API calls, tool usage, and conversation states
- Python 3.8+
- Groq API key
- Google Search API key and Custom Search Engine ID (for web search functionality)
-
Groq API:
- Used for LLM capabilities
- Supports function calling for tools
-
Google Custom Search API:
- Powers the web search functionality
- Returns structured search results
- Clone the repository
- Install dependencies:
pip install -r requirements.txt
- Create a
.env
file with the following keys:GROQ_API_KEY=your_groq_api_key GOOGLE_SEARCH_API=your_google_search_api_key GOOGLE_CSE_ID=your_google_custom_search_engine_id
- Run the application:
python main.py
The system implements comprehensive error handling:
- Tool execution errors are captured and returned as structured responses
- API call failures are logged and reported to the user
- Invalid tool usage is detected and remediated when possible
- Maintains conversation history for context
- Provides debugging feedback messages during tool execution
- Handles multiple tool calls in a single response
- Adding more tools (e.g., weather, translation)
- Web interface for easier interaction
- Streaming responses for better user experience
- Improved tool detection and execution
- More sophisticated routing logic