嵌入向量
實施嵌入向量之前
在選擇嵌入向量提供商時,根據您的需求和偏好,有幾個因素可以考慮:
- 數據集大小和領域特異性:模型訓練數據集的大小及其與您要嵌入的領域的相關性。更大或更具領域特異性的數據通常會產生更好的領域內嵌入向量
- 推理性能:嵌入查找速度和端到端延遲。這對於大規模生產部署來說是一個特別重要的考慮因素
- 自定義:在私有數據上繼續訓練的選項,或針對非常特定領域的模型專業化。這可以提高在獨特詞彙表上的性能
如何通過 Anthropic 獲取嵌入向量
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 AI 入門
要訪問 Voyage 嵌入向量:
- 在 Voyage AI 網站上註冊
- 獲取 API 密鑰
- 為方便起見,將 API 密鑰設置為環境變量:
export VOYAGE_API_KEY="<your secret key>"您可以通過使用官方的 voyageai Python 包或 HTTP 請求來獲取嵌入向量,如下所述。
Voyage Python 庫
可以使用以下命令安裝 voyageai 包:
pip install -U voyageai然後,您可以創建一個客戶端對象並開始使用它來嵌入您的文本:
import voyageai
vo = voyageai.Client()
# 這將自動使用環境變量 VOYAGE_API_KEY。
# 或者,您可以使用 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
您也可以通過請求 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"
}'您將得到的響應是一個包含嵌入向量和令牌使用情況的 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 文檔。
AWS Marketplace
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 的定價頁面獲取最新的定價詳情。