Skip to content

Commit 569b6d7

Browse files
committed
add --system flag to multi_brain_publisher example
Lets a publisher bake in a server-side system prompt (identity, persona, context about being reached via zhub) so clients can send naive prompts like 'tell me about yourself' and get a coherent answer without having to ship the context themselves. Also reads ZHUB_SYSTEM env var as a fallback.
1 parent 232f0ed commit 569b6d7

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

examples/multi_brain_publisher.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ async def main() -> None:
6464
"ws://127.0.0.1:8080"))
6565
parser.add_argument("--list", action="store_true",
6666
help="list available brains and exit")
67+
parser.add_argument("--system", default=os.environ.get("ZHUB_SYSTEM", ""),
68+
help="server-side system prompt prepended to every "
69+
"chat (lets the brain know it lives behind zhub, "
70+
"what its name/persona is, etc). Clients don't "
71+
"have to send any context.")
6772
args = parser.parse_args()
6873

6974
if args.list:
@@ -80,9 +85,14 @@ async def main() -> None:
8085

8186
async def chat_handler(messages, options):
8287
# Fold any system messages into a single system prompt; keep the
83-
# rest of the conversation intact.
84-
system_parts = [m.get("content", "") for m in messages
85-
if m.get("role") == "system"]
88+
# rest of the conversation intact. The publisher-level --system
89+
# always wins position-wise (prepended), so server-side identity
90+
# survives even if a client also sends a system message.
91+
system_parts = []
92+
if args.system:
93+
system_parts.append(args.system)
94+
system_parts += [m.get("content", "") for m in messages
95+
if m.get("role") == "system"]
8696
non_system = [m for m in messages if m.get("role") != "system"]
8797
system = "\n\n".join(p for p in system_parts if p) or None
8898
async for chunk in brain.stream(

0 commit comments

Comments
 (0)