Skip to content

Commit 4a9ea3f

Browse files
authored
feat(MarkdownStream): add starter templates to shiny create (#1943)
1 parent adf6bbb commit 4a9ea3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+783
-69
lines changed

shiny/_main_create.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,6 @@ def apps(self) -> list[ShinyTemplate]:
223223
def packages(self) -> list[ShinyTemplate]:
224224
return self._templates("templates/package")
225225

226-
@property
227-
def chat_starters(self) -> list[ShinyTemplate]:
228-
return self._templates("templates/chat/starters")
229-
230226
@property
231227
def chat_llms(self) -> list[ShinyTemplate]:
232228
return self._templates("templates/chat/llms")
@@ -235,6 +231,14 @@ def chat_llms(self) -> list[ShinyTemplate]:
235231
def chat_enterprise(self) -> list[ShinyTemplate]:
236232
return self._templates("templates/chat/llm-enterprise")
237233

234+
@property
235+
def stream_llms(self) -> list[ShinyTemplate]:
236+
return self._templates("templates/markdown-stream/llms")
237+
238+
@property
239+
def stream_enterprise(self) -> list[ShinyTemplate]:
240+
return self._templates("templates/markdown-stream/llm-enterpise")
241+
238242

239243
shiny_internal_templates = ShinyInternalTemplates()
240244

@@ -261,15 +265,16 @@ def use_internal_template(
261265

262266
app_templates = shiny_internal_templates.apps
263267
pkg_templates = shiny_internal_templates.packages
264-
chat_templates = [
265-
*shiny_internal_templates.chat_starters,
268+
gen_ai_templates = [
266269
*shiny_internal_templates.chat_llms,
267270
*shiny_internal_templates.chat_enterprise,
271+
*shiny_internal_templates.stream_llms,
272+
*shiny_internal_templates.stream_enterprise,
268273
]
269274

270275
menu_choices = [
276+
Choice(title="Generative AI...", value="_gen-ai"),
271277
Choice(title="Custom JavaScript component...", value="_js-component"),
272-
Choice(title="Chat component templates...", value="_chat"),
273278
Choice(
274279
title="Choose from the Shiny Templates website", value="_external-gallery"
275280
),
@@ -279,7 +284,7 @@ def use_internal_template(
279284
question_state = question_choose_template(app_templates, *menu_choices)
280285

281286
template = template_by_name(
282-
[*app_templates, *pkg_templates, *chat_templates], question_state
287+
[*app_templates, *pkg_templates, *gen_ai_templates], question_state
283288
)
284289

285290
if template is not None:
@@ -302,8 +307,8 @@ def use_internal_template(
302307
sys.exit(0)
303308
elif question_state == "_js-component":
304309
use_internal_package_template(dest_dir=dest_dir, package_name=package_name)
305-
elif question_state == "_chat":
306-
use_internal_chat_ai_template(dest_dir=dest_dir, package_name=package_name)
310+
elif question_state == "_gen-ai":
311+
use_internal_gen_ai_template(dest_dir=dest_dir, package_name=package_name)
307312
else:
308313
valid_choices = [t.id for t in app_templates + pkg_templates]
309314
if question_state not in valid_choices:
@@ -345,18 +350,24 @@ def use_internal_package_template(
345350
package_template_questions(template, dest_dir=dest_dir, package_name=package_name)
346351

347352

348-
def use_internal_chat_ai_template(
353+
def use_internal_gen_ai_template(
349354
input: str | None = None,
350355
dest_dir: Optional[Path] = None,
351356
package_name: Optional[str] = None,
352357
):
353358
if input is None:
354359
input = questionary.select(
355-
"Which kind of chat template would you like?",
360+
"Which kind of Gen AI template would you like?",
356361
choices=[
357-
Choice(title="Chat starters...", value="_chat-starters"),
358-
Choice(title="LLM powered chat...", value="_chat-llms"),
359-
Choice(title="Enterprise LLM...", value="_chat-llm_enterprise"),
362+
Choice(title="Chat with LLM...", value="_chat-llms"),
363+
Choice(
364+
title="Chat with enterprise LLM...", value="_chat-llm_enterprise"
365+
),
366+
Choice(title="Stream markdown with LLM...", value="_stream-llms"),
367+
Choice(
368+
title="Stream markdown with enterprise LLM...",
369+
value="_stream-enterprise",
370+
),
360371
back_choice,
361372
cancel_choice,
362373
],
@@ -370,29 +381,34 @@ def use_internal_chat_ai_template(
370381
use_internal_template(dest_dir=dest_dir, package_name=package_name)
371382
return
372383

373-
use_internal_chat_ai_template(
384+
use_internal_gen_ai_template(
374385
input, dest_dir=dest_dir, package_name=package_name
375386
)
376387
return
377388

378-
if input == "_chat-starters":
379-
template_choices = shiny_internal_templates.chat_starters
380-
elif input == "_chat-llms":
389+
if input == "_chat-llms":
381390
template_choices = shiny_internal_templates.chat_llms
382-
else:
391+
elif input == "_chat-llm_enterprise":
383392
template_choices = shiny_internal_templates.chat_enterprise
393+
elif input == "_stream-llms":
394+
template_choices = shiny_internal_templates.stream_llms
395+
elif input == "_stream-enterprise":
396+
template_choices = shiny_internal_templates.stream_enterprise
397+
else:
398+
raise ValueError(f"Invalid Gen AI template choice: {input}")
384399

385400
choice = question_choose_template(template_choices, back_choice)
386401

387402
if choice == "back":
388-
use_internal_chat_ai_template(dest_dir=dest_dir, package_name=package_name)
403+
use_internal_gen_ai_template(dest_dir=dest_dir, package_name=package_name)
389404
return
390405

391406
template = template_by_name(
392407
[
393-
*shiny_internal_templates.chat_starters,
394408
*shiny_internal_templates.chat_llms,
395409
*shiny_internal_templates.chat_enterprise,
410+
*shiny_internal_templates.stream_llms,
411+
*shiny_internal_templates.stream_enterprise,
396412
],
397413
choice,
398414
)

shiny/templates/chat/starters/hello/app-core.py renamed to shiny/api-examples/Chat/app-core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
# Create a welcome message
1010
welcome = """
1111
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
12-
simply repeat it back to you. For more examples, see this
13-
[folder of examples](https://github.com/posit-dev/py-shiny/tree/main/shiny/templates/chat).
12+
simply repeat it back to you.
13+
14+
To learn more about chatbots and how to build them with Shiny, check out
15+
[the documentation](https://shiny.posit.co/py/docs/genai-chatbots.html).
1416
"""
1517

1618

shiny/templates/chat/starters/hello/app-express.py renamed to shiny/api-examples/Chat/app-express.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
# Create a welcome message
1111
welcome = """
1212
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
13-
simply repeat it back to you. For more examples, see this
14-
[folder of examples](https://github.com/posit-dev/py-shiny/tree/main/shiny/templates/chat).
13+
simply repeat it back to you.
14+
15+
To learn more about chatbots and how to build them with Shiny, check out
16+
[the documentation](https://shiny.posit.co/py/docs/genai-chatbots.html).
1517
"""
1618

1719
# Create a chat instance

shiny/templates/chat/llm-enterprise/aws-bedrock-anthropic/_template.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
{
1515
"type": "action",
1616
"text": "Learn more at https://posit-dev.github.io/chatlas/reference/ChatBedrockAnthropic.html"
17+
},
18+
{
19+
"type": "info",
20+
"text": "Want to learn more about AI chatbots?"
21+
},
22+
{
23+
"type": "action",
24+
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
1725
}
1826
]
1927
}

shiny/templates/chat/llm-enterprise/azure-openai/_template.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
{
1515
"type": "action",
1616
"text": "Learn more at https://posit-dev.github.io/chatlas/reference/ChatAzureOpenAI.html"
17+
},
18+
{
19+
"type": "info",
20+
"text": "Want to learn more about AI chatbots?"
21+
},
22+
{
23+
"type": "action",
24+
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
1725
}
1826
]
1927
}

shiny/templates/chat/llms/anthropic/_template.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
{
1515
"type": "action",
1616
"text": "Learn how to obtain one at https://posit-dev.github.io/chatlas/reference/ChatAnthropic.html"
17+
},
18+
{
19+
"type": "info",
20+
"text": "Want to learn more about AI chatbots?"
21+
},
22+
{
23+
"type": "action",
24+
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
1725
}
1826
]
1927
}

shiny/templates/chat/llms/google/_template.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
{
1515
"type": "action",
1616
"text": "Learn how to obtain one at https://posit-dev.github.io/chatlas/reference/ChatGoogle.html"
17+
},
18+
{
19+
"type": "info",
20+
"text": "Want to learn more about AI chatbots?"
21+
},
22+
{
23+
"type": "action",
24+
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
1725
}
1826
]
1927
}

shiny/templates/chat/llms/langchain/_template.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,15 @@
55
"next_steps": [
66
"Put your OpenAI API key in the `template.env` file and rename it to `.env`.",
77
"Run the app with `shiny run app.py`."
8+
],
9+
"follow_up": [
10+
{
11+
"type": "info",
12+
"text": "Want to learn more about AI chatbots?"
13+
},
14+
{
15+
"type": "action",
16+
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
17+
}
818
]
919
}

shiny/templates/chat/llms/langchain/app.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@
3838
# Define a callback to run when the user submits a message
3939
@chat.on_user_submit
4040
async def handle_user_input(user_input: str):
41-
response = await chat_client.stream_async(user_input)
42-
await chat.append_message_stream(response)
41+
response = chat_client.astream(user_input)
42+
43+
async def stream_wrapper():
44+
async for item in response:
45+
yield item.content
46+
47+
await chat.append_message_stream(stream_wrapper())

shiny/templates/chat/llms/ollama/_template.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
{
1616
"type": "action",
1717
"text": "Learn more at https://posit-dev.github.io/chatlas/reference/ChatOllama.html"
18+
},
19+
{
20+
"type": "info",
21+
"text": "Want to learn more about AI chatbots?"
22+
},
23+
{
24+
"type": "action",
25+
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
1826
}
1927
]
2028
}

