通过阿里云Milvus和通义千问快速构建基于专属知识库的问答系统

简介: 本文展示了如何使用阿里云向量检索 Milvus 版和灵积(Dashscope)提供的通用千问大模型能力,快速构建一个基于专属知识库的问答系统。在示例中,我们通过接入灵积的通义千问 API 及文本嵌入(Embedding)API 来实现 LLM 大模型的相关功能。

本文展示了如何使用阿里云向量检索Milvus和灵积(Dashscope)提供的通用千问大模型能力,快速构建一个基于专属知识库的问答系统。在示例中,我们通过接入灵积的通义千问API及文本嵌入(Embedding)API来实现LLM大模型的相关功能。


前提条件


使用限制

请确保您的运行环境中已安装 Python 3.8或以上版本,以便顺利安装并使用 DashScope。


操作流程

准备工作

  1. 安装相关的依赖库。
pip3 install pymilvus tqdm dashscope


  1. 下载所需的知识库。
    本文示例使用了公开数据集 CEC-Corpus。CEC-Corpus 数据集包含332篇针对各类突发事件的新闻报道,语料和标注数据,这里我们只需要提取原始的新闻稿文本,并将其向量化后入库。
git clone https://github.com/shijiebei2009/CEC-Corpus.git


步骤一:知识库向量化

  1. 创建 embedding.py 文件,内容如下所示。
import os
import time
from tqdm import tqdm
import dashscope
from dashscope import TextEmbedding
from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection, utility


def prepareData(path, batch_size=25):
    batch_docs = []
    for file in os.listdir(path):
        with open(path + '/' + file, 'r', encoding='utf-8') as f:
            batch_docs.append(f.read())
            if len(batch_docs) == batch_size:
                yield batch_docs
                batch_docs = []
                
    if batch_docs:
        yield batch_docs
        
        
def getEmbedding(news):
    model = TextEmbedding.call(
        model=TextEmbedding.Models.text_embedding_v1,
        input=news
    )
    embeddings = [record['embedding'] for record in model.output['embeddings']]
    return embeddings if isinstance(news, list) else embeddings[0]


if __name__ == '__main__':
    
    current_path = os.path.abspath(os.path.dirname(__file__))   # 当前目录
    root_path = os.path.abspath(os.path.join(current_path, '..'))   # 上级目录
    data_path = f'{root_path}/CEC-Corpus/raw corpus/allSourceText'  # 数据下载git clone https://github.com/shijiebei2009/CEC-Corpus.git
    
    # 配置Dashscope API KEY
    dashscope.api_key = '<YOUR_DASHSCOPE_API_KEY>'
    
    # 配置Milvus参数
    COLLECTION_NAME = 'CEC_Corpus'
    DIMENSION = 1536
    MILVUS_HOST = 'c-97a7d8038fb8****.milvus.aliyuncs.com'
    MILVUS_PORT = '19530'
    USER = 'root'
    PASSWORD = '<password>'
    
    connections.connect(host=MILVUS_HOST, port=MILVUS_PORT, user=USER, password=PASSWORD)
    
    # Remove collection if it already exists
    if utility.has_collection(COLLECTION_NAME):
        utility.drop_collection(COLLECTION_NAME)
    
    # Create collection which includes the id, title, and embedding.
    fields = [
        FieldSchema(name='id', dtype=DataType.INT64, descrition='Ids', is_primary=True, auto_id=False),
        FieldSchema(name='text', dtype=DataType.VARCHAR, description='Text', max_length=4096),
        FieldSchema(name='embedding', dtype=DataType.FLOAT_VECTOR, description='Embedding vectors', dim=DIMENSION)
    ]
    schema = CollectionSchema(fields=fields, description='CEC Corpus Collection')
    collection = Collection(name=COLLECTION_NAME, schema=schema)
    
    # Create an index for the collection.
    index_params = {
        'index_type': 'IVF_FLAT',
        'metric_type': 'L2',
        'params': {'nlist': 1024}
    }
    collection.create_index(field_name="embedding", index_params=index_params)
    
    id = 0
    for news in tqdm(list(prepareData(data_path))):
        ids = [id + i for i, _ in enumerate(news)]
        id += len(news)
        
        vectors = getEmbedding(news)
        # insert Milvus Collection
        for id, vector, doc in zip(ids, vectors, news):
            insert_doc = (doc[:498] + '..') if len(doc) > 500 else doc
            ins = [[id], [insert_doc], [vector]]  # Insert the title id, the text, and the text embedding vector
            collection.insert(ins)
            time.sleep(2)


