Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    入門
    Claude 簡介快速開始
    模型與定價
    模型概覽選擇模型Claude 4.6 新功能遷移指南模型棄用定價
    使用 Claude 構建
    功能概覽使用 Messages API處理停止原因提示詞最佳實踐
    上下文管理
    上下文視窗壓縮上下文編輯
    功能
    提示詞快取延伸思考自適應思考思考力度串流訊息批次處理引用多語言支援Token 計數嵌入視覺PDF 支援Files API搜尋結果結構化輸出
    工具
    概覽如何實作工具使用細粒度工具串流Bash 工具程式碼執行工具程式化工具呼叫電腦使用工具文字編輯器工具網頁擷取工具網頁搜尋工具記憶工具工具搜尋工具
    Agent Skills
    概覽快速開始最佳實踐企業級 Skills透過 API 使用 Skills
    Agent SDK
    概覽快速開始TypeScript SDKTypeScript V2(預覽版)Python SDK遷移指南
    API 中的 MCP
    MCP 連接器遠端 MCP 伺服器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示詞工程
    概覽提示詞產生器使用提示詞範本提示詞改進器清晰直接使用範例(多範例提示)讓 Claude 思考(CoT)使用 XML 標籤賦予 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.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"
      }'

    您將收到的回應是一個包含嵌入和 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 文件。

    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 的定價頁面以取得最新的定價詳情。

    Was this page helpful?

    • 如何透過 Anthropic 取得嵌入
    • 開始使用 Voyage AI
    • Voyage Python 函式庫
    • Voyage HTTP API
    • AWS Marketplace