forked from sorokinvld/AIOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add open-interpreter adapter (agiresearch#224)
* feat: init * feat: interpreter adapter * docs: interpreter doc * style: clean some code
- Loading branch information
1 parent
b206bca
commit 741207b
Showing
10 changed files
with
278 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from . import adapter | ||
from . import adapter | ||
|
||
adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from . import adapter | ||
|
||
adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# replace run_tool_calling_llm and run_text_llm in interpreter llm | ||
# so that interpreter can run LLM in aios | ||
import json | ||
import sys | ||
|
||
from aios.utils.logger import SDKLogger | ||
from pyopenagi.agents.agent_process import AgentProcessFactory | ||
from pyopenagi.utils.chat_template import Query | ||
from pyopenagi.agents.call_core import CallCore | ||
from dataclasses import dataclass | ||
|
||
try: | ||
from interpreter import interpreter | ||
|
||
except ImportError: | ||
raise ImportError( | ||
"Could not import interpreter python package. " | ||
"Please install it with `open-interpreter`." | ||
) | ||
|
||
logger = SDKLogger("Interpreter Adapter") | ||
|
||
aios_call = None | ||
|
||
|
||
def prepare_interpreter(agent_process_factory: AgentProcessFactory): | ||
"""Prepare the interpreter for running LLM in aios. | ||
Args: | ||
agent_process_factory (AgentProcessFactory): | ||
Used to create agent processes. | ||
""" | ||
|
||
try: | ||
# Set the completion function in the interpreter | ||
interpreter.llm.completions = adapter_aios_completions | ||
|
||
# Initialize the aios_call variable as a CallCore object | ||
global aios_call | ||
aios_call = CallCore("interpreter", agent_process_factory, "console") | ||
except Exception as e: | ||
logger.log("Interpreter prepare failed: " + str(e) + "\n", "error") | ||
|
||
logger.log("Interpreter prepare success\n", "info") | ||
|
||
|
||
@dataclass | ||
class InterpreterFunctionAdapter: | ||
name: str | ||
arguments: str | ||
|
||
|
||
@dataclass | ||
class InterpreterToolCallsAdapter: | ||
function: InterpreterFunctionAdapter | ||
|
||
def __init__(self, name: str, arguments: str): | ||
self.function = InterpreterFunctionAdapter(name, arguments) | ||
|
||
|
||
def adapter_aios_completions(**params): | ||
"""aios completions replace fixed_litellm_completions in interpreter | ||
""" | ||
|
||
if params.get("stream", False) is True: | ||
# TODO: AIOS not supprt stream mode | ||
logger.log('''AIOS does not support stream mode currently. The stream mode has been automatically set to False. | ||
''', level="warn") | ||
params["stream"] = False | ||
|
||
# Run completion | ||
attempts = 2 | ||
first_error = None | ||
|
||
for attempt in range(attempts): | ||
try: | ||
global aios_call | ||
assert isinstance(aios_call, CallCore) | ||
response, _, _, _, _ = aios_call.get_response( | ||
query=Query( | ||
messages=params['messages'], | ||
tools=(params["tools"] if "tools" in params else None) | ||
) | ||
) | ||
|
||
# format similar to completion in interpreter | ||
comletion = {'choices': | ||
[ | ||
{ | ||
'delta': {} | ||
} | ||
] | ||
} | ||
comletion["choices"][0]["delta"]["content"] = response.response_message | ||
if response.tool_calls is not None: | ||
comletion["choices"][0]["delta"]["tool_calls"] = format_tool_calls_to_interpreter(response.tool_calls) | ||
|
||
return [comletion] # If the completion is successful, exit the function | ||
except KeyboardInterrupt: | ||
print("Exiting...") | ||
sys.exit(0) | ||
except Exception as e: | ||
if attempt == 0: | ||
# Store the first error | ||
first_error = e | ||
|
||
if first_error is not None: | ||
raise first_error | ||
|
||
|
||
def format_tool_calls_to_interpreter(tool_calls): | ||
name = tool_calls[0]["name"] | ||
arguments = tool_calls[0]["parameters"] | ||
arguments = json.dumps(arguments) | ||
return [InterpreterToolCallsAdapter(name, arguments)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ Below are the frameworks currently supported | |
:caption: Supported Agent Framework | ||
|
||
autogen | ||
|
||
open_interpreter |
53 changes: 53 additions & 0 deletions
53
docs/source/get_started/agent_framework/open_interpreter.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
Open-Interpreter For AIOS | ||
========================= | ||
|
||
Introduction | ||
------------ | ||
Open Interpreter lets language models run code. We made it | ||
so that agent applications developed with Open Interpreter can run on AIOS by adding | ||
just one line of code. | ||
|
||
Quick start | ||
----------- | ||
For installation and usage of open-interpreter, please refer to the `official open-interpreter documentation <https://docs.openinterpreter.com/getting-started/introduction>`_. | ||
|
||
If you want to run an application developed with open-interpreter on AIOS, please add ``prepare_interpreter()`` | ||
before you use open-interpreter. ``AgentProcessFactory`` is a required parameter. | ||
|
||
.. code-block:: python | ||
from pyopenagi.agents.agent_process import AgentProcessFactory | ||
from aios.sdk.interpreter.adapter import prepare_interpreter | ||
from interpreter import interpreter | ||
# example process_factory | ||
process_factory = AgentProcessFactory() | ||
# prepate interpreter for AIOS | ||
prepare_interpreter(process_factory) | ||
Then nothing needs to change, use interpreter as usual. | ||
|
||
.. code-block:: python | ||
interpreter.chat("In a group of 23 people, the probability of at least two having the same birthday is greater than 50%") | ||
Don't forget to start the scheduler so that AIOS can manage llm call. | ||
Details and More examples can be found in https://github.com/agiresearch/AIOS/tree/main/scripts/aios-interpreter | ||
|
||
|
||
prepare_interpreter() | ||
--------------------- | ||
|
||
.. .. automethod:: aios.sdk.interpreter.adapter.prepare_interpreter | ||
.. :noindex: | ||
``prepare_interpreter()`` | ||
|
||
Prepare the interpreter for running LLM in aios. | ||
|
||
Parameters: | ||
**agent_process_factory** - Used to create agent processes. | ||
|
||
`Source Code <https://github.com/agiresearch/AIOS/blob/main/aios/sdk/interpreter/adapter.py>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import sys | ||
import os | ||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) | ||
|
||
|
||
import warnings | ||
from dotenv import load_dotenv | ||
from aios.sdk.interpreter.adapter import prepare_interpreter | ||
from aios.hooks.llm import useKernel, useFIFOScheduler | ||
from aios.utils.utils import ( | ||
parse_global_args, | ||
delete_directories | ||
) | ||
from pyopenagi.agents.agent_process import AgentProcessFactory | ||
from interpreter import interpreter | ||
|
||
|
||
def clean_cache(root_directory): | ||
targets = { | ||
".ipynb_checkpoints", | ||
"__pycache__", | ||
".pytest_cache", | ||
"context_restoration", | ||
} | ||
delete_directories(root_directory, targets) | ||
|
||
|
||
def main(): | ||
# parse arguments and set configuration for this run accordingly | ||
warnings.filterwarnings("ignore") | ||
parser = parse_global_args() | ||
args = parser.parse_args() | ||
|
||
llm_name = args.llm_name | ||
max_gpu_memory = args.max_gpu_memory | ||
eval_device = args.eval_device | ||
max_new_tokens = args.max_new_tokens | ||
scheduler_log_mode = args.scheduler_log_mode | ||
# agent_log_mode = args.agent_log_mode | ||
llm_kernel_log_mode = args.llm_kernel_log_mode | ||
use_backend = args.use_backend | ||
load_dotenv() | ||
|
||
llm = useKernel( | ||
llm_name=llm_name, | ||
max_gpu_memory=max_gpu_memory, | ||
eval_device=eval_device, | ||
max_new_tokens=max_new_tokens, | ||
log_mode=llm_kernel_log_mode, | ||
use_backend=use_backend | ||
) | ||
|
||
# run agents concurrently for maximum efficiency using a scheduler | ||
|
||
# scheduler = FIFOScheduler(llm=llm, log_mode=scheduler_log_mode) | ||
|
||
startScheduler, stopScheduler = useFIFOScheduler( | ||
llm=llm, | ||
log_mode=scheduler_log_mode, | ||
get_queue_message=None | ||
) | ||
|
||
process_factory = AgentProcessFactory() | ||
|
||
prepare_interpreter(process_factory) | ||
|
||
startScheduler() | ||
|
||
# interpreter.chat("Calculate 10 * 20 / 2") | ||
# interpreter.chat("Plot the sin function") | ||
# interpreter.chat("Use the Euclidean algorithm to calculate the greatest common divisor (GCD) of 78782 and 64.") | ||
interpreter.chat("In a group of 23 people, the probability of at least two having the same birthday is greater than 50%") | ||
|
||
stopScheduler() | ||
|
||
clean_cache(root_directory="./") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |