首页 > 其他分享 >Implementing Memory in LLM Applications Using LangChain

Implementing Memory in LLM Applications Using LangChain

时间:2024-12-14 23:54:05浏览次数:4  
标签:prompt Implementing messages LangChain Applications user memory message id

Implementing Memory in LLM Applications Using LangChain

https://www.codecademy.com/article/implementing-memory-in-llm-applications-using-lang-chain

老版本

https://python.langchain.com/v0.1/docs/modules/memory/types/buffer/

 

How to migrate to LangGraph memory

https://python.langchain.com/docs/versions/migrating_memory/

 

What is Memory in LangChain?

In LangChain, memory is implemented by passing information from the chat history along with the query as part of the prompt. LangChain provides us with different modules we can use to implement memory.

Based on the implementation and functionality, we have the following memory types in LangChain.

  1. Conversation Buffer Memory: This memory stores all the messages in the conversation history.
  2. Conversation Buffer Window Memory: The conversation buffer window memory stores the k most recent interactions of the conversation history. We can specify k according to our needs.
  3. Entity: This type of memory remembers facts about entities, such as people, places, objects, and others, in the conversation. It extracts information about entities and builds its knowledge as the conversation progresses.
  4. Conversation Summary Memory: As the name suggests, conversation summary memory summarizes the conversation and stores the current summary. This memory is helpful for longer conversations and saves costs by minimizing the number of tokens used in the conversation.
  5. Conversation summary buffer memory: The conversation summary buffer memory combines the Conversation Summary Memory and Conversation Buffer Window Memory. It stores the last k messages of the conversation and a summary of the previous messages.

How to Implement Memory in LangChain?

To implement memory in LangChain, we need to store and use previous conversations while answering a new query.

For this, we will first implement a conversation buffer memory that stores the previous interactions. Next, we will create a prompt template that we can use to pass the messages stored in the memory to the LLM application, while running the LLM application for new queries.

Also, we will use an LLM chain to run the queries using the memory, prompt template, and the LLM object, as shown below:

import os 
from langchain.chains import LLMChain 
from langchain.memory import ConversationBufferMemory 
from langchain_core.prompts import HumanMessagePromptTemplate, ChatPromptTemplate, MessagesPlaceholder 
from langchain_google_genai import ChatGoogleGenerativeAI 
os.environ['GOOGLE_API_KEY'] = "YOUR_API_KEY" 
llm = ChatGoogleGenerativeAI(model="gemini-pro") 
first_prompt="Who is elon musk? Answer in 1 sentence." 
second_prompt="When was he born?" 
memory = ConversationBufferMemory(memory_key="chat_history") 
prompt = ChatPromptTemplate( 
    messages=[ 
        MessagesPlaceholder(variable_name="chat_history"), 
        HumanMessagePromptTemplate.from_template("{query}") 
    ] 
) 
conversation_chain = LLMChain( 
    llm=llm, 
    prompt=prompt_template, 
    memory=memory 
) 
first_output=conversation_chain.run({"query":first_prompt}) 
second_output=conversation_chain.run({"query":second_prompt}) 
print("The first prompt is:",first_prompt) 
print("The second prompt is:",second_prompt) 
print("The output for the first prompt is:") 
print(first_output) 
print("The output for the second prompt is:") 
print(second_output) 

 

Memory

https://langchain-ai.github.io/langgraph/concepts/memory/

 

How to add memory to chatbots

https://python.langchain.com/docs/how_to/chatbots_memory/

from langchain_core.messages import HumanMessage, RemoveMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import START, MessagesState, StateGraph

workflow = StateGraph(state_schema=MessagesState)


# Define the function that calls the model
def call_model(state: MessagesState):
    system_prompt = (
        "You are a helpful assistant. "
        "Answer all questions to the best of your ability. "
        "The provided chat history includes a summary of the earlier conversation."
    )
    system_message = SystemMessage(content=system_prompt)
    message_history = state["messages"][:-1]  # exclude the most recent user input
    # Summarize the messages if the chat history reaches a certain size
    if len(message_history) >= 4:
        last_human_message = state["messages"][-1]
        # Invoke the model to generate conversation summary
        summary_prompt = (
            "Distill the above chat messages into a single summary message. "
            "Include as many specific details as you can."
        )
        summary_message = model.invoke(
            message_history + [HumanMessage(content=summary_prompt)]
        )

        # Delete messages that we no longer want to show up
        delete_messages = [RemoveMessage(id=m.id) for m in state["messages"]]
        # Re-add user message
        human_message = HumanMessage(content=last_human_message.content)
        # Call the model with summary & response
        response = model.invoke([system_message, summary_message, human_message])
        message_updates = [summary_message, human_message, response] + delete_messages
    else:
        message_updates = model.invoke([system_message] + state["messages"])

    return {"messages": message_updates}


# Define the node and edge
workflow.add_node("model", call_model)
workflow.add_edge(START, "model")

# Add simple in-memory checkpointer
memory = MemorySaver()
app = workflow.compile(checkpointer=memory)

 

LangMem

https://langchain-ai.github.io/long-term-memory/

import uuid

from langmem import AsyncClient

client = AsyncClient()
user_id = str(uuid.uuid4())
thread_id = str(uuid.uuid4())

messages = [
    {
        "role": "user",
        "content": "Hi, I love playing basketball!",
        "metadata": {"user_id": user_id},
    },
    {
        "role": "assistant",
        "content": "That's great! Basketball is a fun sport. Do you have a favorite player?",
    },
    {
        "role": "user",
        "content": "Yeah, Steph Curry is amazing!",
        "metadata": {"user_id": user_id},
    },
]

