-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlambda_function.py
More file actions
182 lines (144 loc) · 6.2 KB
/
Copy pathlambda_function.py
File metadata and controls
182 lines (144 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import boto3
import os
from tools import ask_model, get_chat_id, get_updates, format_messages
BOT_ID = os.environ['BOT_ID']
def summarize_messages(parameters: list) -> str:
"""
Summarizes messages extracted from a chat.
Args:
parameters (list): A list of dictionaries containing parameters
related to the chat. Each dictionary contains 'name' and 'value'
keys, where 'name' represents the parameter name and 'value'
represents the parameter value.
Returns:
str: A summary of the chat and extracted action points.
"""
messages = next(item["value"] for item in parameters if item["name"] == "messages")
instructions = "Given the messages extracted from the chat, summarize.\n"
return ask_model(messages, instructions)
def query_chat(parameters: list) -> str:
"""
Queries the chat based on the custom prompt.
Args:
parameters (list): A list of dictionaries containing parameters
related to the chat. Each dictionary contains 'name' and 'value'
keys, where 'name' represents the parameter name and 'value'
represents the parameter value.
Returns:
str: A chat-based answer to the custom prompt.
"""
messages = next(item["value"] for item in parameters if item["name"] == "messages")
custom_prompt = next(item["value"] for item in parameters if item["name"] == "customPrompt")
instructions = f"Given messages extracted from the chat, " \
f"answer the following question: {custom_prompt} \n"
return ask_model(messages, instructions)
def translate_messages(parameters: list) -> str:
"""
Translates messages from a specified source language to English.
Args:
parameters (list): A list of dictionaries containing parameters
related to the translation. Each dictionary contains 'name' and 'value'
keys, where 'name' represents the parameter name and 'value'
represents the parameter value.
Returns:
str: Translated messages in English.
"""
messages = next(item["value"] for item in parameters if item["name"] == "messages")
source_lang = next(item["value"] for item in parameters if item["name"] == "sourceLanguage")
if not messages:
return "No messages was found. Consider using other chat."
client = boto3.client('translate', region_name='us-east-1')
response = client.translate_text(
Text=messages,
SourceLanguageCode=source_lang,
TargetLanguageCode='en'
)
translated_message = response['TranslatedText']
return translated_message
def detect_language(parameters: list) -> str:
"""
Detects the language of messages.
Args:
parameters (list): A list of dictionaries containing parameters
related to the chat. Each dictionary contains 'name' and 'value'
keys, where 'name' represents the parameter name and 'value'
represents the parameter value.
Returns:
str: Language code, for example 'pl'.
"""
messages = next(item["value"] for item in parameters if item["name"] == "messages")
if not messages:
return "No messages was found. Consider using other chat."
comprehend = boto3.client('comprehend')
response = comprehend.detect_dominant_language(Text=messages)
detected_language = response['Languages'][0]['LanguageCode']
return detected_language
def pull_messages(parameters: list) -> str:
"""
Gets messages updates from a specific chat.
Args:
parameters (list): A list of dictionaries containing parameters
related to the chat. Each dictionary contains 'name' and 'value'
keys, where 'name' represents the parameter name and 'value'
represents the parameter value.
Returns:
str: Pulled messages from a specific chat.
"""
chat_name = next(item["value"] for item in parameters if item["name"] == "chatName")
chat_id = get_chat_id(BOT_ID, chat_name)
updates = get_updates()
messages = format_messages(chat_id, updates)
return messages
def lambda_handler(event: dict, context: object) -> dict:
"""
Lambda function handler to process incoming API requests.
Args:
event (dict): The event data passed to the lambda function.
context (object): The runtime information of the lambda function.
Returns:
dict: A dictionary containing the response data to be returned
by the lambda function.
"""
print("Received event: ")
print(event)
response_code = None
action = event["actionGroup"]
api_path = event["apiPath"]
parameters = event.get("parameters", None)
if not parameters:
parameters = event['requestBody']['content']['application/json']['properties']
http_method = event["httpMethod"]
if api_path == '/pull-messages':
body = pull_messages(parameters)
response_body = {"application/json": {"body": str(body)}}
response_code = 200
elif api_path == '/detect-language':
body = detect_language(parameters)
response_body = {"application/json": {"body": str(body)}}
response_code = 200
elif api_path == '/translate':
body = translate_messages(parameters)
response_body = {"application/json": {"body": str(body)}}
response_code = 200
elif api_path == '/summarize':
body = summarize_messages(parameters)
response_body = {"application/json": {"body": str(body)}}
response_code = 200
elif api_path == '/query-chat':
body = query_chat(parameters)
response_body = {"application/json": {"body": str(body)}}
response_code = 200
else:
body = {"{}::{} is not a valid api, try another one.".format(action, api_path)}
response_code = 400
response_body = {"application/json": {"body": str(body)}}
print(f"Response body: {response_body}")
action_response = {
"actionGroup": action,
"apiPath": api_path,
"httpMethod": http_method,
"httpStatusCode": response_code,
"responseBody": response_body,
}
api_response = {"messageVersion": "1.0", "response": action_response}
return api_response