Anirudh Esthuri commited on
Commit
4d77281
Β·
1 Parent(s): c5d7dd0

Add better error handling for AWS Bedrock credentials

Browse files
Files changed (2) hide show
  1. app.py +22 -10
  2. llm.py +91 -37
app.py CHANGED
@@ -437,14 +437,22 @@ if msg:
437
  rewritten_msg = rewrite_message(msg, persona_name, show_rationale)
438
  msgs = clean_history(st.session_state.history, persona_name)
439
  msgs = append_user_turn(msgs, rewritten_msg)
440
- txt, lat, tok, tps = chat(msgs, persona_name)
441
- all_answers[persona_name] = txt
 
 
 
 
442
 
443
  rewritten_msg_control = rewrite_message(msg, "Control", show_rationale)
444
  msgs_control = clean_history(st.session_state.history, "Control")
445
  msgs_control = append_user_turn(msgs_control, rewritten_msg_control)
446
- txt_control, lat, tok, tps = chat(msgs_control, "Arnold")
447
- all_answers["Control"] = txt_control
 
 
 
 
448
 
449
  st.session_state.history.append(
450
  {"role": "assistant_all", "axis": "role", "content": all_answers, "is_new": True}
@@ -453,12 +461,16 @@ if msg:
453
  rewritten_msg = rewrite_message(msg, persona_name, show_rationale)
454
  msgs = clean_history(st.session_state.history, persona_name)
455
  msgs = append_user_turn(msgs, rewritten_msg)
456
- txt, lat, tok, tps = chat(
457
- msgs, "Arnold" if persona_name == "Control" else persona_name
458
- )
459
- st.session_state.history.append(
460
- {"role": "assistant", "persona": persona_name, "content": txt, "is_new": True}
461
- )
 
 
 
 
462
  st.rerun()
463
 
464
 
 
437
  rewritten_msg = rewrite_message(msg, persona_name, show_rationale)
438
  msgs = clean_history(st.session_state.history, persona_name)
439
  msgs = append_user_turn(msgs, rewritten_msg)
440
+ try:
441
+ txt, lat, tok, tps = chat(msgs, persona_name)
442
+ all_answers[persona_name] = txt
443
+ except ValueError as e:
444
+ st.error(f"❌ {str(e)}")
445
+ st.stop()
446
 
447
  rewritten_msg_control = rewrite_message(msg, "Control", show_rationale)
448
  msgs_control = clean_history(st.session_state.history, "Control")
449
  msgs_control = append_user_turn(msgs_control, rewritten_msg_control)
450
+ try:
451
+ txt_control, lat, tok, tps = chat(msgs_control, "Arnold")
452
+ all_answers["Control"] = txt_control
453
+ except ValueError as e:
454
+ st.error(f"❌ {str(e)}")
455
+ st.stop()
456
 
457
  st.session_state.history.append(
458
  {"role": "assistant_all", "axis": "role", "content": all_answers, "is_new": True}
 
461
  rewritten_msg = rewrite_message(msg, persona_name, show_rationale)
462
  msgs = clean_history(st.session_state.history, persona_name)
463
  msgs = append_user_turn(msgs, rewritten_msg)
464
+ try:
465
+ txt, lat, tok, tps = chat(
466
+ msgs, "Arnold" if persona_name == "Control" else persona_name
467
+ )
468
+ st.session_state.history.append(
469
+ {"role": "assistant", "persona": persona_name, "content": txt, "is_new": True}
470
+ )
471
+ except ValueError as e:
472
+ st.error(f"❌ {str(e)}")
473
+ st.stop()
474
  st.rerun()
475
 
476
 
llm.py CHANGED
@@ -19,12 +19,37 @@ load_dotenv()
19
  MODEL_STRING = "gpt-4.1-mini" # we default on gpt-4.1-mini
20
  api_key = os.getenv("MODEL_API_KEY")
21
  client = openai.OpenAI(api_key=api_key)
22
- bedrock_runtime = boto3.client(
23
- "bedrock-runtime",
24
- region_name="us-east-1",
25
- aws_access_key_id= os.getenv("AWS_ACCESS_KEY_ID"),
26
- aws_secret_access_key= os.getenv("AWS_SECRET_ACCESS_KEY")
27
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  # ──────────────────────────────────────────────────────────────
30
  # Model switcher
@@ -89,23 +114,37 @@ def chat(messages, persona):
89
  {"role": m["role"], "content": m["content"]} for m in messages
90
  ]
91
 
92
- response = bedrock_runtime.invoke_model(
93
- modelId=MODEL_STRING,
94
- contentType="application/json",
95
- accept="application/json",
96
- body=json.dumps(
97
- {
98
- "anthropic_version": "bedrock-2023-05-31",
99
- "system": system_prompt,
100
- "messages": claude_messages,
101
- "max_tokens": 4000, # Much higher limit for longer responses
102
- "temperature": 0.3, # Lower temperature for more focused responses
103
- }
104
- ),
105
- )
 
 
106
 
107
- dt = time.time() - t0
108
- body = json.loads(response["body"].read())
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  text = "".join(
111
  part["text"] for part in body["content"] if part["type"] == "text"
@@ -136,22 +175,36 @@ def chat(messages, persona):
136
  }
137
  )
138
 
139
- response = bedrock_runtime.invoke_model(
140
- modelId=MODEL_STRING,
141
- contentType="application/json",
142
- accept="application/json",
143
- body=json.dumps(
144
- {
145
- "messages": ds_messages,
146
- "max_completion_tokens": 500,
147
- "temperature": 0.5,
148
- "top_p": 0.9,
149
- }
150
- ),
151
- )
 
 
152
 
153
- dt = time.time() - t0
154
- body = json.loads(response["body"].read())
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
  outputs = body.get("output", [])
157
  text_chunks = []
@@ -303,6 +356,7 @@ def check_credentials():
303
  if MODEL_TO_PROVIDER.get(MODEL_STRING) in bedrock_providers:
304
  # Test AWS Bedrock connection by trying to invoke a simple model
305
  try:
 
306
  # Try a simple test invocation to verify credentials
307
  test_response = bedrock_runtime.invoke_model(
308
  modelId="anthropic.claude-3-haiku-20240307-v1:0",
 
19
  MODEL_STRING = "gpt-4.1-mini" # we default on gpt-4.1-mini
20
  api_key = os.getenv("MODEL_API_KEY")
21
  client = openai.OpenAI(api_key=api_key)
22
+
23
+ # Lazy initialization of bedrock client to avoid errors if credentials are missing
24
+ _bedrock_runtime = None
25
+
26
+ def get_bedrock_client():
27
+ """Get or create the Bedrock runtime client with proper error handling."""
28
+ global _bedrock_runtime
29
+ if _bedrock_runtime is None:
30
+ aws_access_key = os.getenv("AWS_ACCESS_KEY_ID")
31
+ aws_secret_key = os.getenv("AWS_SECRET_ACCESS_KEY")
32
+
33
+ if not aws_access_key or not aws_secret_key:
34
+ raise ValueError(
35
+ "AWS credentials not found. Please set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY "
36
+ "as secrets in Hugging Face Spaces settings."
37
+ )
38
+
39
+ try:
40
+ _bedrock_runtime = boto3.client(
41
+ "bedrock-runtime",
42
+ region_name="us-east-1",
43
+ aws_access_key_id=aws_access_key,
44
+ aws_secret_access_key=aws_secret_key
45
+ )
46
+ except Exception as e:
47
+ raise ValueError(
48
+ f"Failed to initialize AWS Bedrock client: {str(e)}. "
49
+ "Please verify your AWS credentials are valid and have Bedrock access."
50
+ ) from e
51
+
52
+ return _bedrock_runtime
53
 
54
  # ──────────────────────────────────────────────────────────────
55
  # Model switcher
 
114
  {"role": m["role"], "content": m["content"]} for m in messages
115
  ]
116
 
117
+ try:
118
+ bedrock_runtime = get_bedrock_client()
119
+ response = bedrock_runtime.invoke_model(
120
+ modelId=MODEL_STRING,
121
+ contentType="application/json",
122
+ accept="application/json",
123
+ body=json.dumps(
124
+ {
125
+ "anthropic_version": "bedrock-2023-05-31",
126
+ "system": system_prompt,
127
+ "messages": claude_messages,
128
+ "max_tokens": 4000, # Much higher limit for longer responses
129
+ "temperature": 0.3, # Lower temperature for more focused responses
130
+ }
131
+ ),
132
+ )
133
 
134
+ dt = time.time() - t0
135
+ body = json.loads(response["body"].read())
136
+ except ValueError as e:
137
+ # Re-raise ValueError (credential errors) as-is
138
+ raise
139
+ except Exception as e:
140
+ error_msg = str(e)
141
+ if "UnrecognizedClientException" in error_msg or "invalid" in error_msg.lower():
142
+ raise ValueError(
143
+ f"AWS Bedrock authentication failed: {error_msg}. "
144
+ "Please verify your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY secrets "
145
+ "are correct and have Bedrock access permissions."
146
+ ) from e
147
+ raise
148
 
149
  text = "".join(
150
  part["text"] for part in body["content"] if part["type"] == "text"
 
175
  }
176
  )
