Skip to content

(DRAFT) Text to sql#6

Open
DoomkingZERO wants to merge 1 commit intomasterfrom
text_to_sql
Open

(DRAFT) Text to sql#6
DoomkingZERO wants to merge 1 commit intomasterfrom
text_to_sql

Conversation

@DoomkingZERO
Copy link
Copy Markdown
Contributor

Notes:

Initial idea for text to sql...still work in progress.

Copy link
Copy Markdown
Contributor

@James-Burgess James-Burgess left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like a nice start, but also seems to be in the wrong place.

We are better off making a new app and now mixing it in with our chat stuff.
python manage.py startapp db_query

Then move all your code into there.

And before you get ahead of yourself. Have you tested any of these queries on our chat yet?

Comment thread chat/models.py


class SQLQuery(models.Model):
chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name='sql_queries')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to link it to the chat.

Comment thread chat/models.py
chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name='sql_queries')
question = models.TextField()
sql_query = models.TextField()
results = models.JSONField()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we really wanna keep the results in the db... that could end up being very large.
Maybe just some meta of the query instead, result size, execution time...

Comment thread chat/models.py
created_at = models.DateTimeField(auto_now_add=True)


class SQLQuery(models.Model):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also create a model for the DB connection.
That way a user can configure a new connection from the UI, set up the urls and create a schema.
Then you can save the schema in the db and use it when making the query

Comment thread chat/utils.py

def execute_sql_query(query):
# Use your database connection parameters
conn = psycopg2.connect(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be connecting to an external db, not our internal application db.
See connection string for the demo db here: https://colab.research.google.com/drive/1288cz1roJtzWLo2ETeVt5meb0XAtXZnR

Comment thread chat/viewsets.py
return Response({"message": f"Message starred: {message.starred}"})

@action(detail=False, methods=['POST'])
def sql_query(self, request):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start, as I said above, this should be a 2 part process.

Step 1. Load in a db and create the schema for it.
Step 2. Use that schema to build the query.

Big thing here is gonna be getting a nice schema that can express the db arch well.

Comment thread chat/utils.py
Convert the following natural language question into a SQL query:
"{question}"

Return only the SQL query, without any additional explanation.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See how I set the model instructions in the other code.
That should work much better for enfocing SQL only.

Prompt needs some work too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants