Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
ubi integration for agent
Browse files- RAG/bedrock_agent.py +73 -8
RAG/bedrock_agent.py
CHANGED
|
@@ -16,6 +16,9 @@ import utilities.invoke_models as invoke_models
|
|
| 16 |
import streamlit as st
|
| 17 |
import time as t
|
| 18 |
import botocore.exceptions
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
if "inputs_" not in st.session_state:
|
| 21 |
st.session_state.inputs_ = {}
|
|
@@ -36,6 +39,68 @@ bedrock_agent_runtime_client = boto3.client(
|
|
| 36 |
enable_trace:bool = True
|
| 37 |
end_session:bool = False
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def delete_memory():
|
| 40 |
response = bedrock_agent_runtime_client.delete_agent_memory(
|
| 41 |
agentAliasId='DEEEEZM2TM',
|
|
@@ -61,13 +126,11 @@ def query_(inputs):
|
|
| 61 |
last_tool = ""
|
| 62 |
last_tool_name = ""
|
| 63 |
agent_answer = ""
|
|
|
|
| 64 |
try:
|
| 65 |
for event in event_stream:
|
| 66 |
-
#print("***event*********")
|
| 67 |
-
#print(event)
|
| 68 |
if 'trace' in event:
|
| 69 |
-
|
| 70 |
-
#print(event['trace'])
|
| 71 |
if('orchestrationTrace' not in event['trace']['trace']):
|
| 72 |
continue
|
| 73 |
orchestration_trace = event['trace']['trace']['orchestrationTrace']
|
|
@@ -80,10 +143,8 @@ def query_(inputs):
|
|
| 80 |
total_context_item['invocationInput'] = orchestration_trace['invocationInput']['actionGroupInvocationInput']
|
| 81 |
last_tool_name = total_context_item['invocationInput']['function']
|
| 82 |
if('observation' in orchestration_trace):
|
| 83 |
-
#print("trace****observation******")
|
| 84 |
total_context_item['observation'] = event['trace']['trace']['orchestrationTrace']['observation']
|
| 85 |
tool_output_last_obs = event['trace']['trace']['orchestrationTrace']['observation']
|
| 86 |
-
#print(tool_output_last_obs)
|
| 87 |
if(tool_output_last_obs['type'] == 'ACTION_GROUP'):
|
| 88 |
last_tool = tool_output_last_obs['actionGroupInvocationOutput']['text']
|
| 89 |
if(tool_output_last_obs['type'] == 'FINISH'):
|
|
@@ -92,8 +153,12 @@ def query_(inputs):
|
|
| 92 |
total_context_item['thinking'] = orchestration_trace['modelInvocationOutput']['rawResponse']
|
| 93 |
if(total_context_item!={}):
|
| 94 |
total_context.append(total_context_item)
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
except botocore.exceptions.EventStreamError as error:
|
| 98 |
raise error
|
| 99 |
|
|
|
|
| 16 |
import streamlit as st
|
| 17 |
import time as t
|
| 18 |
import botocore.exceptions
|
| 19 |
+
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_ = {}
|
|
|
|
| 39 |
enable_trace:bool = True
|
| 40 |
end_session:bool = False
|
| 41 |
|
| 42 |
+
def now_rfc3339():
|
| 43 |
+
return datetime.now(timezone.utc).isoformat()
|
| 44 |
+
|
| 45 |
+
def send_otel_span(span):
|
| 46 |
+
try:
|
| 47 |
+
|
| 48 |
+
query_payload = {
|
| 49 |
+
"client_id": session_id,
|
| 50 |
+
"query_id": st.session_state["query_id"],
|
| 51 |
+
"application": "Semantic Search",
|
| 52 |
+
"query_response_hit_ids": doc_ids,
|
| 53 |
+
"timestamp": datetime.utcnow().isoformat() + "Z",
|
| 54 |
+
"user_query": json.dumps(hybrid_payload),
|
| 55 |
+
"query": query,
|
| 56 |
+
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
status = ubi.send_to_lambda(".otel-v1-apm-span-default", span)
|
| 60 |
+
|
| 61 |
+
if status == 202:
|
| 62 |
+
print("Traces sent to Lambda")
|
| 63 |
+
else:
|
| 64 |
+
print("Lambda did not accept the request")
|
| 65 |
+
|
| 66 |
+
res = requests.post(OPENSEARCH_ENDPOINT, json=span, auth=OPENSEARCH_AUTH, timeout=3)
|
| 67 |
+
print(f"[OTEL SPAN] {span['name']} -> {res.status_code}")
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"[OTEL ERROR] {e}")
|
| 70 |
+
|
| 71 |
+
def convert_to_span(block, trace_id, index):
|
| 72 |
+
span_id = str(uuid.uuid4()).replace("-", "")[:16]
|
| 73 |
+
name = "step"
|
| 74 |
+
attributes = {}
|
| 75 |
+
|
| 76 |
+
if "invocationInput" in block:
|
| 77 |
+
name = block["invocationInput"].get("function", "invocation")
|
| 78 |
+
attributes = {p["name"]: p["value"] for p in block["invocationInput"].get("parameters", [])}
|
| 79 |
+
elif "observation" in block:
|
| 80 |
+
name = block["observation"].get("type", "observation").lower()
|
| 81 |
+
attributes = block["observation"].get("actionGroupInvocationOutput", {})
|
| 82 |
+
elif "thinking" in block:
|
| 83 |
+
name = "thinking"
|
| 84 |
+
attributes["message"] = block["thinking"].get("content", "")
|
| 85 |
+
elif "rationale" in block:
|
| 86 |
+
name = "rationale"
|
| 87 |
+
attributes["message"] = block["rationale"]
|
| 88 |
+
|
| 89 |
+
return {
|
| 90 |
+
"traceId": trace_id,
|
| 91 |
+
"spanId": span_id,
|
| 92 |
+
"name": name,
|
| 93 |
+
"startTime": now_rfc3339(),
|
| 94 |
+
"endTime": now_rfc3339(),
|
| 95 |
+
"durationInNanos": 10000000 * (index + 1),
|
| 96 |
+
"kind": "INTERNAL",
|
| 97 |
+
"status": {"code": "OK"},
|
| 98 |
+
"attributes": attributes,
|
| 99 |
+
"resource": {
|
| 100 |
+
"service.name": "bedrock-agent"
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
def delete_memory():
|
| 105 |
response = bedrock_agent_runtime_client.delete_agent_memory(
|
| 106 |
agentAliasId='DEEEEZM2TM',
|
|
|
|
| 126 |
last_tool = ""
|
| 127 |
last_tool_name = ""
|
| 128 |
agent_answer = ""
|
| 129 |
+
trace_id = str(uuid.uuid4()).replace("-", "")
|
| 130 |
try:
|
| 131 |
for event in event_stream:
|
|
|
|
|
|
|
| 132 |
if 'trace' in event:
|
| 133 |
+
|
|
|
|
| 134 |
if('orchestrationTrace' not in event['trace']['trace']):
|
| 135 |
continue
|
| 136 |
orchestration_trace = event['trace']['trace']['orchestrationTrace']
|
|
|
|
| 143 |
total_context_item['invocationInput'] = orchestration_trace['invocationInput']['actionGroupInvocationInput']
|
| 144 |
last_tool_name = total_context_item['invocationInput']['function']
|
| 145 |
if('observation' in orchestration_trace):
|
|
|
|
| 146 |
total_context_item['observation'] = event['trace']['trace']['orchestrationTrace']['observation']
|
| 147 |
tool_output_last_obs = event['trace']['trace']['orchestrationTrace']['observation']
|
|
|
|
| 148 |
if(tool_output_last_obs['type'] == 'ACTION_GROUP'):
|
| 149 |
last_tool = tool_output_last_obs['actionGroupInvocationOutput']['text']
|
| 150 |
if(tool_output_last_obs['type'] == 'FINISH'):
|
|
|
|
| 153 |
total_context_item['thinking'] = orchestration_trace['modelInvocationOutput']['rawResponse']
|
| 154 |
if(total_context_item!={}):
|
| 155 |
total_context.append(total_context_item)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# 🔁 Generate + send OpenTelemetry span for each block
|
| 159 |
+
span = convert_to_span(total_context_item, trace_id, i)
|
| 160 |
+
send_otel_span(span)
|
| 161 |
+
|
| 162 |
except botocore.exceptions.EventStreamError as error:
|
| 163 |
raise error
|
| 164 |
|