import asyncio
from upsonic import Chat, Agent, Task
from upsonic.storage.providers import SqliteStorage
async def main():
# Create agent
agent = Agent("anthropic/claude-sonnet-4-5")
# Create storage
storage = SqliteStorage(
sessions_table_name="sessions",
profiles_table_name="profiles",
db_file="chat_sessions.db"
)
# Create chat with comprehensive configuration
chat = Chat(
session_id="comprehensive_session",
user_id="demo_user",
agent=agent,
storage=storage,
full_session_memory=True,
summary_memory=True,
user_analysis_memory=True,
debug=True,
retry_attempts=3,
retry_delay=1.5
)
try:
# Basic conversation
print("=== BASIC CONVERSATION ===")
response = await chat.invoke("Hello! I'm interested in learning about AI.")
print(f"Agent: {response}")
# Follow-up with context
response = await chat.invoke("What are the main types of AI?")
print(f"Agent: {response}")
# Streaming response
print("\n=== STREAMING RESPONSE ===")
async for chunk in chat.stream("Explain machine learning in detail"):
print(chunk, end='', flush=True)
print() # New line after streaming
# Session analytics
print("\n=== SESSION ANALYTICS ===")
print(f"Total messages: {len(chat.all_messages)}")
u = chat.usage
if u.cost is not None:
print(f"Total cost: ${u.cost:.4f}")
print(f"Input tokens: {u.input_tokens}")
print(f"Output tokens: {u.output_tokens}")
print(f"Duration: {chat.duration:.1f}s")
finally:
# Clean up
await chat.close()
if __name__ == "__main__":
asyncio.run(main())