当前位置: 首页 > article >正文

RAG 与历史信息相结合

 

初始化模型
# Step 4. 初始化模型, 该行初始化与 智谱 的 GLM - 4  模型进行连接,将其设置为处理和生成响应。
chat = ChatZhipuAI(
    model="glm-4",
    temperature=0.8,
)

此提示告诉模型接收聊天历史记录和用户的最新问题,然后重新表述问题,以便可以独立于聊天历史记录来理解问题。明确指示模型不要回答问题,而是在必要时重新表述问题。 

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder


# 此提示告诉模型接收聊天历史记录和用户的最新问题,然后重新表述问题,以便可以独立于聊天历史记录来理解问题。明确指示模型不要回答问题,而是在必要时重新表述问题。

contextualize_q_system_prompt = """Given a chat history and the latest user question \
which might reference context in the chat history, formulate a standalone question \
which can be understood without the chat history. Do NOT answer the question, \
just reformulate it if needed and otherwise return it as is."""


# Step 3. 创建提示模板来构建模型的交互
# 该模板包括带有说明的系统消息、聊天历史记录的占位符 ( MessagesPlaceholder ) 以及由 {input} 标记的最新用户问题。
contextualize_q_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", contextualize_q_system_prompt),
        MessagesPlaceholder("chat_history"),
        ("human", "{input}"),
    ]
)

... 

中间省略加载文档并切分文档
# Step 9. 使用 Chroma VectorStore 创建检索器
retriever = chroma_store.as_retriever()

 

# Step 10. 设置历史信息感知检索器:
# create_history_aware_retriever 函数旨在接受输入和“chat_history”的键,用于创建集成聊天历史记录以进行上下文感知处理的检索器。
# 官方文档:https://python.langchain.com/v0.1/docs/modules/chains/
from langchain.chains import create_history_aware_retriever
"""
如果历史记录存在,它会构建一个有效组合提示、大型语言模型 (LLM) 和结构化输出解析器 ( StrOutputParser ) 的序列,后跟检索器。此顺序可确保最新问题在累积的历史数据中得到体现。
"""
# 他会结合历史信息重构子问题,他不实际回答问题,
history_aware_retriever = create_history_aware_retriever(chat,
                                                         retriever,
                                                         contextualize_q_prompt
)

 

# Step 12. 定义 QA 系统的提示模板,指定系统应如何根据检索到的上下文响应输入。
# 该字符串设置语言模型的指令,指示它使用提供的上下文来简洁地回答问题。如果答案未知,则指示模型明确说明这一点。

MessagesPlaceholder(variable_name="chat_history") 是一个占位符,用于在对话模型或系统中插入动态的聊天记录(chat_history)。

在提示结构中合并了一个名为“chat_history”的变量,它充当历史消息的占位符。通过使用“chat_history”输入键,我们可以将以前的消息列表无缝地注入到提示中。
qa_system_prompt = """You are an assistant for question-answering tasks. \  
Use the following pieces of retrieved context to answer the question. \
If you don't know the answer, just say that you don't know. \
Use three sentences maximum and keep the answer concise.\

{context}"""
# 在提示结构中合并了一个名为“chat_history”的变量,它充当历史消息的占位符。通过使用“chat_history”输入键,我们可以将以前的消息列表无缝地注入到提示中。
qa_prompt = ChatPromptTemplate.from_messages(
    [
        ("system", qa_system_prompt),
        MessagesPlaceholder("chat_history"),
        ("human", "{input}"),
    ]
)

 

# 此函数用于创建一个将文档处理与其他流程相结合的链,通常涉及文档检索和在问答等任务中的使用。
from langchain.chains.combine_documents import create_stuff_documents_chain

# Step 13 构建问答链:question_answer_chain 是使用 create_stuff_documents_chain 函数创建的,该函数利用语言模型 ( llm ) 和定义的提示 ( qa_prompt )。
# 官方文档链接:https://python.langchain.com/v0.1/docs/modules/chains/
question_answer_chain = create_stuff_documents_chain(chat, qa_prompt)

