2525
2626
2727class QueryChatBase :
28- """
29- Create a QueryChat instance.
30-
31- This is the main entry point for using querychat.
32- """
33-
3428 def __init__ (
3529 self ,
3630 data_source : IntoFrame | sqlalchemy .Engine ,
@@ -44,70 +38,6 @@ def __init__(
4438 extra_instructions : Optional [str | Path ] = None ,
4539 prompt_template : Optional [str | Path ] = None ,
4640 ):
47- """
48- Initialize QueryChat.
49-
50- Parameters
51- ----------
52- data_source
53- Either a Narwhals-compatible data frame (e.g., Polars or Pandas) or a
54- SQLAlchemy engine containing the table to query against.
55- table_name
56- If a data_source is a data frame, a name to use to refer to the table in
57- SQL queries (usually the variable name of the data frame, but it doesn't
58- have to be). If a data_source is a SQLAlchemy engine, the table_name is
59- the name of the table in the database to query against.
60- id
61- An optional ID for the QueryChat module. If not provided, an ID will be
62- generated based on the table_name.
63- greeting
64- A string in Markdown format, containing the initial message. If a
65- pathlib.Path object is passed, querychat will read the contents of the
66- path into a string with `.read_text()`. You can use
67- `querychat.greeting()` to help generate a greeting from a querychat
68- configuration. If no greeting is provided, one will be generated at the
69- start of every new conversation.
70- client
71- A `chatlas.Chat` object or a string to be passed to
72- `chatlas.ChatAuto()`'s `provider_model` parameter, describing the
73- provider and model combination to use (e.g. `"openai/gpt-4.1"`,
74- "anthropic/claude-sonnet-4-5", "google/gemini-2.5-flash". etc).
75-
76- If `client` is not provided, querychat consults the
77- `QUERYCHAT_CLIENT` environment variable. If that is not set, it
78- defaults to `"openai"`.
79- data_description
80- Description of the data in plain text or Markdown. If a pathlib.Path
81- object is passed, querychat will read the contents of the path into a
82- string with `.read_text()`.
83- categorical_threshold
84- Threshold for determining if a column is categorical based on number of
85- unique values.
86- extra_instructions
87- Additional instructions for the chat model. If a pathlib.Path object is
88- passed, querychat will read the contents of the path into a string with
89- `.read_text()`.
90- prompt_template
91- Path to or a string of a custom prompt file. If not provided, the default querychat
92- template will be used. This should be a Markdown file that contains the
93- system prompt template. The mustache template can use the following
94- variables:
95- - `{{db_engine}}`: The database engine used (e.g., "DuckDB")
96- - `{{schema}}`: The schema of the data source, generated by
97- `data_source.get_schema()`
98- - `{{data_description}}`: The optional data description provided
99- - `{{extra_instructions}}`: Any additional instructions provided
100-
101- Examples
102- --------
103- ```python
104- from querychat import QueryChat
105-
106- qc = QueryChat(my_dataframe, "my_data")
107- qc.app()
108- ```
109-
110- """
11141 self ._data_source = normalize_data_source (data_source , table_name )
11242
11343 # Validate table name (must begin with letter, contain only letters, numbers, underscores)
@@ -481,6 +411,71 @@ def data_source(self):
481411
482412
483413class QueryChat (QueryChatBase ):
414+ """
415+ Create a QueryChat instance.
416+
417+ Examples
418+ --------
419+ ```python
420+ from querychat import QueryChat
421+
422+ qc = QueryChat(my_dataframe, "my_data")
423+ qc.app()
424+ ```
425+
426+ Parameters
427+ ----------
428+ data_source
429+ Either a Narwhals-compatible data frame (e.g., Polars or Pandas) or a
430+ SQLAlchemy engine containing the table to query against.
431+ table_name
432+ If a data_source is a data frame, a name to use to refer to the table in
433+ SQL queries (usually the variable name of the data frame, but it doesn't
434+ have to be). If a data_source is a SQLAlchemy engine, the table_name is
435+ the name of the table in the database to query against.
436+ id
437+ An optional ID for the QueryChat module. If not provided, an ID will be
438+ generated based on the table_name.
439+ greeting
440+ A string in Markdown format, containing the initial message. If a
441+ pathlib.Path object is passed, querychat will read the contents of the
442+ path into a string with `.read_text()`. You can use
443+ `querychat.greeting()` to help generate a greeting from a querychat
444+ configuration. If no greeting is provided, one will be generated at the
445+ start of every new conversation.
446+ client
447+ A `chatlas.Chat` object or a string to be passed to
448+ `chatlas.ChatAuto()`'s `provider_model` parameter, describing the
449+ provider and model combination to use (e.g. `"openai/gpt-4.1"`,
450+ "anthropic/claude-sonnet-4-5", "google/gemini-2.5-flash". etc).
451+
452+ If `client` is not provided, querychat consults the
453+ `QUERYCHAT_CLIENT` environment variable. If that is not set, it
454+ defaults to `"openai"`.
455+ data_description
456+ Description of the data in plain text or Markdown. If a pathlib.Path
457+ object is passed, querychat will read the contents of the path into a
458+ string with `.read_text()`.
459+ categorical_threshold
460+ Threshold for determining if a column is categorical based on number of
461+ unique values.
462+ extra_instructions
463+ Additional instructions for the chat model. If a pathlib.Path object is
464+ passed, querychat will read the contents of the path into a string with
465+ `.read_text()`.
466+ prompt_template
467+ Path to or a string of a custom prompt file. If not provided, the default querychat
468+ template will be used. This should be a Markdown file that contains the
469+ system prompt template. The mustache template can use the following
470+ variables:
471+ - `{{db_engine}}`: The database engine used (e.g., "DuckDB")
472+ - `{{schema}}`: The schema of the data source, generated by
473+ `data_source.get_schema()`
474+ - `{{data_description}}`: The optional data description provided
475+ - `{{extra_instructions}}`: Any additional instructions provided
476+
477+ """
478+
484479 def server (self , * , enable_bookmarking : bool = False ) -> None :
485480 """
486481 Initialize Shiny server logic.
@@ -545,7 +540,7 @@ def title():
545540
546541class QueryChatExpress (QueryChatBase ):
547542 """
548- Use QueryChat with Shiny Express mode .
543+ Use QueryChat with Shiny Express.
549544
550545 This class makes it easy to use querychat within Shiny Express apps --
551546 it automatically calls `.server()` during initialization, so you don't
@@ -583,6 +578,57 @@ def data_table():
583578 app_opts(bookmark_store="url")
584579 ```
585580
581+ Parameters
582+ ----------
583+ data_source
584+ Either a Narwhals-compatible data frame (e.g., Polars or Pandas) or a
585+ SQLAlchemy engine containing the table to query against.
586+ table_name
587+ If a data_source is a data frame, a name to use to refer to the table in
588+ SQL queries (usually the variable name of the data frame, but it doesn't
589+ have to be). If a data_source is a SQLAlchemy engine, the table_name is
590+ the name of the table in the database to query against.
591+ id
592+ An optional ID for the QueryChat module. If not provided, an ID will be
593+ generated based on the table_name.
594+ greeting
595+ A string in Markdown format, containing the initial message. If a
596+ pathlib.Path object is passed, querychat will read the contents of the
597+ path into a string with `.read_text()`. You can use
598+ `querychat.greeting()` to help generate a greeting from a querychat
599+ configuration. If no greeting is provided, one will be generated at the
600+ start of every new conversation.
601+ client
602+ A `chatlas.Chat` object or a string to be passed to
603+ `chatlas.ChatAuto()`'s `provider_model` parameter, describing the
604+ provider and model combination to use (e.g. `"openai/gpt-4.1"`,
605+ "anthropic/claude-sonnet-4-5", "google/gemini-2.5-flash". etc).
606+
607+ If `client` is not provided, querychat consults the
608+ `QUERYCHAT_CLIENT` environment variable. If that is not set, it
609+ defaults to `"openai"`.
610+ data_description
611+ Description of the data in plain text or Markdown. If a pathlib.Path
612+ object is passed, querychat will read the contents of the path into a
613+ string with `.read_text()`.
614+ categorical_threshold
615+ Threshold for determining if a column is categorical based on number of
616+ unique values.
617+ extra_instructions
618+ Additional instructions for the chat model. If a pathlib.Path object is
619+ passed, querychat will read the contents of the path into a string with
620+ `.read_text()`.
621+ prompt_template
622+ Path to or a string of a custom prompt file. If not provided, the default querychat
623+ template will be used. This should be a Markdown file that contains the
624+ system prompt template. The mustache template can use the following
625+ variables:
626+ - `{{db_engine}}`: The database engine used (e.g., "DuckDB")
627+ - `{{schema}}`: The schema of the data source, generated by
628+ `data_source.get_schema()`
629+ - `{{data_description}}`: The optional data description provided
630+ - `{{extra_instructions}}`: Any additional instructions provided
631+
586632 """
587633
588634 def __init__ (
@@ -594,6 +640,7 @@ def __init__(
594640 greeting : Optional [str | Path ] = None ,
595641 client : Optional [str | chatlas .Chat ] = None ,
596642 data_description : Optional [str | Path ] = None ,
643+ categorical_threshold : int = 10 ,
597644 extra_instructions : Optional [str | Path ] = None ,
598645 prompt_template : Optional [str | Path ] = None ,
599646 enable_bookmarking : Literal ["auto" , True , False ] = "auto" ,
@@ -605,6 +652,7 @@ def __init__(
605652 greeting = greeting ,
606653 client = client ,
607654 data_description = data_description ,
655+ categorical_threshold = categorical_threshold ,
608656 extra_instructions = extra_instructions ,
609657 prompt_template = prompt_template ,
610658 )
0 commit comments