Inspect and manage chat history.
"""Inspect and manage chat history.
This example shows:
- Accessing stored items
- Restoring chats from saved history
- Clearing and resetting chat state
For more advanced history access with type safety, see:
examples/developers/chat/history_access.py
"""
import json
import os
import sys
import talu
from talu import repository
MODEL_URI = os.environ.get("MODEL_URI", "LiquidAI/LFM2-350M")
if not repository.is_cached(MODEL_URI):
sys.exit(f"Model '{MODEL_URI}' not found. Run: python examples/python/00_fetch_model.py")
chat = talu.Chat(MODEL_URI, system="Be concise.")
chat("What is 2 + 2?")
chat("What about 3 + 3?")
chat("Give me a quick tip for memorization.")
print(f"Items stored: {len(chat.items)}")
history = chat.to_dict()["messages"]
print(f"\nHistory as list: {type(history)} with {len(history)} messages")
print(f"First role: {history[0]['role']}")
print(f"Last role: {history[-1]['role']}")
print("\nLast 2 messages:")
for msg in history[-2:]:
print(f"- {msg['role']}: {msg['content']}")
restored = talu.Chat.from_dict({"messages": history}, model=MODEL_URI)
print(restored("Continue the conversation in one sentence."))
forked = restored.fork()
print(forked("Continue with a different tip."))
with open("/tmp/talu_18_chat_history_saved.json", "w") as f:
json.dump(history, f, indent=2)
with open("/tmp/talu_18_chat_history_saved.json") as f:
loaded = json.load(f)
loaded_chat = talu.Chat.from_dict({"messages": loaded}, model=MODEL_URI)
print(loaded_chat("Summarize the loaded history."))
chat.clear()
print(f"\nAfter clear: {len(chat.items)} items")
chat.reset()
print(f"After reset: {len(chat.items)} items")