首页 > 其他分享 >langchain long term memory

langchain long term memory

时间:2024-11-18 10:18:48浏览次数:1  
标签:core term long LangChain langchain memory import tools

Message histories

https://python.langchain.com/docs/integrations/memory/

众多数据库支持。

 

redis数据库

https://www.cnblogs.com/mangod/p/18243321

from langchain_community.chat_message_histories import RedisChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI

model = ChatOpenAI(
    model="gpt-3.5-turbo",
    openai_api_key="sk-xxxxxxxxxxxxxxxxxxx",
    openai_api_base="https://api.aigc369.com/v1",
)

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "你是一个擅长{ability}的助手"),
        MessagesPlaceholder(variable_name="history"),
        ("human", "{question}"),
    ]
)

chain = prompt | model

chain_with_history = RunnableWithMessageHistory(
    chain,
    # 使用redis存储聊天记录
    lambda session_id: RedisChatMessageHistory(
        session_id, url="redis://10.22.11.110:6379/3"
    ),
    input_messages_key="question",
    history_messages_key="history",
)

# 每次调用都会保存聊天记录,需要有对应的session_id
chain_with_history.invoke(
    {"ability": "物理", "question": "地球到月球的距离是多少?"},
    config={"configurable": {"session_id": "baily_question"}},
)

chain_with_history.invoke(
    {"ability": "物理", "question": "地球到太阳的距离是多少?"},
    config={"configurable": {"session_id": "baily_question"}},
)

chain_with_history.invoke(
    {"ability": "物理", "question": "地球到他俩之间谁更近"},
    config={"configurable": {"session_id": "baily_question"}},
)

 

Adding Long Term Memory to OpenGPTs

https://blog.langchain.dev/adding-long-term-memory-to-opengpts/

Semantic Memory

Semantic memory is maybe the next most commonly used type of memory. This refers to finding messages that are similar to the current message and bringing them into the prompt in some way.

This is typically done by computing an embedding for each message, and then finding other messages with a similar embedding. This is basically the same idea that powers retrieval augmentation generation (RAG). Except instead of searching for documents, you are searching for messages.

For example, if a user asks "what is my favorite fruit", we would maybe find a previous message like "my favorite fruit is blueberries" (since they are similar in the embedding space). We could then pass that previous message in as context.

However, this approach has some flaws.

 

A Long-Term Memory Agent

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

本质上使用向量数据库,检索相似信息。

import json
from typing import List, Literal, Optional

import tiktoken
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_core.messages import get_buffer_string
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableConfig
from langchain_core.tools import tool
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_openai import ChatOpenAI
from langchain_openai.embeddings import OpenAIEmbeddings
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import END, START, MessagesState, StateGraph
from langgraph.prebuilt import ToolNode

 

Building a Conversational AI Agent with Long-Term Memory Using LangChain and Milvus

https://zilliz.com/blog/building-a-conversational-ai-agent-long-term-memory-langchain-milvus

Large language models (LLMs) have changed the game in artificial intelligence (AI). These advanced models can easily understand and generate human-like text with impressive accuracy, making AI assistants and chatbots much smarter and more useful. Thanks to LLMs, we now have AI tools that can handle complex language tasks, from answering questions to translating languages.

Conversational agents are software programs that chat with users in natural language, just like talking to a real person. They power things like chatbots and virtual assistants, helping us with everyday tasks by understanding and responding to our questions and commands.

LangChain is an open-source framework that makes it easier to build these conversational agents. It provides handy tools and templates to create smart, context-aware chatbots and other AI applications quickly and efficiently.

Introduction to LangChain Agents

LangChain agents are advanced systems that use an LLM to interact with different tools and data sources to complete complex tasks. These agents can understand user inputs, make decisions, and create responses, using the LLM to offer more flexible and adaptive decision-making than traditional methods.

A big advantage of LangChain Agents is their ability to use external tools and data sources. This means they can gather information, perform calculations, and take actions beyond just processing language, making them more powerful and effective for various applications

LangChain Agents vs. Chains

Chains and agents are the two main tools used in LangChain. Chains allow you to create a pre-defined sequence of tool usage, which is useful for tasks that require a specific order of operation.

How LangChain Chains work How LangChain Chains work

On the other hand, Agents enable the large language model to use tools in a loop, allowing it to decide how many times to use tools. This flexibility is ideal for tasks that require iterative processing or dynamic decision-making.

How LangChain Agents work How LangChain Agents work

 

Build a Conversational Agent Using LangChain

Let’s build a conversational agent using LangChain in Python.

Install Dependencies

