选择嵌入提供商时,根据您的需求和偏好,有几个因素可以考虑:
Anthropic 不提供自己的嵌入模型。Voyage AI 是一个嵌入提供商,拥有广泛的选项和功能,涵盖了上述所有考虑因素。
Voyage AI 制造最先进的嵌入模型,并为特定行业领域(如金融和医疗保健)提供定制模型,或为个别客户提供定制微调模型。
本指南的其余部分针对 Voyage AI,但您应该评估各种嵌入供应商,以找到最适合您特定用例的方案。
Voyage 推荐使用以下文本嵌入模型:
| 模型 | 上下文长度 | 嵌入维度 | 描述 |
|---|---|---|---|
voyage-3-large | 32,000 | 1024(默认)、256、512、2048 | 最佳的通用和多语言检索质量。详见博客文章。 |
voyage-3.5 | 32,000 | 1024(默认)、256、512、2048 | 针对通用和多语言检索质量进行了优化。详见博客文章。 |
voyage-3.5-lite | 32,000 | 1024(默认)、256、512、2048 | 针对延迟和成本进行了优化。详见博客文章。 |
voyage-code-3 | 32,000 | 1024(默认)、256、512、2048 | 针对代码检索进行了优化。详见博客文章。 |
voyage-finance-2 | 32,000 | 1024 | 针对金融检索和 RAG 进行了优化。详见博客文章。 |
voyage-law-2 | 16,000 | 1024 | 针对法律和长上下文检索和 RAG 进行了优化。同时改进了所有领域的性能。详见博客文章。 |
此外,还推荐以下多模态嵌入模型:
| 模型 | 上下文长度 | 嵌入维度 | 描述 |
|---|---|---|---|
voyage-multimodal-3 | 32000 | 1024 | 丰富的多模态嵌入模型,可以对交错的文本和内容丰富的图像进行向量化,例如 PDF 截图、幻灯片、表格、图形等。详见博客文章。 |
需要帮助决定使用哪个文本嵌入模型?请查看常见问题。
要访问 Voyage 嵌入:
export VOYAGE_API_KEY="<your secret key>"您可以通过使用官方 voyageai Python 包或 HTTP 请求来获取嵌入,如下所述。
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 来获取嵌入。例如,您可以通过终端中的 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 文档。
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?