|
14 | 14 | from aws_lambda_powertools.utilities.parameters import get_parameter |
15 | 15 | from mypy_boto3_dynamodb.service_resource import Table |
16 | 16 |
|
| 17 | +# we use lru_cache for lots of configs so they are cached |
| 18 | + |
17 | 19 |
|
18 | 20 | @lru_cache() |
19 | 21 | def get_logger() -> Logger: |
20 | 22 | return Logger(service="slackBotFunction") |
21 | 23 |
|
22 | 24 |
|
| 25 | +# set up logger as its used in other functions |
23 | 26 | logger = get_logger() |
24 | 27 |
|
25 | 28 |
|
@@ -58,6 +61,27 @@ def get_ssm_params() -> Tuple[str, str]: |
58 | 61 | return bot_token, signing_secret |
59 | 62 |
|
60 | 63 |
|
| 64 | +@lru_cache |
| 65 | +def get_bot_token() -> str: |
| 66 | + bot_token, _ = get_ssm_params() |
| 67 | + return bot_token |
| 68 | + |
| 69 | + |
| 70 | +@lru_cache |
| 71 | +def get_guardrail_config() -> Tuple[str, str, str, str, str]: |
| 72 | + # Bedrock configuration from environment |
| 73 | + KNOWLEDGEBASE_ID = os.environ["KNOWLEDGEBASE_ID"] |
| 74 | + RAG_MODEL_ID = os.environ["RAG_MODEL_ID"] |
| 75 | + AWS_REGION = os.environ["AWS_REGION"] |
| 76 | + GUARD_RAIL_ID = os.environ["GUARD_RAIL_ID"] |
| 77 | + GUARD_VERSION = os.environ["GUARD_RAIL_VERSION"] |
| 78 | + |
| 79 | + logger.info( |
| 80 | + "Guardrail configuration loaded", extra={"guardrail_id": GUARD_RAIL_ID, "guardrail_version": GUARD_VERSION} |
| 81 | + ) |
| 82 | + return KNOWLEDGEBASE_ID, RAG_MODEL_ID, AWS_REGION, GUARD_RAIL_ID, GUARD_VERSION |
| 83 | + |
| 84 | + |
61 | 85 | @dataclass |
62 | 86 | class Constants: |
63 | 87 | FEEDBACK_PREFIX: str |
@@ -98,37 +122,28 @@ class Constants: |
98 | 122 | ) |
99 | 123 |
|
100 | 124 |
|
101 | | -@lru_cache |
102 | | -def get_bot_token() -> str: |
103 | | - bot_token, _ = get_ssm_params() |
104 | | - return bot_token |
| 125 | +@dataclass |
| 126 | +class BotMessages: |
| 127 | + EMPTY_QUERY: str |
| 128 | + ERROR_RESPONSE: str |
| 129 | + FEEDBACK_POSITIVE_THANKS: str |
| 130 | + FEEDBACK_NEGATIVE_THANKS: str |
| 131 | + FEEDBACK_THANKS: str |
| 132 | + FEEDBACK_PROMPT: str |
| 133 | + FEEDBACK_YES: str |
| 134 | + FEEDBACK_NO: str |
105 | 135 |
|
106 | 136 |
|
107 | 137 | # Bot response messages |
108 | | -BOT_MESSAGES = { |
109 | | - "empty_query": "Hi there! Please ask me a question and I'll help you find information from our knowledge base.", |
110 | | - "error_response": "Sorry, an error occurred while processing your request. Please try again later.", |
111 | | - "feedback_positive_thanks": "Thank you for your feedback.", |
112 | | - "feedback_negative_thanks": ( |
| 138 | +bot_messages = BotMessages( |
| 139 | + EMPTY_QUERY="Hi there! Please ask me a question and I'll help you find information from our knowledge base.", |
| 140 | + ERROR_RESPONSE="Sorry, an error occurred while processing your request. Please try again later.", |
| 141 | + FEEDBACK_POSITIVE_THANKS="Thank you for your feedback.", |
| 142 | + FEEDBACK_NEGATIVE_THANKS=( |
113 | 143 | 'Please let us know how the answer could be improved. Start your message with "feedback:"' |
114 | 144 | ), |
115 | | - "feedback_thanks": "Thank you for your feedback.", |
116 | | - "feedback_prompt": "Was this helpful?", |
117 | | - "feedback_yes": "Yes", |
118 | | - "feedback_no": "No", |
119 | | -} |
120 | | - |
121 | | - |
122 | | -@lru_cache |
123 | | -def get_guardrail_config() -> Tuple[str, str, str, str, str]: |
124 | | - # Bedrock configuration from environment |
125 | | - KNOWLEDGEBASE_ID = os.environ["KNOWLEDGEBASE_ID"] |
126 | | - RAG_MODEL_ID = os.environ["RAG_MODEL_ID"] |
127 | | - AWS_REGION = os.environ["AWS_REGION"] |
128 | | - GUARD_RAIL_ID = os.environ["GUARD_RAIL_ID"] |
129 | | - GUARD_VERSION = os.environ["GUARD_RAIL_VERSION"] |
130 | | - |
131 | | - logger.info( |
132 | | - "Guardrail configuration loaded", extra={"guardrail_id": GUARD_RAIL_ID, "guardrail_version": GUARD_VERSION} |
133 | | - ) |
134 | | - return KNOWLEDGEBASE_ID, RAG_MODEL_ID, AWS_REGION, GUARD_RAIL_ID, GUARD_VERSION |
| 145 | + FEEDBACK_THANKS="Thank you for your feedback.", |
| 146 | + FEEDBACK_PROMPT="Was this helpful?", |
| 147 | + FEEDBACK_YES="Yes", |
| 148 | + FEEDBACK_NO="No", |
| 149 | +) |
0 commit comments