Skip to content

Commit

Permalink
dev: update chatgpt-clone example
Browse files Browse the repository at this point in the history
* add system prompt as api param
  • Loading branch information
ajndkr committed Dec 2, 2023
1 parent ebbe1d0 commit aad6a74
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions examples/chatgpt-clone/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@


@router.post("/chat")
def chat() -> ChatCompletionResource:
system = "You are a sassy assistant"
def chat(system: str = "You are a sassy assistant") -> ChatCompletionResource:
return ChatCompletionResource(system=system, stream=True)


Expand All @@ -22,7 +21,9 @@ def mount_playground(app: Lanarky) -> Lanarky:
blocks = gr.Blocks(
title="ChatGPT-clone",
theme=gr.themes.Default(
primary_hue=gr.themes.colors.teal, secondary_hue=gr.themes.colors.teal
primary_hue=gr.themes.colors.teal,
secondary_hue=gr.themes.colors.teal,
text_size=gr.themes.sizes.text_lg,
),
css="footer {visibility: hidden}",
)
Expand All @@ -39,14 +40,17 @@ def mount_playground(app: Lanarky) -> Lanarky:
gr.HTML(
"""<div align="center"><img src="https://lanarky.ajndkr.com/assets/logo-light-mode.png" width="350"></div>"""
)
system_message = gr.Textbox(
value="You are a sassy assistant", label="System Prompt"
)
chatbot = gr.Chatbot(height=500, show_label=False)
with gr.Row():
user_input = gr.Textbox(
show_label=False, placeholder="Type a message...", scale=5
)
clear_btn = gr.Button("Clear")

def chat(history):
def chat(history, system):
messages = []
for human, assistant in history:
if human:
Expand All @@ -56,7 +60,7 @@ def chat(history):

history[-1][1] = ""
for event in StreamingClient().stream_response(
"POST", "/chat", json={"messages": messages}
"POST", "/chat", json={"messages": messages}, params={"system": system}
):
history[-1][1] += event.data
yield history
Expand All @@ -66,7 +70,7 @@ def chat(history):
[user_input, chatbot],
[user_input, chatbot],
queue=False,
).then(chat, chatbot, chatbot)
).then(chat, [chatbot, system_message], chatbot)
clear_btn.click(lambda: None, None, chatbot, queue=False)

return gr.mount_gradio_app(app, blocks.queue(), "/")
Expand Down

0 comments on commit aad6a74

Please sign in to comment.