Skip to content

Commit

Permalink
Merge pull request #170 from Maplemx/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Maplemx authored Oct 8, 2024
2 parents 6577d1c + c55dbb2 commit 8a252cb
Show file tree
Hide file tree
Showing 10 changed files with 1,583 additions and 6 deletions.
101 changes: 101 additions & 0 deletions Agently/plugins/agent_component/Realtime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import json5
from Agently.utils import Lexer
from .utils import ComponentABC
from Agently.utils import find_json, DataGenerator

class Realtime(ComponentABC):
def __init__(self, agent: object):
self.agent = agent
self._is_enable = False
self._is_init = False
self._output_data_frame = None
self._target_keys = []
self._buffer = ""
self._data_generator = DataGenerator()
self._streamed_keys = []
self._ongoing_key = None
self._ongoing_value = None

def use_realtime(self):
self._is_enable = True
return self.agent

def __get_output_data_frame(self, prompt_output:dict, prefix:str=""):
result = {}
for key, value in prompt_output.items():
if isinstance(key, dict):
prefix += f"{key}."
result.update({ key: self.__get_output_data_frame(value, prefix) })
else:
self._target_keys.append(prefix + key)
result.update({ key: None })
return result

async def __scan_realtime_value(self, realtime_value:dict, prefix:str="", output_data_frame_pointer:dict={}):
for key, value in realtime_value.items():
if isinstance(value, dict):
if key in output_data_frame_pointer:
await self.__scan_realtime_value(value, prefix + f"{key}.", output_data_frame_pointer[key])
else:
if self._ongoing_key == (prefix + key):
self._ongoing_value = value
if self._ongoing_key != prefix + key:
if (prefix + key) not in self._streamed_keys and value not in (None, ""):
if self._ongoing_key != None:
await self.agent.call_event_listeners(f"key_stop:{ self._ongoing_key }", None)
self._ongoing_key = prefix + key
await self.agent.call_event_listeners(f"key_start:{ self._ongoing_key }", None)
self._streamed_keys.append(prefix + key)
self._ongoing_value = value
if (prefix + key) in self._streamed_keys or value in (None, ""):
continue

async def _suffix(self, event:str, data:any):
if not self._is_enable or "type" not in self.agent.request.response_cache or self.agent.request.response_cache["type"] != "JSON":
return None
else:
if not self._is_init:
prompt_output = self.agent.request.response_cache["prompt"]["output"]
if isinstance(prompt_output, dict):
self._output_data_frame = self.__get_output_data_frame(prompt_output)
self._is_init = True
if event == "response:delta":
self._buffer += data
realtime_json_str = find_json(self._buffer)
if realtime_json_str != None:
lexer = Lexer()
lexer.append_string(realtime_json_str)
realtime_value = json5.loads(lexer.complete_json())
if self._output_data_frame == None:
await self.agent.call_event_listeners(
"response:realtime",
{
"key": None,
"value": realtime_value,
"complete_value": realtime_value,
},
)
else:
if isinstance(realtime_value, dict):
await self.__scan_realtime_value(realtime_value, "", self._output_data_frame)
if self._ongoing_key != None:
await self.agent.call_event_listeners(
"response:realtime",
{
"key": self._ongoing_key,
"value": self._ongoing_value,
"complete_value": realtime_value,
},
)

def export(self):
return {
"prefix": None,
"suffix": self._suffix,
"alias": {
"use_realtime": { "func": self.use_realtime }
},
}

def export():
return ("Realtime", Realtime)
2 changes: 1 addition & 1 deletion Agently/plugins/agent_component/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ plugin_settings.agent_component.Session = { "auto_save": true, "max_length": 120

[orders]
prefix = EventListener, Status, ..., Search, Tool, Segment
suffix = ReplyReformer, ..., Session
suffix = ReplyReformer, ..., Session, EventListener
3 changes: 2 additions & 1 deletion Agently/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
from .transform import to_prompt_structure, to_json_desc, to_instruction, find_all_jsons, find_json
from .check_version import check_version
from .load_json import load_json, find_and_load_json
from .DataGenerator import DataGenerator
from .DataGenerator import DataGenerator
from .lexer.lexer import Lexer
21 changes: 21 additions & 0 deletions Agently/utils/lexer/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License


Copyright (c) 2024 Karminski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions Agently/utils/lexer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .lexer import Lexer

__version__ = "0.0.4"

__title__ = "streamingjson"
__description__ = (
"A streamlined, user-friendly JSON streaming preprocessor, crafted in Python."
)
__url__ = "https://github.com/karminski/streaming-json-py"
__uri__ = __url__
__doc__ = f"{__description__} <{__uri__}>"

__author__ = "Karminski"
__email__ = "[email protected]"

__license__ = "MIT"
__copyright__ = "Copyright 2024 Karminski"

__all__ = [
"Lexer",
]
Loading

0 comments on commit 8a252cb

Please sign in to comment.