-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
91 lines (74 loc) · 3.32 KB
/
agent.py
File metadata and controls
91 lines (74 loc) · 3.32 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
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
import json
from typing import Callable, List
from actions import ActionRegistry
from goal import Goal, AgentLanguage, Environment, Memory
from tool_data import Prompt
class Agent:
def __init__(self,
goals: List[Goal],
agent_language: AgentLanguage,
action_registry: ActionRegistry,
generate_response: Callable[[Prompt], str],
environment: Environment):
"""
Initialize an agent with its core GAME components
"""
self.goals = goals
self.generate_response = generate_response
self.agent_language = agent_language
self.actions = action_registry
self.environment = environment
def construct_prompt(self, goals: List[Goal], memory: Memory, actions: ActionRegistry) -> Prompt:
"""Build prompt with memory context"""
return self.agent_language.construct_prompt(
actions=actions.get_actions(),
environment=self.environment,
goals=goals,
memory=memory
)
def get_action(self, response):
invocation = self.agent_language.parse_response(response)
action = self.actions.get_action(invocation["tool"])
return action, invocation
def should_terminate(self, response: str) -> bool:
action_def, _ = self.get_action(response)
return action_def.terminal
def set_current_task(self, memory: Memory, task: str):
memory.add_memory({"type": "user", "content": task})
def update_memory(self, memory: Memory, response: str, result: dict):
"""
Update memory with the agent's decision and the environment's response.
"""
new_memories = [
{"type": "assistant", "content": response},
{"type": "environment", "content": json.dumps(result)}
]
for m in new_memories:
memory.add_memory(m)
def prompt_llm_for_action(self, full_prompt: Prompt) -> str:
response = self.generate_response(full_prompt)
return response
def run(self, user_input: str, memory=None, max_iterations: int = 50) -> Memory:
"""
Execute the GAME loop for this agent with a maximum iteration limit.
"""
memory = memory or Memory()
self.set_current_task(memory, user_input)
for i in range(max_iterations):
# Construct a prompt that includes the Goals, Actions, and the current Memory
prompt = self.construct_prompt(self.goals, memory, self.actions)
print("Agent thinking... interaction:", i, "Interaction :", prompt.messages)
# Generate a response from the agent
response = self.prompt_llm_for_action(prompt)
print(f"Agent Decision: {response}")
# Determine which action the agent wants to execute
action, invocation = self.get_action(response)
# Execute the action in the environment
result = self.environment.execute_action(action, invocation["args"])
print(f"Action Result: {result}")
# Update the agent's memory with information about what happened
self.update_memory(memory, response, result)
# Check if the agent has decided to terminate
if self.should_terminate(response):
break
return memory