To build a LangChain agent, we need to install the following dependencies:

  • LangChain: LangChain is an open-source framework that helps developers create applications using large language models (LLMs).

  • Langchain OpenAI: This package contains the LangChain integrations for OpenAI through their openai SDK.

  • OpenAI API SDK: The OpenAI Python library provides convenient access to the OpenAI REST API from any Python 3.7+ application.

  • Dotenv: Python-dotenv reads key-value pairs from a .env file and can set them as environment variables.

  • Milvus: an open-source vector database best for billion-scale vector storage and similarity search. It is also a popular infrastructure component for building Retrieval Augmented Generation (RAG) applications.

  • Pymilvus: The Python SDK for Milvus. It integrates many popular embedding and reranking models which streamlines the building of RAG applications.

  • Tiktoken: a fast BPE tokeniser for use with OpenAI's models.

 

标签:core,term,long,LangChain,langchain,memory,import,tools
From: https://www.cnblogs.com/lightsong/p/18551893

相关文章

  • 使用 Docker 部署 Next Terminal 轻量级堡垒机
    1)NextTerminal 介绍官网:https://next-terminal.typesafe.cn/GitHub:https://github.com/dushixiang/next-terminal想必经常玩服务器的都了解过 堡垒机,类似于跳板机,但与跳板机的侧重点不同。堡垒机的主要功能是控制和监控对内部网络的远程访问。它提供严格的访问控制、会话审......
  • C# The file is too long. This operation is currently limited to supporting file
    namespaceConsoleApp4{internalclassProgram{staticvoidMain(string[]args){stringbigFile=@"C:\Users\fred\Downloads\ebook-master.zip";ReadBigFile(bigFile);}......
  • langchain_chatchat+ollama部署本地知识库,联网查询以及对数据库(Oracle)数据进行查询
    langchain_chatchat+ollama部署本地知识库,联网查询以及对数据库(Oracle)数据进行查询涉及的内容其实挺多的,所以尽量减少篇幅目录langchain_chatchat+ollama部署本地知识库,联网查询以及对数据库(Oracle)数据进行查询准备工作:部署ollama以及拉取模型部署langchain_chatchat部署ora......
  • LangChain的Prompt组件详解
    在大语言模型的应用中,Prompt设计是至关重要的。LangChain通过其强大的Prompt组件,提供了灵活且高效的Prompt管理和应用方式。本文将详细探讨LangChain中Prompt的基本概念、模板使用、高级设计以及与Few-ShotLearning的结合。##Prompt的基本概念和应用Prompt在自然语言处理任务......
  • 深入探索LangChain的高级功能
    在当今的AI开发领域,LangChain以其独特的模块化设计和强大的功能集,成为大语言模型开发者的重要工具。本文将深入探讨LangChain的高级功能,展示其在复杂应用场景中的应用潜力。###LangChain的架构优势LangChain的设计理念是通过模块化和可扩展性,简化大语言模型的集成与管理。其核......
  • Your last search took too long to run. This is probably a combination of at leas
    Yourlastsearchtooktoolongtorun.Thisisprobablyacombinationofatleasttwothings:1.Thesizeofthecorpus(largercorporalikeiWeborNOWareslowerthansmallercorpora),or2.Thefrequencyofthewordsorstringsinyoursearch(atleasto......
  • RAG_SemanticRouting of langchain langgraph llmrouter
    RAG_SemanticRoutinghttps://github.com/UribeAlejandro/RAG_SemanticRouting/tree/main ChatAgentwithsemanticrouting.Thequestionisevaluatedandroutedtotwopossiblepaths:websearchorRAG.ThisagentleveragesOllama,LangChain,LangGraph,Lang......
  • mysql 导入SQL文件报错, Specified key was too long; max key length is 767 bytes
    【方案1】一、my.ini文件加入配置,然后重启mysql服务innodb_large_prefix=1二、mysql登陆运行命令登录自己的mysql的方法:1.在D:\ProgramFiles\MySQL\MySQLServer5.7\bin路径下运行命令行,2.登录:mysql-h127.0.0.1-uroot-p然后输入密码3.切换到指定数据库  use数......
  • 终端ssh终端模拟软件:Termius激活安装包
    Termius是一款功能强大的跨平台终端管理工具,提供了友好的用户界面,支持SSH、Telnet、SFTP等多种连接协议,方便用户远程连接和管理服务器。此外,Termius还支持多平台同步、文件传输、批量操作、脚本自动化等进阶功能,且具备强大的数据加密和安全性保障。无论是开发人员、系统管理员还......
  • Zino pg walkthrough Intermediate
    nmap扫描发现smba共享文件┌──(root㉿kali)-[~]└─#nmap-p--A192.168.167.64StartingNmap7.94SVN(https://nmap.org)at2024-11-1400:33UTCStats:0:01:42elapsed;0hostscompleted(1up),1undergoingSYNStealthScanSYNStealthScanTiming:Abo......