ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

最新|用Qwen3 Embedding+Milvus,搭建最强企业知识库!!

最新|用Qwen3 Embedding+Milvus,搭建最强企业知识库!! 前言这几天阿里低调放出两款 Qwen3 家族的新模型Qwen3-Embedding和Qwen3-Reranker都分别包括0.6B轻量版、4B平衡版、8B高性能版三种尺寸。两款模型基于 Qwen3 基座训练天然具备强大的多语言理解能力支持119种语言覆盖主流自然语言和编程语言。我简单看了下 Hugging Face 上的数据和评价有几个点蛮值得分享Qwen3-Embedding-8B 在 MTEB 多语言榜上拿到70.58 分超过 BGE、E5、甚至 Google Gemini 等一众明星模型。Qwen3-Reranker-8B 在多语言排序任务中得分69.02中文得分达到77.45在现有开源 reranker 模型中也是顶流。文本向量统一在同一个语义空间中文问句可以直接命中英文结果特别适合做全球化场景下的智能搜索或客服系统。这意味着这两款模型不只是“在开源模型里还不错”而是“全面追平甚至反超主流商用API”在RAG 检索、跨语种搜索、代码查找等系统尤其是中文语境中这两款模型已经具备可直接上生产的实力。那么如何用它来搭建一个RAG系统本文将给出深度教程。01RAG搭建教程Qwen3-Embedding-0.6B Qwen3-Reranker-0.6B)教程亮点手把手教你利用Qwen3最新发布的embedding模型和reranker模型搭建一个RAG两阶段检索设计召回重排平衡了效率与精度环境准备! pip install --upgrade pymilvus openai requests tqdm sentence-transformers transformersRequires transformers4.51.0Requires sentence-transformers2.7.0在本示例中我们将使用 OpenAI 作为文本生成的大型语言模型因此您需要将 API 密钥 OPENAI_API_KEY 作为环境变量准备给大型语言模型使用。importosos.environ[OPENAI_API_KEY]sk-************数据准备我们可以使用Milvus文档2.4. x中的FAQ页面作为RAG中的私有知识这是构建一个基础RAG的良好数据源。下载zip文件并将文档解压缩到文件夹milvus_docs! wget https://github.com/milvus-io/milvus-docs/releases/download/v2.4.6-preview/milvus_docs_2.4.x_en.zip! unzip-q milvus_docs_2.4.x_en.zip-d milvus_docs我们从文件夹milvus_docs/en/faq中加载所有markdown文件对于每个文档我们只需用“#”来分隔文件中的内容就可以大致分隔markdown文件各个主要部分的内容。fromglobimportglobtext_lines[]forfile_pathinglob(milvus_docs/en/faq/*.md,recursiveTrue):withopen(file_path,r)asfile:file_textfile.read()text_linesfile_text.split(# )准备 LLM 和Embedding模型本示例中使用 Qwen3-Embedding-0.6B 来进行文本嵌入使用Qwen3-Reranker-0.6B对检索的结果进行重排序。fromopenaiimportOpenAIfrom sentence_transformersimportSentenceTransformerimport torchfrom transformersimportAutoModel,AutoTokenizer,AutoModelForCausalLM# Initialize OpenAI client for LLM generationopenai_client OpenAI()# Load Qwen3-Embedding-0.6B model for text embeddingsembedding_model SentenceTransformer(Qwen/Qwen3-Embedding-0.6B)# Load Qwen3-Reranker-0.6B model for rerankingreranker_tokenizer AutoTokenizer.from_pretrained(Qwen/Qwen3-Reranker-0.6B, padding_sideleft)reranker_model AutoModelForCausalLM.from_pretrained(Qwen/Qwen3-Reranker-0.6B).eval()# Reranker configurationtoken_false_id reranker_tokenizer.convert_tokens_to_ids(no)token_true_id reranker_tokenizer.convert_tokens_to_ids(yes)max_reranker_length 8192prefix |im_start|system\nJudge whether the Document meets the requirements based on the Query and the Instruct provided. Note that the answer can only be \yes\ or \no\.|im_end|\n|im_start|user\nsuffix |im_end|\n|im_start|assistant\nthink\n\n/think\n\nprefix_tokens reranker_tokenizer.encode(prefix, add_special_tokensFalse)suffix_tokens reranker_tokenizer.encode(suffix, add_special_tokensFalse)输出结果示例定义一个函数利用 Qwen3-Embedding-0.6B 模型生成文本嵌入。该函数将用于生成文档嵌入和查询嵌入。defemb_text(text,is_queryFalse): Generate text embeddings using Qwen3-Embedding-0.6B model. Args: text: Input text to embed is_query: Whether this is a query (True) or document (False) Returns: List of embedding values ifis_query:# For queries, use the query prompt for better retrieval performance embeddings embedding_model.encode([text], prompt_namequery) else: # For documents, use default encoding embeddings embedding_model.encode([text]) return embeddings[0].tolist()定义重排序函数以提升检索质量。这些函数使用Qwen3-Reranker实现完整的重排序管道根据文档与查询的相关性对候选文档进行评估和重新排序。其中各函数主要作用分别是format_instruction(): 将查询、文档和任务指令格式化为重排序模型的标准输入格式process_inputs(): 对格式化后的文本进行分词编码并添加特殊token用于模型判断compute_logits(): 使用重排序模型计算“查询-文档”对的相关性得分0-1之间rerank_documents(): 基于查询相关性对文档进行重新排序返回按相关性得分降序排列的文档列表defformat_instruction(instruction,query,doc):Format instruction for reranker inputifinstructionisNone:instructionGiven a web search query, retrieve relevant passages that answer the queryoutputInstruct: {instruction}\nQuery: {query}\nDocument: {doc}.format(instructioninstruction,queryquery,docdoc)returnoutputdef process_inputs(pairs):Process inputs for rerankerinputsreranker_tokenizer(pairs,paddingFalse,truncationlongest_first,return_attention_maskFalse,max_lengthmax_reranker_length-len(prefix_tokens)-len(suffix_tokens))fori,eleinenumerate(inputs[input_ids]):inputs[input_ids][i]prefix_tokenselesuffix_tokens inputsreranker_tokenizer.pad(inputs,paddingTrue,return_tensorspt,max_lengthmax_reranker_length)forkeyininputs:inputs[key]inputs[key].to(reranker_model.device)returninputstorch.no_grad()defcompute_logits(inputs,**kwargs):Compute relevance scores using rerankerbatch_scoresreranker_model(**inputs).logits[:,-1,:]true_vectorbatch_scores[:,token_true_id]false_vectorbatch_scores[:,token_false_id]batch_scorestorch.stack([false_vector,true_vector],dim1)batch_scorestorch.nn.functional.log_softmax(batch_scores,dim1)scoresbatch_scores[:,1].exp().tolist()returnscoresdef rerank_documents(query,documents,task_instructionNone): Rerank documents based on query relevance using Qwen3-Reranker Args: query: Search query documents: List of documents to rerank task_instruction: Task instruction for reranking Returns: List of (document, score) tuples sorted by relevance score iftask_instructionisNone:task_instructionGiven a web search query, retrieve relevant passages that answer the query# Format inputs for reranker pairs [format_instruction(task_instruction, query, doc) for doc in documents] # Process inputs and compute scores inputs process_inputs(pairs) scores compute_logits(inputs) # Combine documents with scores and sort by score (descending) doc_scores list(zip(documents, scores)) doc_scores.sort(keylambda x: x[1], reverseTrue) return doc_scores生成一个测试向量并打印其维度以及前几个元素。test_embeddingemb_text(This is a test)embedding_dimlen(test_embedding)print(embedding_dim)print(test_embedding[:10])结果示例1024[-0.009923271834850311,-0.030248118564486504,-0.011494234204292297,-0.05980192497372627,-0.0026795873418450356,0.016578301787376404,-0.04073038697242737,0.03180320933461189,-0.024417787790298462,2.1764861230622046e-05]将数据加载到Milvus创建集合frompymilvusimportMilvusClientmilvus_clientMilvusClient(uri./milvus_demo.db)collection_namemy_rag_collection关于MilvusClient的参数设置将URI设置为本地文件例如./milvus.db是最便捷的方法因为它会自动使用Milvus Lite将所有数据存储在该文件中。如果你有大规模数据可以在Docker或Kubernetes上搭建性能更强的Milvus服务器。在这种情况下请使用服务器的URI例如http://localhost:19530作为你的URI。如果你想使用Zilliz CloudMilvus的全托管云服务请调整URI和令牌它们分别对应Zilliz Cloud中的公共端点Public Endpoint和API密钥Api key。检查集合是否已经存在如果存在则将其删除。ifmilvus_client.has_collection(collection_name):milvus_client.drop_collection(collection_name)创建一个具有指定参数的新集合。如果未指定任何字段信息Milvus将自动创建一个默认的ID字段作为主键以及一个向量字段用于存储向量数据。一个预留的JSON字段用于存储未在schema中定义的字段及其值。milvus_client.create_collection(collection_namecollection_name,dimensionembedding_dim,metric_typeIP,# Inner product distance consistency_levelStrong, # Strong consistency level)插入集合逐行遍历文本创建嵌入向量然后将数据插入Milvus。下面是一个新的字段text它是集合中的一个未定义的字段。 它将自动创建一个对应的text字段实际上它底层是由保留的JSON动态字段实现的 你不用关心其底层实现。fromtqdmimporttqdmdata[]fori,lineinenumerate(tqdm(text_lines,descCreating embeddings)):data.append({id:i,vector:emb_text(line),text:line})milvus_client.insert(collection_namecollection_name,datadata)输出结果示例 Creating embeddings:100%|██████████████████████████████████████████████████████████████████████████|72/72[00:0800:00,8.68it/s]{insert_count:72,ids:[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71],cost:0}结合重排序技术增强RAG检索数据我们来指定一个关于Milvus的常见问题。questionHow is data stored in milvus?在集合中搜索该问题并获取具有最高语义匹配度的前10个候选答案然后使用重排序器来选出最佳的3个匹配项。# Step 1: Initial retrieval with larger candidate setsearch_res milvus_client.search( collection_namecollection_name, data[ emb_text(question, is_queryTrue) ], # Use the emb_text function with query prompt to convert the question to an embedding vector limit10, # Return top 10 candidates for reranking search_params{metric_type: IP, params: {}}, # Inner product distance output_fields[text], # Return the text field)# Step 2: Extract candidate documents for rerankingcandidate_docs [res[entity][text] for res in search_res[0]]# Step 3: Rerank documents using Qwen3-Rerankerprint(Reranking documents...)reranked_docs rerank_documents(question, candidate_docs)# Step 4: Select top 3 reranked documentstop_reranked_docs reranked_docs[:3]print(fSelected top {len(top_reranked_docs)} documents after reranking)让我们来看看此次查询的重新排序结果吧importjson# Display reranked results with reranker scoresreranked_lines_with_scores [ (doc, score) for doc, score in top_reranked_docs]print(Reranked results:)print(json.dumps(reranked_lines_with_scores, indent4))# Also show original embedding-based results for comparisonprint(\n *80)print(Original embedding-based results (top 3):)original_lines_with_distances [ (res[entity][text], res[distance]) for res in search_res[0][:3]]print(json.dumps(original_lines_with_distances, indent4))输出结果示例从结果中我们可以看到Qwen3-Reranker的重排序效果明显相关性得分区分度较好Reranked results(top3):[[ Where does Milvus store data?\n\nMilvus deals with two types of data, inserted data and metadata. \n\nInserted data, including vector data, scalar data, and collection-specific schema, are stored in persistent storage as incremental log. Milvus supports multiple object storage backends, including [MinIO](https://min.io/), [AWS S3](https://aws.amazon.com/s3/?nc1h_ls), [Google Cloud Storage](https://cloud.google.com/storage?hlen#object-storage-for-companies-of-all-sizes) (GCS), [Azure Blob Storage](https://azure.microsoft.com/en-us/products/storage/blobs), [Alibaba Cloud OSS](https://www.alibabacloud.com/product/object-storage-service), and [Tencent Cloud Object Storage](https://www.tencentcloud.com/products/cos) (COS).\n\nMetadata are generated within Milvus. Each Milvus module has its own metadata that are stored in etcd.\n\n###,0.9997891783714294],[How does Milvus flush data?\n\nMilvus returns success when inserted data are loaded to the message queue. However, the data are not yet flushed to the disk. Then Milvus data node writes the data in the message queue to persistent storage as incremental logs. If flush() is called, the data node is forced to write all data in the message queue to persistent storage immediately.\n\n###,0.9989748001098633],[Does the query perform in memory? What are incremental data and historical data?\n\nYes. When a query request comes, Milvus searches both incremental data and historical data by loading them into memory. Incremental data are in the growing segments, which are buffered in memory before they reach the threshold to be persisted in storage engine, while historical data are from the sealed segments that are stored in the object storage. Incremental data and historical data together constitute the whole dataset to search.\n\n###,0.9984032511711121]]Original embedding-based results(top3):[[ Where does Milvus store data?\n\nMilvus deals with two types of data, inserted data and metadata. \n\nInserted data, including vector data, scalar data, and collection-specific schema, are stored in persistent storage as incremental log. Milvus supports multiple object storage backends, including [MinIO](https://min.io/), [AWS S3](https://aws.amazon.com/s3/?nc1h_ls), [Google Cloud Storage](https://cloud.google.com/storage?hlen#object-storage-for-companies-of-all-sizes) (GCS), [Azure Blob Storage](https://azure.microsoft.com/en-us/products/storage/blobs), [Alibaba Cloud OSS](https://www.alibabacloud.com/product/object-storage-service), and [Tencent Cloud Object Storage](https://www.tencentcloud.com/products/cos) (COS).\n\nMetadata are generated within Milvus. Each Milvus module has its own metadata that are stored in etcd.\n\n###,0.8306853175163269],[How does Milvus flush data?\n\nMilvus returns success when inserted data are loaded to the message queue. However, the data are not yet flushed to the disk. Then Milvus data node writes the data in the message queue to persistent storage as incremental logs. If flush() is called, the data node is forced to write all data in the message queue to persistent storage immediately.\n\n###,0.7302717566490173],[How does Milvus handle vector data types and precision?\n\nMilvus supports Binary, Float32, Float16, and BFloat16 vector types.\n\n- Binary vectors: Store binary data as sequences of 0s and 1s, used in image processing and information retrieval.\n- Float32 vectors: Default storage with a precision of about 7 decimal digits. Even Float64 values are stored with Float32 precision, leading to potential precision loss upon retrieval.\n- Float16 and BFloat16 vectors: Offer reduced precision and memory usage. Float16 is suitable for applications with limited bandwidth and storage, while BFloat16 balances range and efficiency, commonly used in deep learning to reduce computational requirements without significantly impacting accuracy.\n\n###,0.7003671526908875]]使用大型语言模型LLM构建检索增强生成RAG响应将检索到的文档转换为字符串格式。context\n.join([line_with_distance[0]forline_with_distanceinretrieved_lines_with_distances])为大语言模型提供系统提示system prompt和用户提示user prompt。这个提示是通过从Milvus检索到的文档生成的。 SYSTEM_PROMPTHuman: You are an AI assistant. You are able to find answers to the questions from the contextual passage snippets provided.USER_PROMPTfUse the following pieces of information enclosed in context tags to provide an answer to the question enclosed in question tags.context{context}/contextquestion{question}/question使用Open AI 的大语言模型gpt-4o根据提示生成响应。 responseopenai_client.chat.completions.create(modelgpt-4o,messages[{role:system,content:SYSTEM_PROMPT},{role:user,content:USER_PROMPT},],)print(response.choices[0].message.content)输出结果展示 In Milvus,dataisstoredintwo main forms:inserted dataandmetadata.Inserted data,which includes vector data,scalar data,andcollection-specific schema,isstoredinpersistent storageasincremental logs.Milvus supports multipleobjectstorage backendsforthis purpose,including MinIO,AWS S3,Google Cloud Storage,Azure Blob Storage,Alibaba Cloud OSS,andTencent Cloud Object Storage.MetadataforMilvusisgenerated by its various modulesandstoredinetcd.02小结通过以上教程和输出结果展示不难发现通义千问团队在Qwen3系列中推出的embedding和reranker模型表现相当不错。这两个模型的结合使用为RAG系统提供了一个相对完整且实用的解决方案。在设计理念上Embedding模型支持query和document的差异化处理体现了对检索任务的深入理解Reranker采用交叉编码器架构能够捕捉query-document间的精细交互教程中的两阶段检索设计召回重排更是平衡了效率与精度。特别是Qwen3-Embedding-0.6B1024维和Qwen3-Reranker-0.6B都采用了相对轻量的参数规模支持本地部署减少了对外部API的依赖在保证性能的同时降低了硬件要求适合中小企业和个人开发者使用。事实上Qwen3系列推出embedding和reranker模型其实不是个例不是巧合而是产业共识。原因很简单这两个模块决定了大模型是否具备产品化能力。生成式大模型最大的问题在于不确定性高、评估难、成本重。要解决以上问题无论是 **RAG、LLM Memory、Agent 本质上都依赖一个前提**能否将语义压缩成机器可高效检索和判断的向量表达。Embedding 与 Ranking 则是目前的最优路径**标准清晰、性能可测、成本可控、易于灰度。Embedding 决定你能不能“找得到”Ranking 决定你能不能“选得准”。**这使它们成为模型商品化最先跑通的 API 模块之一调用频率高每次检索都需要、切换成本高与索引绑定、商业价值高可用作底层 infra。最后为什么要学AI大模型当下⼈⼯智能市场迎来了爆发期并逐渐进⼊以⼈⼯通⽤智能AGI为主导的新时代。企业纷纷官宣“ AI ”战略为新兴技术⼈才创造丰富的就业机会⼈才缺⼝将达 400 万DeepSeek问世以来生成式AI和大模型技术爆发式增长让很多岗位重新成了炙手可热的新星岗位薪资远超很多后端岗位在程序员中稳居前列。与此同时AI与各行各业深度融合飞速发展成为炙手可热的新风口企业非常需要了解AI、懂AI、会用AI的员工纷纷开出高薪招聘AI大模型相关岗位。最近很多程序员朋友都已经学习或者准备学习 AI 大模型后台也经常会有小伙伴咨询学习路线和学习资料我特别拜托北京清华大学学士和美国加州理工学院博士学位的鲁为民老师给大家这里给大家准备了一份涵盖了AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频全系列的学习资料这些学习资料不仅深入浅出而且非常实用让大家系统而高效地掌握AI大模型的各个知识点。这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】AI大模型系统学习路线在面对AI大模型开发领域的复杂与深入精准学习显得尤为重要。一份系统的技术路线图不仅能够帮助开发者清晰地了解从入门到精通所需掌握的知识点还能提供一条高效、有序的学习路径。但知道是一回事做又是另一回事初学者最常遇到的问题主要是理论知识缺乏、资源和工具的限制、模型理解和调试的复杂性在这基础上找到高质量的学习资源不浪费时间、不走弯路又是重中之重。AI大模型入门到实战的视频教程项目包看视频学习是一种高效、直观、灵活且富有吸引力的学习方式可以更直观地展示过程能有效提升学习兴趣和理解力是现在获取知识的重要途径光学理论是没用的要学会跟着一起敲要动手实操才能将自己的所学运用到实际当中去这时候可以搞点实战案例来学习。海量AI大模型必读的经典书籍PDF阅读AI大模型经典书籍可以帮助读者提高技术水平开拓视野掌握核心技术提高解决问题的能力同时也可以借鉴他人的经验。对于想要深入学习AI大模型开发的读者来说阅读经典书籍是非常有必要的。600AI大模型报告实时更新这套包含640份报告的合集涵盖了AI大模型的理论研究、技术实现、行业应用等多个方面。无论您是科研人员、工程师还是对AI大模型感兴趣的爱好者这套报告合集都将为您提供宝贵的信息和启示。AI大模型面试真题答案解析我们学习AI大模型必然是想找到高薪的工作下面这些面试题都是总结当前最新、最热、最高频的面试题并且每道题都有详细的答案面试前刷完这套面试题资料小小offer不在话下这份完整版的大模型 AI 学习资料已经上传CSDN朋友们如果需要可以微信扫描下方CSDN官方认证二维码免费领取【保证100%免费】
返回列表