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)
相关推荐
2601_9679801113 分钟前
Flask Web 应用
python·flask·web开发·环境变量·虚拟环境
Flynn Liang20 分钟前
大模型成本优化:基于 Redis 的可验证语义缓存架构
人工智能·redis·缓存·架构·ai agent·智能体
锋行天下28 分钟前
langgraph全栈项目(一)
前端·人工智能·后端
aifannihao29 分钟前
ß 大写变 SS:用 Python 算清 Ruby2Shell 的缓冲区账本
python·安全·标准库·缓冲区溢出·字符串编码
程序员cxuan37 分钟前
不得了,Google 造反了,Opus 5.5 又被干了
人工智能·后端·程序员
liwulin05061 小时前
【OLLAMA】调用SKILL的通用ReAct
python
数字新视界1 小时前
能耗管理系统提升企业能源效率的实践与展望
大数据·人工智能·数据中心·机房管理·模块化机房
郝学胜-神的一滴1 小时前
Effective Python 条款36:优雅拼装迭代器与生成器
开发语言·数据结构·人工智能·python·numpy·scikit-learn
Frag0ut2 小时前
Chrome 2026浏览器AI助手全攻略:智能问答与内容创作实战进阶指南
人工智能·chrome·ai助手·本地大模型·浏览器ai·网页智能总结·侧边栏ai