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)
相关推荐
乌恩大侠1 分钟前
「AI-RAN」Aerial,使用ORAN C1模式
人工智能·5g·spark·o-ru·ai-ran
Zhao-Jichao5 分钟前
AI Prompting for Everyone 课程笔记 - 05 使用深度搜索 Using Deep Research
人工智能·笔记
feasibility.16 分钟前
世界模型WSM技术解读:SLAM内化为具身智能的状态记忆,纯RGB机器人导航反超RGB-D
人工智能·机器人·aigc·slam·导航·具身智能·世界模型
起个名字费劲死了18 分钟前
Halcon学习一:骰子识别
前端·python·学习
alonglong35 分钟前
本地模型部署避坑:HF_HOME 缓存指向 + macOS fd 上限 256
人工智能
注塑机数据采集联网44 分钟前
数据采集网关入门:ZK-D1/D3从零开始构建你的数据运用
大数据·数据仓库·人工智能·物联网·数据库架构·注塑机网关
hantmac1 小时前
不记得文件名时,Mac 上的搜索还能怎么做?
人工智能·macos
郝学胜-神的一滴1 小时前
游戏设计:原型与开发 18:Unity入门的第一课
人工智能·程序人生·游戏·ui·unity·游戏引擎
天树星智源1 小时前
每个 token 各配其师:token 级多教师蒸馏拉高 5.9 分
人工智能
米饭是菜qy1 小时前
深度学习Transformer详细介绍
人工智能·深度学习·transformer