### [Deeplearning.ai GenAI/LLM系列課程筆記](https://learn.deeplearning.ai/) #### [Large Language Models with Semantic Search。大型語言模型與語意搜索 ](https://hackmd.io/@YungHuiHsu/rku-vjhZT) - [Introduction、Keyword/lexical Search。引言與關鍵字搜尋](https://hackmd.io/@YungHuiHsu/rku-vjhZT) - [ Embeddings。內嵌向量](https://hackmd.io/@YungHuiHsu/SJKORzaWp) - [Dense Retrieval。密集檢索](https://hackmd.io/@YungHuiHsu/Sk-hxS0-T) - [ReRank。重新排序](https://hackmd.io/@YungHuiHsu/HyT7uSJzT) - [Generating Answers。生成回答](https://hackmd.io/@YungHuiHsu/ry-Lv3kf6) #### [Finetuning Large Language Models。微調大型語言模型](https://hackmd.io/@YungHuiHsu/HJ6AT8XG6) --- ![](https://hackmd.io/_uploads/BJdGIpyza.png) ![](https://hackmd.io/_uploads/B13553yMT.png =400x) # Large Language Models with Semantic Search<br>大型語言模型與語意搜索 ## [Generating Answers](https://learn.deeplearning.ai/large-language-models-semantic-search/lesson/6/generating-answers) ### 課程概要 * 在搜尋管道結尾添加大語言模型(LLM)生成步驟,以獲得答案而非搜尋結果 * 說明大語言模型的優勢,也指出在某些使用案例中需要幫助 * 新增搜尋元件以提供上下文,可改善生成質量,使生成更貼近特定領域 * 比較僅詢問生成模型與新增搜尋步驟後提供上下文的差異 * 程式碼示範:搜尋專家文章、提供上下文、讓 LLM 從中提取答案 * 提示:透過增加生成次數來測試模型對同一提示的多次回應 * 強調搜尋與生成結合的威力,以及相關的竅門和技巧 ### Search can help LLMs in multiple ways 搜索可以通過以下方式幫助大語言模型: Retrieval can help LLMs with: * 提供事實資訊 Factual information * 搜索可以提供相關的事實資訊給大語言模型,讓其生成更準確的答案。大語言模型本身儲存的知識有限,搜索可以彌補這一缺陷 * 處理私人資訊 Private information * 有些問題涉及到用戶的私人資訊,大語言模型無法獲取這些資訊。添加搜索component可以讓系統從用戶的私人資料中檢索相關資訊 * 更新資訊 Updated information * 大語言模型受訓練時的資訊局限性,搜索可以提供更更新的資訊。例如,針對新聞事件提問,搜索可以提供新聞的最新進展 #### Where is the information stored? 大語言模型內部儲存世界知識和訓練資料。搜索可以從外部資料來源獲取上下文資訊 <div style="text-align: center;"> <figure> <img src="https://hackmd.io/_uploads/rJ7kjhJM6.png" width="500"> <figcaption> <span style="color: #3F7FBF; font-weight: bold;"> <a href="https://learn.deeplearning.ai/large-language-models-semantic-search/lesson/6/generating-answers" target="_blank">Where is the information stored?</a> </figcaption> </figure> </div> #### Search can add some context 直接詢問大語言模型會依賴其內部知識。添加搜索可以為其提供上下文,提升生成質量 <div style="text-align: center;"> <figure> <img src="https://hackmd.io/_uploads/SyLgo31G6.png" width="500"> <figcaption> <span style="color: #3F7FBF; font-weight: bold;"> <a href="https://learn.deeplearning.ai/large-language-models-semantic-search/lesson/6/generating-answers" target="_blank">Search can add some context</a> </figcaption> </figure> </div> #### Generating Answers - Just the LLM - LLM powered by Semantic Search 僅用大語言模型生成回答,依賴模型內部知識 輔以語意搜索生成回答,可以利用搜索提供的上下文資訊,生成更準確答案 綜合利用搜索與大語言模型的優勢,可以生成更佳的回答 <div style="text-align: center;"> <figure> <img src="https://hackmd.io/_uploads/HJmjj2JM6.png" width="500"> <figcaption> <span style="color: #3F7FBF; font-weight: bold;"> <a href="https://learn.deeplearning.ai/large-language-models-semantic-search/lesson/6/generating-answers" target="_blank">Generating Answers</a> </figcaption> </figure> </div> #### 程式實作 部分程式碼時作在先前的章節都有就略過囉 - 設定環境 - Chunking - Embeddings - Build a search index - Searching Articles -`search_index`細節見[Dense Retrieval](https://hackmd.io/@YungHuiHsu/Sk-hxS0-T)一節 ```python= def search_andrews_article(query): # Get the query's embedding query_embed = co.embed(texts=[query]).embeddings # Retrieve the nearest neighbors similar_item_ids = search_index.get_nns_by_vector(query_embed[0], 10, include_distances=True) # Retrieve the 3 nearest neighbors to the query's embedding similar_item_ids = search_index.get_nns_by_vector(query_embed[0], 10, include_distances=True) # Create a DataFrame to format the results search_results = pd.DataFrame(data={'texts': texts[similar_item_ids[0]], 'distance': similar_item_ids[1]}) return search_results ``` ```py= results = search_andrews_article( "Are side projects a good idea when trying to build a career in AI?") ``` - 可以看到目前的檢索結果,並且依照與問題的語意距離排序 - 如果要進一步使用ReRank Model取得較好的相關性結果見[ReRank](https://hackmd.io/@YungHuiHsu/HyT7uSJzT) 一節 ![](https://hackmd.io/_uploads/Syf2lpkG6.png) - Generating Answers - 本章流程摘要圖(注意,沒有使用[ReRank](https://hackmd.io/@YungHuiHsu/HyT7uSJzT)取得優化後的檢索結果) ![](https://hackmd.io/_uploads/S1oYLa1MT.png =600x) 生成答案有兩種主要方法: - 僅使用大語言模型 這種方法直接將問題提示輸入大語言模型,讓模型直接生成答案。 ```python= prompt = "Here is a question: " + question prediction = model.generate(prompt) ``` 優點是簡單直接,但模型的答案完全依賴其內部訓練得到的知識,容易產生錯誤。 - **大語言模型結合語意搜索** 這種方法先用問題執行語意搜索,獲取相關內容作為上下文,然後將上下文和問題一起輸入大語言模型生成答案。 ```python= context = search(question) prompt = "Context: " + context + " Question: " + question prediction = model.generate(prompt) ``` 這種方法可以利用搜索提供的上下文資訊,讓模型生成更準確的答案。結合大語言模型和搜索的優勢,生成結果更好。 兩者相比,後者方法更可靠,但需要額外建立搜索component。需要根據實際情況選擇適當的生成答案方法。 - 實作細節 這裡使用cohere的API 呼叫育訓練的生成模型`co.generate(model="command-nightly")` ```python= def ask_andrews_article(question, num_generations=1): # Search the text archive results = search_andrews_article(question) # Get the top result # context = results[0] context = results.text # Prepare the prompt prompt = f""" Excerpt from the article titled "How to Build a Career in AI" by Andrew Ng: {context} Question: {question} Extract the answer of the question from the text provided. If the text doesn't contain the answer, reply that the answer is not available.""" prediction = co.generate( prompt=prompt, max_tokens=70, model="command-nightly", temperature=0.5, num_generations=num_generations ) return prediction.generations ``` 檢視結果 ```python=! results = ask_andrews_article( "Are side projects a good idea when trying to build a career in AI?",) print(results[0]) # The answer is not clearly stated in the provided excerpt, however, given the context of the article which is discussing ways to build a career in AI, one can infer that the answer is most likely "yes". The author advises on focusing on foundational skills initially and participating in projects, which can be interpreted as a recommendation for side projects. ``` - "command-nightly" 表示使用 Cohere 最新當日更新的實驗模型進行生成。 Cohere 的模型分為兩類: - 穩定版模型 (例如 "command-xlarge-20221108") 這些是經過充分驗證後發佈的正式版本模型,穩定可靠。 - 夜間版模型 (例如 "command-nightly") 這是訓練實驗後的最新模型,包含新的改進,但可能不太穩定。 使用 "command-nightly" 的優點是可以取得 Cohere 最新的實驗模型進行測試。缺點是可能會遇到一些問題。 在生產環境中,建議使用穩定版模型。在測試環境中,可以使用夜間版模型試用最新功能。