Skip to content
Snippets Groups Projects

Finalized Jupyterlab for the sprint

Merged Julian Rasch requested to merge dev into main
5 files
+ 163
18
Compare changes
  • Side-by-side
  • Inline
Files
5
app/callbacks.py 0 → 100644
+ 65
0
 
import os
 
from datetime import datetime
 
 
from dash import (
 
html,
 
Dash
 
)
 
from dash.dependencies import (
 
Input,
 
Output,
 
State
 
)
 
 
from llm_utils.client import ChatGPT, get_openai_client
 
 
 
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):
 
model="gpt4"
 
client = get_openai_client(
 
model=model,
 
config_path=os.environ.get("CONFIG_PATH")
 
)
 
chat_gpt = ChatGPT(
 
client=client,
 
model="gpt4"
 
)
 
 
@app.callback(
 
[Output('chat-container', 'children'),
 
Output('chat-history', 'data')],
 
[Input('send-button', 'n_clicks')],
 
[State('user-input', 'value'),
 
State('chat-history', 'data')]
 
)
 
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