| 27 | |
| 28 | @app.route('/<app_name>/<api_call>') |
| 29 | def api(app_name, api_call): |
| 30 | db = json.load(open('db.json','r')) |
| 31 | print("INPUT DB STATE") |
| 32 | print(db[app_name]["state"]) |
| 33 | gpt3_input = f"""{db[app_name]["prompt"]} |
| 34 | API Call (indexes are zero-indexed): |
| 35 | {api_call} |
| 36 | |
| 37 | Database State: |
| 38 | {db[app_name]["state"]} |
| 39 | |
| 40 | Output the API response as json prefixed with '!API response!:'. Then output the new database state as json, prefixed with '!New Database State!:'. If the API call is only requesting data, then don't change the database state, but base your 'API Response' off what's in the database. |
| 41 | """ |
| 42 | completion = gpt3(gpt3_input) |
| 43 | |
| 44 | # parsing "API Response" and "New Database State" with regex |
| 45 | api_response_match = re.search("(?<=!API Response!:).*(?=!New Database State!:)", completion, re.DOTALL) |
| 46 | new_database_match = re.search("(?<=!New Database State!:).*", completion, re.DOTALL) |
| 47 | |
| 48 | # converting regex result into json string |
| 49 | api_response_text = api_response_match.string[api_response_match.regs[0][0]:api_response_match.regs[0][1]].strip() |
| 50 | new_database_text = new_database_match.string[new_database_match.regs[0][0]:new_database_match.regs[0][1]].strip() |
| 51 | |
| 52 | response = json.loads(json.dumps(ast.literal_eval(api_response_text))) |
| 53 | print("RESPONSE") |
| 54 | print(response) |
| 55 | |
| 56 | new_state = json.loads(json.dumps(ast.literal_eval(new_database_text))) |
| 57 | print("NEW_STATE") |
| 58 | print(new_state) |
| 59 | |
| 60 | db[app_name]["state"] = new_state |
| 61 | json.dump(db, open('db.json', 'w'), indent=4, default=dict_to_json) |
| 62 | return response |
| 63 | |
| 64 | if __name__ == "__main__": |
| 65 | app.run() |