Skip to content
Merged
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
60 changes: 38 additions & 22 deletions shiny/_main_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,6 @@ def apps(self) -> list[ShinyTemplate]:
def packages(self) -> list[ShinyTemplate]:
return self._templates("templates/package")

@property
def chat_starters(self) -> list[ShinyTemplate]:
return self._templates("templates/chat/starters")

@property
def chat_llms(self) -> list[ShinyTemplate]:
return self._templates("templates/chat/llms")
Expand All @@ -235,6 +231,14 @@ def chat_llms(self) -> list[ShinyTemplate]:
def chat_enterprise(self) -> list[ShinyTemplate]:
return self._templates("templates/chat/llm-enterprise")

@property
def stream_llms(self) -> list[ShinyTemplate]:
return self._templates("templates/markdown-stream/llms")

@property
def stream_enterprise(self) -> list[ShinyTemplate]:
return self._templates("templates/markdown-stream/llm-enterpise")


shiny_internal_templates = ShinyInternalTemplates()

Expand All @@ -261,15 +265,16 @@ def use_internal_template(

app_templates = shiny_internal_templates.apps
pkg_templates = shiny_internal_templates.packages
chat_templates = [
*shiny_internal_templates.chat_starters,
gen_ai_templates = [
*shiny_internal_templates.chat_llms,
*shiny_internal_templates.chat_enterprise,
*shiny_internal_templates.stream_llms,
*shiny_internal_templates.stream_enterprise,
]

menu_choices = [
Choice(title="Generative AI...", value="_gen-ai"),
Choice(title="Custom JavaScript component...", value="_js-component"),
Choice(title="Chat component templates...", value="_chat"),
Choice(
title="Choose from the Shiny Templates website", value="_external-gallery"
),
Expand All @@ -279,7 +284,7 @@ def use_internal_template(
question_state = question_choose_template(app_templates, *menu_choices)

template = template_by_name(
[*app_templates, *pkg_templates, *chat_templates], question_state
[*app_templates, *pkg_templates, *gen_ai_templates], question_state
)

if template is not None:
Expand All @@ -302,8 +307,8 @@ def use_internal_template(
sys.exit(0)
elif question_state == "_js-component":
use_internal_package_template(dest_dir=dest_dir, package_name=package_name)
elif question_state == "_chat":
use_internal_chat_ai_template(dest_dir=dest_dir, package_name=package_name)
elif question_state == "_gen-ai":
use_internal_gen_ai_template(dest_dir=dest_dir, package_name=package_name)
else:
valid_choices = [t.id for t in app_templates + pkg_templates]
if question_state not in valid_choices:
Expand Down Expand Up @@ -345,18 +350,24 @@ def use_internal_package_template(
package_template_questions(template, dest_dir=dest_dir, package_name=package_name)


def use_internal_chat_ai_template(
def use_internal_gen_ai_template(
input: str | None = None,
dest_dir: Optional[Path] = None,
package_name: Optional[str] = None,
):
if input is None:
input = questionary.select(
"Which kind of chat template would you like?",
"Which kind of Gen AI template would you like?",
choices=[
Choice(title="Chat starters...", value="_chat-starters"),
Choice(title="LLM powered chat...", value="_chat-llms"),
Choice(title="Enterprise LLM...", value="_chat-llm_enterprise"),
Choice(title="Chat with LLM...", value="_chat-llms"),
Choice(
title="Chat with enterprise LLM...", value="_chat-llm_enterprise"
),
Choice(title="Stream markdown with LLM...", value="_stream-llms"),
Choice(
title="Stream markdown with enterprise LLM...",
value="_stream-enterprise",
),
back_choice,
cancel_choice,
],
Expand All @@ -370,29 +381,34 @@ def use_internal_chat_ai_template(
use_internal_template(dest_dir=dest_dir, package_name=package_name)
return

use_internal_chat_ai_template(
use_internal_gen_ai_template(
input, dest_dir=dest_dir, package_name=package_name
)
return

if input == "_chat-starters":
template_choices = shiny_internal_templates.chat_starters
elif input == "_chat-llms":
if input == "_chat-llms":
template_choices = shiny_internal_templates.chat_llms
else:
elif input == "_chat-llm_enterprise":
template_choices = shiny_internal_templates.chat_enterprise
elif input == "_stream-llms":
template_choices = shiny_internal_templates.stream_llms
elif input == "_stream-enterprise":
template_choices = shiny_internal_templates.stream_enterprise
else:
raise ValueError(f"Invalid Gen AI template choice: {input}")

choice = question_choose_template(template_choices, back_choice)

if choice == "back":
use_internal_chat_ai_template(dest_dir=dest_dir, package_name=package_name)
use_internal_gen_ai_template(dest_dir=dest_dir, package_name=package_name)
return

template = template_by_name(
[
*shiny_internal_templates.chat_starters,
*shiny_internal_templates.chat_llms,
*shiny_internal_templates.chat_enterprise,
*shiny_internal_templates.stream_llms,
*shiny_internal_templates.stream_enterprise,
],
choice,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
# Create a welcome message
welcome = """
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
simply repeat it back to you. For more examples, see this
[folder of examples](https://github.com/posit-dev/py-shiny/tree/main/shiny/templates/chat).
simply repeat it back to you.

To learn more about chatbots and how to build them with Shiny, check out
[the documentation](https://shiny.posit.co/py/docs/genai-chatbots.html).
"""


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
# Create a welcome message
welcome = """
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
simply repeat it back to you. For more examples, see this
[folder of examples](https://github.com/posit-dev/py-shiny/tree/main/shiny/templates/chat).
simply repeat it back to you.

To learn more about chatbots and how to build them with Shiny, check out
[the documentation](https://shiny.posit.co/py/docs/genai-chatbots.html).
"""

# Create a chat instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
{
"type": "action",
"text": "Learn more at https://posit-dev.github.io/chatlas/reference/ChatBedrockAnthropic.html"
},
{
"type": "info",
"text": "Want to learn more about AI chatbots?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
{
"type": "action",
"text": "Learn more at https://posit-dev.github.io/chatlas/reference/ChatAzureOpenAI.html"
},
{
"type": "info",
"text": "Want to learn more about AI chatbots?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
}
]
}
8 changes: 8 additions & 0 deletions shiny/templates/chat/llms/anthropic/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
{
"type": "action",
"text": "Learn how to obtain one at https://posit-dev.github.io/chatlas/reference/ChatAnthropic.html"
},
{
"type": "info",
"text": "Want to learn more about AI chatbots?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
}
]
}
8 changes: 8 additions & 0 deletions shiny/templates/chat/llms/google/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
{
"type": "action",
"text": "Learn how to obtain one at https://posit-dev.github.io/chatlas/reference/ChatGoogle.html"
},
{
"type": "info",
"text": "Want to learn more about AI chatbots?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
}
]
}
10 changes: 10 additions & 0 deletions shiny/templates/chat/llms/langchain/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@
"next_steps": [
"Put your OpenAI API key in the `template.env` file and rename it to `.env`.",
"Run the app with `shiny run app.py`."
],
"follow_up": [
{
"type": "info",
"text": "Want to learn more about AI chatbots?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
}
]
}
9 changes: 7 additions & 2 deletions shiny/templates/chat/llms/langchain/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@
# Define a callback to run when the user submits a message
@chat.on_user_submit
async def handle_user_input(user_input: str):
response = await chat_client.stream_async(user_input)
await chat.append_message_stream(response)
response = chat_client.astream(user_input)

async def stream_wrapper():
async for item in response:
yield item.content

await chat.append_message_stream(stream_wrapper())
8 changes: 8 additions & 0 deletions shiny/templates/chat/llms/ollama/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
{
"type": "action",
"text": "Learn more at https://posit-dev.github.io/chatlas/reference/ChatOllama.html"
},
{
"type": "info",
"text": "Want to learn more about AI chatbots?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
}
]
}
8 changes: 8 additions & 0 deletions shiny/templates/chat/llms/openai/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
{
"type": "action",
"text": "Learn how to obtain one at https://posit-dev.github.io/chatlas/reference/ChatOpenAI.html"
},
{
"type": "info",
"text": "Want to learn more about AI chatbots?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
}
]
}
8 changes: 8 additions & 0 deletions shiny/templates/chat/llms/playground/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
{
"type": "action",
"text": "Learn how to obtain them at https://posit-dev.github.io/chatlas/reference/"
},
{
"type": "info",
"text": "Want to learn more about AI chatbots?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-chatbots.html"
}
]
}
8 changes: 0 additions & 8 deletions shiny/templates/chat/starters/sidebar-dark/_template.json

This file was deleted.

30 changes: 0 additions & 30 deletions shiny/templates/chat/starters/sidebar-dark/app.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"type": "app",
"id": "stream-ai-anthropic-aws",
"title": "Stream from Anthropic via AWS Bedrock",
"next_steps": [
"Put your Bedrock credentials in the `template.env` file and rename it to `.env`.",
"Run the app with `shiny run app.py`."
],
"follow_up": [
{
"type": "info",
"text": "Need help connecting to Bedrock?"
},
{
"type": "action",
"text": "Visit https://posit-dev.github.io/chatlas/reference/ChatBedrockAnthropic.html"
},
{
"type": "info",
"text": "Want to learn more about streaming content?"
},
{
"type": "action",
"text": "Visit https://shiny.posit.co/py/docs/genai-stream.html"
}
]
}
Loading
Loading