Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions pkg-py/src/querychat/querychat.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,19 +617,23 @@ async def query(query: str):
chat.register_tool(update_dashboard)
chat.register_tool(query)

# Add greeting if provided
if greeting and any(len(g) > 0 for g in greeting.split("\n")):
# Display greeting in chat UI
pass
else:
# Generate greeting using the chat model
pass
@reactive.extended_task
async def append_stream(chat_obj: chatlas.Chat, user_input: str):
stream = await chat_obj.stream_async(user_input, echo="none")
await chat_ui.append_message_stream(stream)

# Handle user input
@chat_ui.on_user_submit
async def _(user_input: str):
stream = await chat.stream_async(user_input, echo="none")
await chat_ui.append_message_stream(stream)
def _(user_input: str):
append_stream(chat, user_input)

@reactive.effect
async def _():
if append_stream.status() == "error":
await chat_ui.append_message(
"An error occurred while processing your input. "
f"Error: {append_stream.result()}.",
)

@reactive.effect
async def greet_on_startup():
Expand All @@ -642,5 +646,18 @@ async def greet_on_startup():
)
await chat_ui.append_message_stream(stream)

@reactive.calc
def current_query_rv():
return current_query.get()

@reactive.calc
def current_title_rv():
return current_title.get()

# Return the interface for other components to use
return QueryChat(chat, current_query.get, current_title.get, filtered_df)
return QueryChat(
chat,
current_query_rv,
current_title_rv,
filtered_df,
)
1 change: 1 addition & 0 deletions pkg-r/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Imports:
ellmer (>= 0.3.0),
htmltools,
lifecycle,
promises,
purrr,
rlang,
shiny,
Expand Down
2 changes: 2 additions & 0 deletions pkg-r/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
* the `querychat.client` R option, which can be any of the above options,
* the `QUERYCHAT_CLIENT` environment variable, which should be a provider-model string,
* or the default model from `ellmer::chat_openai()`.

* `querychat_server()` now uses a `shiny::ExtendedTask` for streaming the chat response, which allows the dashboard to update and remain responsive while the chat response is streaming in. (#63)
21 changes: 15 additions & 6 deletions pkg-r/R/querychat.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,22 @@ querychat_server <- function(id, querychat_config) {
)
}

# Handle user input
append_stream_task <- shiny::ExtendedTask$new(
function(client, user_input) {
stream <- client$stream_async(
user_input,
stream = "content"
)

p <- promises::promise_resolve(stream)
promises::then(p, function(stream) {
shinychat::chat_append("chat", stream)
})
}
)

shiny::observeEvent(input$chat_user_input, {
# Add user message to the chat history
shinychat::chat_append(
"chat",
chat$stream_async(input$chat_user_input, stream = "content")
)
append_stream_task$invoke(chat, input$chat_user_input)
})

list(
Expand Down
Loading