langchain 0.3.x 版本如何初始化本地模型

langchain 0.3.25

本地模型使用Xinference 搭建,地址为http://127.0.0.1:9997/v1

langchain 0.3.25 创建本地模型

from langchain.chat_models import init_chat_model

model = init_chat_model(model="qwen2.5-instruct", model_provider="openai")

其中"qwen2.5-instruct" 是本地模型的名称

这么创建,没有错,但是在调用的时候

model.invoke([HumanMessage(content="hi!,who are you ?")])

会报 connection error,是本地模型的url 和api_key 没有传进来,还是会调openai公开的接口

查看init_chat_model 源码

在***/site-packages/langchain/chat_models/base_client.py 函数里

def _init_chat_model_helper(
    model: str, *, model_provider: Optional[str] = None, **kwargs: Any
) -> BaseChatModel:
    model, model_provider = _parse_model(model, model_provider)
    if model_provider == "openai":
        _check_pkg("langchain_openai")
        from langchain_openai import ChatOpenAI
        return ChatOpenAI(model=model, **kwargs)

model_provider 为openai ,其底层还是使用的ChatOpenAI,然后kwargs 代表默认的参数,这样的话,我们把其他必要参数再传进来基本就可以了

model = init_chat_model(model="qwen2.5-instruct", model_provider="openai",temperature=0,base_url="http://127.0.0.1:9997/v1", openai_api_key="unknown")

这样再使用model.invoke([HumanMessage(content="hi!,who are you ?")]),就不会报错了

nit_chat_model 提供了多种模型初始化方式,功能比较强大,还在等待进一步学习

参考资料:

init_chat_model — 🦜🔗 LangChain documentation

https://python.langchain.ac.cn/docs/how_to/chat_models_universal_init/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值