Files
AILab/bbit_ai/test/milvus/chainMain copy.ipynb
T
2025-09-18 17:18:18 +08:00

5.3 KiB

In [1]:
from langchain_milvus import BM25BuiltInFunction, Milvus
from typing import List
URI = "http://10.10.10.9:19530"
tongyiKey = "sk-9464b2498c184982a9fe9d2c2e725ab5"
from langchain_community.embeddings import DashScopeEmbeddings
embeddings = DashScopeEmbeddings(
    model="text-embedding-v3",
    dashscope_api_key= tongyiKey, 
)
memVectorstore = Milvus(
    embedding_function=embeddings,
    connection_args={"uri": URI, "token": "root:Milvus", "db_name": "bbit_ai_lab"},
    collection_name="memory",
    index_params={"index_type": "FLAT", "metric_type": "L2"},
    consistency_level="Strong",
    auto_id=True,

    primary_field = "id",
    text_field="text",
    vector_field="vector",
    partition_key_field = "ai_id",
    enable_dynamic_field = True,
    drop_old=False,  # set to True if seeking to drop the collection with that name if it exists
)
In [2]:
def get_memory_by_key_words(key_words: str, ai_ids: List[str]) -> str:
    print("ai_id是:" , ai_ids)
    """
    根据关键词和 ai_ids 列表,在知识库中检索相关内容,并返回整理后的文本字符串
    """
    # 构建过滤表达式:只查 kn_ids 范围内的
    if ai_ids:
        ids_expr = " or ".join([f'ai_id == "{kid}"' for kid in ai_ids])
        expr = f"({ids_expr})"
    else:
        expr = ""  # 不限制 kn_id todo 实际上应该不反悔任何内容
    
    result = knVectorstore.similarity_search(
        query=key_words,
        k=5,  # 可调节返回条数
        expr=expr
    )
    
    # 整理成字符串
    doc_texts = []
    for idx, doc in enumerate(result, start=1):
        text = doc.page_content.strip()
        if text:
            # 可以加个编号,便于LLM区分
            doc_texts.append(f"[记忆{idx}]: {text}")
    
    # 拼成一个大字符串,用换行隔开
    combined_text = "\n\n".join(doc_texts)
    return combined_text
In [3]:
get_memory_by_key_words("共育室 部署 地方",["3730f279-8b56-46ec-bde9-8a9e6c27f021"])
ai_id是: ['3730f279-8b56-46ec-bde9-8a9e6c27f021']
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 get_memory_by_key_words("共育室 部署 地方",["3730f279-8b56-46ec-bde9-8a9e6c27f021"])

Cell In[2], line 13, in get_memory_by_key_words(key_words, ai_ids)
     10 else:
     11     expr = ""  # 不限制 kn_id todo 实际上应该不反悔任何内容
---> 13 result = knVectorstore.similarity_search(
     14     query=key_words,
     15     k=5,  # 可调节返回条数
     16     expr=expr
     17 )
     19 # 整理成字符串
     20 doc_texts = []

NameError: name 'knVectorstore' is not defined