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)
相关推荐
北巷`3 分钟前
CC Workflow Studio 解析与落地方案
人工智能·团队开发
十铭忘4 分钟前
连续扩散语言模型
人工智能
AI算法沐枫6 分钟前
深度学习python代码处理科研测序数据
数据结构·人工智能·python·深度学习·决策树·机器学习·线性回归
迁移科技9 分钟前
告别人工分拣!迁移科技 AI+3D 视觉让机器人 “看懂” 无序抓取
人工智能·科技·3d·机器人·自动化·视觉检测
IT_陈寒26 分钟前
Redis缓存击穿把我整不会了,原来还有这手操作
前端·人工智能·后端
YuanDaima204831 分钟前
Linux 进阶运维与 AI 环境实战:进程管理、网络排错与 GPU 监控
linux·运维·服务器·网络·人工智能
跨境数据猎手39 分钟前
跨境商城反向海淘系统开发全流程逻辑(上)
人工智能·爬虫·系统架构
听你说321 小时前
丈八科技与浪潮海若达成战略合作:共建人工智能产测一体化超级工厂
人工智能·科技
初心未改HD1 小时前
深度学习之Attention注意力机制详解
人工智能·深度学习
X1A0RAN1 小时前
解决Pycharm中部分文件或文件夹被隐藏不展示问题
ide·python·pycharm