自然语言处理从入门到应用——LangChain:记忆(Memory)-[聊天消息记录]

分类目录:《自然语言处理从入门到应用》总目录


Cassandra聊天消息记录

Cassandra是一种分布式数据库,非常适合存储大量数据,是存储聊天消息历史的良好选择,因为它易于扩展,能够处理大量写入操作。

csharp 复制代码
# List of contact points to try connecting to Cassandra cluster.
contact_points = ["cassandra"]

from langchain.memory import CassandraChatMessageHistory

message_history = CassandraChatMessageHistory(
    contact_points=contact_points, session_id="test-session"
)

message_history.add_user_message("hi!")

message_history.add_ai_message("whats up?")
message_history.messages
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]

DynamoDB聊天消息记录

首先确保我们已经正确配置了AWS CLI,并再确保我们已经安装了boto3。接下来,创建我们将存储消息 DynamoDB表:

csharp 复制代码
import boto3

# Get the service resource.
dynamodb = boto3.resource('dynamodb')

# Create the DynamoDB table.
table = dynamodb.create_table(
    TableName='SessionTable',
    KeySchema=[
        {
            'AttributeName': 'SessionId',
            'KeyType': 'HASH'
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'SessionId',
            'AttributeType': 'S'
        }
    ],
    BillingMode='PAY_PER_REQUEST',
)

# Wait until the table exists.
table.meta.client.get_waiter('table_exists').wait(TableName='SessionTable')

# Print out some data about the table.
print(table.item_count)

输出:

复制代码
0
DynamoDBChatMessageHistory
csharp 复制代码
from langchain.memory.chat_message_histories import DynamoDBChatMessageHistory

history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="0")
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages

输出:

复制代码
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]
使用自定义端点URL的DynamoDBChatMessageHistory

有时候在连接到AWS端点时指定URL非常有用,比如在本地使用Localstack进行开发。对于这种情况,我们可以通过构造函数中的endpoint_url参数来指定URL。

csharp 复制代码
from langchain.memory.chat_message_histories import DynamoDBChatMessageHistory

history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="0", endpoint_url="http://localhost.localstack.cloud:4566")
Agent with DynamoDB Memory
csharp 复制代码
from langchain.agents import Tool
from langchain.memory import ConversationBufferMemory
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.utilities import PythonREPL
from getpass import getpass

message_history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="1")
memory = ConversationBufferMemory(memory_key="chat_history", chat_memory=message_history, return_messages=True)
python_repl = PythonREPL()

# You can create the tool to pass to an agent
tools = [Tool(
    name="python_repl",
    description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",
    func=python_repl.run
)]
llm=ChatOpenAI(temperature=0)
agent_chain = initialize_agent(tools, llm, agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION, verbose=True, memory=memory)
agent_chain.run(input="Hello!")

日志输出:

复制代码
> Entering new AgentExecutor chain...
{
    "action": "Final Answer",
    "action_input": "Hello! How can I assist you today?"
}

> Finished chain.

输出:

复制代码
'Hello! How can I assist you today?'

输入:

复制代码
agent_chain.run(input="Who owns Twitter?")

日志输出:

复制代码
> Entering new AgentExecutor chain...
{
    "action": "python_repl",
    "action_input": "import requests\nfrom bs4 import BeautifulSoup\n\nurl = 'https://en.wikipedia.org/wiki/Twitter'\nresponse = requests.get(url)\nsoup = BeautifulSoup(response.content, 'html.parser')\nowner = soup.find('th', text='Owner').find_next_sibling('td').text.strip()\nprint(owner)"
}
Observation: X Corp. (2023--present)Twitter, Inc. (2006--2023)

Thought:{
    "action": "Final Answer",
    "action_input": "X Corp. (2023--present)Twitter, Inc. (2006--2023)"
}

> Finished chain.

输出:

复制代码
'X Corp. (2023--present)Twitter, Inc. (2006--2023)'

输入:

复制代码
agent_chain.run(input="My name is Bob.")

日志输出:

复制代码
> Entering new AgentExecutor chain...
{
    "action": "Final Answer",
    "action_input": "Hello Bob! How can I assist you today?"
}

> Finished chain.

输出:

复制代码
  'Hello Bob! How can I assist you today?'

输入:

复制代码
agent_chain.run(input="Who am I?")

