rag学习5

本周基于 LangChain 框架完成了支持多轮对话的 RAG 检索增强问答服务开发,编写RagService核心类,整合通义大模型、DashScope 向量嵌入模型与自研向量库服务,依托配置文件统一管理模型参数,完成各项基础组件的初始化搭建。

本次开发中设计了专属提示词模板,通过ChatPromptTemplate搭配MessagesPlaceholder预留对话历史位置,明确模型应答要求,优先结合检索到的参考资料作答。同时自定义函数完成检索文档内容、元数据的格式化处理,规范数据展示形式。

项目运用 LangChain 各类 Runnable 组件完成链路编排,借助RunnablePassthrough实现数据透传、RunnableLambda完成自定义数据转换,串联文档检索、数据处理、提示词拼接、模型调用、结果解析全流程,并使用StrOutputParser统一输出格式。

本次实践掌握了 LangChain 链式调用逻辑与 RAG 完整业务流程,通过RunnableWithMessageHistory对接历史会话读取接口,实现问答场景的上下文记忆能力。后续将开展功能测试,优化文档格式化逻辑,并进一步拓展服务适配更多业务场景。

python 复制代码
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough, RunnableWithMessageHistory, RunnableLambda

from file_history_store import get_history
from vectoer_stores import VectorStoreService
from langchain_community.embeddings import DashScopeEmbeddings
import config_data as config
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.documents import Document

class RagService:
    def __init__(self):
        self.vector_service = VectorStoreService(embedding = DashScopeEmbeddings(model = config.embedding_model_name ))

        self.prompt_template = ChatPromptTemplate.from_messages(
            [
                ("system","以我提供的已知参考资料为主,简洁和专业的回答用户问题。参考资料{context}"),
                ("system", "并且我提供的用户的历史对话如下"),
                MessagesPlaceholder("history"),
                ("user", "请回答用户提问:{input}")
            ]
        )

        self.chat_model = ChatTongyi(model = config.chat_model_name)

        self.chain = self.__get_chain()

    def __get_chain(self) :

        retriever = self.vector_service.get_retriever()

        def format_document(docs: list[Document]) :
            if not docs :
                return "没有参考资料"

            formatted_str = ""
            for doc in docs :
                formatted_str += f"文档片段:{doc.page_content}\n文档原数据:{doc.metadata}\n"

            return formatted_str

        def format_for_retriever(value):
            return value["input"]

        def format_for_prompt_template(value):
            new_dict = {}
            new_dict["input"] = value["input"]["input"]
            new_dict["context"] = value["context"]
            new_dict["history"] = value["input"]["history"]
            return new_dict



        chain = (
            {"input":RunnablePassthrough() ,
             "context":RunnableLambda(format_for_retriever)| retriever | format_document}
            |RunnableLambda(format_for_prompt_template)
            | self.prompt_template
            | self.chat_model
            | StrOutputParser()
        )

        conversation_chain =RunnableWithMessageHistory(
            chain,
            get_history,
            input_messages_key="input",
            history_messages_key="history",
        )


        return conversation_chain
相关推荐
qq_589666054 小时前
TypeScript 完整入门教程
前端·javascript·typescript
tkevinjd4 小时前
MiniCode 项目详解6:原项目控制系统的10个缺陷(已修复)
python·llm·agent
FoldWinCard5 小时前
D5 Linux 网络及端口命令
linux·运维·服务器
dNGUZ7UGj6 小时前
10分钟完成第一个Python小游戏
python·django
没有梦想的咸鱼185-1037-16636 小时前
AI-Python机器学习与深度学习技术:CNN/Transformer/扩散模型、SHAP可解释及Hermes智能体自动化
人工智能·python·深度学习·机器学习·chatgpt·cnn·transformer
霸道流氓气质6 小时前
SpringBoot中通用工具类库(Utils)封装与使用实践
spring boot·后端·python
零涂毕业设计6 小时前
毕业设计模块开发-OLED显示屏(IIC协议0.96寸)STM32 ESP32 FPGA Linux驱动免费分享
linux·stm32·单片机·嵌入式·esp32·fpga·oled
tedcloud1237 小时前
OmniRoute怎么部署?开源AI模型路由平台Linux部署教程
linux·服务器·人工智能·开源·音视频
霸道流氓气质7 小时前
KMS 密钥管理服务(Key Management Service)原理与实践
linux·服务器·数据库
SakitamaX7 小时前
LVS(Linux Virtual Server)概念详解
linux·运维·lvs