Vanilla AI Agent with Gemini
This project is a lightweight, multi-hop reasoning AI agent built from scratch using Python and Google's Gemini API. It implements a basic ReAct (Reasoning and Acting) framework, allowing the language model to "think" about a problem, choose an "action" (tool) to use, and "observe" the results before formulating its final answer.
๐ Project Structure
agent.py: The core execution loop of the agent. It manages the prompt instructions, parses the LLM's JSON responses, executes tools, and tracks chat history.
tools.py: Contains the external capabilities the agent can use to gather information:
Wikipedia: Used for finding entity summaries and facts.
DuckDuckGo: A general web search used for recent events or broader context.
gemini_api.py: Custom LangChain-compatible wrappers (NewGeminiLLM and NewGeminiEmbeddings) for the new google-genai SDK.
๐ Prerequisites
Make sure you have Python 3.9+ installed. You will also need the following dependencies:
pip install google-genai langchain-core wikipedia duckduckgo-search
๐ ๏ธ Setup & Installation
Clone the repository (or download the files to your local machine).
Configure your API Key:
You need a Google Gemini API key to run the model. Currently, the key is set in gemini_api.py.
Security Note: It is highly recommended to remove the hardcoded key from gemini_api.py and instead set it in your environment:
export GEMINI_API_KEY="your_actual_api_key_here"
๐ป Usage
Run the agent from the command line using agent.py. You can provide a custom question using the --question flag.
Basic Example:
python agent.py --question "Who was the lead actor in the highest-grossing movie the year the first iPad was released?"
Expected Output:
The agent will print its step-by-step thought process to the console.
Question: Who was the lead actor in the highest-grossing movie the year the first iPad was released?
--- Step 1 ---
Thought: I need to determine the year the first iPad was released...
-> Executing DuckDuckGo with query: 'year the first iPad was released'
-> Observation: Title: iPad (1st generation) - Wikipedia...
--- Step 2 ---
Thought: The first iPad was released in 2010. Now I need to find the highest-grossing movie of 2010.
-> Executing DuckDuckGo with query: 'highest-grossing movie of 2010'
-> Observation: Title: 2010 in film - Wikipedia...
--- Step 3 ---
Thought: The highest-grossing movie of 2010 was 'Toy Story 3'...
Result: The highest-grossing movie of 2010 was Toy Story 3, and the lead actor (voice actor for the main character, Woody) is Tom Hanks.
Once finished, it will output the trace to chat_history.json so you can review the full prompt history and JSON exchanges.
๐ง How it Works (Learning Guide)
If you are learning about AI agents, this project is a perfect bare-bones example of how they operate under the hood, without relying on heavy agent frameworks.
1. The System Prompt (agent.py)
The agent relies on strict instructions given in get_system_prompt(). It tells the LLM:
- Who it is ("a research agent").
- What tools it has available (dynamically loaded from
TOOLS).
- Exactly how to format its response (a single JSON object with
thought, action, and action_input).
2. The ReAct Loop (run_agent in agent.py)
The application enters a loop (max_steps = 10):
- Generate: It sends the conversation history to Gemini.
- Parse: It cleans the output (stripping markdown like
```json) and parses the string into a Python dictionary.
- Evaluate:
- If the JSON contains a
"final_answer", the loop breaks, and the user gets the answer.
- If the JSON contains an
"action", the code looks up the matching Python function in the TOOLS dictionary.
- Execute & Observe: The python script calls the tool (e.g.,
search_duckduckgo(query)), gets the string result, and appends it to the messages history as an "Observation".
- Repeat: The LLM is called again, now equipped with the new information!
3. Custom Wrappers (gemini_api.py)
This project features custom integrations with LangChain's Base classes (LLM and Embeddings) to wrap the newest google-genai SDK. This allows the lightweight agent to easily hook into the LangChain ecosystem in the future if desired.