日志输出:

复制代码
> Entering new AgentExecutor chain...
{
    "action": "Final Answer",
    "action_input": "Your name is Bob."
}

> Finished chain.

输出:

复制代码
'Your name is Bob.'

Momento聊天消息记录

本节介绍如何使用Momento Cache来存储聊天消息记录,我们会使用MomentoChatMessageHistory类。需要注意的是,默认情况下,如果不存在具有给定名称的缓存,我们将创建一个新的缓存。我们需要获得一个Momento授权令牌才能使用这个类。这可以直接通过将其传递给momento.CacheClient实例化,作为MomentoChatMessageHistory.from_client_params的命名参数auth_token,或者可以将其设置为环境变量MOMENTO_AUTH_TOKEN

csharp 复制代码
from datetime import timedelta
from langchain.memory import MomentoChatMessageHistory

session_id = "foo"
cache_name = "langchain"
ttl = timedelta(days=1)
history = MomentoChatMessageHistory.from_client_params(
    session_id, 
    cache_name,
    ttl,
)

history.add_user_message("hi!")

history.add_ai_message("whats up?")
history.messages

输出:

复制代码
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]

MongoDB聊天消息记录

本节介绍如何使用MongoDB存储聊天消息记录。MongoDB是一个开放源代码的跨平台文档导向数据库程序。它被归类为NoSQL数据库程序,使用类似JSON的文档,并且支持可选的模式。MongoDB由MongoDB Inc.开发,并在服务器端公共许可证(SSPL)下许可。

csharp 复制代码
# Provide the connection string to connect to the MongoDB database
connection_string = "mongodb://mongo_user:password123@mongo:27017"
from langchain.memory import MongoDBChatMessageHistory

message_history = MongoDBChatMessageHistory(
        connection_string=connection_string, session_id="test-session"
    )

message_history.add_user_message("hi!")

message_history.add_ai_message("whats up?")
message_history.messages

输出:

复制代码
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]

Postgres聊天消息历史记录

本节介绍了如何使用 Postgres 来存储聊天消息历史记录。

csharp 复制代码
from langchain.memory import PostgresChatMessageHistory

history = PostgresChatMessageHistory(connection_string="postgresql://postgres:mypassword@localhost/chat_history", session_id="foo")

history.add_user_message("hi!")

history.add_ai_message("whats up?")
history.messages

Redis聊天消息历史记录

本节介绍了如何使用Redis来存储聊天消息历史记录。

csharp 复制代码
from langchain.memory import RedisChatMessageHistory

history = RedisChatMessageHistory("foo")

history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages

输出:

复制代码
[AIMessage(content='whats up?', additional_kwargs={}),
HumanMessage(content='hi!', additional_kwargs={})]

参考文献:

1\] LangChain官方网站:https://www.langchain.com/ \[2\] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/ \[3\] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

相关推荐
kalvin_y_liu2 分钟前
智能体框架大PK!谷歌ADK VS 微软Semantic Kernel
人工智能·microsoft·谷歌·智能体
爱看科技6 分钟前
智能眼镜行业腾飞在即,苹果/微美全息锚定“AR+AI眼镜融合”之路抢滩市场!
人工智能·ar
Juchecar3 小时前
LLM模型与ML算法之间的关系
人工智能
FIN66683 小时前
昂瑞微:深耕射频“芯”赛道以硬核实力冲刺科创板大门
前端·人工智能·科技·前端框架·信息与通信·智能
benben0443 小时前
京东agent之joyagent解读
人工智能
LONGZETECH3 小时前
【龙泽科技】汽车动力与驱动系统综合分析技术1+X仿真教学软件(1.1.3 -初级)
人工智能·科技·汽车·汽车仿真教学软件·汽车教学软件
lisw053 小时前
SolidWorks:现代工程设计与数字制造的核心平台
人工智能·机器学习·青少年编程·软件工程·制造
大刘讲IT3 小时前
AI 生产工艺参数优化:中小型制造企业用 “智能调参“ 提升产品合格率与生产效率
人工智能·制造
图欧学习资源库3 小时前
人工智能领域、图欧科技、IMYAI智能助手2025年9月更新月报
人工智能·科技
Wild_Pointer.4 小时前
面向Qt/C++开发工程师的Ai提示词(附Trae示例)
人工智能·ai·大模型