1 Star 0 Fork 0

linjk/Jarvis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vdb.py 2.67 KB
一键复制 编辑 原始数据 按行查看 历史
linjk 提交于 2024-04-20 14:24 +08:00 . collection_name配置化
import config
from pymilvus import connections, FieldSchema, DataType, CollectionSchema, Collection, utility
class Milvus:
def __init__(self):
super(Milvus, self).__init__()
vdbs = config.get_config().models.vdbs
for v in vdbs:
if v.name == "milvus" and v.enable == 1:
self._db = v
self._connections = connections.connect(host=self._db.host, port=self._db.port, user=self._db.user, password=self._db.password)
def create_collection(self, dim):
fields = [
FieldSchema(name="id", dtype=DataType.INT64, is_primary=True),
FieldSchema(name="question", dtype=DataType.VARCHAR, max_length=500),
FieldSchema(name="answer", dtype=DataType.VARCHAR, max_length=3000),
FieldSchema(name="source", dtype=DataType.VARCHAR, max_length=200),
FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=dim)
]
schema = CollectionSchema(fields=fields, description="", enable_dynamic_field=True)
collection = Collection(name=self._db.collection_name, schema=schema, using="default")
index_params = {
'index_type': 'IVF_FLAT', 'metric_type': "L2", 'params': {'nlist': 1}
}
collection.create_index(field_name="embedding", index_params=index_params)
collection.load()
def delete_collection(self):
if utility.has_collection(collection_name=self._db.collection_name):
utility.drop_collection(collection_name=self._db.collection_name)
def upsert_document(self, entities):
if utility.has_collection(collection_name=self._db.collection_name) is False:
return False
collection = Collection(name=self._db.collection_name, using="default")
collection.insert(entities)
collection.flush()
return True
def search_document(self, data, top_k):
if utility.has_collection(collection_name=self._db.collection_name) is False:
return []
collection = Collection(name=self._db.collection_name, using="default")
documents = collection.search(
data=data, anns_field="embedding", param={
"metric_type": "L2", "params": { "nprobe": 1 }, "offset": 0
}, limit=top_k, output_fields=["*"]
)
document_list = []
for i in range(len(documents[0])):
# get column name
fields = documents[0][i].entity.fields
document = {'id': documents[0][i].id, 'distance': documents[0][i].distance}
for f in fields:
document[f] = documents[0][i].entity.get(f)
document_list.append(document)
return document_list
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/linjk/jarvis.git
git@gitee.com:linjk/jarvis.git
linjk
jarvis
Jarvis
master

搜索帮助