commit
9e5fe73fba
@ -0,0 +1,125 @@
|
||||
"""
|
||||
Exposes a POST endpoint called /computer. Things from there go into the queue.
|
||||
|
||||
Exposes a ws endpoint called /user. Things from there go into the queue. We also send things in the queue back (that are role: assistant)
|
||||
|
||||
In a while loop we watch the queue.
|
||||
"""
|
||||
|
||||
import json
|
||||
import time
|
||||
import queue
|
||||
import os
|
||||
from threading import Thread
|
||||
import uvicorn
|
||||
import re
|
||||
from fastapi import FastAPI
|
||||
from threading import Thread
|
||||
from starlette.websockets import WebSocket
|
||||
from create_interpreter import create_interpreter
|
||||
from stt import stt
|
||||
from tts import tts
|
||||
|
||||
# Create interpreter
|
||||
interpreter = create_interpreter()
|
||||
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
conversation_history_path = os.path.join(script_dir, 'conversations', 'user.json')
|
||||
|
||||
# Create Queue objects
|
||||
to_user = queue.Queue()
|
||||
to_assistant = queue.Queue()
|
||||
|
||||
# This is so we only say() full sentences
|
||||
accumulated_text = ""
|
||||
def is_full_sentence(text):
|
||||
return text.endswith(('.', '!', '?'))
|
||||
def split_into_sentences(text):
|
||||
return re.split(r'(?<=[.!?])\s+', text)
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.post("/computer")
|
||||
async def read_computer(item: dict):
|
||||
to_assistant.put(item)
|
||||
|
||||
@app.websocket("/user")
|
||||
async def websocket_endpoint(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
while True:
|
||||
data = await websocket.receive_json()
|
||||
to_assistant.put(data)
|
||||
if not to_user.empty():
|
||||
message = to_user.get()
|
||||
await websocket.send_json(message)
|
||||
|
||||
audio_chunks = []
|
||||
|
||||
def queue_listener():
|
||||
while True:
|
||||
# Check 10x a second for new messages
|
||||
while to_assistant.empty():
|
||||
time.sleep(0.1)
|
||||
message = to_assistant.get()
|
||||
|
||||
# Hold the audio in a buffer. If it's ready (we got end flag, stt it)
|
||||
if message["type"] == "audio":
|
||||
if "content" in message:
|
||||
audio_chunks.append(message)
|
||||
if "end" in message:
|
||||
text = stt(audio_chunks)
|
||||
audio_chunks = []
|
||||
message = {"role": "user", "type": "message", "content": text}
|
||||
else:
|
||||
continue
|
||||
|
||||
# Custom stop message will halt us
|
||||
if message.get("content") and message.get("content").lower().strip(".,!") == "stop":
|
||||
continue
|
||||
|
||||
# Load, append, and save conversation history
|
||||
with open(conversation_history_path, 'r') as file:
|
||||
messages = json.load(file)
|
||||
messages.append(message)
|
||||
with open(conversation_history_path, 'w') as file:
|
||||
json.dump(messages, file)
|
||||
|
||||
accumulated_text = ""
|
||||
|
||||
for chunk in interpreter.chat(messages):
|
||||
|
||||
# Send it to the user
|
||||
to_user.put(chunk)
|
||||
|
||||
# Speak full sentences out loud
|
||||
accumulated_text += chunk["content"]
|
||||
sentences = split_into_sentences(accumulated_text)
|
||||
if is_full_sentence(sentences[-1]):
|
||||
for sentence in sentences:
|
||||
for audio_chunk in tts(sentence):
|
||||
to_user.put(audio_chunk)
|
||||
accumulated_text = ""
|
||||
else:
|
||||
for sentence in sentences[:-1]:
|
||||
for audio_chunk in tts(sentence):
|
||||
to_user.put(audio_chunk)
|
||||
accumulated_text = sentences[-1]
|
||||
|
||||
if chunk["type"] == "message" and "content" in sentence:
|
||||
sentence += chunk.get("content")
|
||||
|
||||
# If we have a new message, save our progress and go back to the top
|
||||
if not to_assistant.empty():
|
||||
with open(conversation_history_path, 'w') as file:
|
||||
json.dump(interpreter.messages, file)
|
||||
break
|
||||
|
||||
# Create a thread for the queue listener
|
||||
queue_thread = Thread(target=queue_listener)
|
||||
|
||||
# Start the queue listener thread
|
||||
queue_thread.start()
|
||||
|
||||
# Run the FastAPI app
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
@ -0,0 +1,127 @@
|
||||
from interpreter import interpreter
|
||||
import os
|
||||
import glob
|
||||
import json
|
||||
import requests
|
||||
|
||||
def create_interpreter():
|
||||
### SYSTEM MESSAGE
|
||||
|
||||
# The system message is where most of the 01's behavior is configured.
|
||||
# You can put code into the system message {{ in brackets like this }} which will be rendered just before the interpreter starts writing a message.
|
||||
|
||||
system_message = """
|
||||
|
||||
You are an executive assistant AI that helps the user manage their tasks. You can run Python code.
|
||||
|
||||
Store the user's tasks in a Python list called `tasks`.
|
||||
|
||||
---
|
||||
|
||||
The user's current task is: {{ tasks[0] if tasks else "No current tasks." }}
|
||||
|
||||
{{
|
||||
if len(tasks) > 1:
|
||||
print("The next task is: ", tasks[1])
|
||||
}}
|
||||
|
||||
---
|
||||
|
||||
When the user completes the current task, you should remove it from the list and read the next item by running `tasks = tasks[1:]\ntasks[0]`. Then, tell the user what the next task is.
|
||||
|
||||
When the user tells you about a set of tasks, you should intelligently order tasks, batch similar tasks, and break down large tasks into smaller tasks (for this, you should consult the user and get their permission to break it down). Your goal is to manage the task list as intelligently as possible, to make the user as efficient and non-overwhelmed as possible. They will require a lot of encouragement, support, and kindness. Don't say too much about what's ahead of them— just try to focus them on each step at a time.
|
||||
|
||||
After starting a task, you should check in with the user around the estimated completion time to see if the task is completed. Use the `schedule(datetime, message)` function, which has already been imported.
|
||||
|
||||
To do this, schedule a reminder based on estimated completion time using the function `schedule(datetime_object, "Your message here.")`, WHICH HAS ALREADY BEEN IMPORTED. YOU DON'T NEED TO IMPORT THE `schedule` FUNCTION. IT IS AVALIABLE. You'll recieve the message at `datetime_object`.
|
||||
|
||||
You guide the user through the list one task at a time, convincing them to move forward, giving a pep talk if need be. Your job is essentially to answer "what should I (the user) be doing right now?" for every moment of the day.
|
||||
|
||||
Remember: You can run Python code. Be very concise. Ensure that you actually run code every time! THIS IS IMPORTANT. You NEED to write code. **Help the user by being very concise in your answers.** Do not break down tasks excessively, just into simple, few minute steps. Don't assume the user lives their life in a certain way— pick very general tasks if you're breaking a task down.
|
||||
|
||||
""".strip()
|
||||
|
||||
interpreter.custom_instructions = system_message
|
||||
|
||||
### LLM SETTINGS
|
||||
|
||||
# Local settings
|
||||
# interpreter.llm.model = "local"
|
||||
# interpreter.llm.api_base = "https://localhost:8080/v1" # Llamafile default
|
||||
# interpreter.llm.max_tokens = 1000
|
||||
# interpreter.llm.context_window = 3000
|
||||
|
||||
# Hosted settings
|
||||
interpreter.llm.api_key = os.getenv('OPENAI_API_KEY')
|
||||
interpreter.llm.model = "gpt-4"
|
||||
interpreter.auto_run = True
|
||||
interpreter.force_task_completion = True
|
||||
|
||||
|
||||
### MISC SETTINGS
|
||||
|
||||
interpreter.offline = True
|
||||
interpreter.id = 206 # Used to identify itself to other interpreters. This should be changed programatically so it's unique.
|
||||
|
||||
|
||||
### RESET conversations/user.json
|
||||
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
user_json_path = os.path.join(script_dir, 'conversations', 'user.json')
|
||||
with open(user_json_path, 'w') as file:
|
||||
json.dump([], file)
|
||||
|
||||
|
||||
### CONNECT TO /run
|
||||
|
||||
class Python:
|
||||
"""
|
||||
This class contains all requirements for being a custom language in Open Interpreter:
|
||||
|
||||
- name (an attribute)
|
||||
- run (a method)
|
||||
- stop (a method)
|
||||
- terminate (a method)
|
||||
"""
|
||||
|
||||
# This is the name that will appear to the LLM.
|
||||
name = "python"
|
||||
|
||||
def run(self, code):
|
||||
"""Generator that yields a dictionary in LMC Format."""
|
||||
|
||||
# Prepare the data
|
||||
data = {"language": "python", "code": code}
|
||||
|
||||
# Send the data to the /run endpoint
|
||||
response = requests.post("http://localhost:8000/run", json=data, stream=True)
|
||||
|
||||
# Stream the response
|
||||
for line in response.iter_lines():
|
||||
if line: # filter out keep-alive new lines
|
||||
yield json.loads(line)
|
||||
|
||||
def stop(self):
|
||||
"""Stops the code."""
|
||||
# Not needed here, because e2b.run_code isn't stateful.
|
||||
pass
|
||||
|
||||
def terminate(self):
|
||||
"""Terminates the entire process."""
|
||||
# Not needed here, because e2b.run_code isn't stateful.
|
||||
pass
|
||||
|
||||
interpreter.computer.languages = [Python]
|
||||
|
||||
### SKILLS
|
||||
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
skills_dir = os.path.join(script_dir, 'skills')
|
||||
for file in glob.glob(os.path.join(skills_dir, '*.py')):
|
||||
with open(file, 'r') as f:
|
||||
for chunk in interpreter.computer.run("python", f.read()):
|
||||
print(chunk)
|
||||
|
||||
### RETURN INTERPRETER
|
||||
|
||||
return interpreter
|
@ -0,0 +1,6 @@
|
||||
"""
|
||||
Defines a function which takes text and returns a path to an audio file.
|
||||
"""
|
||||
|
||||
def tts(text):
|
||||
return path_to_audio
|
@ -1,4 +1,133 @@
|
||||
"""
|
||||
Watches the kernel. When it sees something that passes a filter,
|
||||
it sends POST request with that to /computer.
|
||||
"""
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import time
|
||||
import requests
|
||||
import platform
|
||||
|
||||
class Device:
|
||||
def __init__(self, device_type, device_info):
|
||||
self.device_type = device_type
|
||||
self.device_info = device_info
|
||||
|
||||
def get_device_info(self):
|
||||
info = f"Device Type: {self.device_type}\n"
|
||||
for key, value in self.device_info.items():
|
||||
info += f"{key}: {value}\n"
|
||||
return info
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Device):
|
||||
return self.device_type == other.device_type and self.device_info == other.device_info
|
||||
return False
|
||||
|
||||
|
||||
def get_connected_devices():
|
||||
"""
|
||||
Get all connected devices on macOS using system_profiler
|
||||
"""
|
||||
devices = []
|
||||
usb_output = subprocess.check_output(['system_profiler', 'SPUSBDataType'])
|
||||
network_output = subprocess.check_output(['system_profiler', 'SPNetworkDataType'])
|
||||
|
||||
usb_lines = usb_output.decode('utf-8').split('\n')
|
||||
network_lines = network_output.decode('utf-8').split('\n')
|
||||
|
||||
device_info = {}
|
||||
for line in usb_lines:
|
||||
if 'Product ID:' in line or 'Serial Number:' in line or 'Manufacturer:' in line:
|
||||
key, value = line.strip().split(':')
|
||||
device_info[key.strip()] = value.strip()
|
||||
if 'Manufacturer:' in line:
|
||||
devices.append(Device('USB', device_info))
|
||||
device_info = {}
|
||||
|
||||
for line in network_lines:
|
||||
if 'Type:' in line or 'Hardware:' in line or 'BSD Device Name:' in line:
|
||||
key, value = line.strip().split(':')
|
||||
device_info[key.strip()] = value.strip()
|
||||
if 'BSD Device Name:' in line:
|
||||
devices.append(Device('Network', device_info))
|
||||
device_info = {}
|
||||
|
||||
return devices
|
||||
|
||||
|
||||
def run_kernel_watch_darwin():
|
||||
prev_connected_devices = None
|
||||
while True:
|
||||
messages_to_send = []
|
||||
connected_devices = get_connected_devices()
|
||||
if prev_connected_devices is not None:
|
||||
for device in connected_devices:
|
||||
if device not in prev_connected_devices:
|
||||
messages_to_send.append(f'New device connected: {device.get_device_info()}')
|
||||
for device in prev_connected_devices:
|
||||
if device not in connected_devices:
|
||||
messages_to_send.append(f'Device disconnected: {device.get_device_info()}')
|
||||
|
||||
if messages_to_send:
|
||||
requests.post('http://localhost:8000/computer', json = {'messages': messages_to_send})
|
||||
prev_connected_devices = connected_devices
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
def get_dmesg(after):
|
||||
"""
|
||||
Is this the way to do this?
|
||||
"""
|
||||
messages = []
|
||||
with open('/var/log/dmesg', 'r') as file:
|
||||
lines = file.readlines()
|
||||
for line in lines:
|
||||
timestamp = float(line.split(' ')[0].strip('[]'))
|
||||
if timestamp > after:
|
||||
messages.append(line)
|
||||
return messages
|
||||
|
||||
|
||||
def custom_filter(message):
|
||||
# Check for {TO_INTERPRETER{ message here }TO_INTERPRETER} pattern
|
||||
if '{TO_INTERPRETER{' in message and '}TO_INTERPRETER}' in message:
|
||||
start = message.find('{TO_INTERPRETER{') + len('{TO_INTERPRETER{')
|
||||
end = message.find('}TO_INTERPRETER}', start)
|
||||
return message[start:end]
|
||||
# Check for USB mention
|
||||
elif 'USB' in message:
|
||||
return message
|
||||
# Check for network related keywords
|
||||
elif any(keyword in message for keyword in ['network', 'IP', 'internet', 'LAN', 'WAN', 'router', 'switch']):
|
||||
return message
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def run_kernel_watch_linux():
|
||||
last_timestamp = time.time()
|
||||
|
||||
while True:
|
||||
messages = get_dmesg(after=last_timestamp)
|
||||
last_timestamp = time.time()
|
||||
|
||||
messages_for_core = []
|
||||
for message in messages:
|
||||
if custom_filter(message):
|
||||
messages_for_core.append(message)
|
||||
if messages_for_core:
|
||||
requests.post('http://localhost:8000/computer', json = {'messages': messages_for_core})
|
||||
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
current_platform = platform.system()
|
||||
if current_platform == "Darwin":
|
||||
run_kernel_watch_darwin()
|
||||
elif current_platform == "Linux":
|
||||
run_kernel_watch_linux()
|
||||
else:
|
||||
print("Unsupported platform. Exiting.")
|
||||
|
Loading…
Reference in new issue