fergos80 commited on
Commit
ecf8a9f
·
verified ·
1 Parent(s): 4664f72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -40
app.py CHANGED
@@ -1,40 +1,35 @@
1
- # -----------------------------------------------------------------------------
2
- # A basic example of dynamically re-rendering a Shiny Chat instance with different models.
3
- # To run it, you'll need an OpenAI API key.
4
- # To get one, follow the instructions at https://platform.openai.com/docs/quickstart
5
- # -----------------------------------------------------------------------------
6
- import os
7
-
8
- from langchain_openai import ChatOpenAI
9
-
10
- from shiny.express import input, render, ui
11
-
12
- ui.input_select("model", "Model", choices=["gpt-4o", "gpt-3.5-turbo"])
13
-
14
-
15
- @render.express
16
- def chat_ui():
17
-
18
- chat = ui.Chat(
19
- id="chat",
20
- messages=[
21
- {
22
- "content": f"Hi! I'm a {input.model()} model. How can I help you today?",
23
- "role": "assistant",
24
- }
25
- ],
26
- )
27
-
28
- chat.ui()
29
-
30
- llm = ChatOpenAI(
31
- model=input.model(),
32
- # Provide your API key here (or set the environment variable)
33
- api_key=os.environ.get("OPENAI_API_KEY"), # type: ignore
34
- )
35
-
36
- @chat.on_user_submit
37
- async def _():
38
- messages = chat.messages(format="langchain")
39
- response = llm.astream(messages)
40
- await chat.append_message_stream(response)
 
1
+ from shiny.express import ui
2
+
3
+ # Set some Shiny page options
4
+ ui.page_opts(
5
+ title="Hello Shiny Chat",
6
+ fillable=True,
7
+ fillable_mobile=True,
8
+ )
9
+
10
+ # Create a welcome message
11
+ welcome = ui.markdown(
12
+ """
13
+ Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
14
+ simply repeat it back to you. For more examples, see this
15
+ [folder of examples](https://github.com/posit-dev/py-shiny/tree/main/examples/chat).
16
+ """
17
+ )
18
+
19
+ # Create a chat instance
20
+ chat = ui.Chat(
21
+ id="chat",
22
+ messages=[welcome],
23
+ )
24
+
25
+ # Display it
26
+ chat.ui()
27
+
28
+
29
+ # Define a callback to run when the user submits a message
30
+ @chat.on_user_submit
31
+ async def _():
32
+ # Get the user's input
33
+ user = chat.user_input()
34
+ # Append a response to the chat
35
+ await chat.append_message(f"You said: {user}")