forked from TransformerOptimus/SuperAGI
-
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.
Merge branch 'main' of github.com:TransformerOptimus/SuperAGI into to…
…ols_logic
- Loading branch information
Showing
14 changed files
with
428 additions
and
83 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
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,48 @@ | ||
import json | ||
from abc import ABC, abstractmethod | ||
from typing import Dict, NamedTuple | ||
|
||
|
||
class AgentGPTAction(NamedTuple): | ||
name: str | ||
args: Dict | ||
|
||
|
||
class BaseOutputParser(ABC): | ||
@abstractmethod | ||
def parse(self, text: str) -> AutoGPTAction: | ||
"""Return AgentGPTAction""" | ||
|
||
|
||
def preprocess_json_input(input_str: str) -> str: | ||
# Replace single backslashes with double backslashes, | ||
# while leaving already escaped ones intact | ||
corrected_str = re.sub( | ||
r'(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})', r"\\\\", input_str | ||
) | ||
return corrected_str | ||
|
||
|
||
class AgentOutputParser(BaseOutputParser): | ||
def parse(self, text: str) -> AgentGPTAction: | ||
try: | ||
parsed = json.loads(text, strict=False) | ||
except json.JSONDecodeError: | ||
preprocessed_text = preprocess_json_input(text) | ||
try: | ||
parsed = json.loads(preprocessed_text, strict=False) | ||
except Exception: | ||
return AgentGPTAction( | ||
name="ERROR", | ||
args={"error": f"Could not parse invalid json: {text}"}, | ||
) | ||
try: | ||
return AgentGPTAction( | ||
name=parsed["command"]["name"], | ||
args=parsed["command"]["args"], | ||
) | ||
except (KeyError, TypeError): | ||
# If the command is null or incomplete, return an erroneous tool | ||
return AgentGPTAction( | ||
name="ERROR", args={"error": f"Incomplete command args: {parsed}"} | ||
) |
Oops, something went wrong.