Overview
Qwen models support ReAct (Reasoning and Acting) prompting, enabling them to use external tools through a thought-action-observation loop. This allows the model to break down complex tasks, call appropriate tools, and reason about the results.ReAct Prompting Pattern
ReAct prompting follows this iterative pattern:Setting Up ReAct Prompting
Define Tool Descriptions
Example Tool Definitions
Complete Implementation
Text Completion with Stop Words
Configure stop words to halt generation at “Observation:“:Implementing Tool Execution
Complete Example
Expected Output
Configuration Tips
Important Configuration Notes:
- Stop Words: Use
stop_words_idsparameter to set “Observation:” as a stop word - Top-p Sampling: Lower top_p (e.g., 0.5) improves accuracy but reduces diversity
- Greedy Decoding: Set
model.generation_config.do_sample = Falsefor deterministic outputs - JSON Parsing: Use
json5.loads()instead ofjson.loads()for more flexible parsing
Adjusting Generation Parameters
Integration Patterns
With LangChain
Multi-Turn Conversations
The implementation supports multi-turn conversations with context preservation:Best Practices
Clear Tool Descriptions
Provide detailed descriptions to help the model choose the right tool
Robust Parsing
Use json5 for parsing and handle malformed JSON gracefully
Error Handling
Implement proper error handling in tool execution
Stop Words
Configure stop words properly to control generation
Next Steps
Function Calling
Learn about OpenAI-style function calling
Building Agents
Create intelligent agents with Qwen