有奖:语音产品征文挑战赛等你来> HOT
AI 套件是腾讯云向量数据库(Tencent Cloud VectorDB)提供的一站式文档检索解决方案,包含自动化文档解析、信息补充、向量化、内容检索等能力。用户仅需上传原始文档,数分钟内即可快速构建专属知识库,大幅提高知识接入效率。本文介绍通过 Python SDK 操作 AI 类数据库上传文件并进行内容相似度检索的完整示例。
import json
import time
from typing import Optional

import tcvectordb
from tcvectordb import exceptions
from tcvectordb.model.ai_database import AIDatabase
from tcvectordb.model.collection_view import Embedding, SplitterProcess, Language, CollectionView
from tcvectordb.model.document import Filter, Document
from tcvectordb.model.document_set import DocumentSet, Rerank
from tcvectordb.model.enum import FieldType, IndexType, ReadConsistency
from tcvectordb.model.index import Index, FilterIndex

#create vdb client
vdbclient = tcvectordb.VectorDBClient(url="your vdb url",
key="your vdb api_key",
username="root")

def vdbInit():
# create database
db = vdbclient.create_ai_database("test_db")
# create collectionView
collView = db.create_collection_view("test_collView")
# upload and split text
collView.load_and_split_text(local_file_path="/docs/fileName.md")
print('upload file sucess')

def knowledgeSearch(query):
db = vdbclient.database('test_db')
collView = db.collection_view('test_collView')
doc_list = collView.search(
content=query,
limit=3
)
knowledge = "以下是根据输入问题检索到的知识内容:\\n"
knowledge_id = 1
for item in doc_list:
knowledge += ("知识内容%s:\\n%s"%(knowledge_id,item.data.text))
knowledge_id += 1
return(knowledge)

if __name__ == "__main__":
vdbInit()
query = input("请输入查询内容:")
print("============================================")
print(knowledgeSearch(query))