RAG系统重构实战:类封装、参数化设计与工程化升级
2026/4/6 14:38:24 网站建设 项目流程
从脚本到工程项目的转变前面咱们写了很多的代码但是代码本质上还是处于零散状态的学生脚本这种东西和企业输出的工程师项目还是有很大差距的今天咱们就开始把脚本一步步封装暴露功能RAG系统一点点开始有一点工程的味道。下面我们就一步步开始吧第一步先创建一个RAG系统的类classRAGSystem:def__init__(self,chunks):self.chunkschunks self.indexNoneself.embeddingsNone有了类以后我们需要在类中封装一些核心的功能包括存知识数据、相关知识检索以及提供可以完成询问功能的接口defbuild_index(self):# 把FAISS封进去embeddings[get_embedding(c)forcinself.chunks]self.embeddingsnp.vstack(embeddings)dimself.embeddings.shape[1]self.indexfaiss.IndexFlatL2(dim)self.index.add(self.embeddings)defretrieve(self,query,k5):#封装retrievequery_vecget_embedding(query).reshape(1,-1)distances,indicesself.index.search(query_vec,k)return[self.chunks[i]foriinindices[0]]defask(self,question):retrievedself.retrieve(question,k5)# 可以加 rerank咱们前面写过context\n.join(retrieved)responseclient.chat.completions.create(modeldeepseek-chat,messages[{role:system,content:Answer based only on context.},{role:user,content:context\n\nquestion}])returnresponse.choices[0].message.content有了这样一个类咱们的系统再使用的时候只需要ragRAGSystem(chunks)rag.build_index()answerrag.ask(What is the core contribution?)print(answer)这样便实现了一个完整的可调用的功能模块全部代码我这里给大家贴出来importfaissimportnumpyasnpfromopenaiimportOpenAI clientOpenAI(api_key,base_urlhttps://api.deepseek.com)client2OpenAI(api_key,base_urlhttps://api.shubiaobiao.com/v1)defget_embedding(text):responseclient2.embeddings.create(modeltext-embedding-3-small,# 改这里inputtext)returnnp.array(response.data[0].embedding,dtypefloat32)defsplit_text(text,chunk_size200,overlap50):chunks[]foriinrange(0,len(text),chunk_size-overlap):chunks.append(text[i:ichunk_size])returnchunksclassRAGSystem:def__init__(self,chunks):self.chunkschunks self.indexNoneself.embeddingsNonedefbuild_index(self):embeddings[get_embedding(c)forcinself.chunks]self.embeddingsnp.vstack(embeddings)dimself.embeddings.shape[1]self.indexfaiss.IndexFlatL2(dim)self.index.add(self.embeddings)defretrieve(self,query,k5):query_vecget_embedding(query).reshape(1,-1)distances,indicesself.index.search(query_vec,k)return[self.chunks[i]foriinindices[0]]defask(self,question):retrievedself.retrieve(question,k5)# 可以加 rerank咱们前面写过context\n.join(retrieved)responseclient.chat.completions.create(modeldeepseek-chat,messages[{role:system,content:Answer based only on context.},{role:user,content:context\n\nquestion}])returnresponse.choices[0].message.contentif__name____main__:contextVoice over IP (VoIP) steganography based on low-bit-rate speech codecs has attracted increasing attention due to its high imperceptibility and large embedding capacity, particularly in the fixed codebook (FCB) parameter domain. However, effective steganalysis remains challenging under extreme embedding conditions. At low embedding rates, steganographic artifacts are weak and sparsely distributed, making them difficult to distinguish from natural speech variations. In contrast, at high embedding rates, the recompression-based calibration process may introduce structural distortions that interfere with reliable feature extraction. To address these challenges, this paper proposes a calibration-aware cross-view steganalysis netword for VoIP steganalysis (CACVAN). An embedding-rate-aware data augmentation (ERADA) strategy is first introduced to construct cross-intensity training samples, which improves the robustness of the model under embedding-rate mismatch scenarios. Furthermore, a cross-view interaction backbone (CVIB) is designed to jointly analyze the original speech stream and its recompressed counterpart, enabling the network to capture subtle inconsistencies introduced by steganographic embedding while suppressing content-related variations. A hybrid attention refinement neck (HARN) is then employed to enhance discriminative feature responses and stabilize the modeling of sparse steganographic artifacts. Extensive experiments on public VoIP steganalysis datasets demonstrate that the proposed method consistently outperforms existing state-of-the-art approaches under various embedding rates and speech durations, especially in challenging scenarios involving low embedding rates and short speech segments. Moreover, the proposed framework achieves high computational efficiency and satisfies the real-time requirements of streaming VoIP steganalysis, indicating its practical applicability.chunkssplit_text(context,chunk_size200)ragRAGSystem(chunks)rag.build_index()answerrag.ask(What is the core contribution?)print(answer)封装基础上2次优化增加缓存减少API调用的费用有了封装后的接口后呢咱们还可以对封装后的版本进行2次优化而且这里首先就会遇到一个坑前面的代码关于嵌入的操作是这样写的embeddings[get_embedding(c)forcinself.chunks]这样会遇到一个问题每次调用都会重新调用API慢贵所以咱们需要有点加缓存的意识加上缓存后系统会将过去嵌入过的知识存贮在类中这样下次调用的时候就可以不用再调词嵌入的接口了可以省下很多小钱钱。defbuild_index(self):ifself.embeddingsisNone:embeddings[get_embedding(c)forcinself.chunks]self.embeddingsnp.vstack(embeddings)#这一步便是缓存嵌入后的结果被装入类变量dimself.embeddings.shape[1]self.indexfaiss.IndexFlatL2(dim)self.index.add(self.embeddings)增加Rerank操作前面讲过为了提高模型的质量咱们可以在模型中增加rerank操作这样可以让模型结合知识的相关程度回答问题大大提高模型效果所以咱们这次将rerank操作封装在这个类内defrerank(self,query,chunks):promptf You are a ranking assistant. Query:{query}Rank the following passages from most relevant to least relevant. Passages: fori,cinenumerate(chunks):promptf\n[{i}]{c}\nprompt\nReturn ONLY the indices in sorted order, like [2,0,1].responseclient.chat.completions.create(modeldeepseek-chat,messages[{role:user,content:prompt}])importast# 可以将字符串传化为list的工具包try:returnast.literal_eval(response.choices[0].message.content)except:returnlist(range(len(chunks)))修改ask前面增加了rerank操作后咱们的ask操作可以跟着升级将Rerank后的结果加进去defask(self,question):retrievedself.retrieve(question,k5)# reranksorted_indicesself.rerank(question,retrieved)best_chunks[retrieved[i]foriinsorted_indices[:3]]context\n.join(best_chunks)responseclient.chat.completions.create(modeldeepseek-chat,messages[{role:system,content:Answer based only on context.},{role:user,content:context\n\nquestion}])returnresponse.choices[0].message.content最后进行一定的参数化那么这个模块运行起来的就会很灵活def__init__(self,chunks,top_k5,rerank_k3):self.chunkschunks self.top_ktop_k self.rerank_krerank_k self.indexNoneself.embeddingsNone然后修改retrievedself.retrieve(question,kself.top_k)best_chunks[retrieved[i]foriinsorted_indices[:self.rerank_k]]到这里咱们做的不再是一个可运行的脚本了是一个可运行可调的系统完整代码如下importfaissimportnumpyasnpfromopenaiimportOpenAI clientOpenAI(api_key,base_urlhttps://api.deepseek.com)client2OpenAI(api_key,base_urlhttps://api.shubiaobiao.com/v1)defget_embedding(text):responseclient2.embeddings.create(modeltext-embedding-3-small,# 改这里inputtext)returnnp.array(response.data[0].embedding,dtypefloat32)defsplit_text(text,chunk_size200,overlap50):chunks[]foriinrange(0,len(text),chunk_size-overlap):chunks.append(text[i:ichunk_size])returnchunksclassRAGSystem:def__init__(self,chunks,top_k5,rerank_k3):self.chunkschunks self.top_ktop_k self.rerank_krerank_k self.indexNoneself.embeddingsNonedefbuild_index(self):ifself.embeddingsisNone:#加缓存避免重复计算消耗APIembeddings[get_embedding(c)forcinself.chunks]self.embeddingsnp.vstack(embeddings)dimself.embeddings.shape[1]self.indexfaiss.IndexFlatL2(dim)self.index.add(self.embeddings)defretrieve(self,query,k5):query_vecget_embedding(query).reshape(1,-1)distances,indicesself.index.search(query_vec,k)return[self.chunks[i]foriinindices[0]]defrerank(self,query,chunks):promptf You are a ranking assistant. Query:{query}Rank the following passages from most relevant to least relevant. Passages: fori,cinenumerate(chunks):promptf\n[{i}]{c}\nprompt\nReturn ONLY the indices in sorted order, like [2,0,1].responseclient.chat.completions.create(modeldeepseek-chat,messages[{role:user,content:prompt}])importasttry:returnast.literal_eval(response.choices[0].message.content)except:returnlist(range(len(chunks)))defask(self,question):retrievedself.retrieve(question,kself.top_k)# 可以加 rerank你已经有了#context \n.join(retrieved)sorted_indicesself.rerank(question,retrieved)best_chunks[retrieved[i]foriinsorted_indices[:self.rerank_k]]context\n.join(best_chunks)responseclient.chat.completions.create(modeldeepseek-chat,messages[{role:system,content:Answer based only on context.},{role:user,content:context\n\nquestion}])returnresponse.choices[0].message.contentif__name____main__:contextVoice over IP (VoIP) steganography based on low-bit-rate speech codecs has attracted increasing attention due to its high imperceptibility and large embedding capacity, particularly in the fixed codebook (FCB) parameter domain. However, effective steganalysis remains challenging under extreme embedding conditions. At low embedding rates, steganographic artifacts are weak and sparsely distributed, making them difficult to distinguish from natural speech variations. In contrast, at high embedding rates, the recompression-based calibration process may introduce structural distortions that interfere with reliable feature extraction. To address these challenges, this paper proposes a calibration-aware cross-view steganalysis netword for VoIP steganalysis (CACVAN). An embedding-rate-aware data augmentation (ERADA) strategy is first introduced to construct cross-intensity training samples, which improves the robustness of the model under embedding-rate mismatch scenarios. Furthermore, a cross-view interaction backbone (CVIB) is designed to jointly analyze the original speech stream and its recompressed counterpart, enabling the network to capture subtle inconsistencies introduced by steganographic embedding while suppressing content-related variations. A hybrid attention refinement neck (HARN) is then employed to enhance discriminative feature responses and stabilize the modeling of sparse steganographic artifacts. Extensive experiments on public VoIP steganalysis datasets demonstrate that the proposed method consistently outperforms existing state-of-the-art approaches under various embedding rates and speech durations, especially in challenging scenarios involving low embedding rates and short speech segments. Moreover, the proposed framework achieves high computational efficiency and satisfies the real-time requirements of streaming VoIP steganalysis, indicating its practical applicability.chunkssplit_text(context,chunk_size200)ragRAGSystem(chunks)rag.build_index()answerrag.ask(What is the core contribution?)print(answer)如果这篇文章对你有帮助可以点个赞完整代码地址https://github.com/1186141415/A-Paper-Rag-Agent

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询