Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,40 @@
|
|
1 |
-
#
|
2 |
-
# A
|
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
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
messages=[
|
27 |
-
{"role": "system", "content": recipe_prompt},
|
28 |
-
{
|
29 |
-
"role": "assistant",
|
30 |
-
"content": "Hello! I'm a recipe extractor. Please enter a URL to a recipe page. For example, <https://www.thechunkychef.com/epic-dry-rubbed-baked-chicken-wings/>",
|
31 |
-
},
|
32 |
-
],
|
33 |
-
)
|
34 |
-
|
35 |
-
chat.ui(placeholder="Enter a recipe URL...")
|
36 |
-
|
37 |
-
|
38 |
-
# A function to transform user input
|
39 |
-
# Note that, if an exception occurs, the function will return a message to the user
|
40 |
-
# "short-circuiting" the conversation and asking the user to try again.
|
41 |
-
@chat.transform_user_input
|
42 |
-
async def try_scrape_page(input: str) -> str | None:
|
43 |
-
try:
|
44 |
-
return await scrape_page_with_url(input)
|
45 |
-
except Exception:
|
46 |
-
await chat.append_message(
|
47 |
-
"I'm sorry, I couldn't extract content from that URL. Please try again. "
|
48 |
-
)
|
49 |
-
return None
|
50 |
-
|
51 |
-
|
52 |
-
@chat.on_user_submit
|
53 |
-
async def _():
|
54 |
-
response = await llm.chat.completions.create(
|
55 |
-
model="gpt-4o",
|
56 |
-
messages=chat.messages(format="openai"),
|
57 |
-
temperature=0,
|
58 |
-
stream=True,
|
59 |
)
|
60 |
-
await chat.append_message_stream(response)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
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)
|