1+ import subprocess
2+ import boto3
3+ import sys
4+ import json
5+ from aws_lambda_powertools .utilities import parameters
6+ import pandas as pd
7+
8+ subprocess .call ([sys .executable , "-m" , "pip" , "install" , "pg8000" , "-t" , "/tmp/" ])
9+ sys .path .insert (0 , "/tmp/" )
10+ import pg8000
11+
12+ GEN_AI_LAMBDA = "More_Org_GenAI_Py_v3126"
13+
14+ def get_orgs_from_db (location , category ):
15+ try :
16+ creds = json .loads (parameters .get_parameter (
17+ '/dev/saayam/db/Virginia/Analytics/user' ,
18+ decrypt = True ,
19+ max_age = 3600
20+ ))
21+ database = creds ['DATABASE NAME' ]
22+
23+ conn = pg8000 .connect (
24+ host = creds ['HOST' ],
25+ user = creds ['USERNAME' ],
26+ password = creds ['PASSWORD' ],
27+ database = database ,
28+ port = creds ['PORT' ],
29+ ssl_context = True
30+ )
31+
32+ df = pd .read_sql (
33+ f"SELECT * FROM { database } .organizations WHERE mission = '{ category } ' AND city_name = '{ location } '" ,
34+ conn
35+ )
36+ conn .close ()
37+ return df
38+
39+ except parameters .GetParameterError as e :
40+ raise Exception (f'Failed to retrieve DB credentials: { str (e )} ' )
41+ except pg8000 .DatabaseError as e :
42+ raise Exception (f'Database error: { str (e )} ' )
43+ except Exception as e :
44+ raise Exception (f'Error fetching from DB: { str (e )} ' )
45+
46+
47+ def get_ai_orgs (subject , description , location ):
48+ try :
49+ response = boto3 .client ('lambda' ).invoke (
50+ FunctionName = GEN_AI_LAMBDA ,
51+ InvocationType = 'RequestResponse' ,
52+ Payload = json .dumps ({
53+ "subject" : subject ,
54+ "description" : description ,
55+ "location" : location
56+ })
57+ )
58+
59+ payload = json .loads (response ['Payload' ].read ())
60+
61+ if payload .get ('statusCode' ) != 200 :
62+ raise Exception (f'GenAI Lambda returned error: { payload } ' )
63+
64+ return pd .DataFrame (payload ['body' ]['organizations' ])
65+
66+ except boto3 .exceptions .Boto3Error as e :
67+ raise Exception (f'Failed to invoke GenAI Lambda: { str (e )} ' )
68+ except (KeyError , TypeError ) as e :
69+ raise Exception (f'Unexpected response structure from GenAI Lambda: { str (e )} ' )
70+ except Exception as e :
71+ raise Exception (f'Error fetching AI orgs: { str (e )} ' )
72+
73+
74+ def merge_organizations (db_organizations , genAI_organizations ):
75+ try :
76+ db_organizations = db_organizations .rename (columns = {
77+ 'org_name' : 'name' ,
78+ 'city_name' : 'location' ,
79+ 'phone' : 'contact'
80+ })[['name' , 'location' , 'contact' , 'email' , 'web_url' , 'mission' , 'source' ]]
81+
82+ genAI_organizations = genAI_organizations .rename (columns = {
83+ 'organization_name' : 'name'
84+ })[['name' , 'location' , 'contact' , 'email' , 'web_url' , 'mission' , 'source' ]]
85+
86+ return pd .concat ([db_organizations , genAI_organizations ], ignore_index = True )
87+
88+ except KeyError as e :
89+ raise Exception (f'Missing expected column during merge: { str (e )} ' )
90+ except Exception as e :
91+ raise Exception (f'Error merging organizations: { str (e )} ' )
0 commit comments