导读
登录 TiDB Cloud 并创建 cluster
选择 Serverless并设置 Region 为 Frankfurt (eu-central-1)
开启 Vector Search 并设置集群名
创建集群

create schema dify;
复制

# 将 VECTOR_STORE 修改为 tidb_vector,文件中的默认值是 weaviate
VECTOR_STORE: tidb_vector
# 将以下配置改为保存好的连接 TiDB 配置
TIDB_VECTOR_HOST: xxx.eu-central-1.prod.aws.tidbcloud.com
TIDB_VECTOR_PORT: 4000
TIDB_VECTOR_USER: xxx.root
TIDB_VECTOR_PASSWORD: xxxxxx
TIDB_VECTOR_DATABASE: dify复制
docker compose up -d
复制



向量检索:基于 ANN 查询的检索,Reranker 模型为可选
全文检索:基于 BM25 检索,Reranker 模型为可选(目前 TiDB Vector 类型未支持)
混合(向量+全文):ANN + BM25 检索,Reranker 模型为必选


CREATE TABLE IF NOT EXISTS ${collection_name} (
# id: 这里的id是在 Dify 中生成 uuid
id CHAR(36) PRIMARY KEY,
# text: 分片后的文本内容
text TEXT NOT NULL,
# meta: 元数据,记录数据集id、文档id、知识库id等,用于条件查询
meta JSON NOT NULL,
# vector: 分片向量,需要设置向量维度
vector VECTOR<FLOAT>(${dimension}) NOT NULL COMMENT "hnsw(distance=${distance_func})",
create_time DATETIME DEFAULT CURRENT_TIMESTAMP,
update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);复制
${dimension} 表示向量的维度,这个取决于选择的 Embedding 模型。
${distance_func} 表示用户设置的距离度量方法,目前支持的值有cosine 和 l2 ,目前仅支持cosine。
SELECT meta, text FROM (
SELECT meta, text, ${tidb_func}(vector, "${query_vector}") as distance
FROM ${collection_name}
ORDER BY distance
LIMIT ${top_k}
) t WHERE distance < ${distance};复制
${query_vector} 表示查询向量,即用户问题向量化后的结果
${tidb_func} 表示 TiDB Vector 中支持的向量距离度量防范,目前支持的方法有 Vec_Cosine_Distance 和 Vec_l2_Distance
${top_k} 表示结果 TopK 的具体个数
${distance} 表示向量库中的节点离查询节点的距离,Dify 知识库可以设置距离/分数阈值
[2]LangChain: https://python.langchain.com/v0.1/docs/integrations/vectorstores/tidb_vector/
[3]LlamaIndex: https://docs.llamaindex.ai/en/stable/examples/vector_stores/TiDBVector/
[4]TiDB Cloud: https://www.notion.so/TiDB-Vector-Dify-AI-Agent-06b03cc8eaff434fa3064d3a320f3440?pvs=21
[5]官方文档: https://github.com/langgenius/dify/blob/main/README_CN.md
[6]weaviate: https://weaviate.io/
[7]GIthub Release: https://github.com/langgenius/dify/releases
[8]这里: https://docs.dify.ai/v/zh-hans/getting-started/install-self-hosted/docker-compose
[9]开源代码库:https://github.com/langgenius/dify/blob/main/docker/docker-compose.yaml
[10]通义千问: https://help.aliyun.com/zh/dashscope/developer-reference/generic-text-vector/?spm=a2c4g.11186623.0.0.2b31696bUwAwpF
[11]MINIMAX: https://platform.minimaxi.com/document/guides/Embeddings
[12]JINA: https://help.aliyun.com/zh/dashscope/developer-reference/generic-text-vector/?spm=a2c4g.11186623.0.0.2b31696bUwAwpF
[13]JINA: https://jina.ai/reranker
[14]Cohere: https://docs.cohere.com/docs/rerank-2
[15]官方文档: https://docs.dify.ai/v/zh-hans/guides/knowledge-base/integrate_knowledge_within_application
[16]文档: Vector Search Indexes in TiDB: https://docs.google.com/document/d/15eAO0xrvEd6_tTxW_zEko4CECwnnSwQg8GGrqK1Caiw/edit
[17]Dify 中文文档: https://docs.dify.ai/v/zh-hans
[18]LangChain - TiDB Vector 文档: https://python.langchain.com/v0.1/docs/integrations/vectorstores/tidb_vector/
[19]LlamaIndex - TiDB Vector Store 文档: https://docs.llamaindex.ai/en/stable/examples/vector_stores/TiDBVector/
[20]tidb-vector-python 源码: https://github.com/pingcap/tidb-vector-python
[21]Dify 源码: https://github.com/langgenius/dify
[22]langchain - tidb_vector 源码: https://github.com/langchain-ai/langchain/blob/master/libs/community/langchain_community/vectorstores/tidb_vector.py
[23]llama-index-vector-stores-tidbvector 源码: https://github.com/run-llama/llama_index/tree/main/llama-index-integrations/vector_stores/llama-index-vector-stores-tidbvector
/ 相关推荐 /
PingCAP AI Lab 负责人李粒:TiDB 如何利用数据库 Copilot 优化数据库操作,提升用户体验与内部效率?

文章转载自PingCAP,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
史诗级革新 | Apache Flink 2.0 正式发布
严少安
185次阅读
2025-03-25 00:55:05
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
159次阅读
2025-04-14 09:40:20
TiDB 社区第四届专栏征文大赛联合墨天轮火热开启!TiDB 业务场景实战、运维开发攻略两大赛道,重磅礼品等你来挑战!
墨天轮编辑部
132次阅读
2025-04-15 17:01:41
PingCAP 2025 校园招聘 | 开源领航,星耀前程
PingCAP
124次阅读
2025-03-27 09:55:55
2025w11 | 图数据库了解一下
严少安
81次阅读
2025-03-21 16:51:24
从Oracle和TiDB的HTAP说起
薛晓刚
67次阅读
2025-04-07 19:09:00
postgres 向量检索插件
遗失的猫
57次阅读
2025-03-19 17:25:53
国产数据库如何赋能智慧医疗?3.28 TiDB 与您相约宜昌 “医院云数智技术实践研讨及成果展示交流会”
PingCAP
50次阅读
2025-03-25 18:14:39
国产数据库再获资本赋能,「九有数据库」完成A轮融资
严少安
45次阅读
2025-03-31 13:00:54
TiDB × AI :DeepSeek 时代你需要什么样的数据基座
PingCAP
37次阅读
2025-03-21 10:33:31