企業(yè)知識(shí)庫)
前言這幾天阿里低調(diào)放出兩款 Qwen3 家族的新模型Qwen3-Embedding和Qwen3-Reranker都分別包括0.6B輕量版、4B平衡版、8B高性能版三種尺寸。兩款模型基于 Qwen3 基座訓(xùn)練天然具備強(qiáng)大的多語言理解能力支持119種語言覆蓋主流自然語言和編程語言。我簡單看了下 Hugging Face 上的數(shù)據(jù)和評價(jià)有幾個(gè)點(diǎn)蠻值得分享Qwen3-Embedding-8B 在 MTEB 多語言榜上拿到70.58 分超過 BGE、E5、甚至 Google Gemini 等一眾明星模型。Qwen3-Reranker-8B 在多語言排序任務(wù)中得分69.02中文得分達(dá)到77.45在現(xiàn)有開源 reranker 模型中也是頂流。文本向量統(tǒng)一在同一個(gè)語義空間中文問句可以直接命中英文結(jié)果特別適合做全球化場景下的智能搜索或客服系統(tǒng)。這意味著這兩款模型不只是“在開源模型里還不錯(cuò)”而是“全面追平甚至反超主流商用API”在RAG 檢索、跨語種搜索、代碼查找等系統(tǒng)尤其是中文語境中這兩款模型已經(jīng)具備可直接上生產(chǎn)的實(shí)力。那么如何用它來搭建一個(gè)RAG系統(tǒng)本文將給出深度教程。01RAG搭建教程Qwen3-Embedding-0.6B Qwen3-Reranker-0.6B)教程亮點(diǎn)手把手教你利用Qwen3最新發(fā)布的embedding模型和reranker模型搭建一個(gè)RAG兩階段檢索設(shè)計(jì)召回重排平衡了效率與精度環(huán)境準(zhǔn)備! pip install--upgrade pymilvus openai requests tqdm sentence-transformers transformersRequires transformers4.51.0Requires sentence-transformers2.7.0在本示例中我們將使用 OpenAI 作為文本生成的大型語言模型因此您需要將 API 密鑰 OPENAI_API_KEY 作為環(huán)境變量準(zhǔn)備給大型語言模型使用。importosos.environ[OPENAI_API_KEY]sk-************數(shù)據(jù)準(zhǔn)備我們可以使用Milvus文檔2.4. x中的FAQ頁面作為RAG中的私有知識(shí)這是構(gòu)建一個(gè)基礎(chǔ)RAG的良好數(shù)據(jù)源。下載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文件對于每個(gè)文檔我們只需用“#”來分隔文件中的內(nèi)容就可以大致分隔markdown文件各個(gè)主要部分的內(nèi)容。fromglobimportglobtext_lines[]forfile_pathinglob(milvus_docs/en/faq/*.md,recursiveTrue):withopen(file_path,r)asfile:file_textfile.read()text_linesfile_text.split(# )準(zhǔn)備 LLM 和Embedding模型本示例中使用 Qwen3-Embedding-0.6B 來進(jìn)行文本嵌入使用Qwen3-Reranker-0.6B對檢索的結(jié)果進(jìn)行重排序。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)輸出結(jié)果示例定義一個(gè)函數(shù)利用 Qwen3-Embedding-0.6B 模型生成文本嵌入。該函數(shù)將用于生成文檔嵌入和查詢嵌入。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()定義重排序函數(shù)以提升檢索質(zhì)量。這些函數(shù)使用Qwen3-Reranker實(shí)現(xiàn)完整的重排序管道根據(jù)文檔與查詢的相關(guān)性對候選文檔進(jìn)行評估和重新排序。其中各函數(shù)主要作用分別是format_instruction(): 將查詢、文檔和任務(wù)指令格式化為重排序模型的標(biāo)準(zhǔn)輸入格式process_inputs(): 對格式化后的文本進(jìn)行分詞編碼并添加特殊token用于模型判斷compute_logits(): 使用重排序模型計(jì)算“查詢-文檔”對的相關(guān)性得分0-1之間rerank_documents(): 基于查詢相關(guān)性對文檔進(jìn)行重新排序返回按相關(guān)性得分降序排列的文檔列表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生成一個(gè)測試向量并打印其維度以及前幾個(gè)元素。test_embeddingemb_text(This is a test)embedding_dimlen(test_embedding)print(embedding_dim)print(test_embedding[:10])結(jié)果示例1024[-0.009923271834850311,-0.030248118564486504,-0.011494234204292297,-0.05980192497372627,-0.0026795873418450356,0.016578301787376404,-0.04073038697242737,0.03180320933461189,-0.024417787790298462,2.1764861230622046e-05]將數(shù)據(jù)加載到Milvus創(chuàng)建集合frompymilvusimportMilvusClientmilvus_clientMilvusClient(uri./milvus_demo.db)collection_namemy_rag_collection關(guān)于MilvusClient的參數(shù)設(shè)置將URI設(shè)置為本地文件例如./milvus.db是最便捷的方法因?yàn)樗鼤?huì)自動(dòng)使用Milvus Lite將所有數(shù)據(jù)存儲(chǔ)在該文件中。如果你有大規(guī)模數(shù)據(jù)可以在Docker或Kubernetes上搭建性能更強(qiáng)的Milvus服務(wù)器。在這種情況下請使用服務(wù)器的URI例如http://localhost:19530作為你的URI。如果你想使用Zilliz CloudMilvus的全托管云服務(wù)請調(diào)整URI和令牌它們分別對應(yīng)Zilliz Cloud中的公共端點(diǎn)Public Endpoint和API密鑰Api key。檢查集合是否已經(jīng)存在如果存在則將其刪除。ifmilvus_client.has_collection(collection_name):milvus_client.drop_collection(collection_name)創(chuàng)建一個(gè)具有指定參數(shù)的新集合。如果未指定任何字段信息Milvus將自動(dòng)創(chuàng)建一個(gè)默認(rèn)的ID字段作為主鍵以及一個(gè)向量字段用于存儲(chǔ)向量數(shù)據(jù)。一個(gè)預(yù)留的JSON字段用于存儲(chǔ)未在schema中定義的字段及其值。milvus_client.create_collection(collection_namecollection_name,dimensionembedding_dim,metric_typeIP,# Inner product distance consistency_levelStrong, # Strong consistency level)插入集合逐行遍歷文本創(chuàng)建嵌入向量然后將數(shù)據(jù)插入Milvus。下面是一個(gè)新的字段text它是集合中的一個(gè)未定義的字段。 它將自動(dòng)創(chuàng)建一個(gè)對應(yīng)的text字段實(shí)際上它底層是由保留的JSON動(dòng)態(tài)字段實(shí)現(xiàn)的 你不用關(guān)心其底層實(shí)現(xiàn)。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)輸出結(jié)果示例 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}結(jié)合重排序技術(shù)增強(qiáng)RAG檢索數(shù)據(jù)我們來指定一個(gè)關(guān)于Milvus的常見問題。questionHow is data stored in milvus?在集合中搜索該問題并獲取具有最高語義匹配度的前10個(gè)候選答案然后使用重排序器來選出最佳的3個(gè)匹配項(xiàng)。# 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)讓我們來看看此次查詢的重新排序結(jié)果吧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))輸出結(jié)果示例從結(jié)果中我們可以看到Qwen3-Reranker的重排序效果明顯相關(guān)性得分區(qū)分度較好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構(gòu)建檢索增強(qiáng)生成RAG響應(yīng)將檢索到的文檔轉(zhuǎn)換為字符串格式。context\n.join([line_with_distance[0]forline_with_distanceinretrieved_lines_with_distances])為大語言模型提供系統(tǒng)提示system prompt和用戶提示user prompt。這個(gè)提示是通過從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根據(jù)提示生成響應(yīng)。 responseopenai_client.chat.completions.create(modelgpt-4o,messages[{role:system,content:SYSTEM_PROMPT},{role:user,content:USER_PROMPT},],)print(response.choices[0].message.content)輸出結(jié)果展示 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小結(jié)通過以上教程和輸出結(jié)果展示不難發(fā)現(xiàn)通義千問團(tuán)隊(duì)在Qwen3系列中推出的embedding和reranker模型表現(xiàn)相當(dāng)不錯(cuò)。這兩個(gè)模型的結(jié)合使用為RAG系統(tǒng)提供了一個(gè)相對完整且實(shí)用的解決方案。在設(shè)計(jì)理念上Embedding模型支持query和document的差異化處理體現(xiàn)了對檢索任務(wù)的深入理解Reranker采用交叉編碼器架構(gòu)能夠捕捉query-document間的精細(xì)交互教程中的兩階段檢索設(shè)計(jì)召回重排更是平衡了效率與精度。特別是Qwen3-Embedding-0.6B1024維和Qwen3-Reranker-0.6B都采用了相對輕量的參數(shù)規(guī)模支持本地部署減少了對外部API的依賴在保證性能的同時(shí)降低了硬件要求適合中小企業(yè)和個(gè)人開發(fā)者使用。事實(shí)上Qwen3系列推出embedding和reranker模型其實(shí)不是個(gè)例不是巧合而是產(chǎn)業(yè)共識(shí)。原因很簡單這兩個(gè)模塊決定了大模型是否具備產(chǎn)品化能力。生成式大模型最大的問題在于不確定性高、評估難、成本重。要解決以上問題無論是RAG、LLM Memory、Agent 本質(zhì)上都依賴一個(gè)前提能否將語義壓縮成機(jī)器可高效檢索和判斷的向量表達(dá)。Embedding 與 Ranking 則是目前的最優(yōu)路徑標(biāo)準(zhǔn)清晰、性能可測、成本可控、易于灰度。Embedding 決定你能不能“找得到”Ranking 決定你能不能“選得準(zhǔn)”。這使它們成為模型商品化最先跑通的 API 模塊之一調(diào)用頻率高每次檢索都需要、切換成本高與索引綁定、商業(yè)價(jià)值高可用作底層 infra。最后為什么要學(xué)AI大模型當(dāng)下??智能市場迎來了爆發(fā)期并逐漸進(jìn)?以??通?智能AGI為主導(dǎo)的新時(shí)代。企業(yè)紛紛官宣“ AI ”戰(zhàn)略為新興技術(shù)?才創(chuàng)造豐富的就業(yè)機(jī)會(huì)?才缺?將達(dá) 400 萬DeepSeek問世以來生成式AI和大模型技術(shù)爆發(fā)式增長讓很多崗位重新成了炙手可熱的新星崗位薪資遠(yuǎn)超很多后端崗位在程序員中穩(wěn)居前列。與此同時(shí)AI與各行各業(yè)深度融合飛速發(fā)展成為炙手可熱的新風(fēng)口企業(yè)非常需要了解AI、懂AI、會(huì)用AI的員工紛紛開出高薪招聘AI大模型相關(guān)崗位。最近很多程序員朋友都已經(jīng)學(xué)習(xí)或者準(zhǔn)備學(xué)習(xí) AI 大模型后臺(tái)也經(jīng)常會(huì)有小伙伴咨詢學(xué)習(xí)路線和學(xué)習(xí)資料我特別拜托北京清華大學(xué)學(xué)士和美國加州理工學(xué)院博士學(xué)位的魯為民老師給大家這里給大家準(zhǔn)備了一份涵蓋了AI大模型入門學(xué)習(xí)思維導(dǎo)圖、精品AI大模型學(xué)習(xí)書籍手冊、視頻教程、實(shí)戰(zhàn)學(xué)習(xí)等錄播視頻全系列的學(xué)習(xí)資料這些學(xué)習(xí)資料不僅深入淺出而且非常實(shí)用讓大家系統(tǒng)而高效地掌握AI大模型的各個(gè)知識(shí)點(diǎn)。這份完整版的大模型 AI 學(xué)習(xí)資料已經(jīng)上傳CSDN朋友們?nèi)绻枰梢晕⑿艗呙柘路紺SDN官方認(rèn)證二維碼免費(fèi)領(lǐng)取【保證100%免費(fèi)】AI大模型系統(tǒng)學(xué)習(xí)路線在面對AI大模型開發(fā)領(lǐng)域的復(fù)雜與深入精準(zhǔn)學(xué)習(xí)顯得尤為重要。一份系統(tǒng)的技術(shù)路線圖不僅能夠幫助開發(fā)者清晰地了解從入門到精通所需掌握的知識(shí)點(diǎn)還能提供一條高效、有序的學(xué)習(xí)路徑。但知道是一回事做又是另一回事初學(xué)者最常遇到的問題主要是理論知識(shí)缺乏、資源和工具的限制、模型理解和調(diào)試的復(fù)雜性在這基礎(chǔ)上找到高質(zhì)量的學(xué)習(xí)資源不浪費(fèi)時(shí)間、不走彎路又是重中之重。AI大模型入門到實(shí)戰(zhàn)的視頻教程項(xiàng)目包看視頻學(xué)習(xí)是一種高效、直觀、靈活且富有吸引力的學(xué)習(xí)方式可以更直觀地展示過程能有效提升學(xué)習(xí)興趣和理解力是現(xiàn)在獲取知識(shí)的重要途徑光學(xué)理論是沒用的要學(xué)會(huì)跟著一起敲要?jiǎng)邮謱?shí)操才能將自己的所學(xué)運(yùn)用到實(shí)際當(dāng)中去這時(shí)候可以搞點(diǎn)實(shí)戰(zhàn)案例來學(xué)習(xí)。海量AI大模型必讀的經(jīng)典書籍PDF閱讀AI大模型經(jīng)典書籍可以幫助讀者提高技術(shù)水平開拓視野掌握核心技術(shù)提高解決問題的能力同時(shí)也可以借鑒他人的經(jīng)驗(yàn)。對于想要深入學(xué)習(xí)AI大模型開發(fā)的讀者來說閱讀經(jīng)典書籍是非常有必要的。600AI大模型報(bào)告實(shí)時(shí)更新這套包含640份報(bào)告的合集涵蓋了AI大模型的理論研究、技術(shù)實(shí)現(xiàn)、行業(yè)應(yīng)用等多個(gè)方面。無論您是科研人員、工程師還是對AI大模型感興趣的愛好者這套報(bào)告合集都將為您提供寶貴的信息和啟示。AI大模型面試真題答案解析我們學(xué)習(xí)AI大模型必然是想找到高薪的工作下面這些面試題都是總結(jié)當(dāng)前最新、最熱、最高頻的面試題并且每道題都有詳細(xì)的答案面試前刷完這套面試題資料小小offer不在話下這份完整版的大模型 AI 學(xué)習(xí)資料已經(jīng)上傳CSDN朋友們?nèi)绻枰梢晕⑿艗呙柘路紺SDN官方認(rèn)證二維碼免費(fèi)領(lǐng)取【保證100%免費(fèi)】