• 在Langchain中使用ConversationSummaryMemory给链增加记忆


    刚接触Langchain,整体感觉下来就是langchain中的事件顺序都是按照一定的套路来实现,例如常见的链LLMChain,需要使用到的大模型LLM和Langchain自带的PromptTemplate两块来实现,要求PromptTemplate有预留好下一步插入的内容。比如下面这个例子。

    import os
    os.environ["OPENAI_API_KEY"]="..."
    from langchain.prompts import PromptTemplate
    from langchain.llms import OpenAI
    from langchain.chains import LLMChain
    
    
    llm = OpenAI(temperature=0.9)
    prompt = PromptTemplate(
        input_variables=["product"],
        template="What is a good name for a company that make {product}"
    )
    chain = LLMChain(llm=llm,prompt=prompt)
    
    llama_full_prompt = PromptTemplate.from_template(
        template="[INST]<>{sys_msg}<>\n\nContext:\n{history}\n\nHuman: {input}\n[/INST] {primer}",
    )
    
    chain.run("colorful socks")
    
    print(llama_full_prompt)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    除此之外呢,像ConversationChain是让模型记住历史信息,

    from langchain import OpenAI,ConversationChain
    llm = OpenAI(temperature=0)
    conversation = ConversationChain(llm=llm,verbose=True)
    output = conversation.predict(input="Hi I'm barry")
    print(output)
    output2 = conversation.predict(input="Hello, What is my name?")
    print(output2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    这里模型就会记住第一部输入的内容
    在这里插入图片描述

    但是有可能考虑到使用ConversationChain时会导致输入的内容超过模型输入的上下文,所以适用了ConversationSummaryMemory,就是让模型自己对过去的对话进行总结,但是这种方式可能会丢失一定的关键信息。

    from langchain.chains import ConversationChain  
    from langchain.memory import ConversationSummaryMemory  
    from langchain.prompts import PromptTemplate  
    # from langchain import LLM, DefaultChainConfig  
      
    # 假设你已经有了一个可用的语言模型实例 llm  
    # llm = LLM(...)  
      
    # # 创建一个ConversationSummaryMemory实例  
    memory = ConversationSummaryMemory(llm=llm, temperature=0, verbose=True)  
      
    # 定义对话提示模板,包含input和history变量  
    conversation_prompt = PromptTemplate.from_template("""  
    You are a helpful and knowledgeable assistant.  
      
    {history}  
      
    Human: {input}  
    Assistant:  
    """)  
      
    # 定义摘要提示模板  
    summary_template = """Summary:  
    {summary}  
      
    Latest Chat:  
    {new_lines}  
      
    New Summary:  
    """  
    
    print(summary_template)
    
    # 从模板创建PromptTemplate实例  
    summary_prompt = PromptTemplate.from_template(template=summary_template)  
      
    print(summary_prompt)
    
    # 使用更新后的摘要提示模板实例化ConversationSummaryMemory  
    memory = ConversationSummaryMemory(llm=llm, temperature=0, prompt=summary_prompt, verbose=True)  
    
    print(conversation_prompt)
    print("#$@#$")
    # 创建ConversationChain实例,使用提示模板和memory  
    conv_chain = ConversationChain(  
        llm=llm,   
        prompt=conversation_prompt,  # 使用包含history变量的对话提示模板  
        memory=memory,  
        verbose=True  
    )  
    
    
    # 运行对话链,并传入用户的输入  
    print(conv_chain.run(input="Hello World! My name is John Doe"))
    print(conv_chain.run(input="Who are you anyways?"))
    print(conv_chain.run(input="What was the first thing I asked you?"))
    print(conv_chain.run(input="What is my name?"))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    这里主要涉及到两个部分,一个是使用conversation_prompt,在这个prompt里面要包含**{history}{input}**,另一个部分就是要配置好ConversationSummaryMemory,这个要是依赖于一个prompt,这个prompt必须包含{summary},{new_lines}这两个。

    # # 创建一个ConversationSummaryMemory实例  
    memory = ConversationSummaryMemory(llm=llm, temperature=0, verbose=True)  
    # 定义摘要提示模板  
    summary_template = """Summary:  
    {summary}  
      
    Latest Chat:  
    {new_lines}  
      
    New Summary:  
    """  
    # print(summary_template)
    
    # 从模板创建PromptTemplate实例  
    summary_prompt = PromptTemplate.from_template(template=summary_template)  
    # print(summary_prompt)
    
    # 使用更新后的摘要提示模板实例化ConversationSummaryMemory  
    memory = ConversationSummaryMemory(llm=llm, temperature=0, prompt=summary_prompt, verbose=True)  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    逻辑器件与热插拔
    QT网络协议知识体系(一)
    2022/11/12 题目练习
    ESP8266-Arduino编程实例-TTP223 电容式触摸传感器驱动
    Springboot毕设项目基于javaweb电费管理系统v39ge(java+VUE+Mybatis+Maven+Mysql)
    【论文阅读】Search-Based Testing Approach for Deep Reinforcement Learning Agents
    GBase 8s 产品功能-高可用和ER
    第1章 软件架构与需求分析方法
    OpenCV图像处理-灰度处理
    深度学习 --- stanford cs231 编程作业(assignment1,Q2: SVM分类器)
  • 原文地址:https://blog.csdn.net/xdg15294969271/article/details/138216515