大家好,今天我們將會一起學習如何快速建置一個本地知識庫查詢應用,
這將使用最新的語言模型技術Llama3和檢索增強生成RAG(Retrieval-Augmented Generation)技術。
希望透過今天的課程,大家可以學習這些最新的技術,帶回到你的工作職場
進一步的思考,結合單位內的應用場景,然後進行實作落地。
同時也替你帶來更大的價值,讓你成為組織內創新開發的領頭羊。
龍龍 希望讓大家能夠應用新技術,提升競爭力,一起成長、共好~~
如果你還沒有建置一些環境,建議可以先看這個LLM的首部份
會帶你安裝ollama,這是可以很快建置相關服務的好平台
https://youtu.be/3-mJAN5TlaI
****課程清單(快速跳轉)****
01:03 - Demo原生LLM與結合RAG的使用者體驗
05:13 - RAG是什麼?
10:02 - 解決方案架構
10:50 - 安裝套件
11:30 - 實操演示
🔹 小額贊助 龍龍AI與程式實戰
-------------------------------------------------
🎉方式一:加入頻道會員 (加入後可觀看會員專區的影片喔) https://www.youtube.com/channel/UC9zG1ZsTERusFSzmuMVFr3A/join
🎉方式二:在 YouTube 影片右下角點擊「超級感謝」按鈕
🎉方式三(請 龍龍 喝咖啡):https://buymeacoffee.com/changlunglung
-------------------------------------------------
強力推薦,讓你提升職場競爭力課程,由龍龍與巨匠電腦合作:
零基礎也能搞定:快速學習打造自己的企業級語音轉文字服務!(手刀下單)
https://www.worker360.com.tw/video/DiT000519/7c956133af28fa9c62cfadc38f80aa72
ChatGPT全方位職場情境應用:強化提詞效率(手刀下單)
https://www.worker360.com.tw/video/DiT000500/7c956133af28fa9cb5a56bf048ec0a62
=================================
🔹與本主題或LLM相關的精選教程
=================================
https://youtu.be/9TurA-iA1_Y
https://youtu.be/PXuQqMgvUW0
https://youtu.be/d11L0JynGq4
https://youtu.be/3bp8JJ91EkI
https://youtu.be/3-mJAN5TlaI
https://youtu.be/QnRLoMA7qzY
https://youtu.be/nOYtEk-m8RQ
https://youtu.be/ILQIZJ1lpW4
https://youtu.be/druwSeJNoPc
https://youtu.be/1GiyKxAuYcU
🔹 練習用知識QA檔案下載
https://docs.google.com/spreadsheets/d/11osu2s0ndkJWd4skSw53I8jcnhoT53k_/edit?usp=sharing&ouid=118068502502106904151&rtpof=true&sd=true
🔹 使用到的指令
安裝嵌入模型:ollama pull mxbai-embed-large
安裝聯發科LLM模型:ollama run ycchen/breeze-7b-instruct-v1_0
安裝chromadb(向量庫):pip install chromadb
🔹 程式碼Code
import streamlit as st # 導入Streamlit庫,用於建立網頁應用
import ollama # 導入ollama庫,用於自然語言處理
import chromadb # 導入chromadb庫,用於數據存儲和查詢
import pandas as pd # 導入pandas庫,用於數據分析和處理
# 定義一個初始化函數,用於設置Streamlit的會話狀態
def initialize():
# 檢查'session_state'(會話狀態)中是否已有'already_executed'這個變量
# 這個變量用來檢查是否已經進行過一次資料庫初始化操作
if "already_executed" not in st.session_state:
st.session_state.already_executed = False # 若不存在,則設置為False
# 如果'already_executed'為False,表示還未初始化過資料庫
if not st.session_state.already_executed:
setup_database() # 呼叫setup_database函數來進行資料庫的設置和數據加載
# 定義設置資料庫的函數
def setup_database():
client = chromadb.Client() # 創建一個chromadb的客戶端,用於與資料庫交互
file_path = 'QA50.xlsx' # 指定Excel文件的路徑和名稱
documents = pd.read_excel(file_path, header=None) # 使用pandas讀取Excel文件
# 使用chromadb客戶端創建或獲取名為'demodocs'的集合
collection = client.get_or_create_collection(name="demodocs")
# 遍歷從Excel文件中讀取的數據,每一行代表一條記錄
for index, content in documents.iterrows():
response = ollama.embeddings(model="mxbai-embed-large", prompt=content[0]) # 通過ollama生成該行文本的嵌入向量
collection.add(ids=[str(index)], embeddings=[response["embedding"]], documents=[content[0]]) # 將文本和其嵌入向量添加到集合中
st.session_state.already_executed = True # 設置'already_executed'為True,表示已完成初始化
st.session_state.collection = collection # 將集合保存在會話狀態中,供後續使用
# 定義創建新chromadb客戶端的函數,每次需要時創建新的連接
def create_chromadb_client():
return chromadb.Client() # 返回一個新的chromadb客戶端實例
# 主函數,負責構建UI和處理用戶事件
def main():
initialize() # 呼叫初始化函數
st.title("我的第一個LLM+RAG本地知識問答") # 在網頁應用中設置標題
user_input = st.text_area("您想問什麼?", "") # 創建一個文本區域供用戶輸入問題
# 如果用戶點擊"送出"按鈕
if st.button("送出"):
if user_input:
handle_user_input(user_input, st.session_state.collection) # 處理用戶輸入,進行查詢和回答
else:
st.warning("請輸入問題!") # 如果用戶沒有輸入,顯示警告消息
# 定義處理用戶輸入的函數
def handle_user_input(user_input, collection):
response = ollama.embeddings(prompt=user_input, model="mxbai-embed-large") # 生成用戶輸入的嵌入向量
results = collection.query(query_embeddings=[response["embedding"]], n_results=3) # 在集合中查詢最相關的三個文檔
data = results['documents'][0] # 獲取最相關的文檔
output = ollama.generate(
model="ycchen/breeze-7b-instruct-v1_0",
prompt=f"Using this data: {data}. Respond to this prompt and use chinese: {user_input}" # 生成回應
)
st.text("回答:") # 顯示"回答:"
st.write(output['response']) # 將生成的回應顯示在網頁上
if __name__ == "__main__":
main() # 如果直接執行此文件,則執行main函數
==================
#OllamaEmbedding #Local #Nomic #OllamaEmbeddings #OllamaNomic #OllamaNomicEmbedding #NomicEmbedding #NomicEmbeddings #NomicOllama #EmbeddingOllama #Embed #Embedding #LocalRAG #OllamaLocalRAG
#ollama #runollamalocally #howtoinstallollama #ollamaonmacos #installingollama #localllm #mistral7b #installllmlocally #opensource #llms #opensourceLLM #custommodel #localagents #opensourceAI #llmlocal #localAI #llmslocally #opensource #Olama #Mistral #OllamaMistral #Chroma #ChromaDB #LangChain