-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from Maplemx/dev
Dev
- Loading branch information
Showing
10 changed files
with
1,583 additions
and
6 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
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) |
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
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,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. |
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,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", | ||
] |
Oops, something went wrong.