AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
Merge branch 'master' into dev
Andres Caicedo4ca12e216d84
diff --cc scripts/browse.pyindex 214ccadb02,7eeaaf4d94..c15214e7dd--- a/scripts/browse.py+++ b/scripts/browse.py@@@ -6,8 -6,14 +6,15 @@@ from llm_utils import create_chat_complcfg = Config()def scrape_text(url):+ """Scrape text from a webpage"""- response = requests.get(url, headers=cfg.user_agent_header)+ # Most basic check if the URL is valid:+ if not url.startswith('http'):+ return "Error: Invalid URL"++ try:+ response = requests.get(url, headers=cfg.user_agent_header)+ except requests.exceptions.RequestException as e:+ return "Error: " + str(e)# Check if the response contains an HTTP errorif response.status_code >= 400:diff --cc scripts/call_ai_function.pyindex 82cf1a76dc,db1c955683..f823865869--- a/scripts/call_ai_function.py+++ b/scripts/call_ai_function.py@@@ -3,10 -2,12 +3,12 @@@ from config import Conficfg = Config()from llm_utils import create_chat_completion-# This is a magic function that can do anything with no-code. See# https://github.com/Torantulino/AI-Functions for more info.- def call_ai_function(function, args, description, model=cfg.smart_llm_model):- """Call an AI function with the given args and description."""+ def call_ai_function(function, args, description, model=None):++ """Call an AI function"""+ if model is None:+ model = cfg.smart_llm_model# For each arg, if any are None, convert to "None":args = [str(arg) if arg is not None else "None" for arg in args]# parse args to comma seperated stringdiff --cc scripts/json_parser.pyindex 38df107c27,6a5f073f3d..07de530cab--- a/scripts/json_parser.py+++ b/scripts/json_parser.py@@@ -21,37 -22,56 +22,58 @@@ JSON_SCHEMA = """criticism": "constructive self-criticism","speak": "thoughts summary to say to user"}- }- """+ }+ """+-def fix_and_parse_json(++def fix_and_parse_json(+ json_str: str,+ try_to_fix_with_gpt: bool = True+ ) -> Union[str, Dict[Any, Any]]:++ """Fix and parse JSON string"""try:json_str = json_str.replace('\t', '')return json.loads(json_str)- except Exception as e:- # Let's do something manually - sometimes GPT responds with something BEFORE the braces:- # "I'm sorry, I don't understand. Please try again."{"text": "I'm sorry, I don't understand. Please try again.", "confidence": 0.0}- # So let's try to find the first brace and then parse the rest of the string+ except json.JSONDecodeError as _: # noqa: F841+ json_str = correct_json(json_str)try:- brace_index = json_str.index("{")- json_str = json_str[brace_index:]- last_brace_index = json_str.rindex("}")- json_str = json_str[:last_brace_index+1]- return json.loads(json_str)- except Exception as e:- if try_to_fix_with_gpt:- print(f"Warning: Failed to parse AI output, attempting to fix.\n If you see this warning frequently, it's likely that your prompt is confusing the AI. Try changing it up slightly.")+ return json.loads(json_str)+ except json.JSONDecodeError as _: # noqa: F841+ pass+ # Let's do something manually:+ # sometimes GPT responds with something BEFORE the braces:+ # "I'm sorry, I don't understand. Please try again."+ # {"text": "I'm sorry, I don't understand. Please try again.",+ # "confidence": 0.0}+ # So let's try to find the first brace and then parse the rest+ # of the string+ try:+ brace_index = json_str.index("{")+ json_str = json_str[brace_index:]+ last_brace_index = json_str.rindex("}")+ json_str = json_str[:last_brace_index+1]+ return json.loads(json_str)+ except json.JSONDecodeError as e: # noqa: F841+ if try_to_fix_with_gpt:+ print("Warning: Failed to parse AI output, attempting to fix."+ "\n If you see this warning frequently, it's likely that"+ " your prompt is confusing the AI. Try changing it up"+ " slightly.")# Now try to fix this up using the ai_functions- ai_fixed_json = fix_json(json_str, json_schema, cfg.debug)+ ai_fixed_json = fix_json(json_str, JSON_SCHEMA, cfg.debug)if ai_fixed_json != "failed":- return json.loads(ai_fixed_json)+ return json.loads(ai_fixed_json)else:- print(f"Failed to fix ai output, telling the AI.") # This allows the AI to react to the error message, which usually results in it correcting its ways.- return json_str- else:+ # This allows the AI to react to the error message,+ # which usually results in it correcting its ways.+ print("Failed to fix ai output, telling the AI.")+ return json_str+ else:raise e-++def fix_json(json_str: str, schema: str, debug=False) -> str:+ """Fix the given JSON string to make it parseable and fully complient with the provided schema."""# Try to fix the JSON using gpt:function_string = "def fix_json(json_str: str, schema:str=None) -> str:"args = [f"'''{json_str}'''", f"'''{schema}'''"]@@@ -69,11 -93,10 +95,11 @@@print("-----------")print(f"Fixed JSON: {result_string}")print("----------- END OF FIX ATTEMPT ----------------")+try:- json.loads(result_string) # just check the validity+ json.loads(result_string) # just check the validityreturn result_string- except:+ except: # noqa: E722# Get the call stack:# import traceback# call_stack = traceback.format_exc()