DeepSeek R1 Unchained! A New Era of Structured Reasoning with PydanticAI
Improve Agent Performance with DeepSeek R1's Reasoning Model and GPT-4o Functional Agents in a Structured PydanticAI Graph
A small, yet ambitious Chinese startup, DeepSeek, has stunned the AI world with its open-source reasoning model, R1!
Competing head-to-head with industry giants like OpenAI and Google, R1 has emerged as a game-changer, showcasing superior performance across benchmarks. Whether you're building agent-based systems or delving into advanced reasoning workflows, DeepSeek R1 deserves your attention.
In this newsletter, we’ll explore how to set up and use DeepSeek R1, run practical examples, and integrate it with PydanticAI for multi-agent reasoning systems. Let's get started!
Setting Up DeepSeek R1: Step-by-Step Guide
Prerequisites
A machine with at least 16GB of RAM (M1/M2/M3 MacBooks work but performance may vary).
Python installed with access to
pip
.
1. Install Ollama and Required Libraries
DeepSeek R1 uses the Ollama server to run locally. Start by installing Ollama with Homebrew:
brew install ollama
2. Download DeepSeek R1 Models
DeepSeek offers models in various sizes (7B, 14B, etc.). To download the 7B model:
ollama pull deepseek-R1:latest
For the 14B model, replace latest
with 14b
.
3. Verify Installation
Ensure the models are installed correctly by listing them:
ollama list
4. Run DeepSeek R1 Locally
Start the Ollama server with the downloaded model:
ollama run deepseek-r1
Your can now run queries against DeepSeek R1 using this URL http://localhost:11434/v1
.
Running Examples: Product Recommendations and Beyond
Example 1: Product Evaluation
Ask DeepSeek R1 for a product recommendation:
"Should I buy a MacBook Pro 16-inch?"
The model processes pros and cons, delivering a concise verdict with reasoning.
Example 2: Advanced Queries
Transitioning to AI-enabled development? Ask:
"What are 10 strategies for developers to transition into AI roles?"
DeepSeek R1 provides actionable, detailed recommendations.
Advanced Applications: Multi-Agent Reasoning with PydanticAI
DeepSeek R1’s reasoning capabilities shine in complex workflows. Pair it with PydanticAI to build multi-agent systems, enabling structured debates and automated decision-making.
PydanticAI is a framework for creating AI agents. The framework is model-agnostic, meaning you can integrate various models, and it supports tools and structured responses. PydanticAI Graphs provide a sophisticated way to model, execute, and control complex workflows using a finite state machine. This approach allows for the creation of complex agent networks and enables complex logic and abstraction.
Setting Up a Debate Graph
Define Agents:
Pro Agent: Argues for a product.
Con Agent: Argues against the product.
Create a Graph: Use Pydantic AI’s finite-state machines to manage workflows.
Workflow Example
Agents debate product features and consider positive and negative sentiments online
Arguments are passed to a reasoning agent (DeepSeek R1)
Final verdict is formatted by a decision formatting agent
Here’s a simple workflow visualization:
And the Mermaid script:
stateDiagram-v2
[*] --> Moderator
Moderator --> ProAgent
ProAgent --> Moderator
Moderator --> ReasoningAgent
ReasoningAgent --> DecisionFormatter
Moderator --> ConAgent
ConAgent --> Moderator
DecisionFormatter --> [*]
Insights & Recommendations: Unlocking DeepSeek R1’s Potential
Key Takeaways
Speed vs. Size: Smaller models (7B) are faster but less detailed. Larger models (14B+) deliver better reasoning but require more resources.
Limitations: Outdated training data impacts responses for the latest products unless paired with external tools (e.g., web search). Several strategies to overcome those limitations:
Call R1, passing the entire message history and getting the “reasoned unstructured answer”, which is then passed to another gpt-4o formatter agent, creating structured output (Easier to understand)
Define R1 as a tool and ask one of the gpt-4o agents to use the tool (More convoluted)
Integration: Pair DeepSeek R1 with tool-supporting GPT-4o agents and use tools like Tavily Search for richer outputs.
Overcoming DeepSeek R1’s Limitations
The DeepSeek R1 model lacks support for function calling and structured outputs. To overcome this, we can use a strategy of stripping the deliberation from the R1 model's text output and passing the remaining text to a formatting agent to obtain a structured response. There are different ways of limiting tool calls, and using a 'prepare' parameter to determine how and when to call them is one of the most-effective ways.
Recommendations
Start with the 7B model for testing and development.
Leverage tools to supplement outdated training data.
Explore multi-agent workflows for advanced use cases like product research or marketing analysis.
Closing Thoughts: The Age of Reasoning Has Begun
DeepSeek R1 demonstrates the power of open-source AI to disrupt the status quo. By combining robust reasoning with flexible integrations, it offers developers a powerful tool for innovation. Whether you’re experimenting with small-scale projects or architecting complex agentic systems, DeepSeek R1 is worth exploring.
DeepSeek R1 and PydanticAI graphs offer the potential for building powerful, flexible, and highly customizable AI systems. The ability to integrate reasoning models like DeepSeek R1 with a structured, graph-based approach opens up numerous opportunities for developers looking to implement advanced AI solutions. By overcoming the challenges of reasoning models through strategic use of agents, tools, and structured data, one can create applications with remarkable capabilities. The provided code and architectural ideas can serve as a starting point for your next innovative AI project. Dive into the code, experiment with the model and graph, and let us know what you create!
Stay tuned for our next newsletter, where we’ll delve into building complete applications using multi-agent systems with PydanticAI. Until then, happy coding!
References:
GitHub Repo: https://github.com/aidev9/tuts/tree/main/pydanticai-graph-deepseek-r1
YouTube video: