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

# BasetenEmbeddings 集成

> 使用 LangChain 集成 BasetenEmbeddings 嵌入模型。

这将帮助您使用LangChain开始基于Baseten嵌入模型的开发。

## 概览

Baseten为生产级应用设计了推理服务。基于Baseten推理栈，这些API为领先的开源模型或自定义模型提供企业级性能和可靠性：[https://www.baseten.co/library/。](https://www.baseten.co/library/。)

### 集成细节

## 配置

要访问Baseten嵌入模型，您需要创建Baseten账户、获取API密钥，并安装 `langchain-baseten` 集成包。

**Baseten嵌入模型仅提供为专用模型部署。** 在使用本集成之前，必须先从 [Baseten 模型库](https://www.baseten.co/library/tag/embedding) 部署一个嵌入模型。

嵌入功能使用Baseten的 [性能客户端](https://www.baseten.co/blog/introducing-baseten-embeddings-inference-bei/) 进行优化，该客户端作为依赖项自动包含。

### 凭据

前往 [https://app.baseten.co](https://app.baseten.co) 注册Baseten并生成API密钥。配置 BASETEN\_API\_KEY 环境变量：

```python theme={null}
import getpass
import os

if not os.getenv("BASETEN_API_KEY"):
    os.environ["BASETEN_API_KEY"] = getpass.getpass("Enter your Baseten API key: ")
```

要启用对模型调用的自动化追踪，设置您的 [LangSmith](https://docs.langchain.com/langsmith/observability) API密钥：

```python theme={null}
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
```

### 安装

LangChain Baseten集成位于 `langchain-baseten` 包中：

```python theme={null}
pip install -qU langchain-baseten
```

## 初始化

现在可以使用已部署模型URL初始化嵌入对象：

```python theme={null}
from langchain_baseten import BasetenEmbeddings

embeddings = BasetenEmbeddings(
    model_url="https://model-<id>.api.baseten.co/environments/production/sync",  # Your model URL
    api_key="your-api-key",  # Or set BASETEN_API_KEY env var
)
```

## 索引与检索

嵌入模型常用于检索增强生成（RAG）流程，既用于数据索引也用于后续检索。更详细的操作请参考我们的 [RAG教程](https://langchain-docs-zh.mintlify.site/langchain/oss/deepagents/rag.md)。

以下是如何使用我们上面初始化的 `embeddings` 对象进行索引和检索数据的示例。本例将索引并检索 `InMemoryVectorStore` 中的样本文档。

```python theme={null}
# Create a vector store with a sample text
from langchain_core.vectorstores import InMemoryVectorStore

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

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

# Use the vectorstore as a retriever
retriever = vectorstore.as_retriever()

# Retrieve the most similar text
retrieved_documents = retriever.invoke("What is LangChain?")

# show the retrieved document's content
retrieved_documents[0].page_content
```

```text theme={null}
'LangChain is the framework for building context-aware reasoning applications'
```

## 直接使用

底层，vectorstore和retriever实现通过 `embeddings.embed_documents(...)` 和 `embeddings.embed_query(...)` 分别为 `from_texts` 和检索 `invoke` 操作生成嵌入。

您可以直接调用这些方法实现自定义嵌入需求。

### 单文本嵌入

可以使用 `embed_query` 嵌入单文本或文档：

```python theme={null}
single_vector = embeddings.embed_query(text)
print(str(single_vector)[:100])  # Show the first 100 characters of the vector
```

```text theme={null}
[0.013201533816754818, 0.02222288027405739, -0.036066457629203796, 0.027374643832445145, -0.01692997
```

### 多文本嵌入

可以使用 `embed_documents` 嵌入多文本：

```python theme={null}
text2 = (
    "LangGraph is a library for building stateful, multi-actor applications with LLMs"
)
two_vectors = embeddings.embed_documents([text, text2])
for vector in two_vectors:
    print(str(vector)[:100])  # Show the first 100 characters of the vector
```

```text theme={null}
[0.013201533816754818, 0.02222288027405739, -0.036066457629203796, 0.027374643832445145, -0.01692997
[0.018247194588184357, 0.007369577884674072, -0.005529594141989946, 0.022589316591620445, -0.0699259
```

***


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.