mirror of
https://github.com/OS-Copilot/OS-Copilot.git
synced 2026-05-05 03:00:15 -04:00
Merge branch 'new_infra_hcc' into dev_wzm
This commit is contained in:
38
examples/4_mac_rest_for_a_few_minutes.py
Normal file
38
examples/4_mac_rest_for_a_few_minutes.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from jarvis.agent.openai_agent import OpenAIAgent
|
||||
from jarvis.agent.skill_creator import SkillCreator
|
||||
# from jarvis.enviroment.old_env import BaseEnviroment
|
||||
from jarvis.enviroment.bash_env import BashEnv
|
||||
|
||||
'''
|
||||
A minimal example for creating new skills
|
||||
'''
|
||||
|
||||
# environment = BaseEnviroment()
|
||||
environment = BashEnv()
|
||||
agent = OpenAIAgent(config_path="examples/config.json")
|
||||
skill_creator = SkillCreator(config_path="examples/config.json")
|
||||
|
||||
response = '''
|
||||
Thought: Taking a 20-minute break means we can focus on the following three sub-tasks: 1. Enable Do Not Disturb mode; 2. Play some light music; 3. Set a 20-minute alarm.
|
||||
|
||||
Actions:
|
||||
# 0. <action>set_30_minute_alarm</action>
|
||||
1. <action>enable_do_not_disturb</action>
|
||||
2. <action>play_light_music</action>
|
||||
3. <action>set_20_minute_alarm</action>'''
|
||||
|
||||
action = agent.extract_action(response, begin_str='<action>', end_str='</action>')
|
||||
import time
|
||||
for a in action:
|
||||
if a in agent.action_lib:
|
||||
command = agent.action_lib[a]
|
||||
print("Successfully read the '{}' command from the action lib.".format(a))
|
||||
else:
|
||||
print("There is no '{}' command in the action lib and a new one needs to be created.".format(a))
|
||||
response = skill_creator.format_message(a)
|
||||
print(response)
|
||||
|
||||
|
||||
print(a, command)
|
||||
print(environment.step(command))
|
||||
time.sleep(2)
|
||||
36
jarvis/action/get_os_version.py
Normal file
36
jarvis/action/get_os_version.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import platform
|
||||
# import os
|
||||
|
||||
def get_os_version():
|
||||
system = platform.system()
|
||||
|
||||
if system == "Darwin":
|
||||
# macOS
|
||||
return 'macOS ' + platform.mac_ver()[0]
|
||||
elif system == "Linux":
|
||||
# Linux: 尝试读取 /etc/os-release 文件
|
||||
try:
|
||||
with open("/etc/os-release") as f:
|
||||
lines = f.readlines()
|
||||
for line in lines:
|
||||
if line.startswith("PRETTY_NAME"):
|
||||
return line.split("=")[1].strip().strip('"')
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
# 如果 /etc/os-release 不存在或无法提供所需信息
|
||||
return platform.version()
|
||||
else:
|
||||
return "Unknown Operating System"
|
||||
|
||||
|
||||
def check_os_version(s):
|
||||
if "mac" in s or "Ubuntu" in s or "CentOS" in s:
|
||||
print("perating System Version:", s)
|
||||
else:
|
||||
raise ValueError("Unknown Operating System")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
os_version = get_os_version()
|
||||
print("Operating System Version:", os_version)
|
||||
12
jarvis/action_lib/enable_do_not_disturb.py
Normal file
12
jarvis/action_lib/enable_do_not_disturb.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import subprocess
|
||||
|
||||
def enable_do_not_disturb():
|
||||
# This function uses AppleScript to enable Do Not Disturb mode on a Mac.
|
||||
applescript = f"""
|
||||
tell application "Shortcuts Events"
|
||||
run shortcut "enable_do_not_disturb"
|
||||
end tell
|
||||
"""
|
||||
subprocess.run(["osascript", "-e", applescript])
|
||||
|
||||
enable_do_not_disturb()
|
||||
12
jarvis/action_lib/play_light_music.py
Normal file
12
jarvis/action_lib/play_light_music.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import subprocess
|
||||
|
||||
def play_light_music():
|
||||
# Plays a playlist named "light music" in iTunes or Music app
|
||||
applescript = """
|
||||
tell application "Music"
|
||||
play playlist "light music"
|
||||
end tell
|
||||
"""
|
||||
subprocess.run(["osascript", "-e", applescript])
|
||||
|
||||
play_light_music()
|
||||
22
jarvis/action_lib/set_20_minute_alarm.py
Normal file
22
jarvis/action_lib/set_20_minute_alarm.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import subprocess
|
||||
import datetime
|
||||
|
||||
def set_20_minute_alarm():
|
||||
# Sets an alarm for 20 minutes from now using AppleScript
|
||||
# The script creates a new reminder with an alert
|
||||
current_time = datetime.datetime.now()
|
||||
alarm_time = current_time + datetime.timedelta(minutes=20)
|
||||
alarm_time_str = alarm_time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
|
||||
applescript = f"""
|
||||
tell application "Reminders"
|
||||
set newReminder to make new reminder
|
||||
tell newReminder
|
||||
set name to "20 Minute Alarm"
|
||||
set remind me date to date "{alarm_time_str}"
|
||||
end tell
|
||||
end tell
|
||||
"""
|
||||
subprocess.run(["osascript", "-e", applescript])
|
||||
|
||||
set_20_minute_alarm()
|
||||
52
jarvis/agent/skill_creator.py
Normal file
52
jarvis/agent/skill_creator.py
Normal file
@@ -0,0 +1,52 @@
|
||||
from action.get_os_version import get_os_version, check_os_version
|
||||
from jarvis.core.llms import OpenAI
|
||||
|
||||
_MAC_SYSTEM_PROMPT = '''
|
||||
You are a helpful assistant that writes AppleScript code to complete any task specified by me.
|
||||
System Version: {system_version}
|
||||
Task: {task}
|
||||
You should only respond in the format as described below:
|
||||
|
||||
import subprocess
|
||||
|
||||
def task_name():
|
||||
# <task description>
|
||||
applescript = f"""
|
||||
<Code Completion>
|
||||
"""
|
||||
subprocess.run(["osascript", "-e", applescript])
|
||||
|
||||
task_name()
|
||||
'''
|
||||
|
||||
_LINUX_SYSTEM_PROMPT = '''
|
||||
|
||||
'''
|
||||
|
||||
|
||||
class SkillCreator():
|
||||
"""
|
||||
SkillCreator is used to generate new skills and store them in the action_lib.
|
||||
"""
|
||||
def __init__(self, config_path=None) -> None:
|
||||
super().__init__()
|
||||
self.llm = OpenAI(config_path)
|
||||
self.system_version = get_os_version()
|
||||
try:
|
||||
check_os_version(self.system_version)
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
# self.mac_systom_prompts =
|
||||
|
||||
def format_message(self, task):
|
||||
self.prompt = _MAC_SYSTEM_PROMPT.format(
|
||||
system_version=self.system_version,
|
||||
task=task
|
||||
)
|
||||
self.message = [
|
||||
{"role": "system", "content": self.prompt},
|
||||
{"role": "user", "content": task},
|
||||
]
|
||||
return self.llm.chat(self.message)
|
||||
|
||||
|
||||
@@ -16,12 +16,13 @@ class OpenAI:
|
||||
openai.organization = config['OPENAI_ORGANIZATION']
|
||||
|
||||
def chat(self, messages, temperature=0, sleep_time=2):
|
||||
response = openai.ChatCompletion.create(
|
||||
response = openai.chat.completions.create(
|
||||
model=self.model_name,
|
||||
messages=messages,
|
||||
temperature=temperature
|
||||
)
|
||||
# time.sleep(sleep_time)
|
||||
return response['choices'][0]['message']
|
||||
# return response['choices'][0]['message']
|
||||
return response.choices[0].message.content
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user