跳到内容

模型

本页介绍如何配置代理使用的聊天模型。

工具调用支持

为了启用工具调用代理,底层 LLM 必须支持工具调用

兼容的模型可以在LangChain 集成目录中找到。

使用 initChatModel

initChatModel 工具简化了带可配置参数的模型初始化

import { initChatModel } from "langchain/chat_models/universal";

const llm = await initChatModel(
  "anthropic:claude-3-7-sonnet-latest",
  {
    temperature: 0,
    maxTokens: 2048
  }
);

有关高级选项,请参阅API 参考

使用特定提供商的 LLM

如果模型提供商无法通过 initChatModel 使用,您可以直接实例化该提供商的模型类。模型必须实现 BaseChatModel 接口并支持工具调用

import { ChatAnthropic } from "@langchain/anthropic";
import { createReactAgent } from "@langchain/langgraph/prebuilt";

const llm = new ChatAnthropic({
  modelName: "claude-3-7-sonnet-latest",
  temperature: 0,
  maxTokens: 2048
});

const agent = createReactAgent({
  llm,
  // other parameters
});

示例

上述示例使用了 ChatAnthropic,它已经通过 initChatModel 支持。此处展示此模式是为了说明如何手动实例化无法通过 initChatModel 使用的模型。

更多资源