Loading...
    • 构建
    • 管理
    • 模型和定价
    • 客户端 SDK
    • API 参考
    Search...
    ⌘K
    第一步
    Claude 简介快速开始
    使用 Claude 构建
    功能概览使用 Messages APIClaude API 技能处理停止原因
    模型能力
    扩展思考自适应思考工作量任务预算(测试版)快速模式(测试版:研究预览)结构化输出引用流式消息批量处理搜索结果流式拒绝多语言支持嵌入
    工具
    概览工具使用原理网络搜索工具网络获取工具代码执行工具顾问工具内存工具Bash 工具计算机使用工具文本编辑器工具
    工具基础设施
    工具参考工具搜索程序化工具调用细粒度工具流式传输
    上下文管理
    上下文窗口压缩上下文编辑提示缓存Token 计数
    处理文件
    Files APIPDF 支持图像和视觉
    技能
    概览快速开始最佳实践企业技能API 中的技能
    MCP
    远程 MCP 服务器MCP 连接器
    提示工程
    概览提示最佳实践Console 提示工具
    测试和评估
    定义成功并构建评估在 Console 中使用评估工具降低延迟
    加强防护栏
    减少幻觉提高输出一致性缓解越狱减少提示泄露
    资源
    术语表
    发布说明
    Claude Platform
    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
    • 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
    模型能力

    嵌入

    文本嵌入是文本的数值表示,可以测量语义相似性。本指南介绍了嵌入、其应用以及如何使用嵌入模型执行搜索、推荐和异常检测等任务。

    实现嵌入之前

    选择嵌入提供商时,根据您的需求和偏好,有几个因素可以考虑:

    • 数据集大小和领域特异性:模型训练数据集的大小及其与您要嵌入的领域的相关性。更大或更特定领域的数据通常会产生更好的领域内嵌入
    • 推理性能:嵌入查找速度和端到端延迟。这对于大规模生产部署来说是一个特别重要的考虑因素
    • 定制化:在私有数据上继续训练的选项,或针对非常特定领域的模型专业化。这可以改进独特词汇上的性能

    如何使用 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 的定价页面以获取最新的定价详情。

    Was this page helpful?

    • 如何使用 Anthropic 获取嵌入
    • Voyage AI 入门
    • Voyage Python 库
    • Voyage HTTP API
    • AWS Marketplace