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

# AIMlAPIEmbeddings 集成

> 使用 LangChain Python 与 AIMlAPIEmbeddings 嵌入模型进行集成。

本指南将帮助您开始使用 LangChain 集成 AI/ML API 嵌入模型。

## 概述

### 集成详情

| 类                   | 包                                                                               | 本地支持 | JS 支持 |                                                 下载量                                                |                                                版本                                               |
| :------------------ | :------------------------------------------------------------------------------ | :--: | :---: | :------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------: |
| `AIMLAPIEmbeddings` | [`langchain-aimlapi`](https://reference.langchain.com/python/langchain-aimlapi) |   ❌  |   ❌   | ![PyPI - Downloads](https://img.shields.io/pypi/dm/langchain-aimlapi?style=flat-square\&label=%20) | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-aimlapi?style=flat-square\&label=%20) |

## 设置

要访问 AI/ML API 嵌入模型，您需要创建一个账户、获取 API 密钥，并安装 `langchain-aimlapi` 集成包。

### 凭证

前往 [aimlapi.com](https://aimlapi.com/app/?utm_source=langchain\&utm_medium=github\&utm_campaign=integration) 注册并生成 API 密钥。完成后，设置 `AIMLAPI_API_KEY` 环境变量：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import getpass
import os

if not os.getenv("AIMLAPI_API_KEY"):
    os.environ["AIMLAPI_API_KEY"] = getpass.getpass("Enter your AI/ML API key: ")
```

要启用模型调用的自动追踪，请设置您的 [LangSmith](/langsmith/home) API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

LangChain AI/ML API 集成位于 `langchain-aimlapi` 包中：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-aimlapi
```

## 实例化

现在我们可以实例化嵌入模型并执行嵌入操作：

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

embeddings = AIMLAPIEmbeddings(
    model="text-embedding-ada-002",
)
```

## 索引与检索

嵌入模型常用于检索增强生成（RAG）流程。以下是如何使用上面初始化的 `embeddings` 对象和 `InMemoryVectorStore` 进行数据索引和检索。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_core.vectorstores import InMemoryVectorStore

text = "LangChain is the framework for building context-aware reasoning applications"

vectorstore = InMemoryVectorStore.from_texts(
    [text],
    embedding=embeddings,
)

retriever = vectorstore.as_retriever()

retrieved_documents = retriever.invoke("What is LangChain?")
retrieved_documents[0].page_content
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
'LangChain is the framework for building context-aware reasoning applications'
```

## 直接使用

您可以直接调用 `embed_query` 和 `embed_documents` 以应对自定义嵌入场景。

### 嵌入单个文本

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
single_vector = embeddings.embed_query(text)
print(str(single_vector)[:100])
```

### 嵌入多个文本

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
text2 = "LangGraph is a library for building stateful, multi-actor applications with LLMs"

vectors = embeddings.embed_documents([text, text2])
for vector in vectors:
    print(str(vector)[:100])
```

***

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\embeddings\aimlapi.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
