|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import warnings |
| 4 | +from copy import deepcopy |
| 5 | +from typing import TYPE_CHECKING, Optional, Union |
| 6 | + |
| 7 | +from shiny import Inputs, Outputs, Session, module, ui |
| 8 | + |
| 9 | +from .querychat import ( |
| 10 | + QueryChatConfig, |
| 11 | + _init_impl, |
| 12 | + _server_impl, |
| 13 | + _system_prompt_impl, |
| 14 | + _ui_impl, |
| 15 | +) |
| 16 | + |
| 17 | +if TYPE_CHECKING: |
| 18 | + from pathlib import Path |
| 19 | + |
| 20 | + import chatlas |
| 21 | + import sqlalchemy |
| 22 | + from narwhals.stable.v1.typing import IntoFrame |
| 23 | + |
| 24 | + from .datasource import DataSource |
| 25 | + |
| 26 | + |
| 27 | +def init( |
| 28 | + data_source: IntoFrame | sqlalchemy.Engine, |
| 29 | + table_name: str, |
| 30 | + *, |
| 31 | + greeting: Optional[str | Path] = None, |
| 32 | + data_description: Optional[str | Path] = None, |
| 33 | + extra_instructions: Optional[str | Path] = None, |
| 34 | + prompt_template: Optional[str | Path] = None, |
| 35 | + system_prompt_override: Optional[str] = None, |
| 36 | + client: Optional[Union[chatlas.Chat, str]] = None, |
| 37 | +) -> QueryChatConfig: |
| 38 | + """ |
| 39 | + Initialize querychat with any compliant data source. |
| 40 | +
|
| 41 | + .. deprecated:: 0.3.0 |
| 42 | + Use :class:`QueryChat` instead. This function will be removed in |
| 43 | + version 1.0. |
| 44 | +
|
| 45 | + Warning: |
| 46 | + ------- |
| 47 | + This function is deprecated and will be removed in querychat 1.0. |
| 48 | + Use ``QueryChat()`` instead. |
| 49 | +
|
| 50 | + """ |
| 51 | + warn_deprecated( |
| 52 | + "init() is deprecated and will be removed in querychat 1.0. " |
| 53 | + "Use QueryChat() instead." |
| 54 | + ) |
| 55 | + return _init_impl( |
| 56 | + data_source, |
| 57 | + table_name, |
| 58 | + greeting=greeting, |
| 59 | + data_description=data_description, |
| 60 | + extra_instructions=extra_instructions, |
| 61 | + prompt_template=prompt_template, |
| 62 | + system_prompt_override=system_prompt_override, |
| 63 | + client=client, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@module.ui |
| 68 | +def mod_ui(**kwargs) -> ui.TagList: |
| 69 | + """ |
| 70 | + Create the UI for the querychat component. |
| 71 | +
|
| 72 | + .. deprecated:: 0.3.0 |
| 73 | + Use :meth:`QueryChat.ui()` instead. This function will be removed in |
| 74 | + a future release. |
| 75 | +
|
| 76 | + """ |
| 77 | + warn_deprecated( |
| 78 | + "ui() is deprecated and will be removed in a future release. " |
| 79 | + "Use QueryChat.ui() instead." |
| 80 | + ) |
| 81 | + return _ui_impl(**kwargs) |
| 82 | + |
| 83 | + |
| 84 | +@module.server |
| 85 | +def mod_server( |
| 86 | + input: Inputs, |
| 87 | + output: Outputs, |
| 88 | + session: Session, |
| 89 | + querychat_config: QueryChatConfig, |
| 90 | +): |
| 91 | + """ |
| 92 | + Initialize the querychat server. |
| 93 | +
|
| 94 | + .. deprecated:: 0.3.0 |
| 95 | + Use :meth:`QueryChat.server()` instead. This function will be removed in |
| 96 | + a future release. |
| 97 | +
|
| 98 | + """ |
| 99 | + warnings.warn( |
| 100 | + "server() is deprecated and will be removed in a future release. " |
| 101 | + "Use QueryChat.server() instead.", |
| 102 | + FutureWarning, |
| 103 | + stacklevel=2, |
| 104 | + ) |
| 105 | + return _server_impl( |
| 106 | + input, |
| 107 | + output, |
| 108 | + session, |
| 109 | + querychat_config, |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def sidebar( |
| 114 | + id: str, |
| 115 | + width: int = 400, |
| 116 | + height: str = "100%", |
| 117 | + **kwargs, |
| 118 | +) -> ui.Sidebar: |
| 119 | + """ |
| 120 | + Create a sidebar containing the querychat UI. |
| 121 | +
|
| 122 | + .. deprecated:: 0.3.0 |
| 123 | + Use :meth:`QueryChat.sidebar()` instead. This function will be removed in |
| 124 | + a future release. |
| 125 | +
|
| 126 | + """ |
| 127 | + warn_deprecated( |
| 128 | + "sidebar() is deprecated and will be removed in a future release. " |
| 129 | + "Use QueryChat.sidebar() instead." |
| 130 | + ) |
| 131 | + return ui.sidebar( |
| 132 | + mod_ui(id), |
| 133 | + width=width, |
| 134 | + height=height, |
| 135 | + class_="querychat-sidebar", |
| 136 | + **kwargs, |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +def system_prompt( |
| 141 | + data_source: DataSource, |
| 142 | + *, |
| 143 | + data_description: Optional[str | Path] = None, |
| 144 | + extra_instructions: Optional[str | Path] = None, |
| 145 | + categorical_threshold: int = 10, |
| 146 | + prompt_template: Optional[str | Path] = None, |
| 147 | +) -> str: |
| 148 | + """ |
| 149 | + Create a system prompt for the chat model based on a data source's schema |
| 150 | + and optional additional context and instructions. |
| 151 | +
|
| 152 | + .. deprecated:: 0.3.0 |
| 153 | + Use :meth:`QueryChat.set_system_prompt` instead. This function will be |
| 154 | + removed in version 1.0. |
| 155 | +
|
| 156 | + Warning: |
| 157 | + ------- |
| 158 | + This function is deprecated and will be removed in querychat 1.0. |
| 159 | + Use ``QueryChat.set_system_prompt()`` instead. |
| 160 | +
|
| 161 | + """ |
| 162 | + warnings.warn( |
| 163 | + "system_prompt() is deprecated and will be removed in querychat 1.0. " |
| 164 | + "Use QueryChat.set_system_prompt() instead.", |
| 165 | + FutureWarning, |
| 166 | + stacklevel=2, |
| 167 | + ) |
| 168 | + return _system_prompt_impl( |
| 169 | + data_source, |
| 170 | + data_description=data_description, |
| 171 | + extra_instructions=extra_instructions, |
| 172 | + categorical_threshold=categorical_threshold, |
| 173 | + prompt_template=prompt_template, |
| 174 | + ) |
| 175 | + |
| 176 | + |
| 177 | +def greeting( |
| 178 | + querychat_config, |
| 179 | + *, |
| 180 | + generate: bool = True, |
| 181 | + stream: bool = False, |
| 182 | + **kwargs, |
| 183 | +) -> str | None: |
| 184 | + """ |
| 185 | + Generate or retrieve a greeting message. |
| 186 | +
|
| 187 | + **Deprecated.** Use `QueryChat.generate_greeting()` instead. |
| 188 | + """ |
| 189 | + warn_deprecated( |
| 190 | + "greeting() is deprecated and will be removed in a future release. " |
| 191 | + "Use QueryChat.generate_greeting() instead." |
| 192 | + ) |
| 193 | + |
| 194 | + not_querychat_config = ( |
| 195 | + not hasattr(querychat_config, "client") |
| 196 | + and not hasattr(querychat_config, "greeting") |
| 197 | + and not hasattr(querychat_config, "system_prompt") |
| 198 | + ) |
| 199 | + |
| 200 | + if not_querychat_config: |
| 201 | + raise TypeError("`querychat_config` must be a QueryChatConfig object.") |
| 202 | + |
| 203 | + greeting_text = querychat_config.greeting |
| 204 | + has_greeting = greeting_text is not None and len(greeting_text.strip()) > 0 |
| 205 | + |
| 206 | + if has_greeting: |
| 207 | + return greeting_text |
| 208 | + |
| 209 | + if not generate: |
| 210 | + return None |
| 211 | + |
| 212 | + chat = deepcopy(querychat_config.client) |
| 213 | + chat.system_prompt = querychat_config.system_prompt |
| 214 | + |
| 215 | + prompt = "Please give me a friendly greeting. Include a few sample prompts in a two-level bulleted list." |
| 216 | + |
| 217 | + if stream: |
| 218 | + return chat.stream_async(prompt, **kwargs) |
| 219 | + else: |
| 220 | + return chat.chat(prompt, **kwargs) |
| 221 | + |
| 222 | + |
| 223 | +def warn_deprecated(msg: str) -> None: |
| 224 | + warnings.warn( |
| 225 | + msg, |
| 226 | + FutureWarning, |
| 227 | + stacklevel=3, |
| 228 | + ) |
0 commit comments