Introduction
In the rapidly evolving world of generative AI, creating intelligent agents has become more accessible than ever. The Phidata framework offers a streamlined approach to building AI agents, making it an excellent choice for beginners and experienced developers alike. In this tutorial, we'll walk through the process of creating your first basic agent using Phidata.
Prerequisites
Before we dive in, make sure you have:
- Python 3.7 or later installed
- An OpenAI API key
- Basic familiarity with Python programming
Setting Up Your Environment
First, let's set up our project environment:
-
Create a new directory for your project:
mkdir my_first_agent cd my_first_agent
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate
On Windows, use venv\Scripts\activate
3. Install Phidata:
```bash
pip install phidata
Creating Your First Agent
Now that we have our environment set up, let's create our first agent:
- Create a new Python file named
my_agent.py
:from phi.llm.openai import OpenAIChat from phi.agent import Agent
Initialize the OpenAI chat model
llm = OpenAIChat(model="gpt-3.5-turbo")
Define our agent
my_agent = Agent( name="MyFirstAgent", description="A helpful assistant that can answer questions and provide information.", llm=llm, tools=[],
We'll leave this empty for now
)
Run the agent
my_agent.run("Hello! Can you tell me what day it is?")
2. Set your OpenAI API key as an environment variable:
```bash
export OPENAI_API_KEY=your_api_key_here
- Run your agent:
python my_agent.py
Congratulations! You've just created and run your first AI agent using Phidata.
Understanding the Code
Let's break down what's happening in our my_agent.py
file:
- We import the necessary components from Phidata.
- We initialize an
OpenAIChat
instance, which will be our language model. - We create an
Agent
instance, giving it a name, description, and the language model to use. - We call the
run
method on our agent with a simple question.
Enhancing Your Agent
Now that we have a basic agent, let's add some capabilities:
-
Add a tool to get the current date:
from datetime import datetime def get_current_date(): return datetime.now().strftime("%Y-%m-%d") my_agent = Agent( name="MyFirstAgent", description="A helpful assistant that can answer questions and provide the current date.", llm=llm, tools=[get_current_date], )
-
Run the agent with a more specific question:
my_agent.run("What is today's date?")
Now your agent can provide the current date when asked!
Experimenting Further
Here are some ideas to expand your agent's capabilities:
- Add more tools, such as a calculator or a weather API.
- Implement memory to allow your agent to remember previous interactions.
- Create a simple chat interface to interact with your agent.
Conclusion
Building your first AI agent with Phidata is just the beginning. As you become more familiar with the framework, you'll be able to create increasingly complex and capable agents. Remember to always consider ethical implications and potential biases when developing AI systems.
Happy coding, and enjoy your journey into the world of AI agents!