Basic Agentic AI Example
Learn how to implement a basic agentic AI system using Galileo and OpenAI.
When implementing agentic AI systems, it’s crucial to properly handle tool definitions, function calling, and response processing. This guide demonstrates a basic agentic AI implementation using Galileo’s observability features.
What You’ll Need
- OpenAI API key
- Galileo API key
- Python environment with required packages
- Basic understanding of agentic AI concepts
Setup Instructions
Set Up Your Environment
Create a .env
file with your API keys:
Install Dependencies
Install required dependencies:
Create Tool Definitions
Create a tools.json
file with tool definitions:
Running and Monitoring
Execute the application:
Use Galileo to monitor:
- Tool usage patterns
- Query processing performance
- Error rates and types
- System performance metrics
Implementation Guide
Let’s break down the implementation into manageable sections:
1. Setting Up the Environment
First, we’ll set up our imports and initialize our environment:
This section:
- Imports necessary libraries including Galileo components
- Loads environment variables
- Sets up rich console output
- Initializes the OpenAI client with Galileo integration
2. Defining Pydantic Models for Structured Output
Key points:
- Uses Pydantic for data validation
- Defines clear models for structured outputs
- Loads tool definitions from external JSON file
3. Implementing Agent Tools
The agent has two main tools, each decorated with Galileo’s logging:
This section:
- Uses
@log
decorator with thetool
span type for Galileo observability - Implements text-to-number conversion using LLM
- Implements a calculator for arithmetic operations
- Includes robust error handling and fallback mechanisms
4. Query Processing Logic
The core agent functionality:
This section:
- Loads tool definitions
- Constructs a clear system prompt with instructions
- Provides examples for the LLM to follow
- Makes the initial API call with tools
5. Processing Tool Calls
The agent processes tool calls and manages the conversation flow:
This section:
- Processes tool calls from the LLM
- Maintains conversation history
- Executes the appropriate tool functions
- Tracks the state of the conversation
6. Handling Incomplete Sequences
The agent ensures that calculations are completed:
This section:
- Detects incomplete calculation sequences
- Prompts the LLM to complete the calculation
- Processes additional tool calls
- Handles error cases
7. Main Application Loop
The interactive interface:
This section provides:
- User-friendly terminal interface
- Galileo context for request tracking
- Interactive question-answer loop
- Error handling and graceful exits
Key Features
- Tool-Based Architecture: Modular design with specialized tools
- Galileo Observability: Track tool usage and performance
- Robust Error Handling: Graceful handling of API and runtime errors
- Conversation Management: Proper tracking of conversation state
- Interactive Experience: User-friendly terminal interface
Next Steps
- Add more sophisticated tools for complex operations
- Implement memory for multi-turn conversations
- Add evaluation metrics for agent performance
- Integrate advanced Galileo logging features
- Implement parallel tool execution for efficiency