(DRAFT) Text to sql#6
Conversation
James-Burgess
left a comment
There was a problem hiding this comment.
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?
|
|
||
|
|
||
| class SQLQuery(models.Model): | ||
| chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name='sql_queries') |
There was a problem hiding this comment.
no need to link it to the chat.
| chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name='sql_queries') | ||
| question = models.TextField() | ||
| sql_query = models.TextField() | ||
| results = models.JSONField() |
There was a problem hiding this comment.
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...
| created_at = models.DateTimeField(auto_now_add=True) | ||
|
|
||
|
|
||
| class SQLQuery(models.Model): |
There was a problem hiding this comment.
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
|
|
||
| def execute_sql_query(query): | ||
| # Use your database connection parameters | ||
| conn = psycopg2.connect( |
There was a problem hiding this comment.
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
| return Response({"message": f"Message starred: {message.starred}"}) | ||
|
|
||
| @action(detail=False, methods=['POST']) | ||
| def sql_query(self, request): |
There was a problem hiding this comment.
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.
| Convert the following natural language question into a SQL query: | ||
| "{question}" | ||
|
|
||
| Return only the SQL query, without any additional explanation. |
There was a problem hiding this comment.
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.
Notes:
Initial idea for text to sql...still work in progress.