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

# Moorcheh 集成

> 使用最大信息二值化（MIB）和信息论得分（ITS）的闪电般快速的语义搜索引擎和向量存储

# Moorcheh

[Moorcheh](https://www.moorcheh.ai/) 是一个闪电般快速的语义搜索引擎和向量存储。它不使用像 L2 或余弦相似度这样的简单距离度量，而是使用最大信息二值化（MIB）和信息论得分（ITS）来检索准确的文档块。

以下教程将指导您如何使用 Moorcheh 和 LangChain 来上传和存储文本文档及向量嵌入，并为您的所有查询检索相关块。

## 设置

首先，安装必要的软件包：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install langchain-moorcheh
```

## 初始化

开始使用 Moorcheh

1. 在 [Moorcheh 控制台](https://console.moorcheh.ai/) 注册或登录。
2. 转到“API 密钥”选项卡并生成一个 API 密钥。
3. 将密钥保存为名为 `MOORCHEH_API_KEY` 的环境变量（您将在下面使用它）。
4. 要创建用于存储数据的命名空间：
   * 在控制台中，打开“命名空间”选项卡并点击“创建命名空间”；或者
   * 使用下一节中的向量存储代码以编程方式初始化它。
5. 使用您的 API 密钥来创建命名空间、上传文档和检索答案。

有关 Moorcheh SDK 函数的更多信息，请参阅 [GitHub 仓库](https://github.com/moorcheh-ai/moorcheh-python-sdk)。

## 导入包

导入以下包：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_moorcheh import MoorchehVectorStore
from moorcheh_sdk import MoorchehClient

import logging
import os
from uuid import uuid4
import asyncio
from typing import Any, List, Optional, Literal, Tuple, Type, TypeVar, Sequence
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_core.vectorstores import VectorStore
from google.colab import userdata
```

## 代码设置

在环境变量中设置您的 Moorcheh API 密钥：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
MOORCHEH_API_KEY = os.environ['MOORCHEH_API_KEY']
```

设置您的命名空间名称、类型，并创建向量存储：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
namespace = "your_namespace_name"
namespace_type = "text" # 或 vector
store = MoorchehVectorStore(
            api_key=MOORCHEH_API_KEY,
            namespace=namespace,
            namespace_type=namespace_type
        )
```

## 添加文档

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
document_1 = Document(
    page_content="冲泡了一杯新鲜的埃塞俄比亚咖啡，并搭配了一个温热的羊角面包。",
    metadata={"source": "blog"},
)

document_2 = Document(
    page_content="明天的天气将是晴朗，有微风，最高气温将达到 78°F。",
    metadata={"source": "news"},
)

document_3 = Document(
    page_content="正在尝试使用 LangChain 构建一个 AI 驱动的笔记助手！",
    metadata={"source": "tweet"},
)

document_4 = Document(
    page_content="当地面包店向社区食品银行捐赠了 500 条面包。",
    metadata={"source": "news"},
)

document_5 = Document(
    page_content="昨晚的音乐会绝对令人难忘——多么精彩的表演！",
    metadata={"source": "tweet"},
)

document_6 = Document(
    page_content="查看我们的最新文章：在家办公时提高生产力的 5 种方法。",
    metadata={"source": "website"},
)

document_7 = Document(
    page_content="掌握自制披萨面团的终极指南。",
    metadata={"source": "website"},
)

document_8 = Document(
    page_content="LangGraph 让多智能体工作流变得简单多了——真的令人印象深刻！",
    metadata={"source": "tweet"},
)

document_9 = Document(
    page_content="由于主要出口国意外削减供应，今日油价上涨了 3%。",
    metadata={"source": "news"},
)

document_10 = Document(
    page_content="我真希望这篇帖子不会消失在数字虚空中……",
    metadata={"source": "tweet"},
)

documents = [
    document_1,
    document_2,
    document_3,
    document_4,
    document_5,
    document_6,
    document_7,
    document_8,
    document_9,
    document_10,
]

uuids = [str(uuid4()) for _ in range(len(documents))]

store.add_documents(documents=documents, ids=uuids)
```

## 删除文档

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
store.delete(ids=["chunk_id_here"])
```

## 查询引擎

一旦您的命名空间创建完毕并且您已将文档上传到其中，您就可以直接通过向量存储查询文档。设置您希望用于回答查询的查询内容和 LLM。有关支持的 LLM 的更多信息，请访问我们的 [GitHub 页面](https://github.com/moorcheh-ai/moorcheh-python-sdk)。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
query = "给我一个所提供文档的简要摘要"
answer = store.generative_answer(query, ai_model = "anthropic.claude-sonnet-4-6")
print(answer)
```

## 更多资源

有关 Moorcheh 的更多信息，请随时访问以下资源：

* [GitHub 页面](https://github.com/moorcheh-ai/moorcheh-python-sdk)
* [示例 GitHub 页面](https://github.com/moorcheh-ai/moorcheh-examples)
* [网站](https://www.moorcheh.ai/)
* [文档](https://console.moorcheh.ai/docs)
* [Youtube](https://www.youtube.com/@moorchehai)
* [X](https://x.com/moorcheh_ai)

***

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

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