Skip to content

Commit

Permalink
Merge pull request #95 from 0ptim/92-create-initial-message-automatic…
Browse files Browse the repository at this point in the history
…ally-in-backend

92 create initial message automatically in backend
  • Loading branch information
0ptim authored Jul 26, 2023
2 parents 08d879f + 3f7a8a8 commit 1581270
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions backend/api_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -53,6 +50,33 @@ 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)
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}")
Expand Down Expand Up @@ -106,10 +130,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)
Expand Down

0 comments on commit 1581270

Please sign in to comment.