news 2026/4/16 10:43:26

【大模型】-LangChain--RAG文档系统

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
【大模型】-LangChain--RAG文档系统

文章目录

  • 1.完整代码
  • 2.结果展示
  • 3. RAG介绍

1.完整代码

由于使用的是通义,所以代码改造了下,因为openAI需要钱

importstreamlitasstimporttempfileimportosfromlangchain_classic.memoryimportConversationBufferMemoryfromlangchain_community.chat_message_historiesimportStreamlitChatMessageHistoryfromlangchain_community.document_loadersimportTextLoaderfromlangchain_community.vectorstoresimportChromafromlangchain_community.embeddingsimportDashScopeEmbeddingsfromlangchain_core.promptsimportPromptTemplatefromlangchain_text_splittersimportRecursiveCharacterTextSplitterfromlangchain_classic.agentsimportAgentExecutor,create_tool_calling_agentfromlangchain_community.callbacks.streamlitimportStreamlitCallbackHandlerfromlangchain_openaiimportChatOpenAIfromlangchain_community.chat_modelsimportChatTongyifromlangchain_core.toolsimporttool,create_retriever_toolfromdotenvimportload_dotenv load_dotenv()# 设置Streamlit应用的页面标题和布局st.set_page_config(page_title="文档问答",layout="wide")# 设置应用的标题st.title("📚 文档问答系统")# 上传txt文件,允许上传多个文件uploaded_files=st.sidebar.file_uploader(label="上传txt文件",type=["txt"],accept_multiple_files=True)# 如果没有上传文件,提示用户上传文件并停止运行ifnotuploaded_files:st.info("请先上传按TXT文档。")st.stop()@st.cache_resource(ttl="1h")defconfigure_retriever(uploaded_files):# 读取上传的文档,并写入一个临时目录docs=[]temp_dir=tempfile.TemporaryDirectory(dir=r"D:\蒋辉\AI大模型")forfileinuploaded_files:temp_filepath=os.path.join(temp_dir.name,file.name)withopen(temp_filepath,"wb")asf:f.write(file.getvalue())# 使用TextLoader加载文本文件loader=TextLoader(temp_filepath,encoding="utf-8")docs.extend(loader.load())# 进行文档分割text_splitter=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200)splits=text_splitter.split_documents(docs)# 使用向量模型生成文档的向量表示embeddings=DashScopeEmbeddings(model="text-embedding-v3",dashscope_api_key="sk-秘钥")vectordb=Chroma.from_documents(splits,embeddings)# 创建文档检索器retriever=vectordb.as_retriever()returnretriever# 配置检索器retriever=configure_retriever(uploaded_files)# 如果session_state中没有消息记录或用户点击了清空聊天记录按钮,则初始化消息记录if"messages"notinst.session_stateorst.sidebar.button("清空聊天记录"):st.session_state["messages"]=[{"role":"assistant","content":"您好,我是文档问答助手"}]# 加载历史聊天记录formsginst.session_state.messages:st.chat_message(msg["role"]).write(msg["content"])# 创建用于文档检索的工具retrieval_tool=create_retriever_tool(retriever,name="文档检索",description="用于检索用户提出的问题,并基于检索到的文档内容进行回复.",)tools=[retrieval_tool]# 创建聊天消息历史记录msgs=StreamlitChatMessageHistory()# 创建对话缓冲区内存memory=ConversationBufferMemory(chat_memory=msgs,return_messages=True,memory_key="chat_history",output_key="output")# 指令模板instructions="""您是一个设计用于查询文档来回答问题的代理。 您可以使用文档检索工具,并基于检索内容来回答问题 您可能不查询文档就知道答案,但是您仍然应该查询文档来获得答案。 如果您从文档中找不到任何信息用于回答问题,则只需返回“抱歉,这个问题我还不知道。”作为答案。 """# 基础提示模板 - 修改这里!base_prompt_template=""" {instructions} TOOLS: ------ You have access to the following tools: {tools} To use a tool, please use the following format: Thought: Do I need to use a tool? Yes Action: The action to take, should be one of [{tool_names}] Action Input: {input} Observation: the result of the action When you have a response to say to the Human, or if you do not need to use a tool, you MUST use the format: Thought: Do I need to use a tool? No Final Answer: [your response here] Begin! Previous conversation history: {chat_history} New input: {input} {agent_scratchpad}"""# 创建基础提示模板 - 修改这里!# 需要传递tools和tool_namestool_names=", ".join([tool.namefortoolintools])tool_descriptions="\n".join([f"{tool.name}:{tool.description}"fortoolintools])base_prompt=PromptTemplate(template=base_prompt_template,input_variables=["agent_scratchpad","chat_history","input"],partial_variables={"instructions":instructions,"tools":tool_descriptions,"tool_names":tool_names})# 创建Llmllm=ChatTongyi(model="qwen-plus")# 创建react Agentagent=create_tool_calling_agent(llm,tools,base_prompt)# 创建Agent执行器''' AgentExecutor 的这几个参数作用如下: agent=agent: 指定要使用的 Agent 实例,这里是通过 create_tool_calling_agent 创建的工具调用代理 tools=tools: 传入可用的工具列表,代理会根据需要调用这些工具(如文档检索工具) memory=memory: 配置对话内存,用于保存对话历史,使代理能够记住之前的交互内容 verbose=True: 启用详细日志输出,方便调试和查看代理的思考过程 handle_parsing_errors=True: 启用解析错误处理机制,当 Agent 输出格式不正确时自动处理而不是抛出异常 '''agent_executor=AgentExecutor(agent=agent,tools=tools,memory=memory,verbose=True,handle_parsing_errors=True)# 创建聊天输入框user_query=st.chat_input(placeholder="请开始提问吧!")# 如果有用户输入的查询ifuser_query:# 添加用户消息到session_statest.session_state.messages.append({"role":"user","content":user_query})# 显示用户消息st.chat_message("user").write(user_query)withst.chat_message("assistant"):# 创建streamlit回调处理器st_cb=StreamlitCallbackHandler(st.container())# agent执行过程日志回调显示在Streamlit Containerconfig={"callbacks":[st_cb]}try:# 执行Agent并获取响应response=agent_executor.invoke({"input":user_query},config=config)# 添加助手消息到session_statest.session_state.messages.append({"role":"assistant","content":response["output"]})# 显示助手响应st.write(response["output"])exceptExceptionase:st.error(f"处理问题时出错:{str(e)}")st.session_state.messages.append({"role":"assistant","content":"抱歉,处理您的问题时出现了错误。"})