# Step 14. 组装 RAG 链条:该链代表完整的工作流程,其中历史感知检索器首先处理查询以合并任何相关的历史上下文,然后由 question_answer_chain 处理处理后的查询以生成最终答案。
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)

 

 手动构建 历史聊天信息

# 以下代码演示了如何使用 RAG 链来处理一系列问题,并能够引用之前的交互。该代码模拟聊天交互,其中用户提出问题,收到答案,然后提出可以利用初始交流上下文的后续问题。以下是包含代码片段的详细说明:
from langchain_core.messages import HumanMessage

# 聊天历史记录被初始化为空列表。该列表将存储会话期间交换的消息以维护上下文。
chat_history = []

# 第一个问题和响应:定义一个问题,并使用该问题和当前(空)聊天历史记录调用 RAG 链。
question = "What is Task Decomposition?"
ai_msg_1 = rag_chain.invoke({"input": question, "chat_history": chat_history})
# print("First ans: %s" % ai_msg_1["answer"])

# 然后,用户的问题和 AI 生成的答案分别作为 HumanMessage 实例和响应对象添加到聊天历史记录中。
chat_history.extend([HumanMessage(content=question), ai_msg_1["answer"]])

# 第二个问题和响应:利用现在包含第一次交流上下文的更新的聊天历史记录,提出后续问题。
second_question = "What are common ways of doing it?"
ai_msg_2 = rag_chain.invoke({"input": second_question, "chat_history": chat_history})

自动化构聊天记录

# Step 9. 使用基本字典结构管理聊天历史记录
store = {}

def get_session_history(session_id: str) -> BaseChatMessageHistory:
    if session_id not in store:
        store[session_id] = ChatMessageHistory()
    return store[session_id]

# 官方Docs:https://python.langchain.com/v0.2/docs/how_to/message_history/
conversational_rag_chain = RunnableWithMessageHistory(
    rag_chain,
    get_session_history,
    input_messages_key="input",
    history_messages_key="chat_history",
    output_messages_key="answer",
)

# 现在我们问第一个问题
first_ans = conversational_rag_chain.invoke(
    {"input": "What is Task Decomposition?"},
    config={
        "configurable": {"session_id": "abc123"}
    },
)["answer"]


secone_ans = conversational_rag_chain.invoke(
    {"input": "What are common ways of doing it?"},
    config={"configurable": {"session_id": "abc123"}},
)["answer"]


print(f"first_ans:{first_ans}")
print(f"secone_ans:{secone_ans}")


http://www.kler.cn/a/531765.html

相关文章:

  • 【C语言篇】“三子棋”
  • VSCode源码分析参考资料
  • Vue和Java使用AES加密传输
  • 【单层神经网络】基于MXNet库简化实现线性回归
  • C++哈希表深度解析:从原理到实现,全面掌握高效键值对存储
  • 使用 PyTorch 实现逻辑回归并评估模型性能
  • 自动化运维的未来:从脚本到AIOps的演进
  • 基于LabVIEW的Modbus-RTU设备通信失败问题分析与解决
  • 自托管看板工具BanBan
  • 利用redux与react实现异步操作
  • Mac本地部署DeekSeek-R1下载太慢怎么办?
  • 基于Spring Security 6的OAuth2 系列之九 - 授权服务器--token的获取
  • Golang 并发机制-5:详解syn包同步原语
  • 【玩转 Postman 接口测试与开发2_015】第12章:模拟服务器(Mock servers)在 Postman 中的创建与用法(含完整实测效果图)
  • 大模型综述一镜到底(全文八万字) ——《Large Language Models: A Survey》
  • DeepSeek:以AI创新引领全球科技潮流
  • 容器适配器(以stack和queue为例)
  • DeepSeek 提示词之角色扮演的使用技巧
  • openssl 静态编译
  • 下载hugging face上的数据集
  • 【Go - 小顶堆/大顶堆】
  • CSDN原力值提升秘籍:解锁社区活跃新姿势
  • AI开发学习之——PyTorch框架
  • Java控制台登录系统示例代码
  • nginx 新手指南
  • 强化学习数学原理(五)——随机近似与随机