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 思考(思维链)使用 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"
      }'

    您将获得的响应是一个包含嵌入和令牌使用量的 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