在選擇嵌入提供商時,根據您的需求和偏好,有幾個因素可以考慮:
Anthropic 不提供自己的嵌入模型。一個擁有廣泛選項和功能、涵蓋上述所有考量的嵌入提供商是 Voyage AI。
Voyage AI 製作最先進的嵌入模型,並為特定產業領域(如金融和醫療保健)提供客製化模型,或為個別客戶提供量身定制的微調模型。
本指南的其餘部分是關於 Voyage AI 的,但我們鼓勵您評估各種嵌入供應商,以找到最適合您特定使用案例的方案。
Voyage 建議使用以下文字嵌入模型:
| 模型 | 上下文長度 | 嵌入維度 | 描述 |
|---|---|---|---|
voyage-3-large | 32,000 | 1024(預設)、256、512、2048 | 最佳通用和多語言檢索品質。詳情請參閱部落格文章。 |
voyage-3.5 | 32,000 | 1024(預設)、256、512、2048 | 針對通用和多語言檢索品質進行最佳化。詳情請參閱部落格文章。 |
voyage-3.5-lite | 32,000 | 1024(預設)、256、512、2048 | 針對延遲和成本進行最佳化。詳情請參閱部落格文章。 |
voyage-code-3 | 32,000 | 1024(預設)、256、512、2048 | 針對程式碼檢索進行最佳化。詳情請參閱部落格文章。 |
voyage-finance-2 | 32,000 | 1024 | 針對金融檢索和 RAG 進行最佳化。詳情請參閱部落格文章。 |
voyage-law-2 | 16,000 | 1024 | 針對法律和長上下文檢索和 RAG 進行最佳化。同時改善了所有領域的效能。詳情請參閱部落格文章。 |
此外,建議使用以下多模態嵌入模型:
| 模型 | 上下文長度 | 嵌入維度 | 描述 |
|---|---|---|---|
voyage-multimodal-3 | 32000 | 1024 | 豐富的多模態嵌入模型,可以向量化交錯的文字和內容豐富的圖像,例如 PDF、投影片、表格、圖表等的螢幕截圖。詳情請參閱部落格文章。 |
需要幫助決定使用哪個文字嵌入模型?請查看常見問題。
要存取 Voyage 嵌入:
export VOYAGE_API_KEY="<your secret key>"您可以透過使用官方 voyageai Python 套件或 HTTP 請求來取得嵌入,如下所述。
voyageai 套件可以使用以下命令安裝:
pip install -U voyageai然後,您可以建立一個客戶端物件並開始使用它來嵌入您的文字:
import voyageai
vo = voyageai.Client()
# This will automatically use the environment variable VOYAGE_API_KEY.
# Alternatively, you can use vo = voyageai.Client(api_key="<your secret key>")
texts = ["Sample text 1", "Sample text 2"]
result = vo.embed(texts, model="voyage-3.5", input_type="document")
print(result.embeddings[0])
print(result.embeddings[1])result.embeddings 將是一個包含兩個嵌入向量的列表,每個向量包含 1024 個浮點數。執行上述程式碼後,兩個嵌入將顯示在螢幕上:
[-0.013131560757756233, 0.019828535616397858, ...] # embedding for "Sample text 1"
[-0.0069352793507277966, 0.020878976210951805, ...] # embedding for "Sample text 2"建立嵌入時,您可以為 embed() 函式指定一些其他參數。
有關 Voyage Python 套件的更多資訊,請參閱 Voyage 文件。
您也可以透過請求 Voyage HTTP API 來取得嵌入。例如,您可以在終端機中透過 curl 命令發送 HTTP 請求:
curl https://api.voyageai.com/v1/embeddings \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $VOYAGE_API_KEY" \
-d '{
"input": ["Sample text 1", "Sample text 2"],
"model": "voyage-3.5"
}'您將收到的回應是一個包含嵌入和 token 使用量的 JSON 物件:
{
"object": "list",
"data": [
{
"embedding": [-0.013131560757756233, 0.019828535616397858, ...],
"index": 0
},
{
"embedding": [-0.0069352793507277966, 0.020878976210951805, ...],
"index": 1
}
],
"model": "voyage-3.5",
"usage": {
"total_tokens": 10
}
}
有關 Voyage HTTP API 的更多資訊,請參閱 Voyage 文件。
Voyage 嵌入可在 AWS Marketplace 上取得。在 AWS 上存取 Voyage 的說明可在此處取得。
現在我們知道如何取得嵌入了,讓我們看一個簡短的範例。
假設我們有一個包含六個文件的小型語料庫需要檢索
documents = [
"The Mediterranean diet emphasizes fish, olive oil, and vegetables, believed to reduce chronic diseases.",
"Photosynthesis in plants converts light energy into glucose and produces essential oxygen.",
"20th-century innovations, from radios to smartphones, centered on electronic advancements.",
"Rivers provide water, irrigation, and habitat for aquatic species, vital for ecosystems.",
"Apple's conference call to discuss fourth fiscal quarter results and business updates is scheduled for Thursday, November 2, 2023 at 2:00 p.m. PT / 5:00 p.m. ET.",
"Shakespeare's works, like 'Hamlet' and 'A Midsummer Night's Dream,' endure in literature."
]
我們將首先使用 Voyage 將每個文件轉換為嵌入向量
import voyageai
vo = voyageai.Client()
# Embed the documents
doc_embds = vo.embed(
documents, model="voyage-3.5", input_type="document"
).embeddings嵌入將允許我們在向量空間中進行語義搜尋/檢索。給定一個範例查詢,
query = "When is Apple's conference call scheduled?"我們將其轉換為嵌入,並進行最近鄰搜尋,根據嵌入空間中的距離找到最相關的文件。
import numpy as np
# Embed the query
query_embd = vo.embed(
[query], model="voyage-3.5", input_type="query"
).embeddings[0]
# Compute the similarity
# Voyage embeddings are normalized to length 1, therefore dot-product
# and cosine similarity are the same.
similarities = np.dot(doc_embds, query_embd)
retrieved_id = np.argmax(similarities)
print(documents[retrieved_id])請注意,我們分別使用 input_type="document" 和 input_type="query" 來嵌入文件和查詢。更多規格可在此處找到。
輸出將是第 5 個文件,這確實是與查詢最相關的:
Apple's conference call to discuss fourth fiscal quarter results and business updates is scheduled for Thursday, November 2, 2023 at 2:00 p.m. PT / 5:00 p.m. ET.如果您正在尋找一套關於如何使用嵌入進行 RAG 的詳細教學手冊,包括向量資料庫,請查看我們的 RAG 教學手冊。
請造訪 Voyage 的定價頁面以取得最新的定價詳情。
Was this page helpful?