Learn Pydantic AI agents, step by step,
A step-by-step guide to building intelligent AI agents using Pydantic AI and local models and (Ollama or any OAI compatible). This tutorial demonstrates how to create structured, vision-capable, and tool-equipped AI agents.
- 📝 Structured output
- 🔧 Custom tool integration
- 👁️ Using Vision models (llama3.2 vision / minicpm-v)
- 🤝 Simple Multi-agent systems
- 📁 File handling
- 🔄 Dynamic prompting
- Clone the repository
- Create conda evironment
conda create -n pydantic python=3.12 pip -y
- Install dependencies:
pip install pydantic-ai llm-sandbox
- Set up Ollama and required models (chosee any of those model)
# Best agents model for local run
ollama pull llama3.1:8b-instruct-q8_0
ollama pull qwen2.5:14b
ollama pull qwen2.5:32b
# vision models
ollama pull llama3.2-vision:latest
ollama pull minicpm-v
If you prefer faster local inference you can use vLLM, here is 2 examples for preparing vLLM
# create vllm conda environment
conda create -n vllm python=3.12 pip -y
conda activate vllm
pip install vllm
# run vllm with LLama3.1 8B with flags that enables tool calling
# assuming you downloaded the GPTQ-Q8 version
vllm serve Meta-Llama-3.1-8B-Instruct-GPTQ-Q_8 \
--port 5003 \
--enforce-eager \
--kv-cache-dtype fp8 \
--enable-chunked-prefill \
--enable-auto-tool-choice \
--tool-call-parser llama3_json \
--chat-template ./tool_chat_template_llama3.1_json.jinja
you can download the jinja template from vllm github repo vLLM repo vllm/examples/tool_chat_template_llama3.1_json.jinja
based on offical documentation Qwen2.5 uses hermes tool calling and hermes jinja template Qwen2.5 function_call
# Running Qwen2.5-32B with dual RTX cards.
vllm serve Qwen2.5-32B-Instruct-AWQ --port 5003 \
--tensor-parallel-size 2 \
--enforce-eager \
--kv-cache-dtype fp8 \
--enable-chunked-prefill \
--enable-auto-tool-choice \
--tool-call-parser hermes \
--chat-template ./tool_chat_template_hermes.jinja
Now all you need is to use OpenAPI model and change the base_url
model = OpenAIModel(
'Qwen2.5-32B-Instruct-AWQ',
base_url='http://localhost:5003/v1',
api_key='vllm',
)
The repository contains progressive examples:
0_hello_world.py
- Basic agent setup1_hello_with_OAI_api.py
- OpenAI compatible API integration2_simple_structured.py
- Structured outputs3_simple_structured_table.py
- Table formatting + Structured outputs
4_lets_make_tools.py
- Custom tool creation5_mix_tools_with_structured_output.py
- Combining tools and structure6_code_as_tool.py
- Code execution capabilities7_code_with_added_libs.py
- External library integration
8_lets_make_dynamic_prompt.py
- Dynamic prompt generation9_mix_multiple_prompts.py
- Multiple prompt handling10_lets_open_files.py
- File operations11_hello_vision.py
- Basic vision capabilities12_running_2_mixed_agents_with_vision.py
- Advanced vision and multi-agent systems
Licensed under the Apache License, Version 2.0. See LICENSE file for details.
Contributions welcome! Please feel free to submit a Pull Request.