|
from fasthtml.common import * |
|
from openai import OpenAI |
|
from dotenv import load_dotenv, find_dotenv |
|
_ = load_dotenv(find_dotenv()) |
|
|
|
upstage_token = os.getenv('UPSTAGE_TOKEN') |
|
|
|
|
|
hdrs = (picolink, Script(src="https://cdn.tailwindcss.com"), |
|
Link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css")) |
|
app = FastHTML(hdrs=hdrs, cls="p-4 max-w-lg mx-auto") |
|
|
|
client = OpenAI( |
|
api_key=upstage_token, |
|
base_url="https://api.upstage.ai/v1/solar" |
|
) |
|
|
|
sp = "You are a helpful and concise assistant." |
|
|
|
def get_completion(prompt, model="solar-1-mini-chat"): |
|
messages = [{"role": "user", "content": prompt}] |
|
response = client.chat.completions.create( |
|
model=model, |
|
messages=messages, |
|
temperature=0, |
|
) |
|
return response.choices[0].message["content"] |
|
|
|
def get_completion_from_messages(messages, model="solar-1-mini-chat", temperature=0): |
|
response = client.chat.completions.create( |
|
model=model, |
|
messages=messages, |
|
temperature=temperature, |
|
) |
|
|
|
|
|
def ChatMessage(msg, user): |
|
bubble_class = "chat-bubble-primary" if user else 'chat-bubble-secondary' |
|
chat_class = "chat-end" if user else 'chat-start' |
|
return Div(cls=f"chat {chat_class}")( |
|
Div('user' if user else 'assistant', cls="chat-header"), |
|
Div(msg, cls=f"chat-bubble {bubble_class}"), |
|
Hidden(msg, name="messages") |
|
) |
|
|
|
|
|
|
|
def ChatInput(): |
|
return Input(name='msg', id='msg-input', placeholder="Type a message", |
|
cls="input input-bordered w-full", hx_swap_oob='true') |
|
|
|
|
|
@app.get |
|
def index(): |
|
page = Form(hx_post=send, hx_target="#chatlist", hx_swap="beforeend")( |
|
Div(id="chatlist", cls="chat-box h-[73vh] overflow-y-auto"), |
|
Div(cls="flex space-x-2 mt-2")( |
|
Group(ChatInput(), Button("Send", cls="btn btn-primary")) |
|
) |
|
) |
|
return Titled('Chatbot Demo', page) |
|
|
|
|
|
@app.post |
|
def send(msg:str, messages:list[str]=None): |
|
if not messages: messages = [] |
|
messages.append(msg.rstrip()) |
|
print(messages[0]) |
|
r = get_completion(messages[0]) |
|
return (ChatMessage(msg, True), |
|
ChatMessage(r.rstrip(), False), |
|
ChatInput()) |
|
|
|
serve() |
|
|