Skip to main content

Overview

Qwen models excel at agent-based tasks, combining reasoning, planning, and tool use to solve complex problems. This guide covers building agents with Qwen using popular frameworks and patterns.

Agent Architectures

Qwen supports multiple agent architectures:
  • ReAct Agents: Reasoning and Acting in an iterative loop
  • HuggingFace Agents: Integration with HuggingFace’s agent framework
  • LangChain Agents: Using LangChain’s agent abstractions
  • Custom Agents: Building specialized agents for specific domains

Building a HuggingFace Agent

Overview

HuggingFace Agents allow you to use Qwen as a controller that can call various ML models from the HuggingFace Hub using natural language. Two Modes:
  • run: Single-turn, no context, excellent at multi-tool composition
  • chat: Multi-turn with context, better for iterative refinement

Installation

Creating a QwenAgent Class

Using the Agent

Available HuggingFace Agent Tools

The HuggingFace Agent framework provides 14 default tools:
  • Document Question Answering: Answer questions about PDF documents (Donut)
  • Unconditional Image Captioning: Generate captions for images (BLIP)
  • Image Question Answering: Answer questions about images (VILT)
  • Image Segmentation: Segment images based on prompts (CLIPSeg)
  • Text to Image: Generate images from text (Stable Diffusion)
  • Image Transformation: Transform and edit images
  • Text Question Answering: Answer questions from long texts (Flan-T5)
  • Zero-shot Text Classification: Classify text into categories (BART)
  • Text Summarization: Summarize long documents (BART)
  • Translation: Translate text between languages (NLLB)
  • Text Downloader: Download text from URLs
  • Speech to Text: Transcribe audio to text (Whisper)
  • Text to Speech: Convert text to speech (SpeechT5)
  • Text to Video: Generate short videos from text (damo-vilab)

Remote vs Local Execution

Remote execution uses HuggingFace’s hosted inference API. Local execution downloads model checkpoints automatically but requires more computational resources.

Building ReAct Agents

ReAct (Reasoning and Acting) agents follow a thought-action-observation loop:

LangChain Integration

Integrate Qwen with LangChain’s agent framework:

Custom Agent Patterns

Task Decomposition Agent

Memory-Augmented Agent

Best Practices for Agent Development

Clear Instructions

Provide clear, structured prompts that guide the agent’s reasoning process

Error Recovery

Implement fallback strategies when tool calls fail or return unexpected results

Context Management

Carefully manage conversation context to avoid exceeding token limits

Tool Selection

Design tool descriptions that help the agent choose the right tool for each task

Agent Performance Tips

Optimization Strategies:
  • Use Greedy Decoding: Set do_sample=False for more consistent agent behavior
  • Limit Iterations: Set max iterations to prevent infinite loops
  • Validate Outputs: Always validate tool outputs before passing to next step
  • Chinese vs English: Current Qwen models perform better with Chinese prompts for agent tasks

Example: Complete Multi-Tool Agent

Next Steps

Function Calling

Deep dive into function calling APIs

Tool Use

Master ReAct prompting patterns

System Prompts

Customize agent behavior with system prompts