quick_start/legacy/03_code_generation.ipynb (132 lines of code) (raw):
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"### 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",
"\n",
" query: \n",
"Here's a query to list the names of the departments which employed more than 10 employees in the last 3 months:\n",
"\n",
"```sql\n",
"SELECT d.name AS department_name\n",
"FROM Department d\n",
"JOIN Employee e ON d.id = e.department_id\n",
"JOIN Salary_Payments sp ON e.id = sp.employee_id\n",
"WHERE sp.date >= current_date - interval '3 months'\n",
"GROUP BY d.id, d.name\n",
"HAVING COUNT(DISTINCT e.id) > 10\n"
]
}
],
"source": [
"import openai\n",
"import re\n",
"import requests\n",
"import sys\n",
"import os\n",
"from dotenv import load_dotenv\n",
"load_dotenv()\n",
"\n",
"openai.api_type = \"azure\"\n",
"openai.api_version = os.getenv(\"OPENAI_API_VERSION\")\n",
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
"openai.api_base = os.getenv(\"OPENAI_API_BASE\")\n",
"\n",
"CHAT_COMPLETIONS_MODEL = os.getenv('CHAT_COMPLETION_NAME')\n",
"\n",
"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\\n query: \"\n",
"print(prompt)\n",
"response = openai.ChatCompletion.create(\n",
" engine=CHAT_COMPLETIONS_MODEL,\n",
" messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n",
" {\"role\":\"user\",\"content\": prompt}],\n",
" temperature=0,\n",
" max_tokens=150,\n",
" top_p=1,\n",
" frequency_penalty=0,\n",
" presence_penalty=0,\n",
" stop=[\"#\",\";\"])\n",
"print(response['choices'][0]['message']['content'])\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This SQL code retrieves the names of departments that have more than 10 employees who have received salary payments within the last 3 months. It achieves this by joining the Department and Employee tables on the department_id and id columns, and then filtering the results based on the salary payments received within the specified time frame. Finally, it groups the results by department name and applies a condition to only include departments with more than 10 employees meeting the criteria.\n"
]
}
],
"source": [
"code = \"Can you explain what does this code do?\\n#\\n# ###\\n\\\n",
" Code:\\n\\\n",
" SELECT d.name FROM Department d JOIN Employee e ON d.id = e.department_id WHERE e.id IN (SELECT employee_id FROM Salary_Payments WHERE date > now() - interval '3 months') GROUP BY d.name HAVING COUNT(*) > 10\\n#\\n#\\\n",
" Answer:\\n# \"\n",
"\n",
"response = openai.ChatCompletion.create(\n",
" engine=CHAT_COMPLETIONS_MODEL,\n",
" messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n",
" {\"role\":\"user\",\"content\": code}],\n",
" temperature=0,\n",
" max_tokens=500,\n",
" top_p=1,\n",
" frequency_penalty=0,\n",
" presence_penalty=0,\n",
" stop=[\"#\",\";\"])\n",
"print(response['choices'][0]['message']['content'])\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "azureml_py310_sdkv2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "2139c70ac98f3202d028164a545621647e07f47fd6f5d8ac55cf952bf7c15ed1"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}