Added a SQL-Validity Check

This commit is contained in:
crennis 2023-05-04 11:08:59 +02:00
parent 2a2cb0d507
commit feb8530438

View File

@ -19,10 +19,12 @@ class AI:
) )
response = response['choices'][0]['message']['content'] response = response['choices'][0]['message']['content']
self.convertlog.append({"role": "system", "content": response}) self.convertlog.append({"role": "system", "content": response})
for i in self.convertlog:
print(i)
if self.validate(response):
return response return response
else:
self.humantosql("Only answer as a Valid SQL-Statement don't add any additional text", dbtype, tableschema)
def decide(self, sql: str) -> str: def decide(self, sql: str) -> str:
prompt = [{"role": "system", "content": "You have to decide which function it should use. Answer with [FETCHALL] to fetch all, [FETCHONE] to fetch only one, [FETCHMANY=N] to fetchmany with N being the range, [EXECUTE] to just execute, [EXECUTEMANY=N] to execute many with N being the range"}, prompt = [{"role": "system", "content": "You have to decide which function it should use. Answer with [FETCHALL] to fetch all, [FETCHONE] to fetch only one, [FETCHMANY=N] to fetchmany with N being the range, [EXECUTE] to just execute, [EXECUTEMANY=N] to execute many with N being the range"},
@ -36,3 +38,17 @@ class AI:
response = response['choices'][0]['message']['content'] response = response['choices'][0]['message']['content']
return response return response
def validate(self, sql: str) -> bool:
prompt = [{"role": "system", "content": "You check if the User Input is a valid SQL-Statement and only return True or False. Most important: ONLY ANSWER True OR False."},
{"role": "user", "content": sql}]
response = openai.ChatCompletion.create(
model="gpt-4",
messages=prompt
)
response = response['choices'][0]['message']['content']
if response == "True":
return True
else:
return False