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 'gpt3.5' of github.com:TransformerOptimus/SuperAGI into …
…gpt3.5
- Loading branch information
Showing
22 changed files
with
465 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,83 @@ | ||
import json | ||
from abc import ABC, abstractmethod | ||
from typing import Dict, NamedTuple | ||
from typing import Dict, NamedTuple, List | ||
import re | ||
|
||
from superagi.helper.json_cleaner import JsonCleaner | ||
|
||
|
||
class AgentGPTAction(NamedTuple): | ||
name: str | ||
args: Dict | ||
name: str | ||
args: Dict | ||
|
||
|
||
class BaseOutputParser(ABC): | ||
@abstractmethod | ||
def parse(self, text: str) -> AgentGPTAction: | ||
"""Return AgentGPTAction""" | ||
class AgentTasks(NamedTuple): | ||
tasks: List[Dict] = [] | ||
error: str = "" | ||
|
||
|
||
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 BaseOutputParser(ABC): | ||
@abstractmethod | ||
def parse(self, text: str) -> AgentGPTAction: | ||
"""Return AgentGPTAction""" | ||
|
||
|
||
|
||
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: | ||
format_prefix_yellow = "\033[93m\033[1m" | ||
format_suffix_yellow = "\033[0m\033[0m" | ||
format_prefix_green = "\033[92m\033[1m" | ||
format_suffix_green = "\033[0m\033[0m" | ||
print(format_prefix_green + "Intelligence : " + format_suffix_green) | ||
print(format_prefix_yellow + "Thoughts: " + format_suffix_yellow + parsed["thoughts"]["reasoning"]+"\n") | ||
print(format_prefix_yellow + "Reasoning: " + format_suffix_yellow + parsed["thoughts"]["reasoning"] + "\n") | ||
print(format_prefix_yellow + "Plan: " + format_suffix_yellow + parsed["thoughts"]["plan"] + "\n") | ||
print(format_prefix_yellow + "Criticism: " + format_suffix_yellow + parsed["thoughts"]["criticism"] + "\n") | ||
print(format_prefix_green + "Action : "+ format_suffix_green) | ||
print(format_prefix_yellow + "Tool: "+ format_suffix_yellow + parsed["command"]["name"] + "\n") | ||
# print(format_prefix_yellow + "Args: "+ format_suffix_yellow + parsed["command"]["args"] + "\n") | ||
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}"} | ||
) | ||
def parse(self, text: str) -> AgentGPTAction: | ||
try: | ||
print(text) | ||
text = JsonCleaner.check_and_clean_json(text) | ||
parsed = json.loads(text, strict=False) | ||
except json.JSONDecodeError: | ||
return AgentGPTAction( | ||
name="ERROR", | ||
args={"error": f"Could not parse invalid json: {text}"}, | ||
) | ||
try: | ||
format_prefix_yellow = "\033[93m\033[1m" | ||
format_suffix_yellow = "\033[0m\033[0m" | ||
format_prefix_green = "\033[92m\033[1m" | ||
format_suffix_green = "\033[0m\033[0m" | ||
print(format_prefix_green + "Intelligence : " + format_suffix_green) | ||
print(format_prefix_yellow + "Thoughts: " + format_suffix_yellow + parsed["thoughts"]["reasoning"] + "\n") | ||
print(format_prefix_yellow + "Reasoning: " + format_suffix_yellow + parsed["thoughts"]["reasoning"] + "\n") | ||
print(format_prefix_yellow + "Plan: " + format_suffix_yellow + parsed["thoughts"]["plan"] + "\n") | ||
print(format_prefix_yellow + "Criticism: " + format_suffix_yellow + parsed["thoughts"]["criticism"] + "\n") | ||
print(format_prefix_green + "Action : " + format_suffix_green) | ||
print(format_prefix_yellow + "Tool: " + format_suffix_yellow + parsed["command"]["name"] + "\n") | ||
# print(format_prefix_yellow + "Args: "+ format_suffix_yellow + parsed["command"]["args"] + "\n") | ||
|
||
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}"} | ||
) | ||
|
||
def parse_tasks(self, text: str) -> AgentTasks: | ||
try: | ||
parsed = json.loads(text, strict=False) | ||
except json.JSONDecodeError: | ||
preprocessed_text = JsonCleaner.preprocess_json_input(text) | ||
try: | ||
parsed = json.loads(preprocessed_text, strict=False) | ||
except Exception: | ||
return AgentTasks( | ||
error=f"Could not parse invalid json: {text}", | ||
) | ||
try: | ||
print("Tasks: ", parsed["tasks"]) | ||
return AgentTasks( | ||
tasks=parsed["tasks"] | ||
) | ||
except (KeyError, TypeError): | ||
# If the command is null or incomplete, return an erroneous tool | ||
return AgentTasks( | ||
error=f"Incomplete tool args: {parsed}", | ||
) | ||
|
||
|
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 json | ||
import re | ||
|
||
|
||
class JsonCleaner: | ||
|
||
@classmethod | ||
def check_and_clean_json(cls, json_string: str): | ||
try: | ||
json_string = json_string.replace("\\t", "") | ||
json_string = json_string.replace("\\n", "") | ||
json_string = cls.remove_escape_sequences(json_string) | ||
json.loads(json_string) | ||
return json_string | ||
except json.JSONDecodeError as e: | ||
# If the json is invalid, try to clean it up | ||
json_string = cls.preprocess_json_input(json_string) | ||
json_string = cls.add_quotes_to_property_names(json_string) | ||
json_string = cls.remove_escape_sequences(json_string) | ||
json_string = cls.balance_braces(json_string) | ||
try: | ||
json.loads(json_string) | ||
return json_string | ||
except json.JSONDecodeError as e: | ||
print(json_string) | ||
# If the json is still invalid, try to extract the json section | ||
json_string = cls.extract_json_section(json_string) | ||
return json_string | ||
return json_string | ||
|
||
@classmethod | ||
def preprocess_json_input(cls, 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 | ||
|
||
@classmethod | ||
def extract_json_section(cls, input_str: str = ""): | ||
try: | ||
first_brace_index = input_str.index("{") | ||
final_json = input_str[first_brace_index:] | ||
last_brace_index = final_json.rindex("}") | ||
final_json = final_json[: last_brace_index + 1] | ||
return final_json | ||
except ValueError: | ||
pass | ||
return input_str | ||
|
||
@classmethod | ||
def remove_escape_sequences(cls, string): | ||
return string.encode('utf-8').decode('unicode_escape') | ||
|
||
@classmethod | ||
def add_quotes_to_property_names(cls, json_string: str) -> str: | ||
def replace(match: re.Match) -> str: | ||
return f'"{match.group(1)}":' | ||
|
||
json_string = re.sub(r'(\b\w+\b):', replace, json_string) | ||
|
||
return json_string | ||
|
||
@classmethod | ||
def balance_braces(cls, json_string: str) -> str: | ||
open_braces_count = json_string.count('{') | ||
closed_braces_count = json_string.count('}') | ||
|
||
while closed_braces_count > open_braces_count: | ||
json_string = json_string.rstrip("}") | ||
closed_braces_count -= 1 | ||
|
||
open_braces_count = json_string.count('{') | ||
closed_braces_count = json_string.count('}') | ||
|
||
if open_braces_count > closed_braces_count: | ||
json_string += '}' * (open_braces_count - closed_braces_count) | ||
|
||
return json_string |
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
Oops, something went wrong.