分類目錄:《自然語言處理從入門到應用》總目錄
Cassandra聊天消息記錄
Cassandra是一種分布式數據庫,非常適合存儲大量數據,是存儲聊天消息歷史的良好選擇,因為它易于擴展,能夠處理大量寫入操作。
# List of contact points to try connecting to Cassandra cluster.
contact_points = ["cassandra"]from langchain.memory import CassandraChatMessageHistorymessage_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表:
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
from langchain.memory.chat_message_histories import DynamoDBChatMessageHistoryhistory = 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。
from langchain.memory.chat_message_histories import DynamoDBChatMessageHistoryhistory = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="0", endpoint_url="http://localhost.localstack.cloud:4566")
Agent with DynamoDB Memory
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 getpassmessage_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
。
from datetime import timedelta
from langchain.memory import MomentoChatMessageHistorysession_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)下許可。
# Provide the connection string to connect to the MongoDB database
connection_string = "mongodb://mongo_user:password123@mongo:27017"
from langchain.memory import MongoDBChatMessageHistorymessage_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 來存儲聊天消息歷史記錄。
from langchain.memory import PostgresChatMessageHistoryhistory = 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來存儲聊天消息歷史記錄。
from langchain.memory import RedisChatMessageHistoryhistory = 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/