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

# OpenGauss 向量存储集成

> 使用 LangChain Python 与 OpenGauss 向量存储集成。

本笔记本介绍如何开始使用 openGauss 向量存储。[openGauss](https://opengauss.org/en/) 是一款高性能关系型数据库，具备原生向量存储和检索能力。此集成使得 LangChain 应用能够进行符合 ACID 标准的向量操作，将传统 SQL 功能与现代 AI 驱动的相似性搜索相结合。
向量存储。

## 设置

### 启动 openGauss 容器

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
docker run --name opengauss \
  -d \
  -e GS_PASSWORD='MyStrongPass@123' \
  -p 8888:5432 \
  opengauss/opengauss-server:latest
```

### 安装 langchain-opengauss

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

**系统要求**：

* openGauss ≥ 7.0.0
* Python ≥ 3.8
* psycopg2-binary

### 凭证

使用您的 openGauss 凭证

## 初始化

<EmbeddingTabs />

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_opengauss import OpenGauss, OpenGaussSettings

# 使用模式验证进行配置
config = OpenGaussSettings(
    table_name="test_langchain",
    embedding_dimension=384,
    index_type="HNSW",
    distance_strategy="COSINE",
)
vector_store = OpenGauss(embedding=embeddings, config=config)
```

## 管理向量存储

### 向向量存储添加项目

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

document_1 = Document(page_content="foo", metadata={"source": "https://example.com"})

document_2 = Document(page_content="bar", metadata={"source": "https://example.com"})

document_3 = Document(page_content="baz", metadata={"source": "https://example.com"})

documents = [document_1, document_2, document_3]

vector_store.add_documents(documents=documents, ids=["1", "2", "3"])
```

### 更新向量存储中的项目

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
updated_document = Document(
    page_content="qux", metadata={"source": "https://another-example.com"}
)

# 如果 id 已存在，将更新文档
vector_store.add_documents(document_id="1", document=updated_document)
```

### 从向量存储中删除项目

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

## 查询向量存储

一旦您的向量存储创建完成并添加了相关文档，您很可能希望在链或代理运行期间对其进行查询。

### 直接查询

执行简单的相似性搜索可以按如下方式进行：

* TODO: 编辑并运行代码单元格以生成输出

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
results = vector_store.similarity_search(
    query="thud", k=1, filter={"source": "https://another-example.com"}
)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
```

如果您想执行相似性搜索并接收相应的分数，可以运行：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
results = vector_store.similarity_search_with_score(
    query="thud", k=1, filter={"source": "https://example.com"}
)
for doc, score in results:
    print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
```

### 通过转换为检索器进行查询

您也可以将向量存储转换为检索器，以便在您的链中更轻松地使用。

* TODO: 编辑并运行代码单元格以生成输出

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
retriever = vector_store.as_retriever(search_type="mmr", search_kwargs={"k": 1})
retriever.invoke("thud")
```

## 用于检索增强生成的用法

有关如何将此向量存储用于检索增强生成 (RAG) 的指南，请参阅以下部分：

* [教程](/oss/python/langchain/rag)
* [操作指南：使用 RAG 进行问答](https://python.langchain.com/docs/how_to/#qa-with-rag)
* [检索概念文档](https://python.langchain.com/docs/concepts/retrieval/)

## 配置

### 连接设置

| 参数                    | 默认值                     | 描述                                                                                                                               |
| --------------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `host`                | localhost               | 数据库服务器地址                                                                                                                         |
| `port`                | 8888                    | 数据库连接端口                                                                                                                          |
| `user`                | gaussdb                 | 数据库用户名                                                                                                                           |
| `password`            | -                       | 复杂密码字符串                                                                                                                          |
| `database`            | postgres                | 默认数据库名称                                                                                                                          |
| `min_connections`     | 1                       | 连接池最小大小                                                                                                                          |
| `max_connections`     | 5                       | 连接池最大大小                                                                                                                          |
| `table_name`          | langchain\_docs         | 存储向量数据和元数据的表名                                                                                                                    |
| `index_type`          | IndexType.HNSW          | 向量索引算法类型。选项：HNSW 或 IVFFLAT\n默认为 HNSW。                                                                                            |
| `vector_type`         | VectorType.vector       | 要使用的向量表示类型。默认为 Vector。                                                                                                           |
| `distance_strategy`   | DistanceStrategy.COSINE | 用于检索的向量相似性度量。选项：euclidean（L2 距离）、cosine（角度距离，适用于文本嵌入）、manhattan（L1 距离，适用于稀疏数据）、negative\_inner\_product（归一化向量的点积）。\n 默认为 cosine。 |
| `embedding_dimension` | 1536                    | 向量嵌入的维度。                                                                                                                         |

### 支持的组合

| 向量类型   | 维度    | 索引类型         | 支持的距离策略                                |
| ------ | ----- | ------------ | -------------------------------------- |
| vector | ≤2000 | HNSW/IVFFLAT | COSINE/EUCLIDEAN/MANHATTAN/INNER\_PROD |

## 性能优化

### 索引调优指南

**HNSW 参数**：

* `m`：16-100（召回率与内存之间的平衡）
* `ef_construction`：64-1000（必须 > 2\*m）

**IVFFLAT 建议**：

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

lists = min(
    int(math.sqrt(total_rows)) if total_rows > 1e6 else int(total_rows / 1000),
    2000,  # openGauss 最大值
)
```

### 连接池

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
OpenGaussSettings(min_connections=3, max_connections=20)
```

## 限制

* `bit` 和 `sparsevec` 向量类型目前仍在开发中
* 最大向量维度：`vector` 类型为 2000

***

***

<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/vectorstores/opengauss.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
