-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_executor_precompile.py
More file actions
46 lines (36 loc) · 1.56 KB
/
agent_executor_precompile.py
File metadata and controls
46 lines (36 loc) · 1.56 KB
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
import json
modelRegistry = {}
reasoningRegistry = {}
Engines = {}
AgentIterationResult = {}
IERCAgentTool = {}
abi = {}
def runNextIteration(modelId, basePrompt, tools, agentReasoning, prompt):
llm = modelRegistry.get(modelId)
reasoningEngine = reasoningRegistry.get(Engines.RE_ACT)
toolsMetadata = [{
'name': tool.name(),
'description': tool.description(),
'inputDescription': tool.inputDescription()
} for tool in tools]
toolsByName = {tool.name: tool for tool in toolsMetadata}
llmPrompt = reasoningEngine.buildPrompt(basePrompt, toolsMetadata, agentReasoning, prompt)
llmOutput = llm.run(llmPrompt)
nextStep = reasoningEngine.parse(llmOutput)
if nextStep.isDone():
return AgentIterationResult(isFinalAnswer=True, finalAnswer=nextStep.answer)
tool = toolsByName.get(nextStep.toolName)
toolInputByName = {input.name: input for input in tool.inputDescription.paramDescriptions}
params = json.parse(nextStep.params)
parsedParams = {}
for param in params.array():
paramDescription = toolInputByName[param.name]
parsedParams[param.name] = IERCAgentTool.ParamValue(
type=paramDescription.type,
value=abi.encode(param.value, paramDescription.type))
abiEncodedParams = abi.encode([params.value for params in parsedParams])
return AgentIterationResult(
isFinalAnswer=False,
tool=tool,
toolInput=IERCAgentTool.Input(params=parsedParams, abiEncodedParams=abiEncodedParams),
agentReasoning=nextStep.reasoning)