177
 
178
+ try:
179
+ bedrock_runtime = get_bedrock_client()
180
+ response = bedrock_runtime.invoke_model(
181
+ modelId=MODEL_STRING,
182
+ contentType="application/json",
183
+ accept="application/json",
184
+ body=json.dumps(
185
+ {
186
+ "messages": ds_messages,
187
+ "max_completion_tokens": 500,
188
+ "temperature": 0.5,
189
+ "top_p": 0.9,
190
+ }
191
+ ),
192
+ )
193
 
194
+ dt = time.time() - t0
195
+ body = json.loads(response["body"].read())
196
+ except ValueError as e:
197
+ # Re-raise ValueError (credential errors) as-is
198
+ raise
199
+ except Exception as e:
200
+ error_msg = str(e)
201
+ if "UnrecognizedClientException" in error_msg or "invalid" in error_msg.lower():
202
+ raise ValueError(
203
+ f"AWS Bedrock authentication failed: {error_msg}. "
204
+ "Please verify your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY secrets "
205
+ "are correct and have Bedrock access permissions."
206
+ ) from e
207
+ raise
208
 
209
  outputs = body.get("output", [])
210
  text_chunks = []
 
356
  if MODEL_TO_PROVIDER.get(MODEL_STRING) in bedrock_providers:
357
  # Test AWS Bedrock connection by trying to invoke a simple model
358
  try:
359
+ bedrock_runtime = get_bedrock_client()
360
  # Try a simple test invocation to verify credentials
361
  test_response = bedrock_runtime.invoke_model(
362
  modelId="anthropic.claude-3-haiku-20240307-v1:0",