已经创建好向量数据库,并且ollama、milvus服务已经启动
基于ollama本地模型处理数据生成嵌入向量插入进milvus向量数据库-CSDN博客
1.连接到 Milvus 服务
from pymilvus import connections, Collection, Index
import logging
# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 连接到 Milvus 服务
connections.connect(
uri="http://localhost:19530"
)
2.获取集合、创建索引
# 获取集合
CO_NAME = "example_collection8"
collection = Collection(CO_NAME)
# 创建索引
try:
index_params = {
"index_type": "IVF_FLAT", # 索引类型
"metric_type": "L2", # 距离度量方式
"params": {"nlist": 1024} # 索引参数
}
collection.create_index(field_name="vector_case", index_params=index_params)
logging.info(f"集合 '{CO_NAME}' 的索引已成功创建。")
except Exception as e:
logging.error(f"为集合 '{CO_NAME}' 创建索引失败: {e}")
exit(1)
3.加载集合到内存
# 加载集合到内存
try:
collection.load()
logging.info(f"集合 '{CO_NAME}' 已成功加载到内存。")
except Exception as e:
logging.error(f"加载集合 '{CO_NAME}' 到内存失败: {e}")
exit(1)
4.调用ollama,对用户问题处理生成exp
# -*- coding: utf-8 -*-
import ollama
# 指定 Ollama 服务的地址
ollama.api_base = 'http://localhost:11434'
# 获取用户输入的问题
question = input("请输入问题:")
# 构造请求,禁用上下文记忆
response = ollama.chat(
model='qwen2.5:3b',
messages=[
{
'role': 'system',
'content': '当用户向你提出问题时,请按照以下步骤进行处理:'
},
{
'role': 'user',
'content': question
}
]
)
# 输出结果
a = response['message']['content']
print(a)
5.创建查询迭代器、遍历查询结果
# 创建查询迭代器
try:
iterator = collection.query_iterator(
batch_size=10,
expr=a,
output_fields=["name"]
)
results = []
# 遍历查询结果
while True:
result = iterator.next()
if not result:
iterator.close()
break
print(result)
results += result
logging.info("查询完成,结果已输出。")
except Exception as e:
logging.error(f"查询集合 '{CO_NAME}' 时发生错误: {e}")
6.完整代码
from pymilvus import connections, Collection, Index
import logging
# 配置日志记录
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 连接到 Milvus 服务
connections.connect(
uri="http://localhost:19530"
)
# 获取集合
CO_NAME = "example_collection8"
collection = Collection(CO_NAME)
# 创建索引
try:
index_params = {
"index_type": "IVF_FLAT", # 索引类型
"metric_type": "L2", # 距离度量方式
"params": {"nlist": 1024} # 索引参数
}
collection.create_index(field_name="vector_case", index_params=index_params)
logging.info(f"集合 '{CO_NAME}' 的索引已成功创建。")
except Exception as e:
logging.error(f"为集合 '{CO_NAME}' 创建索引失败: {e}")
exit(1)
# 加载集合到内存
try:
collection.load()
logging.info(f"集合 '{CO_NAME}' 已成功加载到内存。")
except Exception as e:
logging.error(f"加载集合 '{CO_NAME}' 到内存失败: {e}")
exit(1)
# -*- coding: utf-8 -*-
import ollama
# 指定 Ollama 服务的地址
ollama.api_base = 'http://localhost:11434'
# 获取用户输入的问题
question = input("请输入问题:")
# 构造请求,禁用上下文记忆
response = ollama.chat(
model='qwen2.5:3b',
messages=[
{
'role': 'system',
'content': '当用户向你提出问题时,请按照以下步骤进行处理:'
},
{
'role': 'user',
'content': question
}
]
)
# 输出结果
a = response['message']['content']
print(a)
# 创建查询迭代器
try:
iterator = collection.query_iterator(
batch_size=10,
expr=a,
output_fields=["name"]
)
results = []
# 遍历查询结果
while True:
result = iterator.next()
if not result:
iterator.close()
break
print(result)
results += result
logging.info("查询完成,结果已输出。")
except Exception as e:
logging.error(f"查询集合 '{CO_NAME}' 时发生错误: {e}")