以 Siliconflow (硅基流动) 为例(其他云端供应商类似):

from langchain.chat_models import init_chat_model

model = init_chat_model(
    model_provider="openai",
    model="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
    base_url="https://api.siliconflow.cn/v1/",
    api_key="sk-n********************ywocwd",
    temperature=0
)

 以 本地 Ollama 为例:

from langchain.chat_models import init_chat_model

model = init_chat_model(
    model_provider="ollama",
    model="qwen2.5:3b",
    temperature=0
)

 对接MCP服务流式输出

想要什么服务就去这搜:MCP Servers

MCP接口包,装就完了

pip install -U langchain-mcp-adapters

from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent

# 初始化 MCP 客户端
async with MultiServerMCPClient(
    {
         "tavily-mcp": {
            "command": "npx",
            "args": [
                "-y",
                "tavily-mcp"
            ],
            "env": {
                "TAVILY_API_KEY": "tvly-dev-a**********xAQ"
            },
            "disabled": False,
            "autoApprove": []
        }
    }
) as client:

    # 初始化 Agent
    agent = create_react_agent(
    model=model,
    tools=client.get_tools(),
    )

    # 调用 Agent
    stream = agent.astream({"messages": [
        {"role": "system", "content": "You are a helpful assistant.Please use your tools solving the problem."},
        {"role": "user", "content": "帮我查一下广州的天气?"}
    ]})
    
    # 流式输出结果
    async for chunk in stream:
        print(chunk)
Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