Skip to content

Use ephemeral server-side sessions to temporarily persist messages-list across requests #271

@yangm2

Description

@yangm2

There may be a variable scoping issue in the backend. The current code has 2 scopes for backend variables ...

  • View-class variables: these are global-scope, which can be dangerous if any response or session data is stored there.
  • request arguments: these are scoped to the lifetime of a single request

For multi-turn conversations, each turn (human prompt + ai response) is effectively 1 (or more) requests. In order to properly maintain the context of multi-turn conversation, we probably need to use server-side session storage so that the message history (including reasoning w/ reasoning signatures) is retained for the duration of the session. Server-side storage will have a larger limit on context size than client-side (aka cookie) storage.

Here we see the class definition with a class variable.

class ChatView(View):
def __init__(self) -> None:
self.chat_manager = LangChainChatManager()
def dispatch_request(self, *args, **kwargs) -> Response:
"""
Handle client POST request
Expects JSON body with:
- messages: List of AnyMessage dicts
- city: Optional city name
- state: State abbreviation
"""
data: Dict[str, Any] = request.json
messages: List[AnyMessage] = data["messages"]
city: Optional[OregonCity] = OregonCity.from_maybe_str(data["city"])
state: UsaState = UsaState.from_maybe_str(data["state"])

The LangChainChatManager will be instantiated once for the backend, but called many times by one or more clients per request/response (aka turn). Thus the LangChainChatManager.agent is also a global singleton which services all request/responses. So there may be a problem in how the agent is created with a system prompt for "portland, oregon" but a subsequent session is supposed to prompted for "eugene, oregon".

Data like city/state could be stored in the client-side session (aka cookie) since they are small and sent on a per-request basis.

Note: message history should not be stored in any global-scoped variable because that will pollute concurrent conversations of different users/sessions!

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendBot implementation and other backend concerns

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions