Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    開始使用
    Claude 簡介快速開始
    模型與定價
    模型概覽選擇模型Claude 4.5 新功能遷移至 Claude 4.5模型棄用定價
    使用 Claude 構建
    功能概覽使用 Messages API上下文窗口提示詞最佳實踐
    功能
    提示詞快取上下文編輯擴展思考努力串流消息批次處理引用多語言支援Token 計數嵌入視覺PDF 支援Files API搜尋結果結構化輸出
    工具
    概覽如何實現工具使用細粒度工具串流Bash 工具代碼執行工具程式化工具調用計算機使用工具文字編輯器工具網頁擷取工具網頁搜尋工具記憶體工具工具搜尋工具
    Agent Skills
    概覽快速開始最佳實踐使用 API 的 Skills
    Agent SDK
    概覽快速開始TypeScript SDKTypeScript V2 (預覽)Python SDK遷移指南
    API 中的 MCP
    MCP 連接器遠端 MCP 伺服器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示詞工程
    概覽提示詞生成器使用提示詞範本提示詞改進器清晰直接使用範例 (多次提示)讓 Claude 思考 (CoT)使用 XML 標籤給 Claude 一個角色 (系統提示詞)預填 Claude 的回應鏈接複雜提示詞長上下文提示擴展思考提示
    測試與評估
    定義成功標準開發測試案例使用評估工具降低延遲
    加強防護欄
    減少幻覺增加輸出一致性緩解越獄串流拒絕減少提示詞洩露保持 Claude 的角色
    管理和監控
    Admin API 概覽使用量和成本 APIClaude Code Analytics API
    Console
    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
    • Catalog
    • 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
    • Catalog
    • 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
    功能

    嵌入向量

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

    實施嵌入向量之前

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

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

    如何通過 Anthropic 獲取嵌入向量

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

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

    本指南的其餘部分針對 Voyage AI,但我們鼓勵您評估各種嵌入向量供應商,以找到最適合您特定用例的選擇。

    可用模型

    Voyage 推薦使用以下文本嵌入模型:

    模型上下文長度嵌入維度描述
    voyage-3-large32,0001024(默認)、256、512、2048最佳通用和多語言檢索質量。詳情請參見博客文章。
    voyage-3.5
    • 如何通過 Anthropic 獲取嵌入向量
    • Voyage AI 入門
    • Voyage Python 庫
    • Voyage HTTP API
    • AWS Marketplace
    32,000
    1024(默認)、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()
    # 這將自動使用環境變量 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 的定價頁面獲取最新的定價詳情。