Skip to content

Improve UX for chat interaction #481

@pmeier

Description

@pmeier

I propose three improvements:

  1. Remove the context manager for a Chat

    ragna/ragna/core/_rag.py

    Lines 402 to 409 in 7071cf4

    async def __aenter__(self) -> Chat:
    await self.prepare()
    return self
    async def __aexit__(
    self, exc_type: Type[Exception], exc: Exception, traceback: str
    ) -> None:
    pass

    It was somewhat quirky to begin with, because we only had enter but not exit logic. But after Add metadata filter handling for builtin source storages #456 we have regular use cases where we don't want to call await chat.prepare().

  2. Add a prepare: Optional[bool] = None flag to Chat.answer(). If omitted, i.e. prepare is None, we can check self._prepared and call self.prepare if required. This skips the need for the user to call Chat.prepare() explicitly and streamlines the UX. Note that if Make Chat.prepare idempotent #480 is accepted, we can make the flag prepare: bool = True.

The points above would transform

async with Rag().chat(...) as chat:
    message = await chat.answer(...)

into

chat = Rag().chat(...)
message = await chat.answer(...)
  1. I'm a little on the fence on this one, but I'm going to propose it anyway to see what others think of it: add a __acall__() method to Chat that either defers to or completely replaces Chat.answer().

This would simplify the snippet from above even further to

chat = Rag().chat(...)
message = await chat(...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions