> 歡迎訂閱我的 [AI Engineer 電子報](https://aihao.eo.page/6tcs9) 和瀏覽 [[Generative AI Engineer 知識庫]]
原版: https://www.deeplearning.ai/short-courses/langchain-chat-with-your-data/
簡中翻譯版: https://www.youtube.com/playlist?list=PLiuLMb-dLdWJX_EWk4RtQAjjrPLWaswTV
課程心得:
* 蠻有收穫的,對於 RAG 元件有更深入的內容
* 包括各種 Splitting, 各種 Retrieval 以及可傳入 chat history 的 ConversationalRetrievalChain 的運作(跟我原先直覺想的運作不一樣,竟然是個 chaining)
* langchain 的範例 code 也有簡單易懂
## Introduction
![[Pasted image 20230818220143.png]]
![[Pasted image 20230818220153.png]]
![[Pasted image 20230818220209.png]]
## Document Loading
![[Pasted image 20230921145750.png]]
![[Pasted image 20230921145821.png]]
![[Pasted image 20230921150731.png]]
![[Pasted image 20230921150751.png]]
![[Pasted image 20230921151332.png]]
![[Pasted image 20230921151341.png]]
![[Pasted image 20230921151356.png]]
## Document Splitting
![[Pasted image 20230921151432.png]]
![[Pasted image 20230921151543.png]]
![[Pasted image 20230921151641.png]]
![[Pasted image 20230921160202.png]]
![[Pasted image 20230921160212.png]]
Recursive 預設會先用 \n\n 切,然後再用 \n 切..... 依此類推
![[Pasted image 20230921160234.png]]
![[Pasted image 20230921160323.png]]
## Vector stores and Embedding
![[Pasted image 20230921160509.png]]
![[Pasted image 20230921160558.png]]
![[Pasted image 20230921160700.png]]
![[Pasted image 20230921160720.png]]
![[Pasted image 20230921160754.png]]
Lecture01.pdf 載入兩次,是因為想要示範模擬髒資料
![[Pasted image 20230921160829.png]]
![[Pasted image 20230921160843.png]]
![[Pasted image 20230921160858.png]]
### Failure modes 失敗的場景
![[Pasted image 20230921162235.png]]
前兩個回傳是重複的,因為我們之前有重複載入
![[Pasted image 20230921162345.png]]
我們只想要第三章的 regression 資料,但是 embedding 無法捕捉結構化資料來過濾
## Retrieval 檢索
![[Pasted image 20230921162555.png]]
![[Pasted image 20230921163131.png]]
MMR 最大邊際相關性
我們想要有多樣性 diverse
![[Pasted image 20230921163039.png]]
* 先撈比較大的 fetch_k
* 從從 fetch_k 中,找較有多樣性的子集 k
* 這招就可以避免有重複的資料
![[Pasted image 20230921163355.png]]
self-query 自查詢:
![[Pasted image 20230921163649.png]]
多一步 Compression LLM 來縮減內容 (縮減 chunk 內容,不是合併chunks)。此招成本會增加。
https://python.langchain.com/docs/modules/data_connection/retrievers/contextual_compression/
LLMChainExtractor
![[Pasted image 20230921170148.png]]
![[Pasted image 20230921170159.png]]
![[Pasted image 20230921170219.png]]
![[Pasted image 20230921170229.png]]
![[Pasted image 20230921170257.png]]
![[Pasted image 20230921170303.png]]
![[Pasted image 20230921170316.png]]
![[Pasted image 20230921170323.png]]
![[Pasted image 20230921165847.png]]
![[Pasted image 20230921170402.png]]
![[Pasted image 20230921170410.png]]
TF-IDF 的效果就沒這麼好
最後,作者最喜歡 self-query 這招,建議建立複雜的 metadata 來做過濾
你甚至可以可用 LLM 來推導出 metadata 用來過濾,甚至是 nested metadata
## Question Answering
![[Pasted image 20230921170601.png]]
![[Pasted image 20230921170611.png]]
![[Pasted image 20230921170627.png]]
若相關資料很多時,有這三種方式可以處理
refine 效果好,但是慢
![[Pasted image 20230921173223.png]]
![[Pasted image 20230921173234.png]]
![[Pasted image 20230921173243.png]]
![[Pasted image 20230921173253.png]]
![[Pasted image 20230921173259.png]]
![[Pasted image 20230921173351.png]]
![[Pasted image 20230921173306.png]]
沒有 conversational history,下一章講
## Chat
建構完整的問答聊天機器人
![[Pasted image 20230921173943.png]]
![[Pasted image 20230921174023.png]]
![[Pasted image 20230921174853.png]]
![[Pasted image 20230921174904.png]]
![[Pasted image 20230921174913.png]]
![[Pasted image 20230921174932.png]]
![[Pasted image 20230921174946.png]]
根據對話歷史紀錄,以及用戶的新問題,重寫一個新問題 (chaining prompt)
再用新問題去 RAG
> 這篇 https://txt.cohere.com/rag-chatbot/ 有談到為何需要根據對話紀錄紀錄,重新產生 RAG 用的 query
![[Pasted image 20230921175008.png]]
![[Pasted image 20230921175536.png]]
注意: 這裡沒有建立 Memory,因為 chat history 在 GUI 中處理了
稍後在呼叫 qa 時,需要手動傳入 chat_history
![[Pasted image 20230921175557.png]]
![[Pasted image 20230921175611.png]]
![[Pasted image 20230921175632.png]]
![[Pasted image 20230921174048.png]]