2.结果展示

控制台执行命令

streamlit run D:/jhproject/python_project/LLM/jh-langserve-app/app/RAG_问答系统.py

上传txt文件,目前demo支持txt,可以改造成支持多种文档格式,直接问AI就行,这里就不展示了

每次询问和程序员相关的,他会查询出对应结果,因为知识库来自上传的文档

如果询问奥特曼或者非知识库的内容,它答不上来

之前配置的监控也会看出,smith平台,如何配置请看LangSmith监控

3. RAG介绍

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 10:21:12

昇腾CANN性能剖析实战 - 从工具使用到深度优化

目录 1 摘要 2 性能剖析工具链架构解析 2.1 设计理念与核心价值 2.2 关键性能指标与理论基准 3 性能数据采集实战指南 3.1 环境配置与采集启动 3.2 多维度数据采集策略 4 性能数据深度分析方法 4.1 流水线效率分析 4.2 时间线数据分析 5 常见性能瓶颈优化实战 5.1 …

作者头像 李华
网站建设 2026/4/16 1:05:14

Decoder-Only架构

Decoder-Only架构 Decoder-only 架构摒弃了 Encoder-Decoder 架构中的编码器部分以及与编码器交互的交叉注意力模块。在这种架构下,模型仅使用解码器来构建语言模型。这种架构利用“自回归”机制,在给定上文的情况下,生成流畅且连贯的下文。一…

作者头像 李华
网站建设 2026/4/16 11:55:47

Numpy基础20问

1、什么是numpy?一言以蔽之,numpy是python中基于数组对象的科学计算库。提炼关键字,可以得出numpy以下三大特点:拥有n维数组对象;拥有广播功能(后面讲到);拥有各种科学计算API,任你调…

作者头像 李华
网站建设 2026/4/15 13:54:42

转子动力学:临界转速计算、Workbench建模、模态振型与坎贝尔图

转子动力学之临界转速计算 workbench 建模 模态振型 坎贝尔图在工厂里那台嗡嗡作响的鼓风机又双叒叕出问题了,轴承温度异常升高时老师傅叼着烟说了句:"这转子怕是遇上临界转速了"。要搞懂这个玄学问题,Workbench里藏着不少实用…

作者头像 李华
网站建设 2026/4/16 12:04:39

当AI遇上电影工业:一个让“抽卡式“视频生成彻底翻篇的工程化实践

从剧本到成片,我们如何用关键帧驱动理念重构AI视频生产流程 写在前面的话 说实话,当我第一次看到市面上那些"输入一句话就能生成视频"的AI工具时,内心是激动的。但当我真正尝试用它们做点正经事——比如制作一个30秒的品牌宣传片——我就发现了问题:生成的视频就像开…

作者头像 李华