Skip to content

Commit

Permalink
Merge pull request #377 from Aymane11/patch-1
Browse files Browse the repository at this point in the history
Swap SELECT and WITH order in extract_sql
  • Loading branch information
zainhoda authored Apr 22, 2024
2 parents 033205d + 1bec99a commit 89fd980
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/vanna/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@ def generate_sql(self, question: str, **kwargs) -> str:
return self.extract_sql(llm_response)

def extract_sql(self, llm_response: str) -> str:
# If the llm_response contains a CTE (with clause), extract the sql bewteen WITH and ;
sql = re.search(r"WITH.*?;", llm_response, re.DOTALL)
if sql:
self.log(f"Output from LLM: {llm_response} \nExtracted SQL: {sql.group(0)}")
return sql.group(0)
# If the llm_response is not markdown formatted, extract sql by finding select and ; in the response
sql = re.search(r"SELECT.*?;", llm_response, re.DOTALL)
if sql:
self.log(f"Output from LLM: {llm_response} \nExtracted SQL: {sql.group(0)}"
)
return sql.group(0)

# If the llm_response contains a CTE (with clause), extract the sql bewteen WITH and ;
sql = re.search(r"WITH.*?;", llm_response, re.DOTALL)
if sql:
self.log(f"Output from LLM: {llm_response} \nExtracted SQL: {sql.group(0)}")
return sql.group(0)
# If the llm_response contains a markdown code block, with or without the sql tag, extract the sql from it
sql = re.search(r"```sql\n(.*)```", llm_response, re.DOTALL)
if sql:
Expand Down

0 comments on commit 89fd980

Please sign in to comment.