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

# Valkey

> [Valkey](https://valkey.io/) 是一个开源、高性能的键值数据存储，支持缓存、消息队列等工作负载，也可作为主数据库使用。Valkey 可以作为独立守护进程运行，也可以在集群中运行，并提供复制和高可用性选项。

本页介绍如何将 Valkey 向量存储与 [Amazon ElastiCache for Valkey](https://aws.amazon.com/elasticache/valkey/) 或 [Amazon MemoryDB for Valkey](https://aws.amazon.com/memorydb/) 结合使用。

## 设置

安装所需的依赖项：

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install "langchain-aws[valkey]"
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add langchain-aws --extra valkey
  ```
</CodeGroup>

<Note>
  Valkey 集成需要 `langchain-aws>=1.5.0`。如果您使用的是早期版本，请直接安装依赖项：

  ```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install langchain-aws valkey-glide-sync
  ```
</Note>

## 基本用法

### 使用 Bedrock 嵌入

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws import BedrockEmbeddings
from langchain_aws.vectorstores import ValkeyVectorStore

# 初始化嵌入
embeddings = BedrockEmbeddings(
    model_id="amazon.titan-embed-text-v1",
    region_name="us-east-1"
)

# 从文本创建向量存储
vectorstore = ValkeyVectorStore.from_texts(
    texts=["Valkey is fast", "Valkey supports vector search"],
    embedding=embeddings,
    valkey_url="valkey://localhost:6379",
    index_name="my_index"
)

# 执行相似性搜索
results = vectorstore.similarity_search("fast database", k=2)
for doc in results:
    print(doc.page_content)
```

### 使用 Ollama 嵌入

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_ollama import OllamaEmbeddings
from langchain_aws.vectorstores import ValkeyVectorStore

# 初始化 Ollama 嵌入
embeddings = OllamaEmbeddings(
    model="nomic-embed-text",
    base_url="http://localhost:11434"
)

# 创建向量存储
vectorstore = ValkeyVectorStore(
    embedding=embeddings,
    valkey_url="valkey://localhost:6379",
    index_name="my_index",
    vector_schema={
        "name": "content_vector",
        "algorithm": "FLAT",
        "dims": 768,  # nomic-embed-text 维度
        "distance_metric": "COSINE",
        "datatype": "FLOAT32",
    }
)

# 添加文本
vectorstore.add_texts(
    texts=["Document 1", "Document 2"],
    metadatas=[{"source": "doc1"}, {"source": "doc2"}]
)

# 搜索
results = vectorstore.similarity_search("query", k=2)
```

## 连接 URL

ValkeyVectorStore 支持多种连接 URL 格式：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 独立模式
valkey_url = "valkey://localhost:6379"

# 带身份验证
valkey_url = "valkey://username:password@host:6379"

# SSL/TLS
valkey_url = "valkeyss://host:6379"

# 带身份验证的 SSL
valkey_url = "valkeyss://username:password@host:6379"
```

## AWS ElastiCache for Valkey

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws import BedrockEmbeddings
from langchain_aws.vectorstores import ValkeyVectorStore

embeddings = BedrockEmbeddings()

# 连接到 ElastiCache 集群
vectorstore = ValkeyVectorStore(
    embedding=embeddings,
    valkey_url="valkeyss://my-cluster.cache.amazonaws.com:6379",
    index_name="my_index"
)

# 添加文档
vectorstore.add_texts(
    texts=["Document 1", "Document 2"],
    metadatas=[{"source": "doc1"}, {"source": "doc2"}]
)
```

## 元数据过滤

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws.vectorstores.valkey.filters import ValkeyTag, ValkeyNum

# 添加带元数据的文档
vectorstore.add_texts(
    texts=["AI article from 2024", "ML paper from 2023"],
    metadatas=[
        {"category": "ai", "year": 2024},
        {"category": "ml", "year": 2023}
    ]
)

# 使用过滤器搜索
filter_expr = (ValkeyTag("category") == "ai") & (ValkeyNum("year") >= 2024)
results = vectorstore.similarity_search(
    "artificial intelligence",
    k=5,
    filter=str(filter_expr)
)
```

## 自定义向量模式

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

vectorstore = ValkeyVectorStore(
    embedding=embeddings,
    valkey_url="valkey://localhost:6379",
    index_name="my_index",
    vector_schema={
        "name": "content_vector",
        "algorithm": "HNSW",  # 或 "FLAT"
        "dims": 1536,
        "distance_metric": "COSINE",  # 或 "L2", "IP"
        "datatype": "FLOAT32",
    }
)
```

## API 参考

有关详细的 API 文档，请参阅 [`ValkeyVectorStore`](https://reference.langchain.com/python/langchain-aws/vectorstores/valkey/base/ValkeyVectorStore)。

***

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