Repository navigation
Expand file tree
/
Copy pathagent_utils.py
More file actions
172 lines (130 loc) · 5.92 KB
/
Copy pathagent_utils.py
File metadata and controls
172 lines (130 loc) · 5.92 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
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
from datetime import datetime
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def get_multi_turn_system_message(memory: str, long_term_goal: str, start_time: str, time_limit: str) -> str:
return f"""# Agent System Context
You are an autonomous agent representing a user across multiple tools.
## Agent System Architecture
The agent operates using:
1. **Tasks** - Long-running activities with specific goals
2. **Triggers** - Events that initiate action cycles
3. **Action Cycles** - Short, self-contained sequences of actions (tool calls) responding to triggers
4. **Memory** - Information about your task, identity, background, and past interactions
5. **Tools** - Resources the agent can use across various applications
### Task Lifecycle
- Tasks are initialized with defined goals.
- Tasks concurrently monitor triggers:
- **Notification**: A new message or application update requiring attention
- **Timeout**: Inactivity prompting follow-up actions
- The trigger system processes notifications and timeouts.
- In each action cycle, you will:
* Think and plan relevant tool calls
* Execute these tool calls (e.g., collecting data, posting updates)
* End the action cycle and await the next trigger
- Tasks conclude when goals are explicitly marked complete
## Current Task
**Goal:** {long_term_goal}
## Memory
{memory}
"""
def get_multi_turn_system_message_with_thinking(memory: str, long_term_goal: str, start_time: str, time_limit: str) -> str:
return f"""# Agent System Context
You are an autonomous agent representing a user across multiple tools.
## Agent System Architecture
The agent operates using:
1. **Tasks** - Long-running activities with specific goals
2. **Triggers** - Events that initiate action cycles
3. **Action Cycles** - Short, self-contained sequences of actions (tool calls) responding to triggers
4. **Memory** - Information about your task, identity, background, and past interactions
5. **Tools** - Resources the agent can use across various applications
### Task Lifecycle
- Tasks are initialized with defined goals.
- Tasks concurrently monitor triggers:
- **Notification**: A new message or application update requiring attention
- **Timeout**: Inactivity prompting follow-up actions
- The trigger system processes notifications and timeouts.
- In each action cycle, you will:
* Call relevant tools (e.g., collecting data, posting updates)
* End the action cycle and await the next trigger
- Tasks conclude when goals are explicitly marked complete
## Current Task
**Goal:** {long_term_goal}
## Memory
{memory}
"""
def get_input_message(last_action_time: str, current_trigger_time: str, trigger_type: str, trigger_content: str, tool_names: str) -> str:
return f"""You have received a new trigger. A new action cycle has started.
You may perform a series of tool calls to address this trigger.
## Trigger Details
- **Trigger Type:** {trigger_type}
- **Trigger Content:** {trigger_content}
## Available Tools
{tool_names}
## Guidelines
1. Plan and evaluate tool calls with `think()` before and after execution.
2. Complete all necessary actions for the trigger before ending the action cycle.
3. End the action cycle with `end_action_cycle()` or mark the task done with `complete_task()`.
"""
def get_input_message_with_thinking(last_action_time: str, current_trigger_time: str, trigger_type: str, trigger_content: str, tool_names: str) -> str:
return f"""You have received a new trigger. A new action cycle has started.
You may perform a series of tool calls to address this trigger.
## Trigger Details
- **Trigger Type:** {trigger_type}
- **Trigger Content:** {trigger_content}
## Available Tools
{tool_names}
## Guidelines
1. Take all necessary actions for the trigger before ending the action cycle.
2. End the action cycle with `end_action_cycle()` or mark the task done with `complete_task()`.
"""
def think(thought: str, log_file: str = None) -> None:
r"""Use this tool to plan future tool calls or evaluate results.
This action doesn't change the environment or fetch new data; it simply logs your thought.
Args:
thought (str): The thought to document.
"""
if log_file:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
level = "INFO"
with open(log_file, "a") as f:
f.write(f"{timestamp} - {level} - Thinking..." + '\n')
f.write(f"{timestamp} - {level} - {thought}" + '\n')
else:
logger.info("Thinking...")
logger.info(thought)
def end_action_cycle(end_reason: str, log_file: str = None) -> None:
r"""Ends the current action cycle, specifying why it concluded.
Args:
end_reason (str): Explanation for ending the action cycle.
"""
if log_file:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
level = "INFO"
with open(log_file, "a") as f:
f.write(f"{timestamp} - {level} - Action Cycle Ended." + '\n')
f.write(f"{timestamp} - {level} - Reason: {end_reason}" + '\n')
else:
logger.info("Action Cycle Ended.")
logger.info(f"Reason: {end_reason}")
def complete_task(explanation: str, log_file: str = None) -> None:
r"""Marks the task as completed with a given explanation.
Args:
explanation (str): Explanation of task completion.
"""
if log_file:
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
level = "INFO"
with open(log_file, "a") as f:
f.write(f"{timestamp} - {level} - Task Completed." + '\n')
f.write(f"{timestamp} - {level} - Explanation: {explanation}" + '\n')
else:
logger.info("Task Completed.")
logger.info(f"Explanation: {explanation}")
def get_tool_names(schema: dict) -> str:
tool_names = []
for key, value in schema.items():
if "function" in value:
func_name = value["function"]["name"]
tool_names.append(f"{func_name}()")
return "\n".join(tool_names)