模型是智能体(Agent)的推理核心。它们驱动智能体的决策流程,决定调用哪些工具、如何解读结果,以及何时提供最终答案。
所选模型的质量与能力,直接影响智能体的可靠性和性能表现。不同模型在不同任务中各有所长:部分模型擅长遵循复杂指令,部分在结构化推理方面表现更优,还有一些支持更大的上下文窗口,可处理更多信息。
LangChain的标准模型接口支持与多个不同提供商的集成,便于用户尝试不同模型并灵活切换,从而找到最符合自身需求的模型。
一、模型的使用
模型可通过以下两种方式使用:
- 与智能体(Agent)结合:创建智能体时,可动态指定要使用的模型。
 - 独立使用:可直接调用模型(在智能体循环之外),用于执行文本生成、分类或信息提取等任务,无需依赖智能体框架。
 
同一模型接口在两种场景下均适用,这使得你能够灵活地从简单用法入手,根据需求逐步扩展到更复杂的、基于智能体的工作流。
1、在智能体中使用
大模型在智能体中的使用方法,我们在上一期介绍create_agent接口时已经熟悉。这里不再重复。
2、模型独立使用
前提条件:使用某个模型提供商的模型必须安装对应的langchain集成包。例如,使用"openai:o1",必须安装langchain-openai包。
2.1 init_chat_model
在 LangChain 中使用独立模型入门的最简单方法,是通过 init_chat_model 函数从你选择的对话模型提供商处初始化一个模型(示例如下):
            
            
              python
              
              
            
          
          import os
from langchain.chat_models import init_chat_model
os.environ["OPENAI_API_KEY"] = "sk-..."
model = init_chat_model("gpt-4.1")
response = model.invoke("亚洲总共有多少个国家?")
        通用参数说明:
            
            
              text
              
              
            
          
          model: str,
model_provider: str | None = None,
configurable_fields: None = None,
config_prefix: str | None = None,
api_key:用于向模型提供商验证身份的必需密钥。
temperature: 用于控制随机性的模型温度.
max_tokens: 最大输出令牌数.
timeout: 等待响应的最长时间(以秒为单位).
max_retries: 失败请求的最大重试次.
base_url: 自定义API端点URL.
rate_limiter: 一个 [BaseRateLimiter][langchain_core.rate_limiters.BaseRateLimiter] 实例,用于控制请求速率.
        每种对话模型集成可能包含额外参数,这些参数用于控制特定提供商的专属功能。例如,ChatOpenAI 模型有 use_responses_api 参数,该参数用于指定是否使用 OpenAI 的 Responses API(响应型 API)或 Completions API(补全型 API)。
2.2 Chat Model Class
使用langchain模型集成包提供的对话模型类实例化一个模型。
            
            
              python
              
              
            
          
          import os
from langchain_openai import ChatOpenAI
os.environ["OPENAI_API_KEY"] = "sk-..."
model = ChatOpenAI(model="gpt-4.1")
        二、模型调用
调用模型最直接的方法是使用 invoke() 函数,并传入单条消息或消息列表。
1、传入单条消息
            
            
              python
              
              
            
          
          response = model.invoke("Why do parrots have colorful feathers?")
print(response)
        2、传入消息列表
可向模型提供消息列表以表示对话历史。每条消息都包含一个角色(role),模型通过该角色标识对话中消息的发送方。
2.1 传入字典列表
            
            
              python
              
              
            
          
          conversation = [
    {"role": "system", "content": "You are a helpful assistant that translates English to French."},
    {"role": "user", "content": "Translate: I love programming."},
    {"role": "assistant", "content": "J'adore la programmation."},
    {"role": "user", "content": "Translate: I love building applications."}
]
response = model.invoke(conversation)
print(response)  # AIMessage("J'adore créer des applications.")
        2.2 传入message对象列表
            
            
              python
              
              
            
          
          from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
conversation = [
    SystemMessage("You are a helpful assistant that translates English to French."),
    HumanMessage("Translate: I love programming."),
    AIMessage("J'adore la programmation."),
    HumanMessage("Translate: I love building applications.")
]
response = model.invoke(conversation)
print(response)  # AIMessage("J'adore créer des applications.")
        三、流式输出(Stream)
大多数模型在生成输出内容的过程中,都能以流式方式返回内容。通过逐步展示输出结果,流式输出可显著提升用户体验,对于较长的响应内容而言尤其明显。
调用 stream() 方法会返回一个迭代器(iterator),该迭代器会在输出块(output chunk)生成时逐个返回这些块。你可以通过循环实时处理每个输出块,示例如下:
            
            
              python
              
              
            
          
          for chunk in model.stream("Why do parrots have colorful feathers?"):
    print(chunk.text, end="|", flush=True)
        不同类型的内容块的流式输出:
            
            
              python
              
              
            
          
          for chunk in model.stream("What color is the sky?"):
    for block in chunk.content_blocks:
        if block["type"] == "reasoning" and (reasoning := block.get("reasoning")):
            print(f"Reasoning: {reasoning}")
        elif block["type"] == "tool_call_chunk":
            print(f"Tool call chunk: {block}")
        elif block["type"] == "text":
            print(block["text"])
        else:
            ...
        四、批量输出(Batch)
将一组独立的请求批量发送给模型,可显著提升性能并降低成本,因为这些请求的处理过程能够以并行方式进行:
            
            
              python
              
              
            
          
          responses = model.batch([
    "Why do parrots have colorful feathers?",
    "How do airplanes fly?",
    "What is quantum computing?"
])
for response in responses:
    print(response)
        默认情况下,batch() 只会返回整个批次的最终输出。如果希望在每个单独输入的输出生成完成时就收到该输出,你可以使用 batch_as_completed() 来流式获取结果:
            
            
              python
              
              
            
          
          for response in model.batch_as_completed([
    "Why do parrots have colorful feathers?",
    "How do airplanes fly?",
    "What is quantum computing?"
]):
    print(response)
        五、总结
本期分享了LangChain1.0中模型的基本使用方法、模型调用方法、模型流式输出和批量输出。 模型及其输入输出系统是LangChain框架中极其重要的组件。它们承担了了智能体推理决策以及和外部通信的功能。LangChain提供的标准模型接口,支持用户在不同厂商模型之间灵活切换而不必修改代码。这为灵活选择模型提供了便利。