await client.add_messages(thread_id=thread_id, messages=messages)
await client.trigger_all_for_thread(thread_id=thread_id)

import anthropic
from langsmith import traceable

anthropic_client = anthropic.AsyncAnthropic()


@traceable(name="Claude", run_type="llm")
async def completion(messages: list, model: str = "claude-3-haiku-20240307"):
    system_prompt = messages[0]["content"]
    msgs = []
    for m in messages[1:]:
        msgs.append({k: v for k, v in m.items() if k != "metadata"})
    response = await anthropic_client.messages.create(
        model=model,
        system=system_prompt,
        max_tokens=1024,
        messages=msgs,
    )
    return response


async def completion_with_memory(messages, user_id):
    memories = await client.query_user_memory(
        user_id=user_id,
        text=messages[-1]["content"],
    )
    facts = "\n".join([mem["text"] for mem in memories["memories"]])

    system_prompt = {
        "role": "system",
        "content": "Here are some things you know" f" about the user:\n\n{facts}",
    }

    return await completion([system_prompt] + messages)


new_messages = [
    {
        "role": "user",
        "content": "Do you remember who my favorite basketball player is?",
        "metadata": {"user_id": user_id},
    }
]

response = await completion_with_memory(new_messages, user_id=user_id)
print(response.content[0].text)

 

标签:prompt,Implementing,messages,LangChain,Applications,user,memory,message,id
From: https://www.cnblogs.com/lightsong/p/18607432

相关文章

  • 基于LangChain+ChatGLM 部署本地私有化知识库
    前言随着人工智能技术的不断发展,越来越多的企业和机构开始认识到知识库的重要性。知识库不仅能够集中管理大量的信息和数据,还能通过智能检索和推理功能,为用户提供准确、高效的知识服务。LangChain与ChatGLM作为当前领先的AI技术,为部署本地私有化知识库提供了强大的支......
  • creating chat agent with langchain and openai getting no attribute error
    题意:使用Langchain和OpenAI创建聊天代理时遇到没有属性错误。问题背景:I'mtryingtotestachatagentusingthepythoncodebelow.I'musinglangchainagentandtoolfromlangchain.I'mdefiningacoupleofsimplefunctionsfortheLLMtouseastoolsw......
  • [掌握LangChain:如何有效传递数据到链式步骤中]
    掌握LangChain:如何有效传递数据到链式步骤中在构建链式程序时,能够在不同步骤之间有效地传递数据是至关重要的。在这篇文章中,我们将学习如何在LangChain中使用RunnablePassthrough类轻松处理这一任务,以及如何结合RunnableParallel来实现复杂的数据流传递。引言当我们在编......
  • 论文解读-Graph neural networks: A review of methods and applications
     论文介绍这篇论文是图神经网络领域的综述性质的文章,从各个论文里面凝聚和提取了很多精炼的信息和观点,值得一读。论文是2020年成稿投出去的,有点陈旧的。 GNN的介绍在introduction里面对比了GNN和CNN,表示出CNN的关键是局部连接,共享权重,和多层的使用。其中CNN操作的是常规......
  • Langchain Chain Agent - Zero-shot ReAct
    LangchainChainAgent-Zero-shotReActhttps://zhuanlan.zhihu.com/p/645216766 "ReAct"一词源于ReAct:SynergizingReasoningandActinginLanguageModels(react-lm.github.io),它由单词“Reason”和“Act”组合而成,这两个词代表了两类不同的LLM应用: 1.“Reas......
  • [利用NVIDIA AI模具加速开发:使用LangChain与NIM实现智能应用]
    引言在当今的AI开发中,高性能和可扩展性是关键因素。NVIDIA的NIM(NVIDIAInferenceMicroservice)提供了一个强大的解决方案,使开发者能够轻松集成NVIDIA优化的AI模型,提升应用性能。本篇文章旨在引导您如何使用LangChain与NVIDIA’sNIM构建高效的智能应用。主要内容NVIDIAN......
  • 探索 LangChain 0.2.0 版本中的最新改变:如何适应和转型
    探索LangChain0.2.0版本中的最新改变:如何适应和转型引言在快速发展的编程世界中,库和框架的更新是不可避免的。这篇文章将帮助您理解和适应LangChain0.2.0版本带来的重要变更。本次更新注重使LangChain变得更加集成不可知,这意味着用户需要明确指定使用的模型和工具......
  • 探索LangChain:构建智能应用的框架和工具
    引言在现代应用开发中,特别是在涉及自然语言处理的场景中,LangChain作为一个强大的框架,为开发者提供了丰富的工具和组件,用于创建智能和动态的应用。本文旨在引导您了解LangChain的核心架构,组件和一些使用示例,帮助初学者和专业人士都能迅速上手。主要内容1.LangChain的架构......
  • 4CCSAPPA Programming Practice and Applications
    4CCSAPPAProgrammingPracticeandApplicationsCoursework2:TheSquareInthisassignmentyoumustimplementatext-basedgamecalledTheSquare.Thegameisinspiredbythe1997,sci-fihorrormovie,Cube(Ihighlyrecommendit,ifyouhaven’tseenit).T......
  • LangChain大模型应用开发
    LangChain作为一个新兴的框架,旨在简化大模型应用的开发过程。它提供了一套工具和接口,帮助开发者将大模型无缝集成到各种应用场景中。通过LangChain,开发者可以更专注于业务逻辑的实现,而不必过多关注底层模型的复杂性。......