From 35469f3631d416217c8c451ead140325ae586a86 Mon Sep 17 00:00:00 2001 From: 0ptim Date: Wed, 26 Jul 2023 15:00:14 +0200 Subject: [PATCH 1/2] Extract code into def for user creation --- backend/api_routes.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/backend/api_routes.py b/backend/api_routes.py index 74c99f0..420837e 100644 --- a/backend/api_routes.py +++ b/backend/api_routes.py @@ -25,10 +25,7 @@ def process_input(app_instance, user_token, message): if not is_valid: return error_response, error_code - user_id = check_user_exists(user_token) - if user_id is None: - print("Creating user: ", user_token) - user_id = create_user(user_token) + user_id = get_user_id(user_token) chat_agent = agent_for_user( user_token, CallbackHandlers.FinalOutputHandler(app_instance) @@ -53,6 +50,17 @@ def process_input(app_instance, user_token, message): return jsonify({"response": response}), 200 +def get_user_id(user_token): + """ + Get the user_id for a user_token. If the user does not exist, create a new user. + """ + user_id = check_user_exists(user_token) + if user_id is None: + print("Creating user: ", user_token) + user_id = create_user(user_token) + return user_id + + def log_response_info(callback_obj): print(f"ℹ Total Tokens: {callback_obj.total_tokens}") print(f"ℹ Prompt Tokens: {callback_obj.prompt_tokens}") @@ -106,10 +114,7 @@ def get_user_history(): if not user_token: return make_response("User token is missing or empty", 400) - user_id = check_user_exists(user_token) - if user_id is None: - print("Creating user: ", user_token) - user_id = create_user(user_token) + user_id = get_user_id(user_token) chat_messages = get_chat_history(user_id) return make_response(jsonify(chat_messages), 200) From 3f7a8a8d8b390c74fb80a17d7d1a28f0ef6f0441 Mon Sep 17 00:00:00 2001 From: 0ptim Date: Wed, 26 Jul 2023 15:21:58 +0200 Subject: [PATCH 2/2] Set initial greeting message for every new user --- backend/api_routes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/api_routes.py b/backend/api_routes.py index 420837e..066bfb8 100644 --- a/backend/api_routes.py +++ b/backend/api_routes.py @@ -58,9 +58,25 @@ def get_user_id(user_token): if user_id is None: print("Creating user: ", user_token) user_id = create_user(user_token) + set_inital_message(user_id) return user_id +def set_inital_message(user_id): + """ + Set the initial message from Jelly as greeting. + """ + add_chat_message( + user_id, + "jelly", + """Hi there, it's Jelly 🪼 + +Your friendly undersea guide to everything DeFiChain. Feel comfortable to dive into any question you've got. + +Ready to navigate through this exciting blockchain journey together? 🌊""", + ) + + def log_response_info(callback_obj): print(f"ℹ Total Tokens: {callback_obj.total_tokens}") print(f"ℹ Prompt Tokens: {callback_obj.prompt_tokens}")