Added Upstage ChatBot
Browse files- main.py +60 -54
- requirements.txt +1 -0
main.py
CHANGED
@@ -1,57 +1,63 @@
|
|
1 |
from fasthtml.common import *
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
serve()
|
|
|
|
1 |
from fasthtml.common import *
|
2 |
+
from openai import OpenAI # openai==1.2.0
|
3 |
+
from dotenv import load_dotenv, find_dotenv
|
4 |
+
_ = load_dotenv(find_dotenv())
|
5 |
+
|
6 |
+
upstage_token = os.getenv('UPSTAGE_TOKEN')
|
7 |
+
|
8 |
+
# Set up the app, including daisyui and tailwind for the chat component
|
9 |
+
hdrs = (picolink, Script(src="https://cdn.tailwindcss.com"),
|
10 |
+
Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css"))
|
11 |
+
app = FastHTML(hdrs=hdrs, cls="p-4 max-w-lg mx-auto")
|
12 |
+
|
13 |
+
client = OpenAI(
|
14 |
+
api_key=upstage_token,
|
15 |
+
base_url="https://api.upstage.ai/v1/solar"
|
16 |
+
)
|
17 |
+
|
18 |
+
def get_completion_from_messages(messages, model="solar-1-mini-chat", temperature=0):
|
19 |
+
response = client.ChatCompletion.create(
|
20 |
+
model=model,
|
21 |
+
messages=messages,
|
22 |
+
temperature=temperature, # this is the degree of randomness of the model's output
|
23 |
+
)
|
24 |
+
|
25 |
+
# Chat message component (renders a chat bubble)
|
26 |
+
def ChatMessage(msg, user):
|
27 |
+
bubble_class = "chat-bubble-primary" if user else 'chat-bubble-secondary'
|
28 |
+
chat_class = "chat-end" if user else 'chat-start'
|
29 |
+
return Div(cls=f"chat {chat_class}")(
|
30 |
+
Div('user' if user else 'assistant', cls="chat-header"),
|
31 |
+
Div(msg, cls=f"chat-bubble {bubble_class}"),
|
32 |
+
Hidden(msg, name="messages")
|
33 |
+
)
|
34 |
+
|
35 |
+
# The input field for the user message. Also used to clear the
|
36 |
+
# input field after sending a message via an OOB swap
|
37 |
+
def ChatInput():
|
38 |
+
return Input(name='msg', id='msg-input', placeholder="Type a message",
|
39 |
+
cls="input input-bordered w-full", hx_swap_oob='true')
|
40 |
+
|
41 |
+
# The main screen
|
42 |
+
@app.get
|
43 |
+
def index():
|
44 |
+
page = Form(hx_post=send, hx_target="#chatlist", hx_swap="beforeend")(
|
45 |
+
Div(id="chatlist", cls="chat-box h-[73vh] overflow-y-auto"),
|
46 |
+
Div(cls="flex space-x-2 mt-2")(
|
47 |
+
Group(ChatInput(), Button("Send", cls="btn btn-primary"))
|
48 |
+
)
|
49 |
+
)
|
50 |
+
return Titled('Chatbot Demo', page)
|
51 |
+
|
52 |
+
# Handle the form submission
|
53 |
+
@app.post
|
54 |
+
def send(msg:str, messages:list[str]=None):
|
55 |
+
if not messages: messages = []
|
56 |
+
messages.append(msg.rstrip())
|
57 |
+
r = contents(get_completion_from_messages(messages)) # get response from chat model
|
58 |
+
return (ChatMessage(msg, True), # The user's message
|
59 |
+
ChatMessage(r.rstrip(), False), # The chatbot's response
|
60 |
+
ChatInput()) # And clear the input field via an OOB swap
|
61 |
|
62 |
serve()
|
63 |
+
|
requirements.txt
CHANGED
@@ -3,3 +3,4 @@ huggingface-hub
|
|
3 |
fasthtml-hf
|
4 |
python-multipart
|
5 |
sqlite-utils
|
|
|
|
3 |
fasthtml-hf
|
4 |
python-multipart
|
5 |
sqlite-utils
|
6 |
+
claudette
|