Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI queries (generate query based on AI request and db schema) #118

Open
delfrrr opened this issue May 27, 2023 · 4 comments
Open

AI queries (generate query based on AI request and db schema) #118

delfrrr opened this issue May 27, 2023 · 4 comments

Comments

@delfrrr
Copy link
Contributor

delfrrr commented May 27, 2023

No description provided.

@Tsovak
Copy link
Contributor

Tsovak commented Oct 4, 2023

@delfrrr is it related to #114 or #113? if yes, should we take 114,113 first?

@delfrrr
Copy link
Contributor Author

delfrrr commented Oct 5, 2023

@Tsovak it's unrelated. It's more like Copilot Chat.

@Tsovak
Copy link
Contributor

Tsovak commented Jan 31, 2024

https://vanna.ai/docs/ train a RAG "model" on your data, and then ask questions which will return SQL queries that can be set up to automatically run on your database.

@Tsovak
Copy link
Contributor

Tsovak commented Feb 7, 2024

Azure provides a Complition "Natural language to SQL". One of approach is to use Azure AI service.

Input:

### Postgres SQL tables, with their properties:
#
# Employee(id, name, department_id)
# Department(id, name, address)
# Salary_Payments(id, employee_id, amount, date)
#
### A query to list the names of the departments which employed more than 10 employees in the last 3 months

SELECT

the result is

SELECT Department.name
FROM Department
WHERE Department.id IN (
    SELECT Employee.department_id
    FROM Employee
    WHERE Employee.id IN (
        SELECT Salary_Payments.employee_id
        FROM Salary_Payments
        WHERE Salary_Payments.date >= NOW() - INTERVAL '3 months'
        GROUP BY Salary_Payments.employee_id
        HAVING COUNT(*) > 10
    )
)
#Note: The openai-python library support for Azure OpenAI is in preview.
import os
import openai
openai.api_type = "azure"
openai.api_base = "https://<your-env>.openai.azure.com/"
openai.api_version = "2023-09-15-preview"
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  engine="gpt-35-turbo",
  prompt="### Postgres SQL tables, with their properties:\n#\n# Employee(id, name, department_id)\n# Department(id, name, address)\n# Salary_Payments(id, employee_id, amount, date)\n#\n### A query to list the names of the departments which employed more than 10 employees in the last 3 months\n\nSELECT Department.name\nFROM Department\nWHERE Department.id IN (\n    SELECT Employee.department_id\n    FROM Employee\n    WHERE Employee.id IN (\n        SELECT Salary_Payments.employee_id\n        FROM Salary_Payments\n        WHERE Salary_Payments.date >= NOW() - INTERVAL '3 months'\n        GROUP BY Salary_Payments.employee_id\n        HAVING COUNT(*) > 10\n    )\n)",
  temperature=0,
  max_tokens=150,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0,
  stop=["#",";"])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🌱New
Development

No branches or pull requests

2 participants