reset
Browse files
main.py
CHANGED
|
@@ -1,75 +1,10 @@
|
|
| 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 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
sp = "You are a helpful and concise assistant."
|
| 19 |
-
|
| 20 |
-
def get_completion(prompt, model="solar-1-mini-chat"):
|
| 21 |
-
messages = [{"role": "user", "content": prompt}]
|
| 22 |
-
response = client.chat.completions.create(
|
| 23 |
-
model=model,
|
| 24 |
-
messages=messages,
|
| 25 |
-
temperature=0, # this is the degree of randomness of the model's output
|
| 26 |
-
)
|
| 27 |
-
return response.choices[0].message.content
|
| 28 |
-
|
| 29 |
-
def get_completion_from_messages(messages, model="solar-1-mini-chat", temperature=0):
|
| 30 |
-
response = client.chat.completions.create(
|
| 31 |
-
model=model,
|
| 32 |
-
messages=messages,
|
| 33 |
-
temperature=temperature, # this is the degree of randomness of the model's output
|
| 34 |
-
)
|
| 35 |
-
return response.choices[0].message.content
|
| 36 |
-
|
| 37 |
-
# Chat message component (renders a chat bubble)
|
| 38 |
-
def ChatMessage(msg, user):
|
| 39 |
-
bubble_class = "chat-bubble-primary" if user else 'chat-bubble-secondary'
|
| 40 |
-
chat_class = "chat-end" if user else 'chat-start'
|
| 41 |
-
return Div(cls=f"chat {chat_class}")(
|
| 42 |
-
Div('user' if user else 'assistant', cls="chat-header"),
|
| 43 |
-
Div(msg, cls=f"chat-bubble {bubble_class}"),
|
| 44 |
-
Hidden(msg, name="messages")
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
# The input field for the user message. Also used to clear the
|
| 48 |
-
# input field after sending a message via an OOB swap
|
| 49 |
-
def ChatInput():
|
| 50 |
-
return Input(name='msg', id='msg-input', placeholder="Type a message",
|
| 51 |
-
cls="input input-bordered w-full", hx_swap_oob='true')
|
| 52 |
-
|
| 53 |
-
# The main screen
|
| 54 |
-
@app.get
|
| 55 |
-
def index():
|
| 56 |
-
page = Form(hx_post=send, hx_target="#chatlist", hx_swap="beforeend")(
|
| 57 |
-
Div(id="chatlist", cls="chat-box h-[73vh] overflow-y-auto"),
|
| 58 |
-
Div(cls="flex space-x-2 mt-2")(
|
| 59 |
-
Group(ChatInput(), Button("Send", cls="btn btn-primary"))
|
| 60 |
-
)
|
| 61 |
-
)
|
| 62 |
-
return Titled('Chatbot Demo', page)
|
| 63 |
-
|
| 64 |
-
# Handle the form submission
|
| 65 |
-
@app.post
|
| 66 |
-
def send(msg:str, messages:list[str]=None):
|
| 67 |
-
if not messages: messages = []
|
| 68 |
-
messages.append(msg.rstrip())
|
| 69 |
-
print(messages[0])
|
| 70 |
-
r = get_completion(messages[0]) # get response from chat model
|
| 71 |
-
return (ChatMessage(msg, True), # The user's message
|
| 72 |
-
ChatMessage(r.rstrip(), False), # The chatbot's response
|
| 73 |
-
ChatInput()) # And clear the input field via an OOB swap
|
| 74 |
|
| 75 |
serve()
|
|
|
|
| 1 |
from fasthtml.common import *
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
app = FastHTML()
|
| 4 |
+
rt = app.route
|
| 5 |
|
| 6 |
+
@rt('/')
|
| 7 |
+
def get():
|
| 8 |
+
return 'Hello World!!'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
serve()
|