From 01be3bf72eeb4091347cb7018533c446bd25329e Mon Sep 17 00:00:00 2001 From: hayescode <35790761+hayescode@users.noreply.github.com> Date: Tue, 12 Aug 2025 06:41:58 -0500 Subject: [PATCH 1/2] profile-dynamic-config-docs --- .../lifecycle-hooks/on-profile-switch.mdx | 99 +++++++++++++++++++ backend/config/overview.mdx | 2 + 2 files changed, 101 insertions(+) create mode 100644 api-reference/lifecycle-hooks/on-profile-switch.mdx diff --git a/api-reference/lifecycle-hooks/on-profile-switch.mdx b/api-reference/lifecycle-hooks/on-profile-switch.mdx new file mode 100644 index 0000000..ff2b235 --- /dev/null +++ b/api-reference/lifecycle-hooks/on-profile-switch.mdx @@ -0,0 +1,99 @@ +--- +title: "on_profile_switch" +--- + +Hook to react when a user switches between ChatProfiles. Enables dynamic, session-specific configuration updates at runtime. + +## Usage + +```python +import chainlit as cl +from chainlit.types import ChatProfile + +@cl.on_profile_switch +async def handle_profile_switch(profile: ChatProfile): + if profile.name == "vision-model": + await cl.update_config({ + "features": { + "spontaneous_file_upload": { + "enabled": True, + "accept": ["image/*"], + "max_files": 5 + } + }, + "ui": { + "name": f"Vision Assistant ({profile.name})" + } + }) + elif profile.name == "text-model": + await cl.update_config({ + "features": { + "spontaneous_file_upload": { + "enabled": False + } + }, + "ui": { + "name": "Text Assistant" + } + }) +``` + +## Dynamic Configuration + +Update config at runtime for the current session: + +```python +await cl.update_config({ + "features": { + "latex": True, + "spontaneous_file_upload": { + "enabled": True, + "max_files": 10, + "max_size_mb": 50 + } + }, + "ui": { + "name": "Custom Assistant Name", + "description": "Updated description" + } +}) +``` + +## Access Session Config + +```python +current_config = cl.get_session_config() +if current_config.features.spontaneous_file_upload.enabled: + await cl.Message("File uploads are enabled!").send() +``` + +## Example: Model-specific Capabilities + +```python +@cl.on_profile_switch +async def configure_for_model(profile: ChatProfile): + if "vision" in profile.name: + await cl.update_config({ + "features": { + "spontaneous_file_upload": { + "enabled": True, + "accept": ["image/*"] + } + } + }) + elif "code" in profile.name: + await cl.update_config({ + "features": { + "spontaneous_file_upload": { + "enabled": True, + "accept": ["text/*", "application/json"] + } + } + }) +``` + +## Notes + +- Each session maintains its own config overrides +- Existing config.toml files remain compatible +- Use `cl.get_session_config()` to read the effective config diff --git a/backend/config/overview.mdx b/backend/config/overview.mdx index 949fa0a..bdce4dc 100644 --- a/backend/config/overview.mdx +++ b/backend/config/overview.mdx @@ -4,6 +4,8 @@ title: "Overview" The `.chainlit/config.toml` file is created when you run `chainlit run ...` or `chainlit init`. It allows you to configure your Chainlit app and to enable/disable specific features. +You can also dynamically override specific `config.toml` variables by Chat Profile at runtime. See [Dynamic Profile-based Configuration](/api-reference/lifecycle-hooks/on-profile-switch) for details. + It is composed of three sections: From 272e3cdb50ac03092782c67145f2a837867523b4 Mon Sep 17 00:00:00 2001 From: hayescode <35790761+hayescode@users.noreply.github.com> Date: Thu, 14 Aug 2025 11:22:07 -0500 Subject: [PATCH 2/2] add docs to chat-profile --- api-reference/chat-profiles.mdx | 34 +++++++ .../lifecycle-hooks/on-profile-switch.mdx | 99 ------------------- 2 files changed, 34 insertions(+), 99 deletions(-) delete mode 100644 api-reference/lifecycle-hooks/on-profile-switch.mdx diff --git a/api-reference/chat-profiles.mdx b/api-reference/chat-profiles.mdx index 3fc5089..e7dfe0b 100644 --- a/api-reference/chat-profiles.mdx +++ b/api-reference/chat-profiles.mdx @@ -88,3 +88,37 @@ async def on_chat_start(): content=f"starting chat with {user.identifier} using the {chat_profile} chat profile" ).send() ``` + +## Dynamic Configuration + +You can override the global `config.toml` for specific ChatProfiles by configuring overrides + +```python +from chainlit.config import ( + ChainlitConfigOverrides, + FeaturesSettings, + McpFeature, + UISettings, +) + +@cl.set_chat_profiles +async def chat_profile(current_user: cl.User): + return [ + cl.ChatProfile( + name="MCP Enabled", + markdown_description="Profile with **MCP features enabled**. This profile has *Model Context Protocol* support activated. [Learn more](https://example.com/mcp)", + icon="https://picsum.photos/250", + starters=starters, + config_overrides=ChainlitConfigOverrides( + ui=UISettings(name="MCP UI"), + features=FeaturesSettings( + mcp=McpFeature( + enabled=True, + stdio={"enabled": True}, + sse={"enabled": True}, + streamable_http={"enabled": True}, + ) + ), + ), + ), +``` \ No newline at end of file diff --git a/api-reference/lifecycle-hooks/on-profile-switch.mdx b/api-reference/lifecycle-hooks/on-profile-switch.mdx deleted file mode 100644 index ff2b235..0000000 --- a/api-reference/lifecycle-hooks/on-profile-switch.mdx +++ /dev/null @@ -1,99 +0,0 @@ ---- -title: "on_profile_switch" ---- - -Hook to react when a user switches between ChatProfiles. Enables dynamic, session-specific configuration updates at runtime. - -## Usage - -```python -import chainlit as cl -from chainlit.types import ChatProfile - -@cl.on_profile_switch -async def handle_profile_switch(profile: ChatProfile): - if profile.name == "vision-model": - await cl.update_config({ - "features": { - "spontaneous_file_upload": { - "enabled": True, - "accept": ["image/*"], - "max_files": 5 - } - }, - "ui": { - "name": f"Vision Assistant ({profile.name})" - } - }) - elif profile.name == "text-model": - await cl.update_config({ - "features": { - "spontaneous_file_upload": { - "enabled": False - } - }, - "ui": { - "name": "Text Assistant" - } - }) -``` - -## Dynamic Configuration - -Update config at runtime for the current session: - -```python -await cl.update_config({ - "features": { - "latex": True, - "spontaneous_file_upload": { - "enabled": True, - "max_files": 10, - "max_size_mb": 50 - } - }, - "ui": { - "name": "Custom Assistant Name", - "description": "Updated description" - } -}) -``` - -## Access Session Config - -```python -current_config = cl.get_session_config() -if current_config.features.spontaneous_file_upload.enabled: - await cl.Message("File uploads are enabled!").send() -``` - -## Example: Model-specific Capabilities - -```python -@cl.on_profile_switch -async def configure_for_model(profile: ChatProfile): - if "vision" in profile.name: - await cl.update_config({ - "features": { - "spontaneous_file_upload": { - "enabled": True, - "accept": ["image/*"] - } - } - }) - elif "code" in profile.name: - await cl.update_config({ - "features": { - "spontaneous_file_upload": { - "enabled": True, - "accept": ["text/*", "application/json"] - } - } - }) -``` - -## Notes - -- Each session maintains its own config overrides -- Existing config.toml files remain compatible -- Use `cl.get_session_config()` to read the effective config