本文示例涉及以下参数,请您根据实际环境替换。

参数

说明

data_path

存放CEC-Corpus数据的路径。

COLLECTION_NAME

设置Miluvs Collection名称,您可以自定义。

dashscope_api_key

模型服务灵积的密钥。您可以在模型服务灵积控制台的API-KEY管理页面查看。

DIMENSION

向量维度。固定值为1536。

MILVUS_HOST

Milvus实例的公网地址。您可以在Milvus实例的实例详情页面查看。

MILVUS_PORT

Milvus实例的Proxy Port。您可以在Milvus实例的实例详情页面查看。默认为19530。

USER

配置为创建Milvus实例时,您自定义的用户。

PASSWORD

配置为创建Milvus实例时,您自定义用户的密码。


  1. 在Attu中您可以看到已经创建的Collection,具体操作请参见Attu工具管理


在本文示例中,我们将Embedding向量和新闻报道文稿一起存入Milvus中,同时构建索引类型采用了IVF_FLAT,在向量检索时,同时可以召回原始文稿。


步骤二:向量检索与知识问答

数据写入完成后,即可进行快速的向量检索。在通过提问搜索到相关的知识点后,我们可以按照特定的模板将“提问 + 知识点”作为prompt向LLM发起提问。在这里我们所使用的LLM是通义千问,这是阿里巴巴自主研发的超大规模语言模型,能够在用户自然语言输入的基础上,通过自然语言理解和语义分析,理解用户意图。通过提供尽可能清晰详细的指令(prompt),可以获得更符合预期的结果。这些能力都可以通过通义千问来获得。


本文示例设计的提问模板格式为:请基于我提供的内容回答问题。内容是{___},我的问题是{___},当然您也可以自行设计合适的模板。


创建answer.py文件,内容如下所示。

import os
import dashscope
from dashscope import Generation
from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection
from embedding import getEmbedding


def getAnswer(query, context):
    prompt = f'''请基于```内的报道内容,回答我的问题。
          ```
          {context}
          ```
          我的问题是:{query}
       '''
    
    rsp = Generation.call(model='qwen-turbo', prompt=prompt)
    return rsp.output.text


def search(text):
    # Search parameters for the index
    search_params = {
        "metric_type": "L2"
    }
    
    results = collection.search(
        data=[getEmbedding(text)],  # Embeded search value
        anns_field="embedding",  # Search across embeddings
        param=search_params,
        limit=1,  # Limit to five results per search
        output_fields=['text']  # Include title field in result
    )
    
    ret = []
    for hit in results[0]:
        ret.append(hit.entity.get('text'))
    return ret


if __name__ == '__main__':
    
    current_path = os.path.abspath(os.path.dirname(__file__))   # 当前目录
    root_path = os.path.abspath(os.path.join(current_path, '..'))   # 上级目录
    data_path = f'{root_path}/CEC-Corpus/raw corpus/allSourceText'
    
    # 配置Dashscope API KEY
    dashscope.api_key = '<YOUR_DASHSCOPE_API_KEY>'
    
    # 配置Milvus参数
    COLLECTION_NAME = 'CEC_Corpus'
    DIMENSION = 1536
    MILVUS_HOST = 'c-97a7d8038fb8****.milvus.aliyuncs.com'
    MILVUS_PORT = '19530'
    USER = 'root'
    PASSWORD = '<password>'
    
    connections.connect(host=MILVUS_HOST, port=MILVUS_PORT, user=USER, password=PASSWORD)
    
    fields = [
        FieldSchema(name='id', dtype=DataType.INT64, descrition='Ids', is_primary=True, auto_id=False),
        FieldSchema(name='text', dtype=DataType.VARCHAR, description='Text', max_length=4096),
        FieldSchema(name='embedding', dtype=DataType.FLOAT_VECTOR, description='Embedding vectors', dim=DIMENSION)
    ]
    schema = CollectionSchema(fields=fields, description='CEC Corpus Collection')
    collection = Collection(name=COLLECTION_NAME, schema=schema)
    
    # Load the collection into memory for searching
    collection.load()
    
    question = '北京中央电视台工地发生大火,发生在哪里?出动了多少辆消防车?人员伤亡情况如何?'
    context = search(question)
    answer = getAnswer(question, context)
    print(answer)


运行完成后,针对北京中央电视台工地发生大火,发生在哪里?出动了多少辆消防车?人员伤亡情况如何?的提问,会得到以下结果。


