基于 DeepSeek R1 和 Ollama 开发 RAG 系统

大家好,我是玄姐。

正文开始之前,先给我自己打个广告,大家过年好为了回馈粉丝们的支持,原价199元的《3天 AI Agent 智能体项目实战直播训练营》直接降价到19元今天再开放一天报名特权,仅限99名。

回到正题。

今天,我们探讨一下如何利用目前最受欢迎的开源推理工具 DeepSeek R1 和轻量级的本地AI模型执行框架 Ollama,来构建一个功能强大的 RAG (Retrieval-Augmented Generation)系统。

1

DeepSeek R1:RAG 系统的卓越之选

DeepSeek R1,被誉为开启 AI 推理新时代的开源先锋,在构建 RAG 系统方面表现卓越,拥有众多引人注目的优势,成为开发者不可或缺的利器。相较于OpenAI 的 o1 模型,DeepSeek R1 在性能上与之媲美,但成本却大幅下降,仅占 o1 的 5%,这一经济性使得更广泛的开发者和企业能够轻松采用,推动了 RAG 技术的普及。

在信息检索方面,DeepSeek R1 展现了其卓越的专注力。在生成答案的过程中,仅需引用3个文档片段,便能精确提炼关键信息,有效排除了无关内容的干扰,显著提高了检索效率和回答的精确度。这一特性使得即便在处理大量文档时,系统也能迅速锁定关键内容,向用户提供简洁而高效的答案。

90a1495f3f1ade87256fc30ec7eeb1a6.png

面对复杂问题或不确定答案的情况,DeepSeek R1 的严格提示机制发挥了关键作用。与其他模型可能随意生成答案不同,DeepSeek R1 在不确定时会坦白回答“我不知道”,这种严谨性有效防止了幻觉现象,确保了答案的真实性和可靠性,让用户获得值得信赖的信息。

对于众多开发者而言,数据安全和快速响应是极为重要的考量。DeepSeek R1 支持本地化运行,无需依赖云端 API,这不仅减少了网络延迟带来的问题,还使用户能够在本地环境中安全处理敏感数据,无需担心数据泄露的风险,为特定行业和场景的应用提供了强有力的支持。

2

Ollama:本地模型运行的理想框架

Ollama,作为一个轻量级框架,为本地运行 AI 大模型提供了一个便捷且高效的平台,成为构建本地 RAG 系统的重要组成部分。它的推出,使得开发者能够减少对云端计算资源的依赖,轻松在本地设备上部署和执行模型,显著降低了开发与部署的成本,并增强了系统的独立性和数据隐私保护。

在 Ollama 上下载和安装模型的过程异常简便。以 DeepSeek R1 模型为例,开发者只需在终端输入几条简单的命令即可完成。例如,要运行默认的7B模型,只需执行“ollama run deepseek-r1”命令;而若想体验适用于轻量级 RAG 应用场景的1.5B模型,则运行“ollama run deepseek-r1:1.5b”命令即可。这种简便的操作流程,让即便是技术背景较浅的开发者也能迅速掌握,开始 RAG 系统的开发工作。

Ollama 支持多种 AI 大模型,为开发者提供了广泛的选择余地。这些模型在性能、适用场景和资源需求上各有特点,开发者可以根据项目的具体需求,灵活选择最合适的模型,以达到系统性能的最优化和资源的有效配置。不论是追求更强的推理性能还是注重资源的高效使用,Ollama 都能迎合开发者的多元需求。

3

构建本地 RAG 系统的详细步骤

第一步:导入必要的库

在构建 RAG 系统时,需要利用一系列强大的库来执行不同的功能。LangChain 库在文档处理和检索方面表现卓越,它提供了众多的工具和接口,能够简化文档的加载、文本的分段、嵌入的生成以及检索等复杂流程;而 Streamlit 库则专注于创建易于使用的 Web 界面,使用户能够轻松地与系统互动,提交问题并接收答案。同时,构建过程中还涉及到 PDFPlumberLoader,它用于高效地从 PDF 文件中抽取文本;SemanticChunker 则用于智能地将文本划分为有意义的语义单元;HuggingFaceEmbeddings 用于生成文本的向量表示;FA

### DeepSeek R1 7B Model Combined with RAG Architecture Usage and Examples The **DeepSeek R1** model, particularly its 7 billion parameter (7B) variant, is designed to be highly efficient for inference tasks such as document retrieval and generation within a Retrieval-Augmented Generation (RAG) framework. This combination leverages the strengths of both technologies by enhancing performance while reducing costs significantly compared to proprietary models like those from OpenAI[^2]. #### Key Features of DeepSeek R1 7B in RAG Systems - The **cost-effectiveness**: With up to a 95% reduction in cost relative to similar commercial offerings, this makes it an attractive option for organizations looking to implement advanced NLP capabilities without breaking their budget. - **Local execution support**: By running locally via platforms like Ollama using commands such as `ollama run deepseek-r1:7b`, users can ensure data privacy since no external cloud services are required during operation[^1]. Below is how one might integrate these components into practice: #### Example Implementation Using Python Code Snippet Here’s an illustrative example demonstrating integration between LangChain—a popular library facilitating interactions among various LLMs—and the DeepSeek R1 7B through Ollama. ```python from langchain.llms import Ollama from langchain.prompts import PromptTemplate from langchain.chains import LLMChain # Initialize connection to local instance of DeepSeek R1 7B on Ollama server llm = Ollama(model="deepseek-r1", temperature=0) # Define prompt template suitable for question answering over retrieved documents template = """Answer based solely upon provided context below: {context} Question: {question} Answer:""" prompt_template = PromptTemplate(input_variables=["context", "question"], template=template) # Create chain linking together our custom prompt & chosen language model answer_chain = LLMChain(llm=llm, prompt=prompt_template) # Sample input consisting of relevant passage plus query string sample_context = ("Astronomy involves studying celestial objects including stars," "planets, moons, comets, asteroids etc.") query_string = "What does astronomy study?" response = answer_chain.run({"context": sample_context, "question": query_string}) print(response.strip()) ``` This script initializes communication towards your installed copy of the specified version of DeepSeek inside Ollama service before defining appropriate templates needed when querying information against pre-fetched textual material stored elsewhere beforehand—such setup forms part typical workflow underpinning modern implementations involving RAG architectures today! Additionally, combining tools like Streamlit allows creating interactive web applications where end-users interact directly with underlying logic powering search results presentation alongside generated responses derived therefrom too—all encapsulated neatly behind user-friendly graphical interfaces accessible remotely across networks if desired so long proper security measures remain enforced throughout deployment lifecycle stages accordingly thereafter henceforth furthermore furthermore moreover moreover additionally also likewise similarly correspondingly analogously parallelly comparably equivalently identically uniformly consistently coherently harmoniously congruently compatibly appropriately suitably aptly fittingly properly rightly correctly accurately precisely exactly indeed truly verily surely certainly definitely absolute
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值