From 2adb9cf5b8c5f93ecf9eef31a08e3adb6d99dbb4 Mon Sep 17 00:00:00 2001 From: Alex Co Date: Mon, 25 Nov 2024 15:04:07 +0800 Subject: [PATCH] Return None if field is missing in Airtable record Signed-off-by: Alex Co --- backend/danswer/chat/process_message.py | 2 +- backend/danswer/prompts/prompt_utils.py | 27 ++++++++++++++++--------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/backend/danswer/chat/process_message.py b/backend/danswer/chat/process_message.py index 71896b5c84b..f4bd1fd6db9 100644 --- a/backend/danswer/chat/process_message.py +++ b/backend/danswer/chat/process_message.py @@ -282,7 +282,7 @@ def stream_chat_message_objects( user_id = user.id if user is not None else None user_email = user.email if user is not None else None - + chat_session = get_chat_session_by_id( chat_session_id=new_msg_req.chat_session_id, user_id=user_id, diff --git a/backend/danswer/prompts/prompt_utils.py b/backend/danswer/prompts/prompt_utils.py index e105124a6d1..0e4ecae6bb4 100644 --- a/backend/danswer/prompts/prompt_utils.py +++ b/backend/danswer/prompts/prompt_utils.py @@ -4,18 +4,19 @@ import redis from danswer.chat.models import LlmDoc -from danswer.configs.app_configs import AIRTABLE_API_TOKEN -from danswer.configs.app_configs import AIRTABLE_EMPLOYEE_BASE_ID -from danswer.configs.app_configs import AIRTABLE_EMPLOYEE_TABLE_NAME_OR_ID -from danswer.configs.app_configs import REDIS_DB_NUMBER -from danswer.configs.app_configs import REDIS_HOST -from danswer.configs.app_configs import REDIS_PORT +from danswer.configs.app_configs import ( + AIRTABLE_API_TOKEN, + AIRTABLE_EMPLOYEE_BASE_ID, + AIRTABLE_EMPLOYEE_TABLE_NAME_OR_ID, + REDIS_DB_NUMBER, + REDIS_HOST, + REDIS_PORT, +) from danswer.configs.chat_configs import LANGUAGE_HINT from danswer.configs.constants import DocumentSource from danswer.db.models import Prompt from danswer.llm.answering.models import PromptConfig -from danswer.prompts.chat_prompts import ADDITIONAL_INFO -from danswer.prompts.chat_prompts import CITATION_REMINDER +from danswer.prompts.chat_prompts import ADDITIONAL_INFO, CITATION_REMINDER from danswer.prompts.constants import CODE_BLOCK_PAT from danswer.search.models import InferenceChunk from danswer.utils.logger import setup_logger @@ -85,7 +86,15 @@ def add_employee_context_to_prompt(prompt_str: str, user_email: str) -> str: if "fields" in employee and "MV Email" in employee["fields"]: if employee["fields"]["MV Email"] == user_email: logger.info(f"Employee found: {employee['fields']['Preferred Name']}") - employee_context = f"My Name: {employee['fields']['Preferred Name']}\nMy Title: {employee['fields']['Job Role']}\nMy City Office: {employee['fields']['City Office']}\nMy Division: {employee['fields']['Import: Division']}\nMy Manager: {employee['fields']['Reports To']}\nMy Department: {employee['fields']['Import: Department']}\nMy Employment Status: {employee['fields']['Employment Status']}" + employee_context = ( + f"My Name: {employee['fields'].get('Preferred Name', None)}\n" + f"My Title: {employee['fields'].get('Job Role', None)}\n" + f"My City Office: {employee['fields'].get('City Office', None)}\n" + f"My Division: {employee['fields'].get('Import: Division', None)}\n" + f"My Manager: {employee['fields'].get('Reports To', None)}\n" + f"My Department: {employee['fields'].get('Import: Department', None)}\n" + f"My Employment Status: {employee['fields'].get('Employment Status', None)}" + ) # Store the employee context in Redis with a TTL of 30 days redis_client.setex(user_email, 7 * 24 * 60 * 60, employee_context)