使用 LlamaChat 和 Excel 建立一個簡單的聊天機器人]
Nov 29, 2024 pm 08:31 PM在這篇文章中,我將解釋如何使用 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中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章
倉庫:如何復興隊友
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前
By 尊渡假赌尊渡假赌尊渡假赌
公眾號網頁更新緩存難題:如何避免版本更新後舊緩存影響用戶體驗?
3 週前
By 王林

熱門文章標籤

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)