-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_events.py
114 lines (90 loc) · 2.92 KB
/
system_events.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
"""
This example shows how to use Ollama and macOS System Events together.
Install `ollama` here: https://ollama.com
Download `uv` to run this example: https://github.com/astral-sh/uv
```
ollama pull llama3.2
uv run examples/system_events.py
```
"""
# /// script
# dependencies = [
# "ollama",
# "hype @ git+https://github.com/mattt/hype.git",
# ]
# ///
import datetime
import os
import subprocess
from textwrap import dedent
import ollama
import hype
def _run_apple_script(
script: str, capture_output: bool = False
) -> subprocess.CompletedProcess:
"""
Private helper method to run AppleScript using launchctl.
:param script: The AppleScript to run.
:param capture_output: Whether to capture the output of the script.
:return: A CompletedProcess instance.
"""
uid = os.getuid()
cmd = ["launchctl", "asuser", str(uid), "/usr/bin/osascript", "-e", script]
return subprocess.run(cmd, capture_output=capture_output, text=True, check=True) # noqa: S603
# FIXME: Either Ollama doesn't seem to support multiple tool calls,
# or llama3.2 can't figure out that it should use `get_current_time`
# to get the current time.
@hype.up
def get_current_time() -> float:
"""
Get the current time as a POSIX timestamp.
"""
return datetime.datetime.now().timestamp()
@hype.up
def add_reminder(
title: str, notes: str | None = None, due_date: datetime.datetime | None = None
) -> None:
"""
Add a new reminder to the Reminders app.
:param title: The title of the reminder.
:param notes: Optional notes for the reminder.
:param due_date: Optional due date for the reminder, in ISO format.
"""
print(f"title: {title}")
print(f"notes: {notes}")
print(f"due_date: {due_date}")
apple_script = dedent(f"""
tell application "Reminders"
set newReminder to make new reminder with properties {{name:"{title}"}}
{f'set body of newReminder to "{notes}"' if notes else ''}
{f'set due date of newReminder to date "{due_date:%B %d, %Y at %I:%M:%S %p}"' if due_date else ''}
end tell
""").strip()
_run_apple_script(apple_script)
if __name__ == "__main__":
tools = hype.create_ollama_tools(
[
get_current_time,
add_reminder,
],
)
response = ollama.chat(
model="llama3.2",
messages=[
{
"role": "system",
"content": """
You are a helpful assistant that can interract with macOS System Events.
Always use sentence case when creating reminders.
""",
},
{
"role": "user",
"content": "Add a reminder to buy groceries",
},
],
tools=tools,
)
print(response)
results = tools(response["message"]["tool_calls"]) # pylint: disable=unsubscriptable-object
print(results)