主题
聊天存储是保存聊天历史的集中式接口。聊天历史与其他存储形态不同,消息的顺序对维持一整段对话至关重要。
聊天存储可以按 key(比如 user_ids 或其他可唯一标识的字符串)组织一串聊天消息,并支持 delete、insert、get 操作。
SimpleChatStore
最基础的聊天存储是 SimpleChatStore,它把消息存在内存中,可以存到磁盘/从磁盘读取,也可以序列化成字符串存到别处。
通常你会先实例化一个聊天存储,再把它交给 memory 模块。若未提供,使用聊天存储的 memory 模块会默认采用 SimpleChatStore。
from llama_index.core.storage.chat_store import SimpleChatStore
from llama_index.core.memory import ChatMemoryBuffer
chat_store = SimpleChatStore()
chat_memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)创建好 memory 之后,你可以把它用于某个 agent 或对话引擎:
agent = FunctionAgent(tools=tools, llm=llm)
await agent.run("...", memory=memory)
# OR
chat_engine = index.as_chat_engine(memory=memory)要把聊天存储留待以后使用,可以从磁盘保存/加载
chat_store.persist(persist_path="chat_store.json")
loaded_chat_store = SimpleChatStore.from_persist_path(
persist_path="chat_store.json"
)或者把它转成字符串/从字符串还原,中途把字符串存到别处
chat_store_string = chat_store.json()
loaded_chat_store = SimpleChatStore.parse_raw(chat_store_string)UpstashChatStore
使用 UpstashChatStore,你可以借 Upstash Redis 把聊天历史存在远端。Upstash 提供无服务器的 Redis 方案,非常适合需要可扩展、高效聊天存储的应用。该聊天存储同时支持同步与异步操作。
安装
终端窗口
pip install llama-index-storage-chat-store-upstash用法
from llama_index.storage.chat_store.upstash import UpstashChatStore
from llama_index.core.memory import ChatMemoryBuffer
chat_store = UpstashChatStore(
redis_url="YOUR_UPSTASH_REDIS_URL",
redis_token="YOUR_UPSTASH_REDIS_TOKEN",
ttl=300, # Optional: Time to live in seconds
)
chat_memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)UpstashChatStore 同步、异步操作都支持。下面是一个使用异步方法的例子:
import asyncio
from llama_index.core.llms import ChatMessage
async def main():
# Add messages
messages = [
ChatMessage(content="Hello", role="user"),
ChatMessage(content="Hi there!", role="assistant"),
]
await chat_store.async_set_messages("conversation1", messages)
# Retrieve messages
retrieved_messages = await chat_store.async_get_messages("conversation1")
print(retrieved_messages)
# Delete last message
deleted_message = await chat_store.async_delete_last_message(
"conversation1"
)
print(f"Deleted message: {deleted_message}")
asyncio.run(main())RedisChatStore
使用 RedisChatStore,你可以把聊天历史存在远端,不必再操心手动持久化与加载聊天历史。
from llama_index.storage.chat_store.redis import RedisChatStore
from llama_index.core.memory import ChatMemoryBuffer
chat_store = RedisChatStore(redis_url="redis://localhost:6379", ttl=300)
chat_memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)AzureChatStore
使用 AzureChatStore,你可以把聊天历史存在远端的 Azure Table Storage 或 CosmosDB 中,不必再操心手动持久化与加载聊天历史。
pip install llama-index
pip install llama-index-llms-azure-openai
pip install llama-index-storage-chat-store-azurefrom llama_index.core.chat_engine import SimpleChatEngine
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.storage.chat_store.azure import AzureChatStore
chat_store = AzureChatStore.from_account_and_key(
account_name="",
account_key="",
chat_table_name="ChatUser",
)
memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="conversation1",
)
chat_engine = SimpleChatEngine(
memory=memory, llm=Settings.llm, prefix_messages=[]
)
response = chat_engine.chat("Hello.")DynamoDBChatStore
使用 DynamoDBChatStore,你可以把聊天历史存在 AWS DynamoDB 中。
安装
终端窗口
pip install llama-index-storage-chat-store-dynamodb用法
请确保已按合适的 schema 创建好一张 DynamoDB 表。默认情况下,示例如下:
import boto3
# Get the service resource.
dynamodb = boto3.resource("dynamodb")
# Create the DynamoDB table.
table = dynamodb.create_table(
TableName="EXAMPLE_TABLE",
KeySchema=[{"AttributeName": "SessionId", "KeyType": "HASH"}],
AttributeDefinitions=[
{"AttributeName": "SessionId", "AttributeType": "S"}
],
BillingMode="PAY_PER_REQUEST",
)随后就可以用 DynamoDBChatStore 类来持久化并读取聊天历史:
import os
from llama_index.core.llms import ChatMessage, MessageRole
from llama_index.storage.chat_store.dynamodb.base import DynamoDBChatStore
# Initialize DynamoDB chat store
chat_store = DynamoDBChatStore(
table_name="EXAMPLE_TABLE", profile_name=os.getenv("AWS_PROFILE")
)
# A chat history, which doesn't exist yet, returns an empty array.
print(chat_store.get_messages("123"))
# >>> []
# Initializing a chat history with a key of "SessionID = 123"
messages = [
ChatMessage(role=MessageRole.USER, content="Who are you?"),
ChatMessage(
role=MessageRole.ASSISTANT, content="I am your helpful AI assistant."
),
]
chat_store.set_messages(key="123", messages=messages)
print(chat_store.get_messages("123"))
# >>> [ChatMessage(role=<MessageRole.USER: 'user'>, content='Who are you?', additional_kwargs={}),
# ChatMessage(role=<MessageRole.ASSISTANT: 'assistant'>, content='I am your helpful AI assistant.', additional_kwargs={})]]
# Appending a message to an existing chat history
message = ChatMessage(role=MessageRole.USER, content="What can you do?")
chat_store.add_message(key="123", message=message)
print(chat_store.get_messages("123"))
# >>> [ChatMessage(role=<MessageRole.USER: 'user'>, content='Who are you?', additional_kwargs={}),
# ChatMessage(role=<MessageRole.ASSISTANT: 'assistant'>, content='I am your helpful AI assistant.', additional_kwargs={})],
# ChatMessage(role=<MessageRole.USER: 'user'>, content='What can you do?', additional_kwargs={})]PostgresChatStore
使用 PostgresChatStore,你可以把聊天历史存在远端,不必再操心手动持久化与加载聊天历史。
from llama_index.storage.chat_store.postgres import PostgresChatStore
from llama_index.core.memory import ChatMemoryBuffer
chat_store = PostgresChatStore.from_uri(
uri="postgresql+asyncpg://postgres:password@127.0.0.1:5432/database",
)
chat_memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)TablestoreChatStore
使用 TablestoreChatStore,你可以把聊天历史存在远端,不必再操心手动持久化与加载聊天历史。
安装
终端窗口
pip install llama-index-storage-chat-store-tablestore用法
from llama_index.storage.chat_store.tablestore import TablestoreChatStore
from llama_index.core.memory import ChatMemoryBuffer
# 1. create tablestore vector store
chat_store = TablestoreChatStore(
endpoint="<end_point>",
instance_name="<instance_name>",
access_key_id="<access_key_id>",
access_key_secret="<access_key_secret>",
)
# You need to create a table for the first use
chat_store.create_table_if_not_exist()
chat_memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)Google AlloyDB ChatStore
使用 AlloyDBChatStore,你可以把聊天历史存在 AlloyDB 中,不必再操心手动持久化与加载聊天历史。
本教程演示的是同步接口。所有同步方法都有对应的异步方法。
安装
终端窗口
pip install llama-index
pip install llama-index-alloydb-pg
pip install llama-index-llms-vertex用法
from llama_index.core.chat_engine import SimpleChatEngine
from llama_index.core.memory import ChatMemoryBuffer
from llama_index_alloydb_pg import AlloyDBChatStore, AlloyDBEngine
from llama_index.llms.vertex import Vertex
import asyncio
# Replace with your own AlloyDB info
engine = AlloyDBEngine.from_instance(
project_id=PROJECT_ID,
region=REGION,
cluster=CLUSTER,
instance=INSTANCE,
database=DATABASE,
user=USER,
password=PASSWORD,
)
engine.init_chat_store_table(table_name=TABLE_NAME)
chat_store = AlloyDBChatStore.create_sync(
engine=engine,
table_name=TABLE_NAME,
)
memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)
llm = Vertex(model="gemini-1.5-flash-002", project=PROJECT_ID)
chat_engine = SimpleChatEngine(memory=memory, llm=llm, prefix_messages=[])
response = chat_engine.chat("Hello.")
print(response)Google Cloud SQL for PostgreSQL ChatStore
使用 PostgresChatStore,你可以把聊天历史存在 Cloud SQL for Postgres 中,不必再操心手动持久化与加载聊天历史。
本教程演示的是同步接口。所有同步方法都有对应的异步方法。
安装
终端窗口
pip install llama-index
pip install llama-index-cloud-sql-pg
pip install llama-index-llms-vertex用法
from llama_index.core.chat_engine import SimpleChatEngine
from llama_index.core.memory import ChatMemoryBuffer
from llama_index_cloud_sql_pg import PostgresChatStore, PostgresEngine
from llama_index.llms.vertex import Vertex
import asyncio
# Replace with your own Cloud SQL info
engine = PostgresEngine.from_instance(
project_id=PROJECT_ID,
region=REGION,
instance=INSTANCE,
database=DATABASE,
user=USER,
password=PASSWORD,
)
engine.init_chat_store_table(table_name=TABLE_NAME)
chat_store = PostgresChatStore.create_sync(
engine=engine,
table_name=TABLE_NAME,
)
memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)
llm = Vertex(model="gemini-1.5-flash-002", project=PROJECT_ID)
chat_engine = SimpleChatEngine(memory=memory, llm=llm, prefix_messages=[])
response = chat_engine.chat("Hello.")
print(response)YugabyteDBChatStore
使用 YugabyteDBChatStore,你可以把聊天历史存在远端,不必再操心手动持久化与加载聊天历史。
前提条件
使用这个集成之前,你需要先跑起一个 YugabyteDB 实例。可以照着 YugaByteDB 快速上手指南搭建一个本地 YugabyteDB。
安装
终端窗口
pip install llama-index-storage-chat-store-yugabytedb用法
from llama_index.storage.chat_store.yugabytedb import YugabyteDBChatStore
from llama_index.core.memory import ChatMemoryBuffer
chat_store = YugabyteDBChatStore.from_uri(
uri="yugabytedb+psycopg2://yugabyte:password@127.0.0.1:5433/yugabyte?load_balance=true",
)
chat_memory = ChatMemoryBuffer.from_defaults(
token_limit=3000,
chat_store=chat_store,
chat_store_key="user1",
)连接串参数
传给 YugabyteDBChatStore.from_uri() 的连接串支持多种参数,用于配置与 YugabyteDB 集群的连接。完整的支持参数列表见 YugabyteDB psycopg2 驱动文档。
YugabyteDB 专有的参数包括:
load_balance:开启/关闭负载均衡(默认:false)topology_keys:指定连接路由时优先使用的节点yb_servers_refresh_interval:刷新可用服务器列表的间隔(秒)fallback_to_topology_keys_only:是否只连接 topology_keys 中指定的节点failed_host_ttl_seconds:在重新尝试连接失败节点之前等待的时间(秒)