asp 網(wǎng)站源代碼電商平臺有哪些
最近,DeepSeek v3(一個MoE模型,擁有671B參數(shù),其中37B參數(shù)被激活)模型全球爆火。
作為一款能與Claude 3.5 Sonnet,GPT-4o等模型匹敵的開源模型DeepSeek v3不僅將其算法開源,還放出一份扎實的技術報告,詳盡描述了DeepSeek是如何進行大模型架構、算法工程協(xié)同設計,部署,訓練,數(shù)據(jù)處理等方面的思考,堪稱是一份DeepSeek給開源社區(qū)送上的年末大禮。
本篇文章,我們會對DeepSeek v3的亮點進行梳理,并對其RAG搭建流程與效果,做一個簡單的示例。
01.
DeepSeek v3的亮點

亮點一:超低的訓練成本,將帶來算力的極大富余
相比于海外大廠動輒上萬甚至上十萬的H100集群(例如Meta使用了16K的H100訓練Llama3.1 405B),DeepSeek僅僅使用了2048張丐版顯卡H800就在14.8T數(shù)據(jù)上訓練了一個處于第一梯隊的開源模型。以下是DeepSeek v3的訓練成本數(shù)據(jù)。

不難看出,基于以上數(shù)據(jù),傳統(tǒng)對大模型對算力的供需預測推演直接被推翻,過去Scaling law曲線所估算出的GPU需求數(shù)量會出現(xiàn)極大冗余。
那么問題來了,DeepSeek v3是如何做到的?
亮點二:顛覆GPT架構,極致的工程設計
在去年,大模型領域普遍認為模型的設計已經(jīng)收斂到Decoder-only的GPT架構,但DeepSeek依然沒有放棄對模型架構的進一步探索。
這一次V3的設計延用了V2提出的MLA(Multi-head Latent Attention),這是一種通過低秩壓縮鍵值對來減少緩存需求的創(chuàng)新架構,以提高Transformer模型的推理效率。
另外,此次的MoE模型規(guī)格也比之前大了許多(V3 671B, V2 236B),也體現(xiàn)出了對這個架構擁有更多的信心和經(jīng)驗。DeepSeek V3將除前三層外的所有 FFN 層替換為 MoE 層。每個 MoE 層包含 1 個共享專家和 256 個路由專家。在路由專家中,每個 token 將激活 8 個專家,并確保每個 token 最多會被發(fā)送到 4 個節(jié)點。
同時,論文還對如何在系統(tǒng)中設計將這種架構進行推理的性能優(yōu)化也進行了詳盡的描述。
DeepSeek V3使用了多token預測(MTP),即每個 token 除了精確預測下一個 token 外,還會預測一個額外的 token,通過投機采樣的方式提高推理效率。
關于如何使用FP8進行模型訓練這個各個大模型工程團隊頭痛的問題,DeepSeek V3也對自己的實踐有細致的描述,對這部分感興趣的朋友強烈推薦閱讀論文原文。
亮點三:通過蒸餾推理模型進行后訓練
自從OpenAI發(fā)布了o1模型之后,業(yè)界開始逐漸興起了探索這種內置思維璉(CoT)的模型,它不斷對中間結果探索分析的過程仿佛人的“慢思考”。DeepSeek同樣也開發(fā)了類似的R1模型,在DeepSeek V3中,DeepSeek創(chuàng)新性地通過在后訓練階段使用R1得到的高質量答案來提高了自身的性能。這一點也非常有趣。
眾所周知,類似o1的開源模型大部分都是從基礎模型利用CoT結合強化學習的技巧訓練出來提高了推理效果,而現(xiàn)在又通過蒸餾推理模型獲得了下一代更好的基礎模型,這一種模型和數(shù)據(jù)質量互相交織的發(fā)展模式貫穿著機器學習發(fā)展的歷史,而還將繼續(xù)被見證。
而以發(fā)掘非結構化數(shù)據(jù)價值的廠商Zilliz也相信對于數(shù)據(jù)和知識的高效管理,將會一直在智能化浪潮發(fā)展中扮演著重要的角色。
看到10K$的后訓練成本,相信許多致力于微調專屬大模型的廠商都躍躍欲試,在這里我們也來看一下DeepSeek V3的后訓練過程,整個流程也比傳統(tǒng)的SFT要復雜一些。整個過程分成了SFT階段(監(jiān)督學習)以及RL階段(強化學習),在SFT階段,他們將數(shù)據(jù)分成了兩種類型,推理數(shù)據(jù)以及非推理數(shù)據(jù)
推理數(shù)據(jù):
包括數(shù)學,編程這些問題,DeepSeek訓練了針對性的專家模型,并使用專家模型為每一個問題生成了兩種格式的學習數(shù)據(jù)。
<problem, original response>
<system prompt, problem, R1 response>
非推理數(shù)據(jù):
對于非推理任務(如創(chuàng)意寫作和簡單問答),作者利用DeepSeek-V2.5模型生成初步響應,并聘請人工標注員對其準確性進行驗證
訓練的流程:
SFT階段,使用基于專家模型生成的SFT樣本,進行初步的監(jiān)督微調。通過這些訓練數(shù)據(jù),模型學習如何根據(jù)問題和回答生成精確的推理響應。
RL階段,使用高溫采樣來生成響應,這些響應融合了來自原始數(shù)據(jù)和R1生成數(shù)據(jù)的模式。在RL階段,會使用LeetCode編譯器來檢查編程的答案,以及一些規(guī)則來去檢查數(shù)學問題的答案,對于開放性問題, 會用一個獎勵模型來去判斷。該過程幫助模型在沒有顯式系統(tǒng)提示的情況下進行推理,經(jīng)過數(shù)百次RL步驟,模型學會如何平衡準確與簡潔性的答案。
完成RL訓練后,作者實施拒絕采樣策略(過濾掉模型認為低質量的數(shù)據(jù)),以從生成的樣本中挑選出高質量的SFT數(shù)據(jù)。這些數(shù)據(jù)用于最終模型的微調。
不難發(fā)現(xiàn),做好一個高質量的后訓練,下的功夫遠遠不止10k$的訓練算力。
DeepSeek V3雖然擁有可以與閉源模型匹敵的性能,但是部署它依然不是一個簡單的事,即使作者已經(jīng)為了推理優(yōu)化做了許多工作,但搭建一個DeepSeek V3的服務(考慮到它671B的參數(shù)量),成本依然不低。
02.
使用Milvus和DeepSeek搭建RAG
接下來,我們將展示如何使用Milvus和DeepSeek構建檢索增強生成(RAG)pipeline。
2.1 準備
2.1.1 依賴和環(huán)境
pip?install?--upgrade?pymilvus[model]?openai?requests?tqdm
如果您使用的是Google Colab,要啟用剛剛安裝的依賴項,您可能需要重啟運行環(huán)境(單擊屏幕頂部的“Runtime”菜單,然后從下拉框中選擇“Restart session”)。
DeepSeek啟動了OpenAI風格的API。您可以登錄官網(wǎng)并將api密鑰 DEEPSEEK_API_KEY準備為環(huán)境變量。
import?osos.environ["DEEPSEEK_API_KEY"]?=?"***********"
2.1.2?準備數(shù)據(jù)
我們使用Milvus文檔2.4. x(https://github.com/milvus-io/milvus-docs/releases/download/v2.4.6-preview/milvus_docs_2.4.x_en.zip)中的FAQ頁面作為RAG中的私有知識,這是搭建一個入門RAG pipeline的優(yōu)質數(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文件,對于每個文檔,我們只需簡單地使用“#”來分隔文件中的內容,就可以大致分隔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("#?")
2.1.3?準備LLM和embedding模型
DeepSeek采用了類OpenAI風格的API,您可以使用相同的API并對相應的LLM進行微調。
from?openai?import?OpenAIdeepseek_client?=?OpenAI(api_key=os.environ["DEEPSEEK_API_KEY"],base_url="https://api.deepseek.com",
)
選擇一個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]
2.2 將數(shù)據(jù)加載到Milvus
2.2.1 創(chuàng)建集合
from?pymilvus?import?MilvusClientmilvus_client?=?MilvusClient(uri="./milvus_demo.db")collection_name?=?"my_rag_collection"
對于
MilvusClient
需要說明:
將
uri
設置為本地文件,例如./milvus. db
,是最方便的方法,因為它會自動使用Milvus Lite將所有數(shù)據(jù)存儲在此文件中。如果你有大規(guī)模數(shù)據(jù),你可以在docker或kubernetes上設置一個更高性能的Milvus服務器。在此設置中,請使用服務器uri,例如
http://localhost:19530
,作為你的uri
。如果要使用Milvus的全托管云服務Zilliz Cloud,請調整
uri
和token
,分別對應Zilliz Cloud中的公共端點和Api密鑰。
檢查集合是否已經(jīng)存在,如果存在則將其刪除。
if?milvus_client.has_collection(collection_name):milvus_client.drop_collection(collection_name)
使用指定的參數(shù)創(chuàng)建一個新集合。
如果我們不指定任何字段信息,Milvus將自動為主鍵創(chuàng)建一個默認的id字段,并創(chuàng)建一個向量字段來存儲向量數(shù)據(jù)。保留的JSON字段用于存儲未在schema里定義的標量數(shù)據(jù)。
milvus_client.create_collection(collection_name=collection_name,dimension=embedding_dim,metric_type="IP",??#?Inner?product?distanceconsistency_level="Strong",??#?Strong?consistency?level
)
2.2.2?插入數(shù)據(jù)
逐條取出文本數(shù)據(jù),創(chuàng)建嵌入,然后將數(shù)據(jù)插入Milvus。
這里有一個新的字段“text”,它是集合schema中的非定義字段,會自動添加到保留的JSON動態(tài)字段中。
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:???0%|??????????|?0/72?[00:00<?,??it/s]huggingface/tokenizers:?The?current?process?just?got?forked,?after?parallelism?has?already?been?used.?Disabling?parallelism?to?avoid?deadlocks...
To?disable?this?warning,?you?can?either:-?Avoid?using?`tokenizers`?before?the?fork?if?possible-?Explicitly?set?the?environment?variable?TOKENIZERS_PARALLELISM=(true?|?false)
Creating?embeddings:?100%|██████████|?72/72?[00:00<00:00,?246522.36it/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}
2.3 構建RAG
2.3.1 檢索查詢數(shù)據(jù)
讓我們指定一個關于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]
]
2.3.2 使用LLM獲取RAG響應
將檢索到的文檔轉換為字符串格式。
context?=?"\n".join([line_with_distance[0]?for?line_with_distance?in?retrieved_lines_with_distances]
)
為LLM定義系統(tǒng)和用戶提示。這個提示是由從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-chat
模型根據(jù)提示生成響應。
response?=?deepseek_client.chat.completions.create(model="deepseek-chat",messages=[{"role":?"system",?"content":?SYSTEM_PROMPT},{"role":?"user",?"content":?USER_PROMPT},],
)
print(response.choices[0].message.content)
In?Milvus,?data?is?stored?in?two?main?categories:?inserted?data?and?metadata.1.?**Inserted?Data**:?This?includes?vector?data,?scalar?data,?and?collection-specific?schema.?The?inserted?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?within?Milvus?and?is?specific?to?each?Milvus?module.?This?metadata?is?stored?in?etcd,?a?distributed?key-value?store.Additionally,?when?data?is?inserted,?it?is?first?loaded?into?a?message?queue,?and?Milvus?returns?success?at?this?stage.?The?data?is?then?written?to?persistent?storage?as?incremental?logs?by?the?data?node.?If?the?`flush()`?function?is?called,?the?data?node?is?forced?to?write?all?data?in?the?message?queue?to?persistent?storage?immediately.
太好了!現(xiàn)在我們已經(jīng)成功使用Milvus和DeepSeek構建了一個RAG pipeline。
作者介紹
王翔宇
Zilliz 算法工程師
推薦閱讀