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)
相关推荐
Raas10013 小时前
MAI Gateway(魔芋企业级AI网关)功能全解:AI网关支持本地模型吗?一文看懂AI网关能力矩阵
大数据·人工智能·网关·ai网关·mai gateway·企业级产品
蜗牛互联网13 小时前
AI给面试打分不够,求职者更需要可核对的证据
java·人工智能·后端
不开大的凯207713 小时前
跑分已死,交付为王
人工智能·ai·麦当秀aippt·ai office
databook13 小时前
泊松分布:从基础到实际应用
python·数据挖掘·数据分析
AI多Agent协作实战派14 小时前
【AI探索历程19】飞牛版:给NAS用户的一键部署
人工智能
外收内放14 小时前
Python与AI应用(文件操作)
python·学习
爱签AI电子合同14 小时前
电子合同服务稳定性怎么测?可用性保障维度专项测评
大数据·人工智能·电子合同·电子签名
IC一站式服务14 小时前
AI时代数据中心互联:PCIe重定时器、重驱动器与交换机全解析
人工智能
正经教主14 小时前
【FDE系列】阶段2:Day 35:Python + SQL — 工单接入 MySQL + 本周收官
人工智能·python·fde
Xiu Yan14 小时前
Python 数据分析:Pandas Series 零基础入门教程
python·jupyter·pycharm·numpy·pandas