Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: allow partial chatModelConfig (#5749)

Shahmir Varqha7b9ba3aa0b32
diff --git a/frontend/src/plugins/impl/chat/ChatPlugin.tsx b/frontend/src/plugins/impl/chat/ChatPlugin.tsx
index 376e52ca05..424ea7d1a8 100644
--- a/frontend/src/plugins/impl/chat/ChatPlugin.tsx
+++ b/frontend/src/plugins/impl/chat/ChatPlugin.tsx
@@ -24,12 +24,12 @@ export const ChatPlugin = createPlugin<{ messages: ChatMessage[] }>(
showConfigurationControls: z.boolean(),
maxHeight: z.number().optional(),
config: z.object({
- max_tokens: z.number().default(100),
- temperature: z.number().default(0.5),
- top_p: z.number().default(1),
- top_k: z.number().default(40),
- frequency_penalty: z.number().default(0),
- presence_penalty: z.number().default(0),
+ max_tokens: z.number(),
+ temperature: z.number(),
+ top_p: z.number(),
+ top_k: z.number(),
+ frequency_penalty: z.number(),
+ presence_penalty: z.number(),
}),
allowAttachments: z.union([z.boolean(), z.string().array()]),
}),
diff --git a/marimo/_plugins/ui/_impl/chat/chat.py b/marimo/_plugins/ui/_impl/chat/chat.py
index 9e6aa40211..1711c384a4 100644
--- a/marimo/_plugins/ui/_impl/chat/chat.py
+++ b/marimo/_plugins/ui/_impl/chat/chat.py
@@ -20,6 +20,15 @@ from marimo._runtime.context.types import ContextNotInitializedError
from marimo._runtime.functions import EmptyArgs, Function
from marimo._runtime.requests import SetUIElementValueRequest
+DEFAULT_CONFIG = ChatModelConfigDict(
+ max_tokens=100,
+ temperature=0.5,
+ top_p=1,
+ top_k=40,
+ frequency_penalty=0,
+ presence_penalty=0,
+)
+
@dataclass
class SendMessageRequest:
@@ -114,13 +123,12 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):
controls. Defaults to False.
config (ChatModelConfigDict, optional): Optional configuration to override the
default configuration. Keys include:
- - max_tokens
- - temperature
- - top_p
- - top_k
- - frequency_penalty
- - presence_penalty
- Defaults to None.
+ - max_tokens. The maximum number of tokens to generate. Defaults to 100.
+ - temperature. Defaults to 0.5.
+ - top_p. Defaults to 1.
+ - top_k. Defaults to 40.
+ - frequency_penalty. Defaults to 0.
+ - presence_penalty. Defaults to 0.
allow_attachments (bool | List[str], optional): Allow attachments. True for any
attachments types, or pass a list of mime types. Defaults to False.
max_height (int, optional): Optional maximum height for the chat element.
@@ -136,13 +144,19 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):
prompts: Optional[list[str]] = None,
on_message: Optional[Callable[[list[ChatMessage]], None]] = None,
show_configuration_controls: bool = False,
- config: Optional[ChatModelConfigDict] = None,
+ config: Optional[ChatModelConfigDict] = DEFAULT_CONFIG,
allow_attachments: Union[bool, list[str]] = False,
max_height: Optional[int] = None,
) -> None:
self._model = model
self._chat_history: list[ChatMessage] = []
+ if config is None:
+ config = DEFAULT_CONFIG
+ else:
+ # overwrite defaults with user config
+ config = {**DEFAULT_CONFIG, **config}
+
super().__init__(
component_name=chat._name,
initial_value={"messages": self._chat_history},
diff --git a/tests/_plugins/ui/_impl/chat/test_chat.py b/tests/_plugins/ui/_impl/chat/test_chat.py
index e22600adff..417d8c7b05 100644
--- a/tests/_plugins/ui/_impl/chat/test_chat.py
+++ b/tests/_plugins/ui/_impl/chat/test_chat.py
@@ -14,6 +14,7 @@ from marimo._ai._types import (
from marimo._output.md import md
from marimo._plugins import ui
from marimo._plugins.ui._impl.chat.chat import (
+ DEFAULT_CONFIG,
DeleteChatMessageRequest,
SendMessageRequest,
)
@@ -37,6 +38,7 @@ def test_chat_init():
assert chat._model == mock_model
assert chat._chat_history == []
assert chat.value == []
+ assert chat._component_args["config"] == DEFAULT_CONFIG
def test_chat_with_prompts():
@@ -60,7 +62,11 @@ def test_chat_with_config():
config: ChatModelConfigDict = {"temperature": 0.7, "max_tokens": 100}
chat = ui.chat(mock_model, config=config)
- assert chat._component_args["config"] == config
+ assert chat._component_args["config"] == {
+ **DEFAULT_CONFIG,
+ "temperature": 0.7,
+ "max_tokens": 100,
+ }
async def test_chat_send_prompt():