数码印花图案设计网站,制作网站需要多少时间,网站建设设计服务公司,手机无法访问wordpress为什么尝试使用Tencent Cloud VectorDB替换Milvus向量库#xff1f;
亮点#xff1a;Tencent Cloud VectorDB支持Embedding#xff0c;免去自己搭建模型的负担#xff08;搭建一个生产环境的模型实在耗费精力和体力#xff09;。
腾讯云向量数据库是什么#xff1f;
腾…为什么尝试使用Tencent Cloud VectorDB替换Milvus向量库
亮点Tencent Cloud VectorDB支持Embedding免去自己搭建模型的负担搭建一个生产环境的模型实在耗费精力和体力。
腾讯云向量数据库是什么
腾讯云向量数据库是一款全托管的自研企业级分布式数据库服务专用于存储、检索、分析多维向量数据。该数据库支持多种索引类型和相似度计算方法单索引支持10亿级向量规模可支持百万级 QPS 及毫秒级查询延迟。腾讯云向量数据库不仅能为大模型提供外部知识库提高大模型回答的准确性还可广泛应用于推荐系统、NLP 服务、计算机视觉、智能客服等 AI 领域。
Milvus是什么
Milvus是在2019年创建的其唯一目标是存储、索引和管理由深度神经网络和其他机器学习ML模型生成的大规模嵌入向量。作为一个专门设计用于处理输入向量查询的数据库它能够处理万亿级别的向量索引。与现有的关系型数据库主要处理遵循预定义模式的结构化数据不同Milvus从底层设计用于处理从非结构化数据转换而来的嵌入向量。
项目展示 项目介绍游戏内部接入ChatGPT的智能NPC可以与她进行语音交流。可以回答与游戏相关的问题这个专业问题是为了编写这个文章专门添加到问答缓存库中的游戏内会拒绝回答此类问题。为了加快ChatGPT的回复速度和降低ChatGPT的费用增加问答缓存机制。这里运用向量数据库的相似文本相似度高的特性通过向量搜索匹配相似度大于一定值例如0.95。搜索到相似问题直接返回答案不在进行ChatGPT访问。
其次存在缓存针对相似问题还可以给予特定回复答案。例如上面示例当提问“介绍一下腾讯向量数据库”直接回复“腾讯云向量数据库是一款全托管的自研企业级分布式数据库服务专用于存储、检索、分析多维向量数据。该数据库支持多种索引类型和相似度计算方法单索引支持10亿级向量规模可支持百万级 QPS 及毫秒级查询延迟。腾讯云向量数据库不仅能为大模型提供外部知识库提高大模型回答的准确性还可广泛应用于推荐系统、NLP 服务、计算机视觉、智能客服等 AI 领域。”
为什么使用向量数据库
重点速度 向量相似度匹配是很长的数组例如bge-large-zh模型文本转向量生成的是768维的float数组。拿问题文本转换为的768维向量与缓存的所有问题的向量进行相似性计算然后获取最相似的几条数据这个运算量非常大速度非常慢。 测试代码 与300个768维向量进行相似比对获取最相似的一条数据耗时几秒钟。按照这个速度如果与几千上万条数据进行这么计算简直无法忍受。 这时就必须使用向量数据库了向量数据库可以支持毫秒级检索上百万行数据。本人曾使用Milvus数据库分别插入1000行数数据和插入10万行数据然后进行搜索对比都在几十毫秒返回结果数据量的增多对检索速度几乎没有任何影响。
本项目哪里需要使用向量数据库
玩家提问玩家提问先通过embedding转换为向量在向量库检索相似的问题满足匹配条件直接返回对应的答案。后台相似问题检索后台通过向量检索相似问题以便对特定问题进行增删改查。
使用腾讯云向量数库Tencent Cloud VectorDB的优点
支持Embedding腾讯云向量数据库Tencent Cloud VectorDB提供将非结构化数据转换为向量数据的能力目前已支持文本 Embedding 模型能够覆盖多种主流语言的向量转换包括但不限于中文、英文。对于小型项目这是一个非常大的优势。可以降低自己搭建embedding模型或者使用第三方embedding模型的成本。FilterIndex的field_type支持数据类型简单只有String和Uint64使用起来非常省心。而Milvus数据支持10几种类型对于初学者不友好还要研究具体如何使用。 指定 Filter 字段的数据类型。取值如下 String字符型。若 name 为 id则该参数固定为 FieldType.String。 Uint64指无符号整数该参数可设置为 FieldType.Uint64。 研究Tencent Cloud VectorDB测试并封装代码库my_tc_vector_db.py
if __name__ __main__:# 初始化myTcVectorDB MyTcVectorDB(http://****************.tencentclb.com:30000, root,2epSOV3HK6tiyALo6UqE3mGV**************)# 删除数据库myTcVectorDB.drop_collection(db-qa, question_768)myTcVectorDB.drop_database(db-qa)# 创建数据库myTcVectorDB.create_database(db-qa)# 创建索引和embedding并创建集合index Index(FilterIndex(nameid, field_typeFieldType.String, index_typeIndexType.PRIMARY_KEY),FilterIndex(namequestion, field_typeFieldType.String, index_typeIndexType.FILTER),VectorIndex(namevector, dimension768, index_typeIndexType.HNSW,metric_typeMetricType.COSINE, paramsHNSWParams(m16, efconstruction200)))embedding Embedding(vector_fieldvector, fieldtext, modelEmbeddingModel.BGE_BASE_ZH)collection myTcVectorDB.create_collection(db-qa, question_768, index, embedding)# 批量插入myTcVectorDB.upsert(db-qa, question_768, [Document(id0001, text罗贯中, question罗贯中),Document(id0002, text吴承恩, question吴承恩),Document(id0003, text曹雪芹, question曹雪芹),Document(id0004, text郭富城, question郭富城)])# 单条插入myTcVectorDB.upsert_one(db-qa, question_768, id0005, text周杰伦, question周杰伦)myTcVectorDB.upsert_one(db-qa, question_768, id0006, text林俊杰, question林俊杰)# 删除0003myTcVectorDB.delete_by_id(db-qa, question_768, 0003)# 文本搜索无需向量转换text myTcVectorDB.search_by_text(db-qa, question_768, 郭富城)# 打印结果print_object(text)# 仅打印idif len(text[0]) 0:for i in text[0]:print(i[id])解释代码功能 初始化传入tcVectorDB的url、username和key创建myTcVectorDB. 删除数据库db-qa下的数据集question_768然后删除数据库db-qa 重新创建数据库db-qa 指定索引和embedding并创建集合question_768这里指定id为主键、question为FilterIndex标量索引vector为VectorIndex向量索引注意官方文档说明指定向量索引字段名固定为 vector。因为使用中文检索Embedding使用BGE_BASE_ZH。 批量插入测试数据 单行插入测试数据 测试删除单行数据 测试文本搜索并打印结果
MyTcVectorDB库代码
import jsonimport tcvectordb
from tcvectordb.model.collection import Embedding
from tcvectordb.model.document import Document, SearchParams
from tcvectordb.model.enum import ReadConsistency, MetricType, FieldType, IndexType, EmbeddingModel
from tcvectordb.model.index import Index, FilterIndex, VectorIndex, HNSWParamsclass MyTcVectorDB:def __init__(self, url: str, username: str, key: str, timeout: int 30):self._client tcvectordb.VectorDBClient(urlurl, usernameusername, keykey,read_consistencyReadConsistency.EVENTUAL_CONSISTENCY, timeouttimeout)def create_database(self, database_name: str):Create a database:param database_name: database name:return: databasereturn self._client.create_database(database_namedatabase_name)def drop_database(self, database_name: str):Drop a database:param database_name: database name:return: resultreturn self._client.drop_database(database_namedatabase_name)def create_collection(self, db_name: str, collection_name: str, index: Index, ebd: Embedding):db self._client.database(db_name)# 第二步创建 Collectioncoll db.create_collection(namecollection_name,shard1,replicas0,descriptionthis is a collection of question embedding,indexindex,embeddingebd)return colldef drop_collection(self, db_name: str, collection_name: str):Drop a collection:param db_name: db name:param collection_name: collection name:return: resultdb self._client.database(db_name)return db.drop_collection(collection_name)def upsert_one(self, db_name: str, collection_name: str, **kwargs):Upsert one document to collection:param db_name : db name:param collection_name: collection name:param document: Document:return: resultdb self._client.database(db_name)coll db.collection(collection_name)res coll.upsert(documents[Document(**kwargs)])return resdef upsert(self, db_name: str, collection_name: str, documents):Upsert documents to collection:param db_name : db name:param collection_name: collection name:param documents: list of Document:return: resultdb self._client.database(db_name)coll db.collection(collection_name)res coll.upsert(documentsdocuments)return resdef search_by_text(self, db_name: str, collection_name: str, text: str, limit: int 10):Search documents by text:param db_name : db name:param collection_name: collection name:param text: text:return: resultdb self._client.database(db_name)coll db.collection(collection_name)# searchByText 返回类型为 Dict接口查询过程中 embedding 可能会出现截断如发生截断将会返回响应 warn 信息如需确认是否截断可以# 使用 warning 作为 key 从 Dict 结果中获取警告信息查询结果可以通过 documents 作为 key 从 Dict 结果中获取res coll.searchByText(embeddingItems[text],paramsSearchParams(ef200),limitlimit)return res.get(documents)def delete_by_id(self, db_name: str, collection_name: str, document_id):Delete document by id:param db_name : db name:param collection_name: collection name:param document_id: document id:return: resultdb self._client.database(db_name)coll db.collection(collection_name)res coll.delete(document_ids[document_id])return resdef print_object(obj):Print objectfor elem in obj:# ensure_asciiFalse 保证中文不乱码if hasattr(elem, __dict__):print(json.dumps(vars(elem), indent4, ensure_asciiFalse))else:print(json.dumps(elem, indent4, ensure_asciiFalse))开始动手使用Tencent Cloud VectorDB在项目中替换Milvus
1、创建问题库db-qa和集合question_768
与测试代码基本一致 # 初始化myTcVectorDB MyTcVectorDB(http://****tencentclb.com:30000, root,2epSOV3HK6tiyALo6UqE3mGVMbpP*******)# 创建数据库myTcVectorDB.create_database(db-qa)# 创建索引和embedding并创建集合index Index(FilterIndex(nameid, field_typeFieldType.String, index_typeIndexType.PRIMARY_KEY),FilterIndex(namequestion, field_typeFieldType.String, index_typeIndexType.FILTER),VectorIndex(namevector, dimension768, index_typeIndexType.HNSW,metric_typeMetricType.COSINE, paramsHNSWParams(m16, efconstruction200)))embedding Embedding(vector_fieldvector, fieldtext, modelEmbeddingModel.BGE_BASE_ZH)collection myTcVectorDB.create_collection(db-qa, question_768, index, embedding)2、游戏端和后台文本向量搜索用MyTcVectorDB替换Milvus
两处代码基本一致。这里去掉文本转向量的步骤因为TcVectorDB支持Embedding # 获取问题转换后的向量# success, vector get_vector_from_text(question)# if not success:# return {code: -1, id: 0, answer: 向量计算失败}# results questionCollection.search(vector, limit)results myVectorDB.search_by_text(db-qa, question_768, question, limit)...上面代码需要注意一点腾讯向量数据的search结果与milvus的搜索结果是不一样的需要做一下适配。
3、重建向量数据库
问答缓存的数据保存在mysql数据库向量数据库主要作用是向量搜索。如果更换向量库只需要重建向量库即可。下面代码
从mysql中获取所有的问题遍历所有问答把问题作为向量索引问答的id为标量索引插入向量库中 当前mysql数据库中有大几千条数据重新构建向量就耗时10分钟左右。
def rebuild_vector():# 查找所有的数据select_all qaTable.select_all_qa()# 遍历所有的数据for qa in select_all:insertId qa[0]question qa[1]timestamp int(time.time())print(question)# 计算向量# 更新向量# success, vector get_vector_from_text(question)# if not success:# # 向量计算失败,question# logging.error(向量计算失败,insertId:%s, question:%s, insertId, question)# continue# # 删除原有的向量# questionCollection.delete_question(insertId)# # 插入新的向量# questionCollection.insert_question(insertId, vector, question, timestamp)myVectorDB.delete_by_id(db-qa, question_768, str(insertId))myVectorDB.upsert_one(db-qa, question_768, idstr(insertId), textquestion, questionquestion)return 重建向量库成功4、修改后台展示看下修改后的效果图
使用的文本转向量的模型是BGE_BASE_ZH向量索引是VectorIndex(name‘vector’, dimension768, index_typeIndexType.HNSW, metric_typeMetricType.COSINE, paramsHNSWParams(m16, efconstruction200))搜索文本返回结果代表的是相似度保存在score中。
总结
使用腾讯向量数据库要比使用Milvus更加简单易用无需自己部署服务器。腾讯云向量库支持主流Embedding直接支持文本向量搜索避免自己部署Embedding模型并避免调用文本转向量的过程。对于开发者来说非常便利。 如果是个人或者小型项目开发非常值得使用腾讯云数据库。如果是大型项目不缺钱的话也非常推荐使用腾讯云数据库稳定、高效且安全。 文章转载自: http://www.morning.zlcsz.cn.gov.cn.zlcsz.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.mlcwl.cn.gov.cn.mlcwl.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.ptwrz.cn.gov.cn.ptwrz.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.qflcb.cn.gov.cn.qflcb.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.pbmkh.cn.gov.cn.pbmkh.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.ybmp.cn.gov.cn.ybmp.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.nngq.cn.gov.cn.nngq.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.jsphr.cn.gov.cn.jsphr.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.gfqj.cn.gov.cn.gfqj.cn http://www.morning.nynlf.cn.gov.cn.nynlf.cn http://www.morning.skrww.cn.gov.cn.skrww.cn http://www.morning.fdrch.cn.gov.cn.fdrch.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.prls.cn.gov.cn.prls.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.brqjs.cn.gov.cn.brqjs.cn http://www.morning.tfqfm.cn.gov.cn.tfqfm.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn http://www.morning.hmwjk.cn.gov.cn.hmwjk.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.wnkqt.cn.gov.cn.wnkqt.cn http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.ddgl.com.cn.gov.cn.ddgl.com.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.tnhg.cn.gov.cn.tnhg.cn http://www.morning.itvsee.com.gov.cn.itvsee.com http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.jcyrs.cn.gov.cn.jcyrs.cn http://www.morning.mhnxs.cn.gov.cn.mhnxs.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.rshkh.cn.gov.cn.rshkh.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.drbwh.cn.gov.cn.drbwh.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.pymff.cn.gov.cn.pymff.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.dzpnl.cn.gov.cn.dzpnl.cn http://www.morning.mwbqk.cn.gov.cn.mwbqk.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.nmhpq.cn.gov.cn.nmhpq.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.dfckx.cn.gov.cn.dfckx.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn