forked from awslabs/amazon-bedrock-agent-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
252 lines (208 loc) · 9.2 KB
/
utility.py
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
import json
import boto3
from termcolor import colored
from rich.console import Console
from rich.markdown import Markdown
import boto3
import copy
import uuid
session = boto3.session.Session()
region = session.region_name
# Runtime Endpoints
bedrock_agents_runtime = boto3.client("bedrock-agent-runtime", region_name=region)
def invoke_agent_with_roc(
actionGroups, agent_instruction, model_id, inputText, tool_list
):
# start a new session
sessionId = str(uuid.uuid4())
agent_answer = str()
# prepare request parameters before invoking inline agent
request_params = {
"actionGroups": actionGroups,
"instruction": agent_instruction,
"foundationModel": model_id,
"sessionId": sessionId,
"endSession": False,
"enableTrace": True,
}
request_params["inputText"] = inputText
agent_answer, roc = invoke_inline_agent_helper(request_params)
while not agent_answer:
# prepare request parameters before invoking inline agent
request_params = {
"actionGroups": actionGroups,
"instruction": agent_instruction,
"foundationModel": model_id,
"sessionId": sessionId,
"endSession": False,
"enableTrace": True,
"inlineSessionState": process_roc(
roc, tool_list
), # process return of control
}
request_params["inputText"] = ""
agent_answer, roc = invoke_inline_agent_helper(request_params)
return agent_answer
def process_roc(roc, tool_list):
inlineSessionState = {"returnControlInvocationResults": []}
inlineSessionState["invocationId"] = roc["invocationId"]
for invocationInput in roc["invocationInputs"]:
functionInvocationInput = copy.deepcopy(
invocationInput["functionInvocationInput"]
)
current_tool = tool_list[functionInvocationInput["function"]]
parameters = dict()
for param in functionInvocationInput["parameters"]:
parameters[param["name"]] = param["value"]
if current_tool.get_name() == "read_file":
parameters = {"file_path": "document.txt"}
output = json.dumps(current_tool.invoke(parameters))
if "actionInvocationType" in functionInvocationInput:
del functionInvocationInput["actionInvocationType"]
if "collaboratorName" in functionInvocationInput:
del functionInvocationInput["collaboratorName"]
if "parameters" in functionInvocationInput:
del functionInvocationInput["parameters"]
functionInvocationInput["confirmationState"] = "CONFIRM"
functionInvocationInput["responseState"] = "REPROMPT"
functionInvocationInput["responseBody"] = dict()
functionInvocationInput["responseBody"]["response"] = dict()
functionInvocationInput["responseBody"]["response"]["body"] = output
function_result = {"functionResult": functionInvocationInput}
inlineSessionState["returnControlInvocationResults"].append(function_result)
return inlineSessionState
def create_parameters(tool):
parameters = dict()
for key, value in tool.args.items():
parameters[key] = dict()
parameters[key]["description"] = value["description"]
parameters[key]["type"] = value["type"]
parameters[key]["required"] = True
return parameters
def invoke_inline_agent_helper(request_params):
response = bedrock_agents_runtime.invoke_inline_agent(**request_params)
agent_answer = ""
roc = None
event_stream = response["completion"]
try:
for event in event_stream:
# Get Final Answer
if "chunk" in event:
data = event["chunk"]["bytes"]
agent_answer = data.decode("utf8")
end_event_received = True
# Process trace
if "trace" in event and request_params["enableTrace"]:
process_trace(event, "core")
if "returnControl" in event:
roc = event["returnControl"]
return agent_answer, roc
return agent_answer, roc
except Exception as e:
print(f"Caught exception while processing input to invokeAgent:\n")
input_text = request_params["inputText"]
print(f" for input text:\n{input_text}\n")
print(
f" request ID: {response['ResponseMetadata']['RequestId']}, retries: {response['ResponseMetadata']['RetryAttempts']}\n"
)
print(f"Error: {e}")
raise Exception("Unexpected exception: ", e)
def process_trace(_event, trace_level):
if "failureTrace" in _event["trace"]["trace"]:
print(
colored(
f"Agent error: {_event['trace']['trace']['failureTrace']['failureReason']}",
"red",
)
)
if "orchestrationTrace" in _event["trace"]["trace"]:
_orch = _event["trace"]["trace"]["orchestrationTrace"]
if trace_level in ["core", "outline"]:
if "rationale" in _orch:
_rationale = _orch["rationale"]
print(colored(f"{_rationale['text']}", "blue"))
if "invocationInput" in _orch:
# NOTE: when agent determines invocations should happen in parallel
# the trace objects for invocation input still come back one at a time.
_input = _orch["invocationInput"]
print(_input)
if "actionGroupInvocationInput" in _input:
if "function" in _input["actionGroupInvocationInput"]:
tool = _input["actionGroupInvocationInput"]["function"]
elif "apiPath" in _input["actionGroupInvocationInput"]:
tool = _input["actionGroupInvocationInput"]["apiPath"]
else:
tool = "undefined"
if trace_level == "outline":
print(
colored(
f"Using tool: {tool}",
"magenta",
)
)
else:
print(
colored(
f"Using tool: {tool} with these inputs:",
"magenta",
)
)
if (
len(_input["actionGroupInvocationInput"]["parameters"]) == 1
) and (
_input["actionGroupInvocationInput"]["parameters"][0][
"name"
]
== "input_text"
):
print(
colored(
f"{_input['actionGroupInvocationInput']['parameters'][0]['value']}",
"magenta",
)
)
else:
print(
colored(
f"{_input['actionGroupInvocationInput']['parameters']}\n",
"magenta",
)
)
elif "codeInterpreterInvocationInput" in _input:
if trace_level == "outline":
print(colored(f"Using code interpreter", "magenta"))
else:
console = Console()
_gen_code = _input["codeInterpreterInvocationInput"]["code"]
_code = f"```python\n{_gen_code}\n```"
console.print(Markdown(f"**Generated code**\n{_code}"))
if "observation" in _orch:
if trace_level == "core":
_output = _orch["observation"]
if "actionGroupInvocationOutput" in _output:
print(
colored(
f"--tool outputs:\n{_output['actionGroupInvocationOutput']['text']}...\n",
"magenta",
)
)
if "agentCollaboratorInvocationOutput" in _output:
_collab_name = _output["agentCollaboratorInvocationOutput"][
"agentCollaboratorName"
]
_collab_output_text = _output[
"agentCollaboratorInvocationOutput"
]["output"]["text"]
print(
colored(
f"\n----sub-agent {_collab_name} output text:\n{_collab_output_text}...\n",
"magenta",
)
)
if "finalResponse" in _output:
print(
colored(
f"Final response:\n{_output['finalResponse']['text']}...",
"cyan",
)
)