火灾发生在北京市朝阳区东三环中央电视台新址园区在建的附属文化中心大楼工地。出动了54辆消防车。目前尚无人员伤亡报告。


快速跳转

  1. 向量检索 Milvus 版官网:https://www.aliyun.com/product/milvus
  2. 产品控制台:https://milvus.console.aliyun.com/#/overview
  3. 产品文档:https://help.aliyun.com/zh/milvus/
  4. 标准版申请:https://survey.aliyun.com/apps/zhiliao/JqRjGNFoS



向量检索 Milvus 版用户交流钉钉群

1712734996586.png

相关文章
|
2月前
|
API 开发者
百宝箱开放平台 ✖️ 查询知识库文件的构建状态
本接口用于查询目标文件在知识库中的构建状态,需提供documentId并配置有效token。支持查看初始化、处理中、成功、失败等状态,并返回详细错误信息以便排查问题。
129 4
|
3月前
|
SQL 人工智能 自然语言处理
阿里云 CIO 蒋林泉:AI 大模型时代,我们如何用 RIDE 实现 RaaS 的首次落地?
本文整理自阿里云智能集团 CIO 蒋林泉在 AICon 2025 深圳的演讲,分享了阿里云在大模型应用落地中的实践经验。通过多个数字人项目案例,探讨了企业在 AI 应用中的组织转型、业务识别、产品定义与工程落地等关键环节,并提出了 RIDE 方法论(重组、识别、定义、执行),助力企业实现 AI 有效落地。
|
3月前
|
存储 人工智能 自然语言处理
RAG:增强大模型知识库的新范式
RAG:增强大模型知识库的新范式
534 99
|
2月前
|
缓存 边缘计算 运维
基于 Cloudflare Workers 构建高性能知识库镜像服务:反向代理与 HTML 动态重写实践
基于Cloudflare Workers构建的边缘计算镜像服务,通过反向代理、HTML动态重写与智能缓存,优化维基百科等知识平台的访问性能。支持路径映射、安全头清理与容错回退,实现免运维、低延迟、高可用的Web加速方案,适用于教育、科研等合规场景。
572 8
|
3月前
|
存储 机器学习/深度学习 人工智能
云栖 2025|阿里云 Qwen3 系列领衔:AI 模型全栈突破与开发者落地指南
阿里云发布Qwen3全栈AI体系,七大模型升级、性能全球领先,开源生态稳居第一。从底层基建到开发工具链全面优化,助力企业高效落地AI应用,共建超级AI云生态。
1530 11
|
3月前
|
机器学习/深度学习 人工智能 数据安全/隐私保护
阿里云 Qwen3 全栈 AI 模型:技术解析、开发者实操指南与 100 万企业落地案例
阿里云发布Qwen3全栈AI体系,推出Qwen3-Max、Qwen3-Next等七大模型,性能全球领先,开源生态超6亿次下载。支持百万级上下文、多模态理解,训练成本降90%,助力企业高效落地AI。覆盖制造、金融、创作等场景,提供无代码与代码级开发工具,共建超级AI云生态。
878 6
|
3月前
|
人工智能 API
阿里云百炼API-KEY在哪查询?如何获取阿里云AI百炼大模型的API-KEY?
阿里云百炼是阿里云推出的AI大模型平台,用户可通过其管理控制台获取API-KEY。需先开通百炼平台及大模型服务,即可创建并复制API-KEY。目前平台提供千万tokens免费额度,详细操作流程可参考官方指引。
|
3月前
|
存储 数据采集 人工智能
切块、清洗、烹饪:RAG知识库构建的三步曲
大语言模型明明已经喂了大量文档,为什么还是答非所问?就像米其林厨师需要精心处理食材,RAG系统也需要巧妙处理文档。从文本分块、清洗到结构化索引,这些不起眼的处理步骤决定了AI回答质量的上限。掌握这些技巧,让你的RAG系统从「路边摊」蜕变为「米其林餐厅」。
|
2月前
|
人工智能 自然语言处理 监控
05_用LLM创建知识库:从文档到智能问答系统
在2025年,随着大语言模型(LLM)技术的飞速发展,如何让这些强大的模型更好地理解和应用企业或个人的专业知识,成为了一个重要课题。传统的LLM虽然知识渊博,但在面对特定领域的专业问题时,常常会出现"一本正经地胡说八道"的情况。而检索增强生成(Retrieval-Augmented Generation,简称RAG)技术的出现,为这一问题提供了完美解决方案。

热门文章

最新文章