prasadnu commited on
Commit
a6266bd
·
1 Parent(s): f860fcc

ubi integration for agent

Browse files
Files changed (1) hide show
  1. RAG/bedrock_agent.py +32 -3
RAG/bedrock_agent.py CHANGED
@@ -20,6 +20,22 @@ from datetime import datetime, timezone
20
  import botocore
21
  import utilities.ubi_lambda as ubi
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  if "inputs_" not in st.session_state:
24
  st.session_state.inputs_ = {}
25
 
@@ -117,6 +133,8 @@ def query_(inputs):
117
  trace_id = str(uuid.uuid4()).replace("-", "")
118
  try:
119
  for i,event in enumerate(event_stream):
 
 
120
  if 'trace' in event:
121
 
122
  if('orchestrationTrace' not in event['trace']['trace']):
@@ -127,9 +145,14 @@ def query_(inputs):
127
  total_context_item['tool'] = orchestration_trace['modelInvocationOutput']['rawResponse']
128
  if('rationale' in orchestration_trace):
129
  total_context_item['rationale'] = orchestration_trace['rationale']['text']
 
 
130
  if('invocationInput' in orchestration_trace):
131
  total_context_item['invocationInput'] = orchestration_trace['invocationInput']['actionGroupInvocationInput']
132
  last_tool_name = total_context_item['invocationInput']['function']
 
 
 
133
  if('observation' in orchestration_trace):
134
  total_context_item['observation'] = event['trace']['trace']['orchestrationTrace']['observation']
135
  tool_output_last_obs = event['trace']['trace']['orchestrationTrace']['observation']
@@ -137,15 +160,21 @@ def query_(inputs):
137
  last_tool = tool_output_last_obs['actionGroupInvocationOutput']['text']
138
  if(tool_output_last_obs['type'] == 'FINISH'):
139
  agent_answer = tool_output_last_obs['finalResponse']['text']
 
 
140
  if('modelInvocationOutput' in orchestration_trace and '<thinking>' in orchestration_trace['modelInvocationOutput']['rawResponse']['content']):
141
  total_context_item['thinking'] = orchestration_trace['modelInvocationOutput']['rawResponse']
 
 
142
  if(total_context_item!={}):
143
  total_context.append(total_context_item)
144
 
145
 
146
- # 🔁 Generate + send OpenTelemetry span for each block
147
- span = convert_to_span(total_context_item, trace_id, i)
148
- send_otel_span(span)
 
 
149
 
150
  except botocore.exceptions.EventStreamError as error:
151
  raise error
 
20
  import botocore
21
  import utilities.ubi_lambda as ubi
22
 
23
+ # OTEL imports
24
+ from opentelemetry import trace
25
+ from opentelemetry.sdk.resources import Resource
26
+ from opentelemetry.sdk.trace import TracerProvider
27
+ from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
28
+ from opentelemetry.sdk.trace.export import BatchSpanProcessor
29
+
30
+ # --- OTEL Setup ---
31
+ resource = Resource(attributes={"service.name": "bedrock-agent"})
32
+ trace.set_tracer_provider(TracerProvider(resource=resource))
33
+ tracer = trace.get_tracer_provider().get_tracer("app.tracer")
34
+
35
+ otlp_exporter = OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces")
36
+ span_processor = BatchSpanProcessor(otlp_exporter)
37
+ trace.get_tracer_provider().add_span_processor(span_processor)
38
+
39
  if "inputs_" not in st.session_state:
40
  st.session_state.inputs_ = {}
41
 
 
133
  trace_id = str(uuid.uuid4()).replace("-", "")
134
  try:
135
  for i,event in enumerate(event_stream):
136
+ name = "step"
137
+ attributes = {}
138
  if 'trace' in event:
139
 
140
  if('orchestrationTrace' not in event['trace']['trace']):
 
145
  total_context_item['tool'] = orchestration_trace['modelInvocationOutput']['rawResponse']
146
  if('rationale' in orchestration_trace):
147
  total_context_item['rationale'] = orchestration_trace['rationale']['text']
148
+ name = "rationale"
149
+ attributes["message"] = total_context_item["rationale"]
150
  if('invocationInput' in orchestration_trace):
151
  total_context_item['invocationInput'] = orchestration_trace['invocationInput']['actionGroupInvocationInput']
152
  last_tool_name = total_context_item['invocationInput']['function']
153
+ name = total_context_item["invocationInput"].get("function", "invocation")
154
+ attributes = {p["name"]: p["value"] for p in total_context_item["invocationInput"].get("parameters", [])}
155
+
156
  if('observation' in orchestration_trace):
157
  total_context_item['observation'] = event['trace']['trace']['orchestrationTrace']['observation']
158
  tool_output_last_obs = event['trace']['trace']['orchestrationTrace']['observation']
 
160
  last_tool = tool_output_last_obs['actionGroupInvocationOutput']['text']
161
  if(tool_output_last_obs['type'] == 'FINISH'):
162
  agent_answer = tool_output_last_obs['finalResponse']['text']
163
+ name = total_context_item["observation"].get("type", "observation").lower()
164
+ attributes = total_context_item["observation"].get("actionGroupInvocationOutput", {})
165
  if('modelInvocationOutput' in orchestration_trace and '<thinking>' in orchestration_trace['modelInvocationOutput']['rawResponse']['content']):
166
  total_context_item['thinking'] = orchestration_trace['modelInvocationOutput']['rawResponse']
167
+ name = "thinking"
168
+ attributes["message"] = total_context_item["thinking"].get("content", "")
169
  if(total_context_item!={}):
170
  total_context.append(total_context_item)
171
 
172
 
173
+ # # 🔁 Generate + send OpenTelemetry span for each block
174
+ # span = convert_to_span(total_context_item, trace_id, i)
175
+ # send_otel_span(span)
176
+ with tracer.start_as_current_span(name, attributes=attributes) as span:
177
+ span.set_attribute("trace.source", "agentic-app")
178
 
179
  except botocore.exceptions.EventStreamError as error:
180
  raise error