
前言這幾天阿里低調放出兩款 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%免費】