-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_agent_loop.py
More file actions
95 lines (75 loc) · 2.7 KB
/
Copy pathai_agent_loop.py
File metadata and controls
95 lines (75 loc) · 2.7 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
"""
Example: AI agent loop that observes the screen and takes actions via NanoKVM.
This shows the pattern for integrating NanoKVM with an LLM-based agent.
Replace the `ask_llm()` function with your actual LLM call.
"""
from __future__ import annotations
import time
from nanokvm import NanoKVM
def ask_llm(screenshot_base64: str, instruction: str) -> dict:
"""
Placeholder for your LLM call.
Send the screenshot + instruction to a multimodal LLM and parse its response.
Expected response format:
{"action": "type", "text": "hello"}
{"action": "key", "key": "Enter"}
{"action": "combo", "keys": ["ctrl", "s"]}
{"action": "click", "x": 0.5, "y": 0.5}
{"action": "scroll", "delta": -3}
{"action": "done"}
"""
# Replace this with an actual API call, e.g.:
# response = openai.chat.completions.create(
# model="gpt-4o",
# messages=[
# {"role": "user", "content": [
# {"type": "text", "text": instruction},
# {"type": "image_url", "image_url": {
# "url": f"data:image/jpeg;base64,{screenshot_base64}"
# }}
# ]}
# ]
# )
# return json.loads(response.choices[0].message.content)
return {"action": "done"}
def execute_action(kvm: NanoKVM, action: dict) -> None:
"""Execute a single action returned by the LLM."""
match action["action"]:
case "type":
kvm.type_text(action["text"])
case "key":
kvm.press_key(action["key"])
case "combo":
kvm.key_combo(action["keys"])
case "click":
kvm.mouse_click(
x=action.get("x"),
y=action.get("y"),
button=action.get("button", "left"),
)
case "scroll":
kvm.mouse_scroll(action["delta"])
case "move":
kvm.mouse_move(action["x"], action["y"])
case "done":
pass
case _:
print(f"Unknown action: {action}")
def main() -> None:
kvm = NanoKVM(serial_port="/dev/ttyACM0", video_device=0)
kvm.connect()
instruction = "Open the terminal and type 'ls -la', then press Enter"
max_steps = 20
for step in range(max_steps):
print(f"--- Step {step + 1} ---")
screenshot = kvm.capture_frame_base64(quality=80)
action = ask_llm(screenshot, instruction)
print(f"Action: {action}")
if action["action"] == "done":
print("Agent reports task complete.")
break
execute_action(kvm, action)
time.sleep(0.5) # wait for the action to take effect on screen
kvm.disconnect()
if __name__ == "__main__":
main()