跳到主要内容
本笔记本演示了如何在 LangChain 中使用 YUAN2 API,通过 `langchain.chat_models.ChatYuan2` 实现。 Yuan2.0 是中科院计算所(IEIT System)开发的新一代基础大语言模型。我们已经发布了 Yuan 2.0-102B、Yuan 2.0-51B 和 Yuan 2.0-2B 三个模型。我们还为其他开发者提供了预训练、微调和推理服务的相关脚本。Yuan2.0 基于 Yuan1.0,利用更广泛的高质量预训练数据和指令微调数据集,增强了模型对语义、数学、推理、代码、知识等方面的理解。

入门

安装

首先,Yuan2.0 提供了兼容 OpenAI 的 API,我们通过使用 OpenAI 客户端将 ChatYuan2 集成到 LangChain 聊天模型中。因此,请确保您的 Python 环境中已安装 `openai` 包。运行以下命令:
pip install -qU openai

导入所需模块

安装完成后,将必要的模块导入到您的 Python 脚本中:
from langchain_community.chat_models import ChatYuan2
from langchain.messages import AIMessage, HumanMessage, SystemMessage

设置您的 API 服务器

按照 yuan2 openai api server 的说明设置您的 OpenAI 兼容 API 服务器。如果您在本地部署了 API 服务器,您可以简单地将 yuan2_api_key="EMPTY" 或任何您想要的值。只需确保 yuan2_api_base 设置正确即可。
yuan2_api_key = "your_api_key"
yuan2_api_base = "http://127.0.0.1:8001/v1"

初始化 ChatYuan2 模型

以下是如何初始化聊天模型:
chat = ChatYuan2(
    yuan2_api_base="http://127.0.0.1:8001/v1",
    temperature=1.0,
    model_name="yuan2",
    max_retries=3,
    streaming=False,
)

基本用法

像这样使用系统和人类消息调用模型:
messages = [
    SystemMessage(content="你是一个人工智能助手。"),
    HumanMessage(content="你好,你是谁?"),
]
print(chat.invoke(messages))

带流式传输的基本用法

为了持续交互,请使用流式传输功能:
from langchain_core.callbacks import StreamingStdOutCallbackHandler

chat = ChatYuan2(
    yuan2_api_base="http://127.0.0.1:8001/v1",
    temperature=1.0,
    model_name="yuan2",
    max_retries=3,
    streaming=True,
    callbacks=[StreamingStdOutCallbackHandler()],
)
messages = [
    SystemMessage(content="你是个旅游小助手。"),
    HumanMessage(content="给我介绍一下北京有哪些好玩的。"),
]
chat.invoke(messages)

高级功能

带异步调用的用法

像这样进行非阻塞调用以调用模型:
async def basic_agenerate():
    chat = ChatYuan2(
        yuan2_api_base="http://127.0.0.1:8001/v1",
        temperature=1.0,
        model_name="yuan2",
        max_retries=3,
    )
    messages = [
        [
            SystemMessage(content="你是个旅游小助手。"),
            HumanMessage(content="给我介绍一下北京有哪些好玩的。"),
        ]
    ]

    result = await chat.agenerate(messages)
    print(result)
import asyncio

asyncio.run(basic_agenerate())

带提示模板的用法

像这样进行非阻塞调用并使用聊天模板:
async def ainvoke_with_prompt_template():
    from langchain_core.prompts.chat import (
        ChatPromptTemplate,
    )

    chat = ChatYuan2(
        yuan2_api_base="http://127.0.0.1:8001/v1",
        temperature=1.0,
        model_name="yuan2",
        max_retries=3,
    )
    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", "你是一个诗人,擅长写诗。"),
            ("human", "给我写首诗,主题是{theme}。"),
        ]
    )
    chain = prompt | chat
    result = await chain.ainvoke({"theme": "明月"})
    print(f"type(result): {type(result)}; {result}")
asyncio.run(ainvoke_with_prompt_template())

带流式传输的异步调用用法

对于带流式输出的非阻塞调用,请使用 `astream` 方法:
async def basic_astream():
    chat = ChatYuan2(
        yuan2_api_base="http://127.0.0.1:8001/v1",
        temperature=1.0,
        model_name="yuan2",
        max_retries=3,
    )
    messages = [
        SystemMessage(content="你是个旅游小助手。"),
        HumanMessage(content="给我介绍一下北京有哪些好玩的。"),
    ]
    result = chat.astream(messages)
    async for chunk in result:
        print(chunk.content, end="", flush=True)
import asyncio

asyncio.run(basic_astream())

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.