梅州建站,cf域名免费注册,浦口区网站建站,扬中网站建设要多少钱一、前言 尽管现在的大语言模型已经非常强大#xff0c;可以解决许多问题#xff0c;但在处理复杂情况时#xff0c;仍然需要进行多个步骤或整合不同的流程才能达到最终的目标。然而#xff0c;现在可以利用langchain来使得模型的应用变得更加直接和简单。 通过langchain框… 一、前言 尽管现在的大语言模型已经非常强大可以解决许多问题但在处理复杂情况时仍然需要进行多个步骤或整合不同的流程才能达到最终的目标。然而现在可以利用langchain来使得模型的应用变得更加直接和简单。 通过langchain框架调用本地模型使得用户可以直接提出问题或发送指令而无需担心具体的步骤或流程。langchain会自动将任务分解为多个子任务并将它们传递给适合的语言模型进行处理。 二、术语
2.1.LangChain 是一个全方位的、基于大语言模型这种预测能力的应用开发工具。LangChain的预构建链功能就像乐高积木一样无论你是新手还是经验丰富的开发者都可以选择适合自己的部分快速构建项目。对于希望进行更深入工作的开发者LangChain 提供的模块化组件则允许你根据自己的需求定制和创建应用中的功能链条。 LangChain本质上就是对各种大模型提供的API的套壳是为了方便我们使用这些 API搭建起来的一些框架、模块和接口。 LangChain的主要特性 1.可以连接多种数据源比如网页链接、本地PDF文件、向量数据库等 2.允许语言模型与其环境交互 3.封装了Model I/O输入/输出、Retrieval检索器、Memory记忆、Agents决策和调度等核心组件 4.可以使用链的方式组装这些组件以便最好地完成特定用例。 5.围绕以上设计原则LangChain解决了现在开发人工智能应用的一些切实痛点。
2.2.Hugging Face 是一个知名的开源社区和平台专注于自然语言处理NLP技术和人工智能模型的开发和共享。该社区致力于提供易于使用的工具和资源帮助研究人员、开发者和数据科学家在NLP领域进行创新和应用。 Hugging Face最著名的贡献是其开源软件库其中包括了许多流行的NLP模型的实现和预训练模型的集合如BERT、GPT、RoBERTa等。这些模型在各种NLP任务如文本分类、命名实体识别、情感分析等方面取得了很好的表现并被广泛应用于学术界和工业界。
2.3.Transformers Hugging Face的Transformer是一个流行的开源Python库用于自然语言处理NLP任务和模型开发。它提供了一系列易于使用的API和工具用于加载、训练和部署各种预训练的NLP模型如BERT、GPT、RoBERTa等。 三、前提条件
3.1.安装虚拟环境
conda create --name langchain python3.10
conda activate langchain
conda install pytorch
pip install langchain accelerate
3.2.下载QWen1.5模型
huggingface
https://huggingface.co/Qwen/Qwen1.5-7B-Chat/tree/main
ModelScope
git clone https://www.modelscope.cn/qwen/Qwen1.5-7B-Chat.git
PS
1. 根据实际情况选择不同规格的模型 四、技术实现
4.1.方式一 # -*- coding utf-8 -*-
import warningsfrom langchain import PromptTemplate
from langchain.chains.llm import LLMChain
from langchain.llms import HuggingFacePipelinewarnings.filterwarnings(ignore)model_path /data/model/qwen1.5-7b-chatlocal_llm HuggingFacePipeline.from_model_id(model_idmodel_path,tasktext-generation,model_kwargs{trust_remote_code: True},pipeline_kwargs{max_new_tokens: 8192,top_p:0.9, temperature:0.45,repetition_penalty:1.1, do_sample:True},
)template Question: {question}Answer: Lets think step by step.prompt PromptTemplate.from_template(template)chain LLMChain(promptprompt, llmlocal_llm)
question 我家在广州很好玩哦你能介绍一些我家的特色景点吗?
print(chain.run(question)) 调用结果
4.2.方式二 # -*- coding utf-8 -*-
import warningsfrom langchain import PromptTemplate
from langchain.llms import HuggingFacePipelinewarnings.filterwarnings(ignore)model_path /data/model/qwen1.5-7b-chatlocal_llm HuggingFacePipeline.from_model_id(model_idmodel_path,tasktext-generation,model_kwargs{trust_remote_code: True},pipeline_kwargs{max_new_tokens: 8192,top_p:0.9, temperature:0.45,repetition_penalty:1.1, do_sample:True},
)template Question: {question}Answer: Lets think step by step.prompt PromptTemplate.from_template(template)chain prompt | local_llm
question 我家在广州很好玩哦你能介绍一些我家的特色景点吗?
print(chain.invoke({question: question}))调用结果
五、附带说明
5.1. ValueError: Input length of input_ids is 20, but max_length is set to 20. This can lead to unexpected behavior. You should consider increasing max_length or, better yet, setting max_new_tokens.
配置max_new_tokens 5.2. 使用pipline模型的加载方式
参见huggingface_pipeline.py文件跟往常的模型加载方式一致 5.3. 模型加载很慢
原因当前示例使用CPU加载模型及推理