在Milvus中创建聚集并在聚会合插入数据,然后attu管理工具可以查察

[复制链接]
发表于 2026-1-22 17:46:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
  1. 日志日志打印出来的是这个,现在attu为什么看不到插入的数据信息,集合信息已经可以看到,为什么看不到数据呢
  2. /home/anaconda3/envs/bi-txt-sql/bin/python -X pycache_prefix=/home/.cache/JetBrains/PyCharm2023.2/cpython-cache /home/tools/pycharm-professional-2023.2.5/pycharm-2023.2.5/plugins/python/helpers/pydev/pydevd.py --multiprocess --qt-support=auto --client 127.0.0.1 --port 42745 --file /home/PycharmProjects/bi-txt-sql/app/api/endpoints/testmilvus.py
  3. 已连接到 pydev 调试器(内部版本号 232.10300.41)/home/.local/lib/python3.10/site-packages/torch/__init__.py:235: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  4.   from torch._C import *  # noqa: F403
  5. 成功连接到 Milvus 服务器
  6. 集合 'weather' 不存在,正在创建...
  7. 集合 'weather' 已创建
  8. /home/.local/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: resume_download is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use force_download=True.
  9.   warnings.warn(
  10. /home/.local/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: resume_download is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use force_download=True.
  11.   warnings.warn(
  12. 嵌入向量生成成功
  13. 成功插入 4 条数据到集合 'weather' 中
  14. 现有的 Milvus 集合:  ['weather']
  15. 进程已结束,退出代码为 0
复制代码
根据日记,你的 Milvus 聚集 "weather" 已经创建,而且你乐成插入了 4 条数据。但是你在利用 Attu 时看不到这些数据。这大概是由于以下几种缘故原由:
大概缘故原由及办理方法:

1. 未创建索引

Milvus 在插入数据后,须要创建索引以支持快速搜刮和可见性。如果没有创建索引,固然数据已插入,但你在 Attu 中大概看不到可视化的数据。
办理方法:创建索引
你可以在插入数据后创建一个索引。Milvus 支持多种索引范例,如 IVF_FLAT、IVF_SQ8 等。你可以在插入数据后创建索引:
  1. # 创建索引
  2. index_params = {
  3.     "metric_type": "L2",  # 可以根据需求选择其他相似度度量方式,如 IP
  4.     "index_type": "IVF_FLAT",
  5.     "params": {"nlist": 128}
  6. }
  7. collection.create_index(field_name="embedding", index_params=index_params)
  8. print("索引已创建")
复制代码
2. 数据还未加载

Milvus 利用 lazy loading(耽误加载),这意味着插入的数据并不会立刻可供查询。你须要显式地调用 load() 方法来加载数据,使它们可用于搜刮和可视化。
办理方法:加载聚集数据
  1. # 加载集合数据
  2. collection.load()
  3. print("集合数据已加载")
复制代码
3. Attu 缓存题目

Attu 大概缓存了之前的聚集信息,因此纵然你插入了数据,Attu 大概没有革新数据。
办理方法:革新 Attu
实验手动革新 Attu 页面,大概重启 Attu 服务,确保它可以或许从 Milvus 中获取最新的数据。
  1. docker restart attu
复制代码
4. Milvus 数据插入未见效

只管日记表现插入乐成,偶尔间如果数据没有完全提交到 Milvus 中,大概会导致数据不可见。
办理方法:确保数据插入已提交而且聚集已 flush
  1. # 插入后 flush 数据
  2. collection.flush()
复制代码
5. 查察 Attu 中是否选择了准确的聚集

确保你在 Attu 中查察的是准确的聚集。偶尔,大概是查察的聚集不是你刚刚插入数据的聚集。
完备修改后的代码:

确保你在插入数据后创建索引、加载聚集,并革新 Attu:
  1. import torch
  2. from transformers import AutoTokenizer, AutoModel
  3. from pymilvus import CollectionSchema, FieldSchema, DataType, Collection, connections, utility
  4. import numpy as np
  5. # 计算文本嵌入的函数
  6. def embed_texts(texts, model, tokenizer):
  7.     """
  8.     使用 Jina-embeddings-v2 模型将文本转化为嵌入向量
  9.     """
  10.     inputs = tokenizer(texts, return_tensors='pt', padding=True, truncation=True)
  11.     with torch.no_grad():
  12.         outputs = model(**inputs)
  13.     embeddings = outputs.last_hidden_state[:, 0, :]  # 提取CLS token嵌入
  14.     return embeddings.numpy()
  15. if __name__ == '__main__':
  16.     # 创建连接并检查是否成功
  17.     try:
  18.         connections.connect(alias="default", host="172.19.17.52", port="19530")
  19.         print("成功连接到 Milvus 服务器")
  20.     except Exception as e:
  21.         print(f"连接 Milvus 失败: {e}")
  22.     # 定义集合结构,注意我们为主键添加了 is_primary=True
  23.     id_field = FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True)
  24.     text_field = FieldSchema(name="text", dtype=DataType.VARCHAR, max_length=100)
  25.     embedding_field = FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=768)
  26.     schema = CollectionSchema(fields=[id_field, text_field, embedding_field])
  27.     # 集合名称
  28.     collection_name = "weather"
  29.     # 检查集合是否存在
  30.     if not utility.has_collection(collection_name):
  31.         print(f"集合 '{collection_name}' 不存在,正在创建...")
  32.         # 创建集合
  33.         try:
  34.             collection = Collection(name=collection_name, schema=schema)
  35.             print(f"集合 '{collection_name}' 已创建")
  36.         except Exception as e:
  37.             print(f"创建集合时出错: {e}")
  38.     else:
  39.         print(f"集合 '{collection_name}' 已存在,正在使用现有集合...")
  40.         # 加载现有集合
  41.         collection = Collection(name=collection_name)
  42.     # 加载 Jina-embeddings-v2-base-zh 模型和对应的 tokenizer
  43.     model_name = 'jinaai/jina-embeddings-v2-base-zh'
  44.     tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
  45.     model = AutoModel.from_pretrained(model_name, trust_remote_code=True)
  46.     # 示例文本
  47.     candidate_texts = ['今天天气下雨吗?', '今天是一个晴天', '今天是星期几?', '今天天气怎么样?']
  48.     # 使用 Jina-embeddings-v2 模型生成文本嵌入向量
  49.     try:
  50.         candidate_embeddings = embed_texts(candidate_texts, model, tokenizer)
  51.         print(f"嵌入向量生成成功")
  52.     except Exception as e:
  53.         print(f"生成嵌入向量时出错: {e}")
  54.     # 插入数据
  55.     try:
  56.         collection.insert([candidate_texts, candidate_embeddings.tolist()])
  57.         collection.flush()
  58.         print(f"成功插入 {len(candidate_texts)} 条数据到集合 '{collection_name}' 中")
  59.     except Exception as e:
  60.         print(f"插入数据时出错: {e}")
  61.     # 创建索引
  62.     index_params = {
  63.         "metric_type": "L2",
  64.         "index_type": "IVF_FLAT",
  65.         "params": {"nlist": 128}
  66.     }
  67.     collection.create_index(field_name="embedding", index_params=index_params)
  68.     print("索引已创建")
  69.     # 加载集合数据
  70.     collection.load()
  71.     print("集合数据已加载")
  72.     # 查看所有现有的 Milvus 集合
  73.     try:
  74.         collections = utility.list_collections()
  75.         print("现有的 Milvus 集合: ", collections)
  76.     except Exception as e:
  77.         print(f"列出现有集合时出错: {e}")
复制代码
总结:


  • 创建索引:确保插入后创建索引,以支持查询和可视化。
  • 加载聚集:在插入数据后,显式调用 load() 使数据可用。
  • 革新 Attu:革新或重启 Attu,确保它从 Milvus 获取最新的数据。
  • 确保准确查察聚集:在 Attu 中确认是否选择了准确的聚集。
通过这些步调,你应该可以或许在 Attu 中看到你插入的数据。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表