Files
b9e3722ad2 cleanup: refresh SKU links + strip upstream brands (#1)
Rewrite dead HF links to live successors, refresh stale Zen4 / old Zen-3 chat lineups to current Zen5 canonical + Zen3 specialty, and strip upstream brand mentions from user-facing prose (attribution and base_model chains preserved).

Co-authored-by: zooqueen <[email protected]>
2026-09-11 13:49:59 -07:00

4.4 KiB
Raw Permalink Blame History

LLM Introduction

This document introduces the usage and development process of LLM classes.

1. LLM Usage

Currently, -Agent provides access interfaces to 's DashScope API and OpenAI API, and -VL's DashScope API. Both already support streaming Function Calling.

1.1. Direct External Call

The LLM is uniformly calling using the interface get_chat_model(cfg: Optional[Dict] = None) -> BaseChatModel, with parameters passed in being the configuration file for the LLM. The configuration file format is as follows:

  • model_type: Corresponds to a specific LLM class, which is the registered name of the LLM class, i.e., the unique ID. This parameter can be omitted when using the built-in DashScope and OpenAI API. For externally registered LLM classes, this parameter must be provided to specify the class.
  • model: The specific model name
  • model_server: The model service address
  • generate_cfg: The parameters for model generation

LLM classes uniformly use the llm.chat(...) interface to generate responses, supporting input of message lists, functions, and other parameters. For a detailed parameter list, see BaseChatModel.

See a complete example of LLM usage at Function Calling.


from qwen_agent.llm import get_chat_model

llm_cfg = {
            # Use the model service provided by DashScope:
            # 'model_type': 'qwen_dashscope',
            'model': 'qwen-max',
            'model_server': 'dashscope',
            # Use your own model service compatible with OpenAI API:
            # 'model': 'Qwen',
            # 'model_server': 'http://127.0.0.1:7905/v1',
            # (Optional) LLM hyper-parameters:
            'generate_cfg': {
                'top_p': 0.8
            }
          }
llm = get_chat_model(llm_cfg)
messages = [{
    'role': 'user',
    'content': "What's the weather like in San Francisco?"
}]
functions = [{
    'name': 'get_current_weather',
    'description': 'Get the current weather in a given location',
    'parameters': {
        'type': 'object',
        'properties': {
            'location': {
                'type': 'string',
                'description':
                'The city and state, e.g. San Francisco, CA',
            },
            'unit': {
                'type': 'string',
                'enum': ['celsius', 'fahrenheit']
            },
        },
        'required': ['location'],
    },
}]

# The streaming output responses
responses = []
for responses in llm.chat(messages=messages,
                          functions=functions,
                          stream=True):
    print(responses)

1.2. Internal call by Agent

In the Agent, the _call_llm(...) function is used to call the LLM, and each Agent instance can call the LLM that was initialized for it,llm: Optional[Union[Dict, BaseChatModel]] = None. The supported types include: LLM configuration file or LLM object.

Note that in order to maintain the consistency of output type in the Agent, the Agent’s _call_llm(...) interface by default accesses the LLM using a streaming generation method.

2. LLM Development

-Agent provides a mechanism for registering LLMs. In the LLM Base Class, a uniform llm.chat(...) interface is implemented. Newly registered LLMs only need to implement three specific functions:

  • A non-streaming generation interface;
  • A streaming generation interface (If the LLM itself does not support streaming generation, the non-streaming results can be wrapped into a generator for return);
  • A function call interface.

If the newly registered LLM does not support function calls, it can inherit from the BaseFnCallModel class implemented in -Agent. This class has implemented Function Calling based on the general conversational interface by wrapping a tool call Prompt similar to ReAct.