在这篇文章中,我将解释如何使用 Llama2 模型构建一个聊天机器人来智能查询 Excel 数据。
我们正在建设什么
- 加载 Excel 文件。
- 将数据分割成可管理的块。
- 将数据存储在矢量数据库中以便快速检索。
- 使用本地 Llama2 模型来回答基于 Excel 文件的内容。
先决条件:
Python (≥ 3.8)
库:langchain、pandas、非结构化、Chroma
第 1 步:安装依赖项
%pip install -q unstructured langchain %pip install -q "unstructured[all-docs]"
第 2 步:加载 Excel 文件
import pandas as pd excel_path = "Book2.xlsx" if excel_path: df = pd.read_excel(excel_path) data = df.to_string(index=False) else: print("Upload an Excel file")
第 3 步:将数据分块并存储在向量数据库中
大型文本数据被分割成更小的、重叠的块,以进行有效的嵌入和查询。这些块存储在 Chroma 矢量数据库中。
from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_community.embeddings import OllamaEmbeddings from langchain_community.vectorstores import Chroma text_splitter = RecursiveCharacterTextSplitter(chunk_size=7500, chunk_overlap=100) chunks = text_splitter.split_text(data) embedding_model = OllamaEmbeddings(model="nomic-embed-text", show_progress=False) vector_db = Chroma.from_texts( texts=chunks, embedding=embedding_model, collection_name="local-rag" )
步骤 4:初始化 Llama2 模型
我们使用 ChatOllama 在本地加载 Llama2 模型。
from langchain_community.chat_models import ChatOllama local_model = "llama2" llm = ChatOllama(model=local_model)
第 5 步:创建查询提示
聊天机器人将根据 Excel 文件中的特定列名称进行响应。我们创建一个提示模板来指导模型
from langchain.prompts import PromptTemplate QUERY_PROMPT = PromptTemplate( input_variables=["question"], template="""You are an AI assistant. Answer the user's questions based on the column names: Id, order_id, name, sales, refund, and status. Original question: {question}""" )
第 6 步:设置检索器
我们配置一个检索器从向量数据库中获取相关块,Llama2 模型将使用该数据块来回答问题。
from langchain.retrievers.multi_query import MultiQueryRetriever retriever = MultiQueryRetriever.from_llm( vector_db.as_retriever(), llm, prompt=QUERY_PROMPT )
第 7 步:构建响应链
响应链集成:
- 用于获取上下文的检索器。
- 格式化问题和上下文的提示。
- 用于生成答案的 Llama2 模型。
- 用于格式化响应的输出解析器。
from langchain.prompts import ChatPromptTemplate from langchain_core.runnables import RunnablePassthrough from langchain_core.output_parsers import StrOutputParser template = """Answer the question based ONLY on the following context: {context} Question: {question} """ prompt = ChatPromptTemplate.from_template(template) chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() )
第 8 步:提出问题
现在我们准备好提问了!以下是我们如何调用链来获取响应:
raw_result = chain.invoke("How many rows are there?") final_result = f"{raw_result}\n\nIf you have more questions, feel free to ask!" print(final_result)
样本输出
当我在示例 Excel 文件上运行上述代码时,我得到的结果如下:
Based on the provided context, there are 10 rows in the table. If you have more questions, feel free to ask!
结论:
这种方法利用嵌入和 Llama2 模型的强大功能,为 Excel 数据创建智能、交互式聊天机器人。通过一些调整,您可以扩展它以处理其他类型的文档或将其集成到成熟的应用程序中!
在我的 LinkedIn 上检查 UI 的工作示例:
介绍 BChat Excel:用于 Excel 文件交互的对话式 AI 驱动工具
以上是使用 LlamaChat 和 Excel 构建一个简单的聊天机器人]的详细内容。更多信息请关注PHP中文网其他相关文章!

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

列表sandnumpyArraysInpyThonHavedIfferentMemoryfootprints:listSaremoreFlexibleButlessMemory-效率,而alenumpyArraySareSareOptimizedFornumericalData.1)listsStorReereReereReereReereFerenceStoObjects,withoverHeadeBheadaroundAroundaroundaround64bytaround64bitson64-bitsysysysyssyssyssyssyssyssysssys2)

toensurepythonscriptsbehavecorrectlyacrycrossdevelvermations,登台和生产,USETHESTERTATE:1)Environment varriablesforsimplesettings,2)configurationFilesForefilesForcomPlexSetups,3)dynamiCofforAdaptapity.eachmethodofferSuniquebeneiquebeneiquebeneniqueBenefitsaniqueBenefitsandrefitsandRequiresandRequireSandRequireSca

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),