Skip to content
Snippets Groups Projects

Dev

Merged Julian Rasch requested to merge jr662933/jupyterhub-ai:dev into dev
7 files
+ 48
21
Compare changes
  • Side-by-side
  • Inline
Files
7
+ 43
9
 
from datetime import datetime
 
from dash import (
from dash import (
html,
html,
Dash
Dash
@@ -8,16 +10,48 @@ from dash.dependencies import (
@@ -8,16 +10,48 @@ from dash.dependencies import (
State
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):
def register_callbacks(app: Dash):
 
 
chat_gpt = ChatGPT(model="gpt4")
 
@app.callback(
@app.callback(
Output("output-container", "children"),
[Output('chat-container', 'children'),
[Input("send-button", "n_clicks")],
Output('chat-history', 'data')],
[State("input-text", "value")]
[Input('send-button', 'n_clicks')],
 
[State('user-input', 'value'),
 
State('chat-history', 'data')]
)
)
def generate_response(n_clicks, input_text):
def update_chat(n_clicks, input_value, chat_history):
if n_clicks > 0:
if chat_history is None:
response = "You said: " + input_text
chat_history = []
return html.Div(response)
else:
if n_clicks > 0 and input_value:
return ""
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