shiny/templates/chat/llms/openai/_template.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
{
1515
"type": "action",
1616
"text": "Learn how to obtain one at https://posit-dev.github.io/chatlas/reference/ChatOpenAI.html"
17+
},
18+
{
19+
"type": "info",
20+
"text": "Want to learn more about AI chatbots?"
21+
},
22+
{
23+
"type": "action",
24+
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
1725
}
1826
]
1927
}

shiny/templates/chat/llms/playground/_template.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
{
1515
"type": "action",
1616
"text": "Learn how to obtain them at https://posit-dev.github.io/chatlas/reference/"
17+
},
18+
{
19+
"type": "info",
20+
"text": "Want to learn more about AI chatbots?"
21+
},
22+
{
23+
"type": "action",
24+
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
1725
}
1826
]
1927
}

shiny/templates/chat/starters/sidebar-dark/_template.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

shiny/templates/chat/starters/sidebar-dark/app.py

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"type": "app",
3+
"id": "stream-ai-anthropic-aws",
4+
"title": "Stream from Anthropic via AWS Bedrock",
5+
"next_steps": [
6+
"Put your Bedrock credentials in the `template.env` file and rename it to `.env`.",
7+
"Run the app with `shiny run app.py`."
8+
],
9+
"follow_up": [
10+
{
11+
"type": "info",
12+
"text": "Need help connecting to Bedrock?"
13+
},
14+
{
15+
"type": "action",
16+
"text": "Visit https://posit-dev.github.io/chatlas/reference/ChatBedrockAnthropic.html"
17+
},
18+
{
19+
"type": "info",
20+
"text": "Want to learn more about streaming content?"
21+
},
22+
{
23+
"type": "action",
24+
"text": "Visit https://shiny.posit.co/py/docs/genai-stream.html"
25+
}
26+
]
27+
}

0 commit comments

Comments
 (0)