Databases 11 min read

Build a Serverless AI Vector Search with Alibaba Cloud Milvus and DashScope

This tutorial walks you through creating a serverless Milvus vector search service on Alibaba Cloud, preparing a news corpus, generating embeddings with DashScope, storing them in Milvus, and performing vector‑based retrieval and question answering using a large language model.

Alibaba Cloud Big Data AI Platform
Alibaba Cloud Big Data AI Platform
Alibaba Cloud Big Data AI Platform
Build a Serverless AI Vector Search with Alibaba Cloud Milvus and DashScope

Background

Alibaba Cloud Vector Search Milvus edition is a fully managed serverless service that is 100% compatible with open‑source Milvus and supports seamless migration. It enhances scalability for large‑scale AI vector similarity retrieval and is ideal for multimodal search, retrieval‑augmented generation (RAG), recommendation, content risk detection, and other AI scenarios. The open‑source Attu tool can be used for visual operations.

Prerequisites

A Milvus instance has been created.

An API‑KEY for DashScope has been obtained.

Usage Limits

Ensure Python 3.8 or higher is installed before proceeding.

Preparation

Install required libraries: pip3 install pymilvus tqdm dashscope Download the CEC‑Corpus dataset (public news corpus) and clone the repository:

git clone https://github.com/shijiebei2009/CEC-Corpus.git

Step 1: Knowledge‑Base Vectorization

Create a file embedding.py with the following content:

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'
    dashscope.api_key = '<YOUR_DASHSCOPE_API_KEY>'
    COLLECTION_NAME = 'CEC_Corpus'
    DIMENSION = 1536
    MILVUS_HOST = 'c-****************.milvus.aliyuncs.com'
    MILVUS_PORT = '19530'
    USER = 'root'
    PASSWORD = '<password>'
    connections.connect(host=MILVUS_HOST, port=MILVUS_PORT, user=USER, password=PASSWORD)
    if utility.has_collection(COLLECTION_NAME):
        utility.drop_collection(COLLECTION_NAME)
    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)
    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)
        for id, vector, doc in zip(ids, vectors, news):
            insert_doc = (doc[:498] + '..') if len(doc) > 500 else doc
            collection.insert([[id], [insert_doc], [vector]])
            time.sleep(2)

Replace the placeholder parameters (host, port, user, password, API‑KEY) with your actual values.

data_path : Path where the CEC‑Corpus data is stored.

COLLECTION_NAME : Name of the Milvus collection (customizable).

dashscope_api_key : Your DashScope model service key.

DIMENSION : Vector dimension, fixed at 1536.

MILVUS_HOST : Public address of the Milvus instance.

MILVUS_PORT : Proxy port of the Milvus instance (default 19530).

USER and PASSWORD : Credentials you set when creating the Milvus instance.

Step 2: Vector Retrieval and Knowledge Q&A

After the data is inserted, you can perform fast vector retrieval and use a large language model (Qwen‑Turbo) to answer questions based on the retrieved context.

Create a file answer.py with the following content:

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_params = {"metric_type": "L2"}
    results = collection.search(data=[getEmbedding(text)], anns_field="embedding", param=search_params, limit=1, output_fields=['text'])
    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 = '<YOUR_DASHSCOPE_API_KEY>'
    COLLECTION_NAME = 'CEC_Corpus'
    DIMENSION = 1536
    MILVUS_HOST = 'c-****************.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)
    collection.load()
    question = '北京中央电视台工地发生大火,发生在哪里?出动了多少辆消防车?人员伤亡情况如何?'
    context = search(question)
    answer = getAnswer(question, context)
    print(answer)

Running the script with the example question returns:

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

If you have questions, you can join the Milvus vector search user DingTalk group (ID: 59530004993) for support.

PythonMilvusAI embeddingsDashScopeKnowledge QA
Alibaba Cloud Big Data AI Platform
Written by

Alibaba Cloud Big Data AI Platform

The Alibaba Cloud Big Data AI Platform builds on Alibaba’s leading cloud infrastructure, big‑data and AI engineering capabilities, scenario algorithms, and extensive industry experience to offer enterprises and developers a one‑stop, cloud‑native big‑data and AI capability suite. It boosts AI development efficiency, enables large‑scale AI deployment across industries, and drives business value.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.