recipes / AI Agent with DeepSeek-R1, AutoGen and MAX Serve
Learn How to Build AI Agents with DeepSeek-R1, AutoGen and MAX Serve
Help us improve and tell us what you’d like us to build next.
Request a recipe topicREADME
This recipe demonstrates how to build AI agents using:
DeepSeek-R1-Distill-Qwen-7B
model that runs on GPUWe'll create two example applications that showcase:
The patterns demonstrated here can be adapted for various agent-based applications like:
Please make sure your system meets our system requirements.
To proceed, ensure you have the magic
CLI installed:
curl -ssL https://magic.modular.com/ | bash
or update it via:
magic self-update
Structured output with MAX Serve requires GPU access. For running the app on GPU, ensure your system meets these GPU requirements:
Download the code for this recipe using git:
git clone https://github.com/modular/max-recipes.git
cd max-recipes/deepseek-qwen-autogen-agent
Run the MAX Serve server via in a terminal:
magic run server
In a new terminal, run either example:
For the chat agent:
magic run chat_agent
For the screenplay development team:
magic run screenplay_agents
The agents will be ready when you see the welcome message in your terminal.
Demo shows:
Demo shows:
client = OpenAIChatCompletionClient(
model="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
base_url=LLM_SERVER_URL,
api_key=LLM_API_KEY,
model_info={
"name": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
"family": "deepseek",
"pricing": {"prompt": 0.0, "completion": 0.0},
},
temperature=0.7, # Adjust for more/less creative responses
max_tokens=4096, # Adjust for longer conversation
)
assistant = AssistantAgent(
name="assistant",
model_client=client,
system_message="You are a helpful assistant.",
)
Key features:
max_tokens
for longer conversations# Keep track of conversation history
conversation_history = []
# Add user message to history
conversation_history.append(TextMessage(content=user_input, source="user"))
# Get AI response with full context
response = await assistant.on_messages(
conversation_history, # Pass entire history
CancellationToken(),
)
conversation_history.append(response.chat_message)
Benefits:
The agent separates its thinking from its final response:
content = response.chat_message.content
parts = content.split('')
if len(parts) > 1:
thinking = parts[0].strip()
final_answer = parts[1].strip()
else:
thinking = "No explicit thinking process shown"
final_answer = content.strip()
This allows users to see:
The chat interface uses Rich for enhanced visualization:
console.print(Panel(
Markdown(thinking),
border_style="yellow",
title="[bold]💠Thinking Process[/bold]",
title_align="left"
))
console.print(Panel(
Markdown(final_answer),
border_style="green",
title="[bold]🤖 Response[/bold]",
title_align="left"
))
Features:
The screenplay development system uses multiple specialized agents working together through round-robin conversation. Here's a detailed look at the implementation:
1. Specialized agent definitionsscreenwriter = AssistantAgent(
name="screenwriter",
system_message="""You are an experienced screenwriter who creates engaging movie scenes and dialogue.
First think about the scene carefully. Consider:
- Setting and atmosphere
- Character development
- Plot progression
Then write a brief but vivid scene with clear stage directions and dialogue.""",
model_client=client,
)
story_critic = AssistantAgent(
name="story_critic",
system_message="""You are a story development expert. Review the screenwriter's scene and think about:
- Plot coherence and dramatic tension
- Character motivations and arcs
- Theme development
Then provide:
1. An improved version of the scene
2. A list of specific improvements made and why they work better""",
model_client=client,
)
dialogue_expert = AssistantAgent(
name="dialogue_expert",
system_message="""You are a dialogue specialist. Review both the original and improved scenes, then think about:
- Character voice authenticity
- Subtext and emotional depth
- Natural flow and rhythm
Then provide:
1. A final version with enhanced dialogue
2. A list of specific dialogue improvements made and their impact on the scene""",
model_client=client,
)
Each agent has a specific role:
agent_team = RoundRobinGroupChat(
agents=[screenwriter, story_critic, dialogue_expert],
max_turns=3, # One turn for each agent
speaker_selection_method="round_robin"
)
The round-robin chat ensures:
async for message in stream:
if hasattr(message, 'content') and hasattr(message, 'source'):
content = message.content
source = message.source
# Split thinking and contribution
parts = content.split('')
if len(parts) > 1:
thinking = parts[0].strip()
final_answer = parts[1].strip()
else:
thinking = "No explicit thinking process shown"
final_answer = content.strip()
# Display each agent's contribution
console.print(Panel(
Markdown(thinking),
border_style="yellow",
title=f"[bold]💠{source} Thinking[/bold]",
title_align="left"
))
console.print(Panel(
Markdown(final_answer),
border_style="green",
title=f"[bold]🎬 {source} Contribution[/bold]",
title_align="left"
))
Features:
User: Write a scene about a reunion between old friends
Screenwriter: [Analyzes setting and characters]
Screenwriter: [Creates initial scene with dialogue]
Story Critic: [Evaluates dramatic tension]
Story Critic: [Suggests structural improvements]
Dialogue Expert: [Analyzes character voices]
Dialogue Expert: [Enhances dialogue authenticity]
You can adapt the screenplay system for different creative tasks:
technical_advisor = AssistantAgent(
name="technical_advisor",
system_message="""You ensure accuracy in specialized scenes...""",
model_client=client
)
agent_team = RoundRobinGroupChat(
agents=[...],
max_turns=4, # Add more revision cycles
)
system_message="""You are a genre specialist focusing on:
- Genre conventions
- Typical plot structures
- Character archetypes
..."""
This recipe can be adapted for various applications:
Educational Tutoring
Technical Support
Research Assistant
Common issues and solutions:
Server Connection Issues
magic run server
)8010
is availableAgent Response Issues
Performance Issues
This recipe demonstrates how to:
The patterns shown here provide a foundation for building your own agent-based applications.
Share your agent projects with us using #ModularAI
on social media!
DETAILS
THE CODE
deepseek-qwen-autogen-agent
AUTHOR
Ehsan M. Kermani
AVAILABLE TASKS
magic run server
magic run chat_agent
magic run screenplay_agents
PROBLEMS WITH THE CODE?
File an Issue
TAGS
Help us improve and tell us what you’d like us to build next.
Request a recipe topicENTERPRISES
@ Copyright - Modular Inc - 2025