前言
就在昨晚,DeepSeek v3推出了新版本V3-0324,再次一夜爆火。
雖然官方表示“這只是一次小升級”“API接口和使用方式不變”,但經過Zilliz的第一時間實測,我們發現無論是邏輯能力,還是編程能力,相較原本的v3,都有了極大提升,甚至可以說,這是目前市面上最頂級的開源非推理模型。
具體來說,改變有以下三點:
參數量上,V3-0324為6850億,較上一版本的6710億略有提升;
上下文窗口上,V3-0324升級到了 128k
能力上,V3-0324的代碼、數學、邏輯等能力有了較大提升。
整個V3的設計依然延用了V2提出的MLA(Multi-head Latent Attention)架構,既多頭潛在注意力機制,通過潛在向量(latent vector)對Key-Value(KV)緩存進行壓縮,從而減少推理時的內存占用和計算開銷。
此外DeepSeek V3將除前三層外的所有 FFN 層替換為 MoE 層,每個 MoE 層包含 1 個共享專家和 256 個路由專家。在路由專家中,每個 token 將激活 評分靠前的幾個專家,以平衡整體的計算量。
另外,圍繞準確率的提升,DeepSeek V3還使用了多token預測(MTP),通過多層級聯預測機制,打破了傳統模型只能一次多預測一個token的限制,讓每個 token同時預測多個未來token,進而提升模型性能和數據效率。
那么DeepSeek v3-0324該如何用?企業場景應該如何部署?如何將它與RAG結合?本篇文章,我們會對DeepSeek v3的亮點進行梳理,并對其RAG搭建流程與效果,做一個簡單的示例。
01
使用Milvus和DeepSeek搭建RAG
接下來,我們將展示如何使用Milvus和DeepSeek構建檢索增強生成(RAG)pipeline。
步驟一:準備
(1)依賴和環境
! pip install --upgrade "pymilvus[model]" openai requests tqdm
如果您使用的是Google Colab,要啟用剛剛安裝的依賴項,您可能需要重啟運行環境(單擊屏幕頂部的“Runtime”菜單,然后從下拉框中選擇“Restart session”)。
(2)準備數據
我們使用Milvus文檔2.4. x中的FAQ頁面作為RAG中的私有知識,這是搭建一個入門RAG pipeline的優質數據源。
首先,下載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文件各個主要部分的內容。
from glob import globtext_lines = []for file_path in glob("milvus_docs/en/faq/*.md", recursive=True):with open(file_path, "r") as file:file_text = file.read()text_lines += file_text.split("# ")
(3)準備LLM和embedding模型
OpenRouter是一家提供統一API接口聚合多個主流AI模型(如DeepSeek、Claude等)的服務平臺,讓開發者通過單一接入點調用不同大語言模型。在OpenRouter上免費創建DeepSeek V3的API秘鑰就可以嘗鮮使用DeepSeek V3 0324。(其他平臺操作同理,目前僅OpenRouter上線了最新版V3)
from openai import OpenAIdeepseek_client = OpenAI(api_key="<OPENROUTER_API_KEY>",base_url="https://openrouter.ai/api/v1",
)
選擇一個embedding模型,使用milvus_model來做文本向量化。我們以DefaultEmbeddingFunction模型為例,它是一個預訓練的輕量級embedding模型。
from pymilvus import model as milvus_modelembedding_model = milvus_model.DefaultEmbeddingFunction()
生成測試向量,并輸出向量維度以及測試向量的前幾個元素。
test_embedding = embedding_model.encode_queries(["This is a test"])[0]
embedding_dim = len(test_embedding)
print(embedding_dim)
print(test_embedding[:10])
768
[-0.04836066 0.07163023 -0.01130064 -0.03789345 -0.03320649 -0.01318448-0.03041712 -0.02269499 -0.02317863 -0.00426028]
步驟二:將數據加載到Milvus
創建集合
from pymilvus import MilvusClientmilvus_client = MilvusClient(uri="./milvus_demo.db")collection_name = "my_rag_collection"
對于MilvusClient需要說明:
將uri設置為本地文件,例如./milvus. db,是最方便的方法,因為它會自動使用Milvus Lite將所有數據存儲在此文件中。
如果你有大規模數據,你可以在docker或kubernetes上設置一個更高性能的Milvus服務器。在此設置中,請使用服務器uri,例如http://localhost:19530,作為你的uri。
如果要使用Milvus的全托管云服務Zilliz Cloud,請調整uri和token,分別對應Zilliz Cloud中的Public Endpoint和Api密鑰。
檢查集合是否已經存在,如果存在則將其刪除。
if milvus_client.has_collection(collection_name):milvus_client.drop_collection(collection_name)
使用指定的參數創建一個新集合。
如果我們不指定任何字段信息,Milvus將自動為主鍵創建一個默認的id字段,并創建一個向量字段來存儲向量數據。保留的JSON字段用于存儲未在schema里定義的標量數據。
milvus_client.create_collection(collection_name=collection_name,dimension=embedding_dim,metric_type="IP", # Inner product distanceconsistency_level="Strong", # Strong consistency level
)
插入數據
逐條取出文本數據,創建嵌入,然后將數據插入Milvus。
這里有一個新的字段“text”,它是集合schema中的非定義字段,會自動添加到保留的JSON動態字段中。
from tqdm import tqdmdata = []doc_embeddings = embedding_model.encode_documents(text_lines)for i, line in enumerate(tqdm(text_lines, desc="Creating embeddings")):data.append({"id": i, "vector": doc_embeddings[i], "text": line})milvus_client.insert(collection_name=collection_name, data=data)
Creating embeddings: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 72/72 [00:00<00:00, 1222631.13it/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的常見問題。
question = "How is data stored in milvus?"
在集合中搜索問題并檢索語義top-3匹配項。
search_res = milvus_client.search(collection_name=collection_name,data=embedding_model.encode_queries([question]), # Convert the question to an embedding vectorlimit=3, # Return top 3 resultssearch_params={"metric_type": "IP", "params": {}}, # Inner product distanceoutput_fields=["text"], # Return the text field
)
我們來看一下query的搜索結果
import jsonretrieved_lines_with_distances = [(res["entity"]["text"], res["distance"]) for res in search_res[0]
]
print(json.dumps(retrieved_lines_with_distances, indent=4))
[[" 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/?nc1=h_ls), [Google Cloud Storage](https://cloud.google.com/storage?hl=en#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.6572665572166443],["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.6312146186828613],["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.6115777492523193]
]
使用LLM獲取RAG響應
將檢索到的文檔轉換為字符串格式。
context = "\n".join([line_with_distance[0] for line_with_distance in retrieved_lines_with_distances]
)
為LLM定義系統和用戶提示。這個提示是由從Milvus檢索到的文檔組裝而成的。
SYSTEM_PROMPT = """
Human: You are an AI assistant. You are able to find answers to the questions from the contextual passage snippets provided.
"""
USER_PROMPT = f"""
Use the following pieces of information enclosed in <context> tags to provide an answer to the question enclosed in <question> tags.
<context>
{context}
</context>
<question>
{question}
</question>
"""
使用DeepSeek提供的deepseek/deepseek-chat-v3-0324模型根據提示生成響應。
response = deepseek_client.chat.completions.create(model="deepseek/deepseek-chat-v3-0324",messages=[{"role": "system", "content": SYSTEM_PROMPT},{"role": "user", "content": USER_PROMPT},],
)
print(response.choices[0].message.content)
Milvus stores data in two main categories: inserted data and metadata.1. **Inserted Data**:- This includes vector data, scalar data, and collection-specific schema.- The data is stored in persistent storage as incremental logs.- Milvus supports various object storage backends for this purpose, such as MinIO, AWS S3, Google Cloud Storage (GCS), Azure Blob Storage, Alibaba Cloud OSS, and Tencent Cloud Object Storage (COS).2. **Metadata**:- Metadata is generated internally by Milvus and is specific to each module.- This metadata is stored in etcd.For vector data, Milvus supports multiple types including Binary, Float32, Float16, and BFloat16, each with different precision and use cases. The data is initially loaded into a message queue upon insertion and is later written to persistent storage by the data node, either during regular operations or immediately if `flush()` is called.
至此,通過Milvus和DeepSeek構建了一個RAG pipeline的完整流程正式完成。
02
對比:原裝版DeepSeek-V3-0324 ?VS RAG版
在搭建好一個最基本的RAG之后,接下來,我們對原裝版DeepSeek-V3-0324 以及結合了Milvus+DeepSeek-V3-0324的RAG版本做一下效果對比。
對比問題可以選用"write HTML code to create a fancy website about Milvus"。
我們直接在官網的對話框關閉【深度思考】,然后直接提問,效果如下:
下面是集成了Milvus的RAG版本,將上文代碼中的question進行替換
question="write HTML code to create a fancy website about Milvus"
生成的網站如下:
通過對比我們可以看到,在整體審美風格比較接近的情況下,RAG版網站,無論是產品簡介,還是具體的亮點分析,都更加符合實際的Milvus特性。建立在本地文檔所提供的更豐富的語料語料基礎上,給出的整體網站內容也會更加豐富、專業,不空洞。
03
寫在最后
其實不只是代碼能力,在數學推理、長文本閱讀推理等方面,新版V3均有較大程度的提升,幾乎與Claude 3.7 Sonnet 媲美。
另外,我們通過實驗發現,相比前一段時間風很大的推理模型,作為非推理模型的新版V3,雖然不會在回答中給出詳細的推理過程,但其答案生成仍展現了一定的邏輯與思考能力。
與之形成對比,推理模型做RAG與Agent ,效果很好,但卻會對一些簡單的指令判斷,做一大堆分析與反復思考確認,不僅消耗過多token,回答速度還慢。
而新版V3,對于一些成本敏感的場景來說,或許帶來了問題的新解法。
更進一步,這也印證了諸如 OpenAI 的大模型廠商,后續不再區別推理和非推理模型背后的邏輯,模型服務應該自動根據需求判斷是否推理,以節省不必要的 token 開銷。
作者介紹
王舒虹
Zilliz?Social Media Advocate
推薦閱讀