|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from copy import deepcopy |
| 4 | + |
| 5 | + |
| 6 | +def greeting( |
| 7 | + querychat_config, |
| 8 | + *, |
| 9 | + generate: bool = True, |
| 10 | + stream: bool = False, |
| 11 | + **kwargs, |
| 12 | +) -> str | None: |
| 13 | + """ |
| 14 | + Generate or retrieve a greeting message. |
| 15 | +
|
| 16 | + Use this function to generate a friendly greeting message using the chat |
| 17 | + client and data source specified in the `querychat_config` object. You can |
| 18 | + pass this greeting to `init()` to set an initial greeting for users for |
| 19 | + faster startup times and lower costs. If you don't provide a greeting in |
| 20 | + `init()`, one will be generated at the start of every new conversation. |
| 21 | +
|
| 22 | + Parameters |
| 23 | + ---------- |
| 24 | + querychat_config |
| 25 | + A QueryChatConfig object from `init()`. |
| 26 | + generate |
| 27 | + If `True` and if `querychat_config` does not include a `greeting`, a new |
| 28 | + greeting is generated. If `False`, returns the existing greeting from |
| 29 | + the configuration (if any). |
| 30 | + stream |
| 31 | + If `True`, returns a streaming response suitable for use in a Shiny app |
| 32 | + with `chat_ui.append_message_stream()`. If `False` (default), returns |
| 33 | + the full greeting at once. Only relevant when `generate = True`. |
| 34 | + **kwargs |
| 35 | + Additional arguments passed to the chat client's `chat()` or `stream_async()` method. |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + str | None |
| 40 | + - When `generate = False`: Returns the existing greeting as a string or |
| 41 | + `None` if no greeting exists. |
| 42 | + - When `generate = True`: Returns the chat response containing a greeting and |
| 43 | + sample prompts. |
| 44 | +
|
| 45 | + Examples |
| 46 | + -------- |
| 47 | + ```python |
| 48 | + import pandas as pd |
| 49 | + from querychat import init, greeting |
| 50 | +
|
| 51 | + # Create config with mtcars dataset |
| 52 | + mtcars = pd.read_csv( |
| 53 | + "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv" |
| 54 | + ) |
| 55 | + mtcars_config = init(mtcars, "mtcars") |
| 56 | +
|
| 57 | + # Generate a new greeting |
| 58 | + greeting_text = greeting(mtcars_config) |
| 59 | +
|
| 60 | + # Update the config with the generated greeting |
| 61 | + mtcars_config = init( |
| 62 | + mtcars, |
| 63 | + "mtcars", |
| 64 | + greeting="Hello! I'm here to help you explore and analyze the mtcars...", |
| 65 | + ) |
| 66 | + ``` |
| 67 | +
|
| 68 | + """ |
| 69 | + not_querychat_config = ( |
| 70 | + not hasattr(querychat_config, "client") |
| 71 | + and not hasattr(querychat_config, "greeting") |
| 72 | + and not hasattr(querychat_config, "system_prompt") |
| 73 | + ) |
| 74 | + |
| 75 | + if not_querychat_config: |
| 76 | + raise TypeError("`querychat_config` must be a QueryChatConfig object.") |
| 77 | + |
| 78 | + greeting_text = querychat_config.greeting |
| 79 | + |
| 80 | + if not generate: |
| 81 | + has_greeting = greeting_text is not None and len(greeting_text.strip()) > 0 |
| 82 | + return greeting_text if has_greeting else None |
| 83 | + |
| 84 | + chat = deepcopy(querychat_config.client) |
| 85 | + chat.system_prompt = querychat_config.system_prompt |
| 86 | + |
| 87 | + prompt = "Please give me a friendly greeting. Include a few sample prompts in a two-level bulleted list." |
| 88 | + |
| 89 | + if stream: |
| 90 | + return chat.stream_async(prompt, **kwargs) |
| 91 | + else: |
| 92 | + return chat.chat(prompt, **kwargs) |
0 commit comments