Skip to main content

Basic Usage

Chat implements async context manager for automatic resource cleanup:
import asyncio
from upsonic import Agent, Chat


async def main():
    agent = Agent("openai/gpt-4o")

    async with Chat(
        session_id="session1",
        user_id="user1",
        agent=agent
    ) as chat:
        response = await chat.invoke("Hello")
        print(response)


if __name__ == "__main__":
    asyncio.run(main())

With Storage

import asyncio
from upsonic import Agent, Chat
from upsonic.storage import SqliteStorage


async def main():
    storage = SqliteStorage(db_file="chat.db")
    agent = Agent("openai/gpt-4o")

    async with Chat(
        session_id="session1",
        user_id="user1",
        agent=agent,
        storage=storage
    ) as chat:
        response = await chat.invoke("Hello")
        print(response)


if __name__ == "__main__":
    asyncio.run(main())