Skip to content
Snippets Groups Projects

Dev

Merged Julian Rasch requested to merge jr662933/jupyterhub-ai:dev into dev
5 files
+ 163
18
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 43
9
from datetime import datetime
from dash import (
html,
Dash
@@ -8,16 +10,48 @@ from dash.dependencies import (
State
)
from llm_utils.client import ChatGPT
def format_chat_messages(chat_history):
chat_messages = []
for message in chat_history:
chat_messages.append(html.Div([
html.P(f'{message["sender"]}: {message["message"]}'),
html.P(f'Sent at: {message["timestamp"]}')
]))
return chat_messages
def register_callbacks(app: Dash):
chat_gpt = ChatGPT(model="gpt4")
@app.callback(
Output("output-container", "children"),
[Input("send-button", "n_clicks")],
[State("input-text", "value")]
[Output('chat-container', 'children'),
Output('chat-history', 'data')],
[Input('send-button', 'n_clicks')],
[State('user-input', 'value'),
State('chat-history', 'data')]
)
def generate_response(n_clicks, input_text):
if n_clicks > 0:
response = "You said: " + input_text
return html.Div(response)
else:
return ""
def update_chat(n_clicks, input_value, chat_history):
if chat_history is None:
chat_history = []
if n_clicks > 0 and input_value:
chat_history.append({
'sender': 'User',
'message': input_value,
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
})
response = chat_gpt.chat_with_gpt(input_value)
# Add response to chat history
chat_history.append({
'sender': 'Language Model',
'message': response,
'timestamp': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
})
return format_chat_messages(chat_history), chat_history
Loading