forked from deedy/mac_computer_use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.py
426 lines (367 loc) · 12 KB
/
loop.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""
Agentic sampling loop that calls the Anthropic API and local implementation of anthropic-defined computer use tools.
"""
import platform
from collections.abc import Callable
from datetime import datetime
from enum import StrEnum
from typing import Any, cast
from anthropic import Anthropic, AnthropicBedrock, AnthropicVertex, APIResponse
from anthropic.types import (
ToolResultBlockParam,
)
from anthropic.types.beta import (
BetaContentBlock,
BetaContentBlockParam,
BetaImageBlockParam,
BetaMessage,
BetaMessageParam,
BetaTextBlockParam,
BetaToolResultBlockParam,
)
from tools import BashTool, ComputerTool, EditTool, ToolCollection, ToolResult
BETA_FLAG = "computer-use-2024-10-22"
class APIProvider(StrEnum):
ANTHROPIC = "anthropic"
BEDROCK = "bedrock"
VERTEX = "vertex"
BRICKS = "bricks"
PROVIDER_TO_DEFAULT_MODEL_NAME: dict[APIProvider, str] = {
APIProvider.ANTHROPIC: "claude-3-5-sonnet-20241022",
APIProvider.BEDROCK: "anthropic.claude-3-5-sonnet-20241022-v2:0",
APIProvider.VERTEX: "claude-3-5-sonnet-v2@20241022",
APIProvider.BRICKS: "claude-3-5-sonnet-20241022",
}
# This system prompt is optimized for the Docker environment in this repository and
# specific tool combinations enabled.
# We encourage modifying this system prompt to ensure the model has context for the
# environment it is running in, and to provide any additional information that may be
# helpful for the task at hand.
SYSTEM_PROMPT = f"""<SYSTEM_DEFINITION>
You are an advanced AI assistant operating within a macOS Sequoia Version 15.1 (24B82) environment with comprehensive access to system resources and applications. Your purpose is to provide precise, efficient assistance while leveraging available tools optimally.
<SYSTEM_SPECIFICATIONS>
1. Hardware Configuration:
- Model: MacBook Pro (15-inch, 2018)
- Processor: 2.6 GHz 6-Core Intel Core i7
- Memory: 16 GB 2400 MHz DDR4
- Graphics: Intel UHD Graphics 630 1536 MB
- Display: 15.4-inch Retina (2880 × 1800)
- Architecture: {platform.machine()}
- Internet: Active connection available
- Time Zone: System configured
- Current Date: {datetime.today().strftime('%A, %B %-d, %Y')}
<APPLICATION_ECOSYSTEM>
1. Development Environment:
A. Code Editors & IDEs:
- Visual Studio Code & VS Code Insiders
- Xcode Beta
- Sublime Text
- Adobe Dreamweaver 2021
B. Version Control & Collaboration:
- GitHub Desktop
- Git (command line)
- CodeForces Web Tool
C. Container & Virtual Environments:
- Docker.app
- Docker CLI tools
D. Development Tools:
- Terminal
- Command Line Tools
- Developer.app
2. Professional Suites:
A. Microsoft Office:
- Word
- Excel
- PowerPoint
- OneNote
- Outlook
B. Adobe Creative Cloud:
- Creative Cloud Manager
- Dreamweaver 2021
- Premiere Pro (Beta)
- Adobe UXP Developer Tools
3. Web Browsers & Tools:
A. Primary Browsers:
- Safari & Safari Technology Preview
- Google Chrome Beta
- Firefox
- Microsoft Edge Dev
- Chromium
B. Specialized Browsers:
- Tor Browser (Standard & Alpha)
C. Browser Extensions:
- Grammarly for Safari
- Microsoft Bi for Safari
4. AI & Machine Learning Tools:
- NVIDIA AI Workbench
- Code AI
- AI on Device (MacOS)
- 16x Prompt.app
5. System Utilities:
A. File Management:
- Finder
- Preview
- The Unarchiver
- Unzip - RAR
B. System Tools:
- System Settings
- Automator
- Mission Control
- Time Machine
- Activity Monitor
C. Text Processing:
- TextEdit
- Notes
D. Security:
- Passwords.app
- G Authenticator
- BitPay
- Wasabi Wallet
6. Communication & Collaboration:
- Messages
- Mail
- FaceTime
- Discord
- Zoom
- Messenger
- TextNow
7. Media & Entertainment:
- QuickTime Player
- Photos
- Music
- TV
- Podcasts
- Photo Booth
8. Productivity & Organization:
- Calendar
- Reminders
- Stickies
- Clock
- Calculator
- Weather
- Maps
<OPERATIONAL_CAPABILITIES>
1. File System Access:
- Read/Write operations in user directories
- Application data access
- Temporary file creation
- Archive handling
2. Network Operations:
- HTTP/HTTPS requests
- API interactions
- Download capabilities
- Network diagnostics
3. Automation Framework:
A. System Automation:
- Shortcuts.app
- Automator workflows
- AppleScript execution
- Shell scripting
B. Development Automation:
- Build tools
- Package managers
- Deployment scripts
4. Security Protocols:
- Secure file operations
- Credential management
- Encryption capabilities
- Privacy controls
<PERFORMANCE_GUIDELINES>
1. Resource Management:
- Monitor system resources
- Optimize heavy operations
- Cache management
- Background process awareness
2. Error Handling:
- Graceful failure recovery
- User feedback
- Logging capabilities
- Debug information
3. Operation Chaining:
- Minimize command calls
- Batch operations
- Efficient workflows
- Resource pooling
<INTERACTION_PROTOCOL>
For each user interaction, I will:
1. Analyze request requirements
2. Identify optimal tools/applications
3. Validate resource availability
4. Plan execution strategy
5. Provide clear documentation
6. Monitor execution
7. Handle errors gracefully
8. Confirm successful completion
<RESPONSE_FORMAT>
Each response will include:
1. <thinking> tags for analysis
2. Task acknowledgment
3. Resource identification
4. Step-by-step execution plan
5. Clear documentation
6. Error handling procedures
7. Success confirmation
<LIMITATIONS_AWARENESS>
- Respect system permissions
- Handle resource constraints
- Consider operation timing
- Maintain security protocols
- Preserve user privacy
- Account for network latency"""
async def sampling_loop(
*,
model: str,
provider: APIProvider,
system_prompt_suffix: str,
messages: list[BetaMessageParam],
output_callback: Callable[[BetaContentBlock], None],
tool_output_callback: Callable[[ToolResult, str], None],
api_response_callback: Callable[[APIResponse[BetaMessage]], None],
api_key: str,
only_n_most_recent_images: int | None = None,
max_tokens: int = 8192,
):
"""
Agentic sampling loop for the assistant/tool interaction of computer use.
"""
tool_collection = ToolCollection(
ComputerTool(),
BashTool(),
EditTool(),
)
system = (
f"{SYSTEM_PROMPT}{' ' + system_prompt_suffix if system_prompt_suffix else ''}"
)
while True:
if only_n_most_recent_images:
_maybe_filter_to_n_most_recent_images(messages, only_n_most_recent_images)
if provider == APIProvider.ANTHROPIC:
client = Anthropic(api_key=api_key)
elif provider == APIProvider.VERTEX:
client = AnthropicVertex()
elif provider == APIProvider.BEDROCK:
client = AnthropicBedrock()
elif provider == APIProvider.BRICKS:
client = Anthropic(
api_key=api_key,
base_url="https://api.trybricks.ai/api/providers/anthropic",
)
else:
raise ValueError(f"Unsupported provider: {provider}")
# Call the API
# we use raw_response to provide debug information to streamlit. Your
# implementation may be able call the SDK directly with:
# `response = client.messages.create(...)` instead.
raw_response = client.beta.messages.with_raw_response.create(
max_tokens=max_tokens,
messages=messages,
model=model,
system=system,
tools=tool_collection.to_params(),
betas=[BETA_FLAG],
)
api_response_callback(cast(APIResponse[BetaMessage], raw_response))
response = raw_response.parse()
messages.append(
{
"role": "assistant",
"content": cast(list[BetaContentBlockParam], response.content),
}
)
tool_result_content: list[BetaToolResultBlockParam] = []
for content_block in cast(list[BetaContentBlock], response.content):
print("CONTENT", content_block)
output_callback(content_block)
if content_block.type == "tool_use":
result = await tool_collection.run(
name=content_block.name,
tool_input=cast(dict[str, Any], content_block.input),
)
tool_result_content.append(
_make_api_tool_result(result, content_block.id)
)
tool_output_callback(result, content_block.id)
if not tool_result_content:
return messages
messages.append({"content": tool_result_content, "role": "user"})
def _maybe_filter_to_n_most_recent_images(
messages: list[BetaMessageParam],
images_to_keep: int,
min_removal_threshold: int = 10,
):
"""
With the assumption that images are screenshots that are of diminishing value as
the conversation progresses, remove all but the final `images_to_keep` tool_result
images in place, with a chunk of min_removal_threshold to reduce the amount we
break the implicit prompt cache.
"""
if images_to_keep is None:
return messages
tool_result_blocks = cast(
list[ToolResultBlockParam],
[
item
for message in messages
for item in (
message["content"] if isinstance(message["content"], list) else []
)
if isinstance(item, dict) and item.get("type") == "tool_result"
],
)
total_images = sum(
1
for tool_result in tool_result_blocks
for content in tool_result.get("content", [])
if isinstance(content, dict) and content.get("type") == "image"
)
images_to_remove = total_images - images_to_keep
# for better cache behavior, we want to remove in chunks
images_to_remove -= images_to_remove % min_removal_threshold
for tool_result in tool_result_blocks:
if isinstance(tool_result.get("content"), list):
new_content = []
for content in tool_result.get("content", []):
if isinstance(content, dict) and content.get("type") == "image":
if images_to_remove > 0:
images_to_remove -= 1
continue
new_content.append(content)
tool_result["content"] = new_content
def _make_api_tool_result(
result: ToolResult, tool_use_id: str
) -> BetaToolResultBlockParam:
"""Convert an agent ToolResult to an API ToolResultBlockParam."""
tool_result_content: list[BetaTextBlockParam | BetaImageBlockParam] | str = []
is_error = False
if result.error:
is_error = True
tool_result_content = _maybe_prepend_system_tool_result(result, result.error)
else:
if result.output:
tool_result_content.append(
{
"type": "text",
"text": _maybe_prepend_system_tool_result(result, result.output),
}
)
if result.base64_image:
tool_result_content.append(
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": result.base64_image,
},
}
)
return {
"type": "tool_result",
"content": tool_result_content,
"tool_use_id": tool_use_id,
"is_error": is_error,
}
def _maybe_prepend_system_tool_result(result: ToolResult, result_text: str):
if result.system:
result_text = f"<system>{result.system}</system>\n{result_text}"
return result_text