> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Voyage AI 集成

> 使用 LangChain Python 与 Voyage AI 嵌入模型集成。

> [Voyage AI](https://www.voyageai.com/) 提供尖端的嵌入/向量化模型。

让我们加载 Voyage AI 嵌入类。（使用 `pip install langchain-voyageai` 安装 LangChain 合作伙伴包）

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_voyageai import VoyageAIEmbeddings
```

Voyage AI 使用 API 密钥来监控使用情况和管理权限。要获取您的密钥，请在我们的[主页](https://www.voyageai.com)上创建一个账户。然后，使用您的 API 密钥创建一个 VoyageEmbeddings 模型。您可以使用以下任何模型：（[来源](https://docs.voyageai.com/docs/embeddings)）：

* `voyage-4-large`
* `voyage-4`
* `voyage-4-lite`
* `voyage-context-3`
* `voyage-3.5`
* `voyage-3.5-lite`
* `voyage-3-large`
* `voyage-3`
* `voyage-3-lite`
* `voyage-large-2`
* `voyage-code-2`
* `voyage-2`
* `voyage-law-2`
* `voyage-large-2-instruct`
* `voyage-finance-2`
* `voyage-multilingual-2`

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
embeddings = VoyageAIEmbeddings(
    voyage_api_key="[ Your Voyage API key ]", model="voyage-law-2"
)
```

准备文档并使用 `embed_documents` 获取它们的嵌入向量。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
documents = [
    "缓存嵌入向量可以实现嵌入向量的存储或临时缓存，从而无需每次都重新计算它们。",
    "LLMChain 是一个组合了基本 LLM 功能的链。它由一个 PromptTemplate 和一个语言模型（LLM 或聊天模型）组成。它使用提供的输入键值（以及可用的记忆键值）格式化提示模板，将格式化后的字符串传递给 LLM 并返回 LLM 输出。",
    "Runnable 代表一个通用的工作单元，可以被调用、批处理、流式处理和/或转换。",
]
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
documents_embds = embeddings.embed_documents(documents)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
documents_embds[0][:5]
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[0.0562174916267395,
 0.018221192061901093,
 0.0025736060924828053,
 -0.009720131754875183,
 0.04108370840549469]
```

类似地，使用 `embed_query` 来嵌入查询。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
query = "What's an LLMChain?"
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
query_embd = embeddings.embed_query(query)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
query_embd[:5]
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
[-0.0052348352037370205,
 -0.040072452276945114,
 0.0033957737032324076,
 0.01763271726667881,
 -0.019235141575336456]
```

## 一个极简的检索系统

嵌入向量的主要特点是，两个嵌入向量之间的余弦相似度捕捉了相应原始段落的语义相关性。这使我们能够使用嵌入向量进行语义检索/搜索。

我们可以根据余弦相似度在文档嵌入向量中找到几个最接近的嵌入向量，并使用 LangChain 的 `KNNRetriever` 类检索相应的文档。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.retrievers import KNNRetriever

retriever = KNNRetriever.from_texts(documents, embeddings)

# 检索最相关的文档
result = retriever.invoke(query)
top1_retrieved_doc = result[0].page_content  # 返回检索到的第一条结果

print(top1_retrieved_doc)
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
An LLMChain is a chain that composes basic LLM functionality. It consists of a PromptTemplate and a language model (either an LLM or chat model). It formats the prompt template using the input key values provided (and also memory key values, if available), passes the formatted string to LLM and returns the LLM output.
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档连接](/use-these-docs)到 Claude、VSCode 等，通过 MCP 获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/embeddings/voyageai.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
