> ## 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.

# LangSmithLoader 集成

> 使用 LangChain Python 与 LangSmithLoader 文档加载器集成

本指南为您提供快速入门指南，以使用 `LangSmithLoader` [document loader](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/document_loaders.md)。如需了解所有 `LangSmithLoader` 功能和配置的详细文档，请访问 [API reference](https://reference.langchain.com/python/langchain-core/document_loaders/langsmith/LangSmithLoader)。

## 概览

### 集成细节

| 类 | 包 | 本地 | 可序列化 | JS 支持 |
| :- | :- | :-: | :-: | :-: |
| `LangSmithLoader` | [`langchain-core`](https://reference.langchain.com/python/langchain-core) | ❌ | ❌ | ❌ |

### 加载器特性

| 来源 | 延迟加载 | 原生异步 |
| :-: | :-: | :-: |
| `LangSmithLoader` | ✅ | ❌ |

## 配置

要访问 LangSmith 文档加载器，您需要安装 `langchain-core`，创建 [LangSmith](https://langsmith.com) 账户，并获取 API 密钥。

### 凭据

在 [langsmith.com](https://langsmith.com) 注册并生成 API 密钥。完成此操作后，设置 LANGSMITH\_API\_KEY 环境变量：

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

if not os.environ.get("LANGSMITH_API_KEY"):
    os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
```

若您希望启用 LangSmith 的自动化最佳实践追踪，还可以开启 LangSmith 追踪：

```python theme={null}
os.environ["LANGSMITH_TRACING"] = "true"
```

### 安装

安装 `langchain-core`：

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

### 克隆示例数据集

本例中，我们将克隆并加载一个公开的 LangSmith 数据集。克隆会在您的 LangSmith 账户上创建该数据集的副本。您只能加载您拥有个人副本的数据集。

```python theme={null}
from langsmith import Client as LangSmithClient

ls_client = LangSmithClient()

dataset_name = "LangSmith Few Shot Datasets Notebook"
dataset_public_url = (
    "https://smith.langchain.com/public/55658626-124a-4223-af45-07fb774a6212/d"
)

ls_client.clone_public_dataset(dataset_public_url)
```

## 初始化

现在我们可以实例化文档加载器并加载数据：

```python theme={null}
from langchain_core.document_loaders import LangSmithLoader

loader = LangSmithLoader(
    dataset_name=dataset_name,
    content_key="question",
    limit=50,
    # format_content=...,
    # ...
)
```

## 加载

```python theme={null}
docs = loader.load()
print(docs[0].page_content)
```

```text theme={null}
Show me an example using Weaviate, but customizing the vectorStoreRetriever to return the top 10 k nearest neighbors.
```

```python theme={null}
print(docs[0].metadata["inputs"])
```

```python theme={null}
{'question': 'Show me an example using Weaviate, but customizing the vectorStoreRetriever to return the top 10 k nearest neighbors. '}
```

```python theme={null}
print(docs[0].metadata["outputs"])
```

```python theme={null}
{'answer': 'To customize the Weaviate client and return the top 10 k nearest neighbors, you can utilize the `as_retriever` method with the appropriate parameters. Here\'s how you can achieve this:\n\n\`\`\`python\n# Assuming you have imported the necessary modules and classes\n\n# Create the Weaviate client\nclient = weaviate.Client(url=os.environ["WEAVIATE_URL"], ...)\n\n# Initialize the Weaviate wrapper\nweaviate = Weaviate(client, index_name, text_key)\n\n# Customize the client to return top 10 k nearest neighbors using as_retriever\ncustom_retriever = weaviate.as_retriever(\n    search_type="similarity",\n    search_kwargs={\n        \'k\': 10  # Customize the value of k as needed\n    }\n)\n\n# Now you can use the custom_retriever to perform searches\nresults = custom_retriever.search(query, ...)\n\`\`\`'}
```

```python theme={null}
list(docs[0].metadata.keys())
```

```python theme={null}
['dataset_id',
 'inputs',
 'outputs',
 'metadata',
 'id',
 'created_at',
 'modified_at',
 'runs',
 'source_run_id']
```

## 延迟加载

```python theme={null}
page = []
for doc in loader.lazy_load():
    page.append(doc)
    if len(page) >= 10:
        # do some paged operation, e.g.
        # index.upsert(page)
        # page = []
        break
len(page)
```

```text theme={null}
10
```

***


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