虞城网站建设,外管局网站做延期收款报告,广东建设官方网站,网站开发快递文件目前网上的langchain教程大多数都是关于如何调用OpenAI等远程模型#xff0c;对于本地模型的调用示例写法比较少。而且langchain也在不停迭代#xff0c;文档也比较杂。我个人用Hugging Face的开源模型比较多。因此#xff0c;本文将向大家介绍如何使用Langchain调用Hugging… 目前网上的langchain教程大多数都是关于如何调用OpenAI等远程模型对于本地模型的调用示例写法比较少。而且langchain也在不停迭代文档也比较杂。我个人用Hugging Face的开源模型比较多。因此本文将向大家介绍如何使用Langchain调用Hugging Face本地模型的基础demo帮助大家快速开始langchain的“Hello World”。 相关写法参考的是langchain官方文档https://python.langchain.com/v0.2/docs/integrations/llms/huggingface_pipelines/#model-loading。 【注意】事先安装langchain_huggingface、langchain并下载好模型权重。 写法如下
from langchain_huggingface.llms import HuggingFacePipeline
from langchain_core.prompts import PromptTemplatehf HuggingFacePipeline.from_model_id(model_idrD:\PLMs\qwen\qwen1.5-1.8B-Chat,tasktext-generation,device0,pipeline_kwargs{max_new_tokens: 1000},
)template Question: {question}Answer: 让我们一步步思考。
prompt PromptTemplate.from_template(template)chain prompt | hfquestion C罗是谁print(chain.invoke({question: question}))