搜索可以通過以下方式幫助大語言模型:
Retrieval can help LLMs with:
大語言模型內部儲存世界知識和訓練資料。搜索可以從外部資料來源獲取上下文資訊
直接詢問大語言模型會依賴其內部知識。添加搜索可以為其提供上下文,提升生成質量
僅用大語言模型生成回答,依賴模型內部知識
輔以語意搜索生成回答,可以利用搜索提供的上下文資訊,生成更準確答案
綜合利用搜索與大語言模型的優勢,可以生成更佳的回答
部分程式碼時作在先前的章節都有就略過囉
設定環境
Chunking
Embeddings
Build a search index
Searching Articles
-search_index
細節見Dense Retrieval一節
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
results = search_andrews_article(
"Are side projects a good idea when trying to build a career in AI?")
Generating Answers
生成答案有兩種主要方法:
僅使用大語言模型
這種方法直接將問題提示輸入大語言模型,讓模型直接生成答案。
prompt = "Here is a question: " + question
prediction = model.generate(prompt)
優點是簡單直接,但模型的答案完全依賴其內部訓練得到的知識,容易產生錯誤。
大語言模型結合語意搜索
這種方法先用問題執行語意搜索,獲取相關內容作為上下文,然後將上下文和問題一起輸入大語言模型生成答案。
context = search(question)
prompt = "Context: " + context + " Question: " + question
prediction = model.generate(prompt)
這種方法可以利用搜索提供的上下文資訊,讓模型生成更準確的答案。結合大語言模型和搜索的優勢,生成結果更好。
兩者相比,後者方法更可靠,但需要額外建立搜索component。需要根據實際情況選擇適當的生成答案方法。
實作細節
這裡使用cohere的API 呼叫育訓練的生成模型co.generate(model="command-nightly")
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
檢視結果
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 最新的實驗模型進行測試。缺點是可能會遇到一些問題。
在生產環境中,建議使用穩定版模型。在測試環境中,可以使用夜間版模型試用最新功能。