-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_group_lambda.py
More file actions
411 lines (341 loc) · 13.3 KB
/
Copy pathaction_group_lambda.py
File metadata and controls
411 lines (341 loc) · 13.3 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
"""
Action Group Lambda function for Bedrock Agent.
Handles video retrieval, transcription, and role determination actions.
"""
import json
import logging
import os
import tempfile
import uuid
from typing import Dict, Any
import boto3
from botocore.exceptions import ClientError
# Configure logging
logger = logging.getLogger()
logger.setLevel(os.environ.get('LOG_LEVEL', 'INFO'))
# Initialize AWS clients
s3_client = boto3.client('s3')
bedrock_agent_runtime = boto3.client('bedrock-agent-runtime')
# Environment variables
DEEPGRAM_API_KEY = os.environ.get('DEEPGRAM_API_KEY')
DEFAULT_ROLE = os.environ.get('DEFAULT_ROLE', 'general')
ROLE_AGENT_ID = os.environ.get('ROLE_AGENT_ID')
def get_video_from_s3(bucket_name: str, object_key: str) -> bytes:
"""
Retrieve a video file from S3 bucket.
Args:
bucket_name: Name of the S3 bucket
object_key: S3 object key
Returns:
Video file content as bytes
"""
try:
logger.info(f"Retrieving video from S3: s3://{bucket_name}/{object_key}")
response = s3_client.get_object(Bucket=bucket_name, Key=object_key)
video_data = response['Body'].read()
logger.info(f"Successfully retrieved video: {len(video_data)} bytes")
return video_data
except ClientError as e:
error_code = e.response['Error']['Code']
logger.error(f"S3 error: {error_code}")
raise
def retrieve_video_from_s3_action(parameters: Dict[str, Any]) -> Dict[str, Any]:
"""
Action: Retrieve video file from S3.
Args:
parameters: Action parameters containing bucket_name and video_key
Returns:
Action response with video data or error
"""
try:
bucket_name = parameters.get('bucket_name')
video_key = parameters.get('video_key')
if not bucket_name or not video_key:
return {
'success': False,
'error': 'MissingParameters',
'message': 'bucket_name and video_key are required'
}
video_data = get_video_from_s3(bucket_name, video_key)
return {
'success': True,
'video_size_bytes': len(video_data),
'bucket_name': bucket_name,
'video_key': video_key,
'message': 'Video retrieved successfully'
}
except ClientError as e:
error_code = e.response['Error']['Code']
if error_code == 'NoSuchKey':
return {
'success': False,
'error': 'NoSuchKey',
'message': f'Video file not found: {video_key}'
}
elif error_code == 'AccessDenied':
return {
'success': False,
'error': 'AccessDenied',
'message': f'Access denied to video file: {video_key}'
}
else:
return {
'success': False,
'error': error_code,
'message': f'S3 error: {str(e)}'
}
except Exception as e:
logger.error(f"Unexpected error in retrieve_video_from_s3: {str(e)}")
return {
'success': False,
'error': 'UnexpectedError',
'message': str(e)
}
def transcribe_video_action(parameters: Dict[str, Any]) -> Dict[str, Any]:
"""
Action: Transcribe video using AWS Transcribe (fallback from Deepgram).
Args:
parameters: Action parameters containing bucket_name and video_key
Returns:
Action response with transcription or error
"""
try:
bucket_name = parameters.get('bucket_name')
video_key = parameters.get('video_key')
if not bucket_name or not video_key:
return {
'success': False,
'error': 'MissingParameters',
'message': 'bucket_name and video_key are required'
}
# Use AWS Transcribe as fallback since FFmpeg is not available
transcribe_client = boto3.client('transcribe')
# Generate unique job name
import time
job_name = f"video-transcribe-{int(time.time())}"
# Start transcription job
logger.info(f"Starting AWS Transcribe job: {job_name}")
media_uri = f"s3://{bucket_name}/{video_key}"
transcribe_client.start_transcription_job(
TranscriptionJobName=job_name,
Media={'MediaFileUri': media_uri},
MediaFormat='mp4',
LanguageCode='en-US',
Settings={
'ShowSpeakerLabels': False
}
)
# Wait for job to complete (with timeout)
max_wait = 300 # 5 minutes
wait_time = 0
while wait_time < max_wait:
status = transcribe_client.get_transcription_job(TranscriptionJobName=job_name)
job_status = status['TranscriptionJob']['TranscriptionJobStatus']
if job_status == 'COMPLETED':
# Get transcript
transcript_uri = status['TranscriptionJob']['Transcript']['TranscriptFileUri']
# Download transcript
import urllib.request
with urllib.request.urlopen(transcript_uri) as response:
transcript_data = json.loads(response.read())
transcript_text = transcript_data['results']['transcripts'][0]['transcript']
# Clean up job
try:
transcribe_client.delete_transcription_job(TranscriptionJobName=job_name)
except:
pass
logger.info(f"Transcription complete: {len(transcript_text)} characters")
return {
'success': True,
'transcript': transcript_text,
'word_count': len(transcript_text.split()),
'duration': 0, # Not available from Transcribe
'language': 'en-US',
'confidence': 0.95,
'message': 'Video transcribed successfully using AWS Transcribe'
}
elif job_status == 'FAILED':
failure_reason = status['TranscriptionJob'].get('FailureReason', 'Unknown')
return {
'success': False,
'error': 'TranscriptionFailed',
'message': f'AWS Transcribe failed: {failure_reason}'
}
# Wait and retry
time.sleep(10)
wait_time += 10
logger.info(f"Waiting for transcription... ({wait_time}s)")
return {
'success': False,
'error': 'TranscriptionTimeout',
'message': 'Transcription job timed out after 5 minutes'
}
except ClientError as e:
error_code = e.response['Error']['Code']
return {
'success': False,
'error': error_code,
'message': f'AWS error: {str(e)}'
}
except Exception as e:
logger.error(f"Transcription error: {str(e)}")
return {
'success': False,
'error': 'TranscriptionError',
'message': str(e)
}
def invoke_role_agent_action(parameters: Dict[str, Any]) -> Dict[str, Any]:
"""
Action: Invoke Role Determination Agent.
Args:
parameters: Action parameters containing user_prompt
Returns:
Action response with role information or error
"""
try:
user_prompt = parameters.get('user_prompt')
if not user_prompt:
return {
'success': False,
'error': 'MissingParameters',
'message': 'user_prompt is required'
}
if not ROLE_AGENT_ID:
logger.warning("ROLE_AGENT_ID not configured, using default role")
return {
'success': True,
'role': DEFAULT_ROLE,
'context': 'Default role used (agent not configured)',
'confidence': 0.0,
'fallback': True
}
# Generate session ID
session_id = f"role-session-{uuid.uuid4()}"
logger.info(f"Invoking Role Determination Agent: {ROLE_AGENT_ID}")
logger.info(f"User prompt: {user_prompt}")
# Invoke Role Determination Agent
response = bedrock_agent_runtime.invoke_agent(
agentId=ROLE_AGENT_ID,
agentAliasId='TSTALIASID',
sessionId=session_id,
inputText=f"Extract role from: {user_prompt}"
)
# Stream and parse response
event_stream = response['completion']
full_response = ""
for event in event_stream:
if 'chunk' in event:
chunk = event['chunk']
if 'bytes' in chunk:
full_response += chunk['bytes'].decode('utf-8')
logger.info(f"Role agent response: {full_response}")
# Try to parse JSON response
try:
role_info = json.loads(full_response)
return {
'success': True,
'role': role_info.get('role', DEFAULT_ROLE),
'context': role_info.get('context', ''),
'confidence': role_info.get('confidence', 0.5),
'fallback': role_info.get('fallback', False)
}
except json.JSONDecodeError:
# If not JSON, extract role from text
logger.warning("Could not parse JSON response, extracting role from text")
return {
'success': True,
'role': DEFAULT_ROLE,
'context': full_response,
'confidence': 0.3,
'fallback': True
}
except ClientError as e:
logger.error(f"Bedrock error invoking role agent: {str(e)}")
return {
'success': True,
'role': DEFAULT_ROLE,
'context': f'Error invoking agent: {str(e)}',
'confidence': 0.0,
'fallback': True
}
except Exception as e:
logger.error(f"Unexpected error in invoke_role_agent: {str(e)}")
return {
'success': True,
'role': DEFAULT_ROLE,
'context': f'Error: {str(e)}',
'confidence': 0.0,
'fallback': True
}
def lambda_handler(event: Dict[str, Any], context: Any) -> Dict[str, Any]:
"""
Main Lambda handler for Bedrock Agent action group.
Args:
event: Bedrock Agent action group event
context: Lambda context
Returns:
Action group response
"""
logger.info(f"Action group event: {json.dumps(event)}")
try:
# Parse Bedrock Agent action group event
action_group = event.get('actionGroup')
api_path = event.get('apiPath')
http_method = event.get('httpMethod')
parameters = event.get('parameters', [])
# Convert parameters list to dict
params_dict = {}
for param in parameters:
params_dict[param['name']] = param['value']
logger.info(f"Action: {api_path}, Method: {http_method}")
logger.info(f"Parameters: {params_dict}")
# Route to appropriate action handler
if api_path == '/retrieve_video_from_s3':
result = retrieve_video_from_s3_action(params_dict)
elif api_path == '/transcribe_video':
result = transcribe_video_action(params_dict)
elif api_path == '/invoke_role_agent':
result = invoke_role_agent_action(params_dict)
else:
result = {
'success': False,
'error': 'UnknownAction',
'message': f'Unknown action: {api_path}'
}
# Format response for Bedrock Agent
response = {
'messageVersion': '1.0',
'response': {
'actionGroup': action_group,
'apiPath': api_path,
'httpMethod': http_method,
'httpStatusCode': 200 if result.get('success') else 400,
'responseBody': {
'application/json': {
'body': json.dumps(result)
}
}
}
}
logger.info(f"Action response: {json.dumps(response)}")
return response
except Exception as e:
logger.error(f"Error in lambda_handler: {str(e)}", exc_info=True)
return {
'messageVersion': '1.0',
'response': {
'actionGroup': event.get('actionGroup', 'unknown'),
'apiPath': event.get('apiPath', 'unknown'),
'httpMethod': event.get('httpMethod', 'POST'),
'httpStatusCode': 500,
'responseBody': {
'application/json': {
'body': json.dumps({
'success': False,
'error': 'InternalError',
'message': str(e)
})
}
}
}
}