langchain1.x学习笔记(三):langchain之init_chat_model的新用法

在langchain1.x中,使用init_chat_model函数进行构建model。

1. 第一种方式:支持硅基流动的调用
python 复制代码
from envs.envs import OPENAI_BASE_URL, OPENAI_BASE_MODEL, OPENAI_API_KEY
from langchain.chat_models import init_chat_model

model = init_chat_model(
    model_provider="openai",
    model=OPENAI_BASE_MODEL,
    api_key=OPENAI_API_KEY,
    base_url=OPENAI_BASE_URL,
    temperature=0,
)

response = model.invoke(input="hello")

print(response)
2. 第二种方式:后配置
python 复制代码
from envs.envs import OPENAI_BASE_URL, OPENAI_BASE_MODEL, OPENAI_API_KEY
from langchain.chat_models import init_chat_model

model = init_chat_model(
    temperature=0,
)

response = model.invoke(
    input="hello",
    config={
        "model_provider": "openai",
        "model": OPENAI_BASE_MODEL,
        "api_key": OPENAI_API_KEY,
        "base_url": OPENAI_BASE_URL,
        "max_tokens": 4096,
    },
)

print(response)
3. 第三种:流式输出
python 复制代码
from envs.envs import OPENAI_BASE_URL, OPENAI_BASE_MODEL, OPENAI_API_KEY
from langchain.chat_models import init_chat_model

model = init_chat_model(
    temperature=0,
)

stream = model.stream(
    input="hello",
    config={
        "model_provider": "openai",
        "model": OPENAI_BASE_MODEL,
        "api_key": OPENAI_API_KEY,
        "base_url": OPENAI_BASE_URL,
        "max_tokens": 4096,
    },
)

for chunk in stream:
    print(chunk.content, end="", flush=True)
4. 第四种:异步流式输出
python 复制代码
from envs.envs import OPENAI_BASE_URL, OPENAI_BASE_MODEL, OPENAI_API_KEY
from langchain.chat_models import init_chat_model
import asyncio


async def async_stream():
    model = init_chat_model(
        temperature=0,
    )

    stream = model.astream(
        input="hello",
        config={
            "model_provider": "openai",
            "model": OPENAI_BASE_MODEL,
            "api_key": OPENAI_API_KEY,
            "base_url": OPENAI_BASE_URL,
            "max_tokens": 4096,
        },
    )

    async for chunk in stream:
        print(chunk.content, end="", flush=True)


if __name__ == "__main__":
    asyncio.run(async_stream())
5. 提示词模板结合管道进行使用
python 复制代码
from envs.envs import OPENAI_BASE_URL, OPENAI_BASE_MODEL, OPENAI_API_KEY
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser


prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant."),
        ("human", "{question}"),
    ]
)

model = init_chat_model(
    model_provider="openai",
    model=OPENAI_BASE_MODEL,
    api_key=OPENAI_API_KEY,
    base_url=OPENAI_BASE_URL,
)


chain = prompt | model | StrOutputParser()


result = chain.invoke({"question": "你是谁?"})

print(result)
相关推荐
TheBestRucy1 分钟前
RAG知识库问答系统落地:从向量检索到上下文增强的全链路实践
人工智能·python·langchain·aigc·交互
TechEdu2026067 分钟前
[人工智能]生成式AI开源生态:库、工具与工作流
人工智能·ai
Hui Baby9 分钟前
大模型微调完整分类
人工智能·机器学习
吐了啊取名字太难9 分钟前
美颜系统AI修图本地跑并支持Mac、win、安卓、iOS不卡顿
android·人工智能·windows·数码相机·mac·ai编程
coder_zrx18 分钟前
大语言模型训练范式:从 GPT 到 Llama 的 RLHF 演进
人工智能·深度学习
AOwhisky19 分钟前
Python 学习笔记(第十四期)——运维自动化(下·中篇):远程文件传输——paramiko进阶篇
运维·python·学习·云原生·自动化·文件传输·paramiko
TsingtaoAI23 分钟前
脑控机器人项目交付|用意念指挥机器人,情绪交互与抓取功能全面实现
人工智能·ai·机器人·具身智能
春日见31 分钟前
算法与数据结构----哈希表
数据结构·人工智能·算法·机器学习·自动驾驶·哈希算法·散列表
不懒不懒31 分钟前
Windows 深度学习环境配置(CUDA12.8 + cuDNN9.x + PyTorch)最简避坑指南(2026 最新)
人工智能·pytorch·深度学习