Skip to content

Commit

Permalink
feat: supports front-end visualization of task-solving about tool-using
Browse files Browse the repository at this point in the history
  • Loading branch information
minleminzui committed Nov 23, 2023
1 parent 5a08369 commit 54aba7a
Show file tree
Hide file tree
Showing 17,072 changed files with 41,187 additions and 3,590,789 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
32 changes: 32 additions & 0 deletions AgentVerseIO/BaseIO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from AgentVerseIO.input.CommandLineInput import CommandLineInput
from AgentVerseIO.input.base import BaseInput
from AgentVerseIO.output.CommandLineOutput import CommandLineOutput
from AgentVerseIO.output.base import BaseOutput


class AgentVerseIO:
def __init__(self, input: BaseInput, output: BaseOutput) -> None:
if input is None:
self.Input = CommandLineInput()
else:
if not isinstance(input, BaseInput):
raise TypeError("input must be a BaseInput instance")

self.Input = input

if output is None:
self.Output = CommandLineOutput()
else:
if not isinstance(output, BaseOutput):
raise TypeError("output must be a BaseOutput instance")

self.Output = output

def set_logger(self, logger):
self.logger = logger
self.Input.set_logger(logger)
self.Output.set_logger(logger)

def close(self):
self.Input.close()
self.Output.close()
124 changes: 124 additions & 0 deletions AgentVerseIO/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
class AgentVerseIOError(Exception):
"""Base class for exceptions in this module."""

pass


