ForcePilot/src/core/knowledgebase.py

110 lines
4.0 KiB
Python
Raw Normal View History

import os
from src.models.embedding import EmbeddingModel
2024-10-08 22:16:17 +08:00
from pymilvus import MilvusClient, MilvusException
from src.utils import setup_logger, hashstr
logger = setup_logger("KnowledgeBase")
class KnowledgeBase:
def __init__(self, config=None, embed_model=None) -> None:
2024-10-08 22:16:17 +08:00
self.config = config or {}
2024-07-20 20:30:32 +08:00
assert embed_model, "embed_model=None"
self.embed_model = embed_model
2024-08-07 17:24:46 +08:00
2024-10-08 22:16:17 +08:00
self.client = None
if not self.connect_to_milvus():
raise ConnectionError("Failed to connect to Milvus")
def connect_to_milvus(self):
"""
连接到 Milvus 服务
使用配置中的 URI如果没有配置则使用默认值
"""
try:
uri = os.getenv('MILVUS_URI', self.config.get('milvus_uri', "http://milvus:19530"))
self.client = MilvusClient(uri=uri)
# 可以添加一个简单的测试来确保连接成功
self.client.list_collections()
logger.info(f"Successfully connected to Milvus at {uri}")
return True
except MilvusException as e:
logger.error(f"Failed to connect to Milvus: {e}")
return False
2024-07-16 18:14:27 +08:00
def get_collection_names(self):
return self.client.list_collections()
def get_collections(self):
collections_name = self.client.list_collections()
collections = []
for collection_name in collections_name:
collection = self.get_collection_info(collection_name)
collections.append(collection)
return collections
def get_collection_info(self, collection_name):
collection = self.client.describe_collection(collection_name)
collection.update(self.client.get_collection_stats(collection_name))
# collection["id"] = hashstr(collection_name)
return collection
2024-08-25 20:29:24 +08:00
def add_collection(self, collection_name, dimension=None):
if self.client.has_collection(collection_name=collection_name):
logger.warning(f"Collection {collection_name} already exists, drop it")
self.client.drop_collection(collection_name=collection_name)
self.client.create_collection(
collection_name=collection_name,
2024-08-25 20:29:24 +08:00
dimension= dimension, # The vectors we will use in this demo has 768 dimensions
)
2024-07-16 18:14:27 +08:00
def add_documents(self, docs, collection_name, **kwargs):
"""添加已经分块之后的文本"""
# 检查 collection 是否存在
2024-07-16 18:14:27 +08:00
import random
if not self.client.has_collection(collection_name=collection_name):
2024-08-25 20:29:24 +08:00
logger.error(f"Collection {collection_name} not found, create it")
# self.add_collection(collection_name)
vectors = self.embed_model.encode(docs)
2024-07-16 18:14:27 +08:00
data = [{
"id": int(random.random() * 1e12),
"vector": vectors[i],
"text": docs[i],
2024-09-09 17:07:03 +08:00
"hash": hashstr(docs[i], with_salt=True),
2024-07-16 18:14:27 +08:00
**kwargs} for i in range(len(vectors))]
res = self.client.insert(collection_name=collection_name, data=data)
return res
2024-07-17 18:52:20 +08:00
def search(self, query, collection_name, limit=3):
query_vectors = self.embed_model.encode_queries([query])
2024-08-08 19:51:13 +08:00
return self.search_by_vector(query_vectors[0], collection_name, limit)
2024-08-08 19:51:13 +08:00
def search_by_vector(self, vector, collection_name, limit=3):
res = self.client.search(
collection_name=collection_name, # target collection
2024-08-08 19:51:13 +08:00
data=[vector], # query vectors
limit=limit, # number of returned entities
output_fields=["text", "file_id"], # specifies fields to be returned
)
2024-08-08 19:51:13 +08:00
return res[0]
def examples(self, collection_name, limit=20):
res = self.client.query(
collection_name=collection_name,
limit=10,
output_fields=["id", "text"],
)
return res
def search_by_id(self, collection_name, id, output_fields=["id", "text"]):
res = self.client.get(collection_name, id, output_fields=output_fields)
return res