How to prevent state from being shared between multiple users?

I’ve created a Panel application that uses the ChatInterface widget to interact with an LLM.

A very simplified version of my app.py is:


def setup_ui(model):
    template = pn.template.MaterialTemplate()

    chat_interface = pn.chat.ChatInterface(
        message_params=dict(
            reaction_icons={},
        ),
        callback=model.callback,
        name="Chat",
    )

    template.main.append(chat_interface)
    return template

model = setup_llm()  # do llm model setup, ommited
template = setup_ui(model)

APP_ROUTES = {"app": template}

pn.serve(
    {"app": template},
    ...other params, including OAuth
)

This works well for a single user, but when another user logs in using OAuth, all the state is shared between them, so they see each others conversation.

I see several Discourse entries, issues and documentation on this but they are somewhat dated and are more focused on scalability, and not on state.

I’ve tried one suggestion which was to increase num_procs to 2 or more, but this seems to break OAuth authentication. I’ve looked at other suggestions like implementing a callback, but it wasnt clear how this would prevent shared state or what a best practice is.

How can I prevent one users chat interface and template from being shared with another user?

Thanks!

Using template.servable() and $ panel serve app.py in the CLI would make it unique I think.

Thanks @ahuang11, I was able to temporarily work around this by passing a callback that instantiated the template instead of the template itself.

Wrap the template in a function; otherwise, it is the same instance shared between users.

def routes():
    model = setup_llm()  # do llm model setup, committed
    template = setup_ui(model)
  
 APP_ROUTES = {"app": template()}

pn.serve(
    {"app": template},
    ...other params, including OAuth
)