Loading...
  • 建構
  • 管理
  • 模型與定價
  • 客戶端 SDK
  • API 參考
Search...
⌘K
Log in
嵌入
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Solutions

  • AI agents
  • Code modernization
  • Coding
  • Customer support
  • Education
  • Financial services
  • Government
  • Life sciences

Partners

  • Amazon Bedrock
  • Google Cloud's Vertex AI

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Company

  • Anthropic
  • Careers
  • Economic Futures
  • Research
  • News
  • Responsible Scaling Policy
  • Security and compliance
  • Transparency

Learn

  • Blog
  • Courses
  • Use cases
  • Connectors
  • Customer stories
  • Engineering at Anthropic
  • Events
  • Powered by Claude
  • Service partners
  • Startups program

Help and security

  • Availability
  • Status
  • Support
  • Discord

Terms and policies

  • Privacy policy
  • Responsible disclosure policy
  • Terms of service: Commercial
  • Terms of service: Consumer
  • Usage policy
建構/模型功能

嵌入

文本嵌入是文本的數值表示,可以測量語義相似性。本指南介紹嵌入、其應用,以及如何使用嵌入模型進行搜索、推薦和異常檢測等任務。

Was this page helpful?

  • 如何使用 Anthropic 獲取嵌入
  • Voyage AI 入門
  • Voyage Python 庫
  • Voyage HTTP API
  • AWS Marketplace

實施嵌入之前

在選擇嵌入提供商時,根據您的需求和偏好,有幾個因素可以考慮:

  • 數據集大小和領域特異性:模型訓練數據集的大小及其與您要嵌入的領域的相關性。更大或更特定領域的數據通常會產生更好的領域內嵌入
  • 推理性能:嵌入查找速度和端到端延遲。這對於大規模生產部署來說是一個特別重要的考慮因素
  • 自定義:在私有數據上繼續訓練的選項,或針對非常特定領域的模型專業化。這可以改進在獨特詞彙上的性能

如何使用 Anthropic 獲取嵌入

Anthropic 不提供自己的嵌入模型。Voyage AI 是一個嵌入提供商,具有廣泛的選項和功能,涵蓋上述所有考慮因素。

Voyage AI 製造最先進的嵌入模型,並為金融和醫療保健等特定行業領域提供定制模型,或為個別客戶提供定制微調模型。

本指南的其餘部分適用於 Voyage AI,但您應該評估各種嵌入供應商,以找到最適合您特定用例的方案。

可用模型

Voyage 建議使用以下文本嵌入模型:

模型上下文長度嵌入維度描述
voyage-3-large32,0001024(默認)、256、512、2048最佳的通用和多語言檢索質量。詳見博客文章。
voyage-3.532,0001024(默認)、256、512、2048針對通用和多語言檢索質量進行了優化。詳見博客文章。
voyage-3.5-lite32,0001024(默認)、256、512、2048針對延遲和成本進行了優化。詳見博客文章。
voyage-code-332,0001024(默認)、256、512、2048針對代碼檢索進行了優化。詳見博客文章。
voyage-finance-232,0001024針對金融檢索和 RAG 進行了優化。詳見博客文章。
voyage-law-216,0001024針對法律和長上下文檢索和 RAG 進行了優化。同時改進了所有領域的性能。詳見博客文章。

此外,還推薦以下多模態嵌入模型:

模型上下文長度嵌入維度描述
voyage-multimodal-3320001024豐富的多模態嵌入模型,可以向量化交錯的文本和內容豐富的圖像,如 PDF 截圖、幻燈片、表格、圖形等。詳見博客文章。

需要幫助決定使用哪個文本嵌入模型?查看常見問題解答。

Voyage AI 入門

要訪問 Voyage 嵌入:

  1. 在 Voyage AI 的網站上註冊
  2. 獲取 API 密鑰
  3. 將 API 密鑰設置為環境變量以方便使用:
export VOYAGE_API_KEY="<your secret key>"

您可以通過使用官方 voyageai Python 包或 HTTP 請求來獲取嵌入,如下所述。

Voyage Python 庫

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

您也可以通過請求 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 的說明可在 Voyage AWS Marketplace 文檔中找到。

快速入門示例

以下簡短示例展示了如何使用嵌入。

假設您有一個包含六個文檔的小語料庫要從中檢索

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" 分別用於嵌入文檔和查詢。更多規範可在 Voyage Python 包部分中找到。

輸出將是第 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 的定價頁面以獲取最新的定價詳情。