class AgentVerseIOInterruptError(AgentVerseIOError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO Interrupt!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOTimeoutError(AgentVerseIOError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO Timeout!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOCloseError(AgentVerseIOError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO Close!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOWebSocketError(AgentVerseIOError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO WebSocket Error!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOWebSocketTimeoutError(AgentVerseIOWebSocketError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO WebSocket Timeout!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOWebSocketDisconnectError(AgentVerseIOWebSocketError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO WebSocket Disconnect!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOWebSocketConnectError(AgentVerseIOWebSocketError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO WebSocket Connect Error!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOWebSocketCloseError(AgentVerseIOWebSocketError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO WebSocket Close!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOWebSocketSendError(AgentVerseIOWebSocketError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO WebSocket Send Error!"):
self.message = message
super().__init__(self.message)


class AgentVerseIOWebSocketReceiveError(AgentVerseIOWebSocketError):
"""Exception raised for errors in the input.
Attributes:
message -- explanation of the error
"""

def __init__(self, message="AgentVerse IO WebSocket Receive Error!"):
self.message = message
super().__init__(self.message)
95 changes: 95 additions & 0 deletions AgentVerseIO/input/CommandLineInput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import asyncio
import functools
import time
from colorama import Fore
from AgentVerseIO.exception import AgentVerseIOCloseError, AgentVerseIOTimeoutError
from AgentVerseIO.input.base import BaseInput
from inputimeout import inputimeout, TimeoutOccurred
import math


def timer(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
except:
pass

return wrapper


class CommandLineInput(BaseInput):
def __init__(self, do_interrupt: bool = False, max_wait_seconds: int = 600):
super().__init__(do_interrupt, max_wait_seconds)

async def run(self, input_data):
if self.do_interrupt:
data = await self.interrupt(input_data)
else:
data = input_data
return data

async def get_each_input(self, key, value, res, timeout):
self.logger.typewriter_log(
f"Now, ASK For {key}, Origin Input: {value}", Fore.RED, f""
)
self.logger.typewriter_log(
f"Now, you can modify the current field by entering some information, and then press 'Enter' to continue, if you want to keep the original input, please enter '-1' and then press 'Enter':",
Fore.GREEN,
)
temp = inputimeout(
prompt=f"You have {timeout} seconds to input:\n", timeout=timeout
)
if temp == "-1":
return value
else:
return temp

async def get_input(self, origin_data):
self.logger.typewriter_log(
"Next, you can start modifying the original input by typing 'Y/y/yes' or skip this step by typing 'N/n/no' and then press 'Enter' to continue the loop:",
Fore.RED,
)
update = inputimeout(
prompt=f"You have to make a decision within 60 seconds:\n", timeout=60
)
res = {"args": {}}
if update in ["y", "Y", "yes"]:
execute_time = self.max_wait_seconds
if isinstance(origin_data, dict):
args = origin_data.get("args", "")
self.logger.typewriter_log(
f"Next, you will have a total of {self.max_wait_seconds} seconds to modify each option:",
Fore.RED,
)
for key, value in args.items():
if key == "done":
res[key] = False
continue
start_time = time.time()
res["args"][key] = await self.get_each_input(
key, value, res, execute_time
)
end_time = time.time()
execute_time = math.floor(execute_time - (end_time - start_time))
self.logger.info(f"modify the input, receive the data: {res}")
else:
res = origin_data
self.logger.info("skip this step")
self.logger.info("continue the loop")
res["done"] = True
return res

async def interrupt(self, input_data):
try:
data = await self.get_input(input_data)
return data
except TimeoutOccurred:
self.logger.error(f"Waiting timemout, close connection!")
raise AgentVerseIOTimeoutError("timeout!")

def close(self):
raise AgentVerseIOCloseError("close connection!")
9 changes: 9 additions & 0 deletions AgentVerseIO/input/HttpInput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from AgentVerseIO.input.base import BaseInput


class HttpInput(BaseInput):
def __init__(self):
super().__init__()

def run(self):
raise NotImplementedError
9 changes: 9 additions & 0 deletions AgentVerseIO/input/RestApiInput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from AgentVerseIO.input.base import BaseInput


class RestApiInput(BaseInput):
def __init__(self):
super().__init__()

def run(self):
raise NotImplementedError
71 changes: 71 additions & 0 deletions AgentVerseIO/input/WebSocketInput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import asyncio
import json

from fastapi import WebSocket, WebSocketDisconnect

from AgentVerseIO.exception import (
AgentVerseIOWebSocketDisconnectError,
AgentVerseIOWebSocketTimeoutError,
)
from AgentVerseIO.input.base import BaseInput
from AgentVerseServer.loggers.logs import Logger
from AgentVerseServer.response_body import WebsocketResponseBody


class WebSocketInput(BaseInput):
def __init__(
self,
websocket: WebSocket,
do_interrupt: bool = False,
max_wait_seconds: int = 600,
):
super().__init__(do_interrupt, max_wait_seconds)
self.websocket = websocket

def set_logger(self, logger: Logger):
self.logger = logger

def set_interrupt(self, do_interrupt: bool = True):
self.do_interrupt = do_interrupt

async def interrupt(self):
wait = 0
while wait < self.max_wait_seconds:
print(
f"\r waiting for {wait} second, remaining {self.max_wait_seconds - wait} second",
end="",
)
try:
data = await asyncio.wait_for(self.auto_receive(), 1)
if isinstance(data, dict):
data_type = data.get("type", None)
# if data_type == "ping":
# await self.websocket.send_json({"type": "pong"})
# continue
if data_type == "data":
self.logger.info(f"Receiving data change request...")
self.logger.info(f"Received :{data}")
wait = 0
return data
else:
pass
except asyncio.TimeoutError:
wait += 1
self.logger.error(f"Wait timeout, close.")
self.websocket.send_text(
WebsocketResponseBody(
data=None, status="failed", message="Wait timeout, close."
).to_text()
)
raise AgentVerseIOWebSocketTimeoutError

async def auto_receive(self):
data = await self.websocket.receive_json()
return data

async def run(self, input):
if self.do_interrupt:
data = await self.interrupt()
return data
else:
return input
27 changes: 27 additions & 0 deletions AgentVerseIO/input/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from abc import ABCMeta, abstractmethod
import asyncio

from AgentVerseIO.exception import AgentVerseIOCloseError


class BaseInput(metaclass=ABCMeta):
def __init__(self, do_interrupt: bool = False, max_wait_seconds: int = 600):
self.do_interrupt = do_interrupt
if self.do_interrupt:
self.max_wait_seconds = max_wait_seconds

def set_wait(self, do_interrupt: bool = True):
self.do_interrupt = do_interrupt

def set_logger(self, logger):
self.logger = logger

async def interrupt(self):
raise NotImplementedError

def run(self):
raise NotImplementedError

def close(self):
# raise AgentVerseIOCloseError("close connection!")
pass
13 changes: 13 additions & 0 deletions AgentVerseIO/output/CommandLineOutput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from AgentVerseIO.exception import AgentVerseIOCloseError
from AgentVerseIO.output.base import BaseOutput


class CommandLineOutput(BaseOutput):
def __init__(self):
super().__init__()

async def run(self, output):
pass

def close(self):
raise AgentVerseIOCloseError("close connection!")
9 changes: 9 additions & 0 deletions AgentVerseIO/output/HttpOutput.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from AgentVerseIO.output.base import BaseOutput


class HttpOutput(BaseOutput):
def __init__(self):
super().__init__()

def run(self, output):
print(output)
Loading

0 comments on commit 54aba7a

Please sign in to comment.