Add {username} as a template param in AI persona system prompts

This is my first time contributing. I apologize if I fumbled anything, and I’m deeply grateful for patience and any advice if there’s a better way to do this.

For what little it’s worth, I am a Pro customer.

I needed a robust way to know which user is interacting with the AI agent. This is what I came up with. Thank you so much for your time to consider, and any advice.

Add {username} as a template param in AI persona system prompts

I submitted PR #41446 ( FEATURE: Add {username} template param to BotContext - Pull Request #41446 - discourse/discourse - GitHub ) adding {username} to BotContext::TEMPLATE_PARAMS.

The use case: a persona acting as a bridge to an external service needs the current user’s identity resolved on the server side, not inferred by the LLM from post text. Right now there is no clean way to do that. {participants} only populates for private messages, and asking the LLM to parse the username from topic metadata is fragile and has an injection surface.

The change is small (one new method, one string added to the allowlist) and additive. Existing prompts are unaffected unless they already contained the literal {username}.

Thanks again!

6 Likes

Oh, I have completed the CLA too. I think I covered all the bases. Thanks again.

2 Likes

Just touching base. If there is anything I can do to help, please let me now. Thank you :purple_heart:

1 Like

Thanks for the PR!

Merged it now.

6 Likes

Amazing! Thank you so much :purple_heart:

3 Likes

I discussed this with @falco as well, but would like to warn that this is a very sharp implement.

Particularly we already include the user name in the “User” message:

This is visible from in the ai api logs.

The danger around inserting name at the system prompt is that in cases where there is a multi user interaction in a chain then you can cause system prompt thrashing which causes expiry of cache and which is not ideal and can lead to bad perf.

Now … one thing I noticed in our vllm templates is that we are throwing away name, that explains why CDCK hosted models are missing this feature. I am working on a patch now to fix this.

3 Likes