向量数据库检索

if not self.collection:

raise RuntimeError("请先创建集合")

生成查询向量

query_embedding = self.generate_embedding(query_text)

搜索参数

search_params = {

"metric_type": "L2",

"params": {"nprobe": 16}

}

执行搜索

print(f"正在搜索相似文本: '{query_text}'")

print('--------query_embedding----------------')

print(query_text)

print(query_embedding)

print('--------query_embedding----------------')

results = self.collection.search(

data=[query_embedding],

anns_field="embedding",

param=search_params,

limit=top_k,

output_fields=["text", "metadata"]

)

print('-----------------------results--------------------')

print(results)

print('-----------------------results--------------------')

正在搜索相似文本: '人工智能是计算机科学的一个分支'

--------query_embedding----------------

人工智能是计算机科学的一个分支

0.0868442 0.03383261 0.8867534 0.40404728 0.25568193 0.8143026 0.9898358 0.13523214 0.7775536 0.1047672 0.97602385 0.24655622 0.26915395 0.24497598 0.3665343 0.46741247 0.68997544 0.2505883 0.06964615 0.03187205 0.52018636 0.62224406 0.69120926 0.9545073 0.08183594 0.01488718 0.15640016 0.5334772 0.21839626 0.68861496 0.1709311 0.20307513 0.6517363 0.6155564 0.7335377 0.94831115 0.8792044 0.95748454 0.6033911 0.2018383 0.930259 0.56085527 0.20518194 0.9262032 0.38187274 0.6069298 0.93542224 0.37852377 0.30073825 0.8376683 0.24371381 0.02810273 0.08106949 0.13087785 0.5004337 0.51431286 0.13320969 0.71445334 0.6194988 0.01581137 0.9501668 0.1934796 0.742703 0.70844597 0.1604538 0.6802646 0.34634224 0.5456539 0.46275988 0.16551328 0.83565605 0.6016001 0.61109984 0.30627385 0.97093976 0.99930257 0.29578993 0.5469492 0.28768337 0.31434414 0.5663442 0.45112413 0.2169044 0.6302972 0.9722437 0.02455941 0.8936516 0.8774959 0.57403 0.47009948 0.03824434 0.63443965 0.9589547 0.6899516 0.01201063 0.19486319 0.9027687 0.6574339 0.47412872 0.30099493 0.48049414 0.6311632 0.4573544 0.07649028 0.55467784 0.3708412 0.26274177 0.4506525 0.4223067 0.4124977 0.21018541 0.0047362 0.7705015 0.5451533 0.40615085 0.49359718 0.9902851 0.3093196 0.46972254 0.94213045 0.23006302 0.25168714 0.6346079 0.659631 0.18597034 0.1427686 0.00319374 0.07725212 0.248803 0.55326843 0.9220962 0.24718809 0.3429383 0.72895765 0.34254518 0.8834778 0.16492361 0.54141337 0.80008495 0.54066867 0.10767661 0.11337695 0.9722788 0.22725064 0.6083555 0.16575086 0.91433066 0.56114274 0.45415127 0.55633867 0.29271686 0.6290875 0.03038282 0.24328907 0.44549546 0.8016935 0.9557495 0.78130025 0.76477706 0.5649261 0.01037048 0.6484271 0.89897346 0.07986014 0.78563076 0.5275594 0.1287399 0.3641922 0.6627957 0.37034252 0.84199804 0.12278508 0.7041375 0.14166668 0.35956717 0.43021822 0.22173028 0.8375897 0.35586387 0.44742876 0.43792158 0.5106019 0.6179151 0.7982999 0.2936258 0.06909464 0.12378242 0.74651486 0.842421 0.35129678 0.2411833 0.10194065 0.05604327 0.47398013 0.03655601 0.18847401 0.66929865 0.5095275 0.64663976 0.80956185 0.13733079 0.04057472 0.34126204 0.83236104 0.33787668 0.7696634 0.50997025 0.02123572 0.9333264 0.22548226 0.20558542 0.16446085 0.05452509 0.74170697 0.9693923 0.35016173 0.27317715 0.22076762 0.13911383 0.3224318 0.62780064 0.01266924 0.60345036 0.77932215 0.20671229 0.09256509 0.6936266 0.9155535 0.43447575 0.8300272 0.13336998 0.7257102 0.5290116 0.6394721 0.46035555 0.28333265 0.7138636 0.09579473 0.03855465 0.08419628 0.28320348 0.25461447 0.36263362 0.10132778 0.6519665 0.13829006 0.12856436 0.02197313 0.95218045 0.53907984 0.8223095 0.8976899 0.68432736 0.323823 0.53508615 0.6927347 0.58701444 0.52786356 0.91721946 0.08061525 0.9977211 0.36784837 0.6412361 0.255943 0.42969427 0.16225992 0.00704757 0.31084278 0.5016951 0.93285614 0.35453308 0.12418976 0.88261133 0.08708221 0.57548743 0.84515256 0.971033 0.6419028 0.66201556 0.06947455 0.34324905 0.844067 0.8153894 0.8421173 0.9815654 0.25903043 0.46167833 0.45508775 0.5619033 0.47016808 0.8963042 0.95521957 0.11644958 0.32468733 0.99515224 0.8401741 0.01084056 0.67823964 0.5356839 0.26089817 0.72670436 0.2750553 0.49014148 0.40102965 0.8850007 0.91662025 0.22271399 0.38651085 0.34219617 0.04020097 0.4357878 0.43785694 0.5755065 0.24301112 0.91679806 0.35708296 0.10404775 0.40478405 0.11741883 0.21977386 0.0758483 0.75821483 0.2708614 0.22748029 0.66955537 0.5346489 0.67480105 0.09770001 0.99467295 0.9864922 0.3401579 0.66972685 0.01236195 0.33266217 0.13441859 0.19665805 0.29058382 0.98292196 0.71233046 0.28721112 0.08888026 0.690176 0.6657358 0.21345249 0.876079 0.7000211 0.6327144 0.43972552 0.09437454 0.29941922 0.4209495 0.7543784 0.32779083 0.11911198 0.6029227 0.8451076 0.28211638 0.4349335 0.7753495 0.51077855 0.06970291 0.46349403 0.96664506 0.25283307 0.85630757 0.6759779 0.8347701 0.24785449 0.93369955 0.6848017 0.01395762 0.9805456 0.9136605 0.85786355 0.3543869 0.5901738 0.52866274 0.553448 0.6957444 0.28894326 0.44469547 0.9105798 0.21184073 0.8289437

--------query_embedding----------------

-----------------------results--------------------

data: [[{'id': 462627561339618327, 'distance': 0.0, 'entity': {'text': '人工智能是计算机科学的一个分支', 'metadata': {'category': 'AI基础', 'priority': 1}}}, {'id': 462627561339618331, 'distance': 58.727821350097656, 'entity': {'text': '计算机视觉让机器能够理解图像', 'metadata': {'category': 'AI应用', 'priority': 3}}}, {'id': 462627561339618329, 'distance': 62.76405334472656, 'entity': {'text': '深度学习是机器学习的一个子集', 'metadata': {'category': '深度学习', 'priority': 2}}}]]

-----------------------results--------------------

相关推荐
AI探索者2 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者2 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh4 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅4 小时前
Python函数入门详解(定义+调用+参数)
python
曲幽5 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时8 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿11 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python