You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: assistant-prompts/python_chat.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,8 @@ Note that for Chat applications, you should use Shiny Express syntax by default.
23
23
24
24
Pay very close attention to the chatlas documentation and Shiny example in it. Where there are differences between the chatlas API and the regular Shiny chat API, use the chatlas API instead, from that example.
25
25
26
+
If someone asks for a Chat application, use an OpenAI's gpt-4o (not gpt-4, but gpt-4o) model by default.
As we'll see later, there a couple other ways to append messages to the chat, like `chat.append_message()` and `with chat.message_stream_context()`.
230
230
--->
231
231
232
-
## Bookmark messages
233
-
234
-
When a Shiny app reloads, the app returns to it's original state, unless the URL includes [bookmarked](../api/express/session.Session.qmd) state.[^1]
235
-
Automatically updating the URL to include a bookmark of the chat state is a great way to help users return to their work if they accidentally refresh the page or unexpectedly lose their connection.
236
-
237
-
Adding bookmark support to an app generally requires some extra effort.
238
-
At the very least, Shiny needs to know where and when to save state, and in some cases, how to save/restore it as well.[^2]
239
-
The `.enable_bookmarking()` method makes this all a bit easier for bookmarking both the `chat` and `chat_client` instances.
240
-
241
-
::: {.panel-tabset .panel-pills}
242
-
243
-
### Express
244
-
245
-
```python
246
-
from chatlas import ChatOllama
247
-
from shiny.express import ui
248
-
249
-
chat_client = ChatOllama(model="llama3.2")
250
-
251
-
chat = ui.Chat(id="chat")
252
-
chat.ui(messages=["Welcome!"])
253
-
254
-
chat.enable_bookmarking(
255
-
chat_client,
256
-
bookmark_store="url", # or "server"
257
-
bookmark_on="response", # or None
258
-
)
259
-
```
260
-
261
-
### Core
262
-
263
-
```python
264
-
from chatlas import ChatOllama
265
-
from shiny import ui, App
266
-
267
-
app_ui = ui.page_fixed(
268
-
ui.chat_ui(id="chat", messages=["Welcome!"])
269
-
)
270
-
271
-
defserver(input):
272
-
chat_client = ChatOllama(model="llama3.2")
273
-
chat = ui.Chat(id="chat")
274
-
275
-
chat.enable_bookmarking(
276
-
chat_client,
277
-
bookmark_on="response", # or None
278
-
)
279
-
280
-
app = App(app_ui, server, bookmark_store="url")
281
-
```
282
-
283
-
:::
284
-
285
-
Adding this `.enable_bookmarking()` call handles the where, when, and how of bookmarking chat state:
286
-
287
-
1. Where (`store`)
288
-
-`"url"` store the state in the URL.
289
-
-`"server"` store the state on the server. Consider this over `"url"` if you want to support a large amount of state, or have other bookmark state that can't be serialized to JSON.
290
-
2. When (`bookmark_on`)
291
-
-`"response"`: triggers a bookmark when an `"assistant"` response is appended.
292
-
-`None`: don't trigger a bookmark. This assumes you'll be triggering bookmarks through other means (e.g., a button).
293
-
3. How is handled automatically by registering the relevant `on_bookmark` and `on_restore` callbacks.
294
-
295
-
296
-
Also note that when `.enable_bookmarking()` triggers a bookmark for you, it'll also update the URL query string to include the bookmark state.
297
-
This way, when the user unexpectedly loses connection, they can load the current URL to restore the chat state, or go back to the original URL to start over.
298
-
299
-
[^1]: This can be especially frustrating behavior since hosted apps, by default, will close a idle session after a certain ([configurable](https://docs.posit.co/shinyapps.io/guide/applications/#advanced-settings)) amount of time.
300
-
301
-
[^2]: When server-side state can't be fully determined by the UI's `input` values alone, you'll need to register `on_bookmark` and `on_restore` callbacks to save and restore that server-state.
0 commit comments