Skip to main content

Overview

Qwen models support function calling capabilities, allowing the model to intelligently decide when to call external functions and generate appropriate parameters. This enables integration with APIs, databases, and other external services. Qwen supports both Qwen-style and GPT-style function calling formats, providing flexibility for different use cases.

Function Calling Formats

Qwen-Style Functions

Qwen uses a custom function definition format that includes both human-readable and model-readable descriptions:

GPT-Style Functions

Qwen also supports OpenAI-compatible function definitions:

Using Function Calling with OpenAI API

Qwen provides an OpenAI-compatible API server for function calling:

Setting Up the OpenAI API Server

1

Install Dependencies

2

Clone Repository

3

Start API Server

The server will start on http://localhost:8000/v1

Function Calling Workflow

1

Define Functions

Create function definitions with clear descriptions and parameter schemas.
2

Send User Query

Send the user’s message along with available functions to the model.
3

Model Decides

The model decides whether to call a function and generates appropriate arguments.
4

Execute Function

Your application executes the function with the provided arguments.
5

Return Results

Send the function results back to the model for final response generation.

Integration with LangChain

Qwen works seamlessly with LangChain for agent-based function calling:

Best Practices

Important Considerations:
  • Clear Descriptions: Provide detailed function descriptions to help the model understand when to use each function
  • Parameter Validation: Always validate function arguments before execution
  • Error Handling: Implement proper error handling for function calls
  • Chinese vs English: The current version (as of 2023.08) of Qwen-7B-Chat performs better with Chinese tool-use prompts than English ones

Supported Models

Function calling is supported on:
  • Qwen-7B-Chat
  • Qwen-14B-Chat
  • Qwen-72B-Chat
  • Qwen-1.8B-Chat
All models support both Qwen-style and GPT-style function definitions.

Example Use Cases

  • Web Search: Integrate search engines to answer factual questions
  • Image Generation: Create images based on text descriptions
  • Weather APIs: Get real-time weather information
  • Database Queries: Retrieve information from databases
  • Calculator: Perform mathematical calculations
  • Code Execution: Execute code and return results

Next Steps

Tool Use

Learn about ReAct prompting and tool integration

Agent Building

Build intelligent agents with Qwen