|
@@ -5,8 +5,6 @@
|
|
|
"id": "e8cba0b6",
|
|
"id": "e8cba0b6",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
"source": [
|
|
"source": [
|
|
|
- "<a href=\"https://colab.research.google.com/github/meta-llama/llama-cookbook/blob/main/end-to-end-use-cases/coding/text2sql/quickstart/quickstart.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a> \n",
|
|
|
|
|
- "\n",
|
|
|
|
|
"## Quick Demo of Text2SQL Using Llama 3.3\n",
|
|
"## Quick Demo of Text2SQL Using Llama 3.3\n",
|
|
|
"\n",
|
|
"\n",
|
|
|
"This demo shows how to use Llama 3.3 to answer questions about a SQLite DB. \n",
|
|
"This demo shows how to use Llama 3.3 to answer questions about a SQLite DB. \n",
|
|
@@ -26,7 +24,7 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 2,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "fa4562d3",
|
|
"id": "fa4562d3",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
"outputs": [],
|
|
"outputs": [],
|
|
@@ -65,7 +63,7 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 4,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "3bb99f39-cd7a-4db6-91dd-02f3bf80347c",
|
|
"id": "3bb99f39-cd7a-4db6-91dd-02f3bf80347c",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
"outputs": [],
|
|
"outputs": [],
|
|
@@ -81,36 +79,10 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 5,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "8d793ce7-324b-4861-926c-54973d7c9b43",
|
|
"id": "8d793ce7-324b-4861-926c-54973d7c9b43",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
- "outputs": [
|
|
|
|
|
- {
|
|
|
|
|
- "name": "stdout",
|
|
|
|
|
- "output_type": "stream",
|
|
|
|
|
- "text": [
|
|
|
|
|
- "Based on the table schema below, write a SQL query that would answer the user's question; just return the SQL query and nothing else.\n",
|
|
|
|
|
- "\n",
|
|
|
|
|
- "Scheme:\n",
|
|
|
|
|
- "\n",
|
|
|
|
|
- "CREATE TABLE nba_roster (\n",
|
|
|
|
|
- "\t\"Team\" TEXT, \n",
|
|
|
|
|
- "\t\"NAME\" TEXT, \n",
|
|
|
|
|
- "\t\"Jersey\" TEXT, \n",
|
|
|
|
|
- "\t\"POS\" TEXT, \n",
|
|
|
|
|
- "\t\"AGE\" INTEGER, \n",
|
|
|
|
|
- "\t\"HT\" TEXT, \n",
|
|
|
|
|
- "\t\"WT\" TEXT, \n",
|
|
|
|
|
- "\t\"COLLEGE\" TEXT, \n",
|
|
|
|
|
- "\t\"SALARY\" TEXT\n",
|
|
|
|
|
- ")\n",
|
|
|
|
|
- "\n",
|
|
|
|
|
- "Question: What team is Stephen Curry on?\n",
|
|
|
|
|
- "\n",
|
|
|
|
|
- "SQL Query:\n"
|
|
|
|
|
- ]
|
|
|
|
|
- }
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ "outputs": [],
|
|
|
"source": [
|
|
"source": [
|
|
|
"question = \"What team is Stephen Curry on?\"\n",
|
|
"question = \"What team is Stephen Curry on?\"\n",
|
|
|
"prompt = f\"\"\"Based on the table schema below, write a SQL query that would answer the user's question; just return the SQL query and nothing else.\n",
|
|
"prompt = f\"\"\"Based on the table schema below, write a SQL query that would answer the user's question; just return the SQL query and nothing else.\n",
|
|
@@ -127,18 +99,10 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 6,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "70776558",
|
|
"id": "70776558",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
- "outputs": [
|
|
|
|
|
- {
|
|
|
|
|
- "name": "stdout",
|
|
|
|
|
- "output_type": "stream",
|
|
|
|
|
- "text": [
|
|
|
|
|
- "SELECT Team FROM nba_roster WHERE NAME = 'Stephen Curry'\n"
|
|
|
|
|
- ]
|
|
|
|
|
- }
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ "outputs": [],
|
|
|
"source": [
|
|
"source": [
|
|
|
"answer = llm.invoke(prompt).content\n",
|
|
"answer = llm.invoke(prompt).content\n",
|
|
|
"print(answer)"
|
|
"print(answer)"
|
|
@@ -154,21 +118,10 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 7,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "62472ce6-794b-4a61-b88c-a1e031e28e4e",
|
|
"id": "62472ce6-794b-4a61-b88c-a1e031e28e4e",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
- "outputs": [
|
|
|
|
|
- {
|
|
|
|
|
- "data": {
|
|
|
|
|
- "text/plain": [
|
|
|
|
|
- "\"[('Golden State Warriors',)]\""
|
|
|
|
|
- ]
|
|
|
|
|
- },
|
|
|
|
|
- "execution_count": 7,
|
|
|
|
|
- "metadata": {},
|
|
|
|
|
- "output_type": "execute_result"
|
|
|
|
|
- }
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ "outputs": [],
|
|
|
"source": [
|
|
"source": [
|
|
|
"# note this is a dangerous operation and for demo purpose only; in production app you'll need to safe-guard any DB operation\n",
|
|
"# note this is a dangerous operation and for demo purpose only; in production app you'll need to safe-guard any DB operation\n",
|
|
|
"result = db.run(answer)\n",
|
|
"result = db.run(answer)\n",
|
|
@@ -177,18 +130,10 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 8,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "39ed4bc3",
|
|
"id": "39ed4bc3",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
- "outputs": [
|
|
|
|
|
- {
|
|
|
|
|
- "name": "stdout",
|
|
|
|
|
- "output_type": "stream",
|
|
|
|
|
- "text": [
|
|
|
|
|
- "I don't have enough information to determine whose salary you are referring to. Could you please provide more context or specify the person you are asking about?\n"
|
|
|
|
|
- ]
|
|
|
|
|
- }
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ "outputs": [],
|
|
|
"source": [
|
|
"source": [
|
|
|
"# how about a follow up question\n",
|
|
"# how about a follow up question\n",
|
|
|
"follow_up = \"What's his salary?\"\n",
|
|
"follow_up = \"What's his salary?\"\n",
|
|
@@ -205,39 +150,10 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 9,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "0c305278-29d2-4e88-9b3d-ad67c94ce0f2",
|
|
"id": "0c305278-29d2-4e88-9b3d-ad67c94ce0f2",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
- "outputs": [
|
|
|
|
|
- {
|
|
|
|
|
- "name": "stdout",
|
|
|
|
|
- "output_type": "stream",
|
|
|
|
|
- "text": [
|
|
|
|
|
- "Based on the table schema, question, SQL query, and SQL response below, write a new SQL response; be concise, just output the SQL response.\n",
|
|
|
|
|
- "\n",
|
|
|
|
|
- "Scheme:\n",
|
|
|
|
|
- "\n",
|
|
|
|
|
- "CREATE TABLE nba_roster (\n",
|
|
|
|
|
- "\t\"Team\" TEXT, \n",
|
|
|
|
|
- "\t\"NAME\" TEXT, \n",
|
|
|
|
|
- "\t\"Jersey\" TEXT, \n",
|
|
|
|
|
- "\t\"POS\" TEXT, \n",
|
|
|
|
|
- "\t\"AGE\" INTEGER, \n",
|
|
|
|
|
- "\t\"HT\" TEXT, \n",
|
|
|
|
|
- "\t\"WT\" TEXT, \n",
|
|
|
|
|
- "\t\"COLLEGE\" TEXT, \n",
|
|
|
|
|
- "\t\"SALARY\" TEXT\n",
|
|
|
|
|
- ")\n",
|
|
|
|
|
- "\n",
|
|
|
|
|
- "Question: What's his salary?\n",
|
|
|
|
|
- "SQL Query: What team is Stephen Curry on?\n",
|
|
|
|
|
- "SQL Result: [('Golden State Warriors',)]\n",
|
|
|
|
|
- "\n",
|
|
|
|
|
- "New SQL Response:\n",
|
|
|
|
|
- "\n"
|
|
|
|
|
- ]
|
|
|
|
|
- }
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ "outputs": [],
|
|
|
"source": [
|
|
"source": [
|
|
|
"prompt = f\"\"\"Based on the table schema, question, SQL query, and SQL response below, write a new SQL response; be concise, just output the SQL response.\n",
|
|
"prompt = f\"\"\"Based on the table schema, question, SQL query, and SQL response below, write a new SQL response; be concise, just output the SQL response.\n",
|
|
|
"\n",
|
|
"\n",
|
|
@@ -255,18 +171,10 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 10,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "03739b96-e607-4fa9-bc5c-df118198dc7f",
|
|
"id": "03739b96-e607-4fa9-bc5c-df118198dc7f",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
- "outputs": [
|
|
|
|
|
- {
|
|
|
|
|
- "name": "stdout",
|
|
|
|
|
- "output_type": "stream",
|
|
|
|
|
- "text": [
|
|
|
|
|
- "SELECT SALARY FROM nba_roster WHERE NAME = \"Stephen Curry\"\n"
|
|
|
|
|
- ]
|
|
|
|
|
- }
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ "outputs": [],
|
|
|
"source": [
|
|
"source": [
|
|
|
"new_answer = llm.invoke(prompt).content\n",
|
|
"new_answer = llm.invoke(prompt).content\n",
|
|
|
"print(new_answer)"
|
|
"print(new_answer)"
|
|
@@ -282,32 +190,13 @@
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
"cell_type": "code",
|
|
"cell_type": "code",
|
|
|
- "execution_count": 11,
|
|
|
|
|
|
|
+ "execution_count": null,
|
|
|
"id": "6ecfca53-be7e-4668-bad1-5ca7571817d7",
|
|
"id": "6ecfca53-be7e-4668-bad1-5ca7571817d7",
|
|
|
"metadata": {},
|
|
"metadata": {},
|
|
|
- "outputs": [
|
|
|
|
|
- {
|
|
|
|
|
- "data": {
|
|
|
|
|
- "text/plain": [
|
|
|
|
|
- "\"[('$51,915,615',)]\""
|
|
|
|
|
- ]
|
|
|
|
|
- },
|
|
|
|
|
- "execution_count": 11,
|
|
|
|
|
- "metadata": {},
|
|
|
|
|
- "output_type": "execute_result"
|
|
|
|
|
- }
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ "outputs": [],
|
|
|
"source": [
|
|
"source": [
|
|
|
"db.run(new_answer)"
|
|
"db.run(new_answer)"
|
|
|
]
|
|
]
|
|
|
- },
|
|
|
|
|
- {
|
|
|
|
|
- "cell_type": "code",
|
|
|
|
|
- "execution_count": null,
|
|
|
|
|
- "id": "9d79bbb1-e91d-4b56-b6ef-98c94ff414d0",
|
|
|
|
|
- "metadata": {},
|
|
|
|
|
- "outputs": [],
|
|
|
|
|
- "source": []
|
|
|
|
|
}
|
|
}
|
|
|
],
|
|
],
|
|
|
"metadata": {
|
|
"metadata": {
|
|
@@ -331,5 +220,7 @@
|
|
|
"pygments_lexer": "ipython3",
|
|
"pygments_lexer": "ipython3",
|
|
|
"version": "3.10.14"
|
|
"version": "3.10.14"
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
|
|
+ },
|
|
|
|
|
+ "nbformat": 4,
|
|
|
|
|
+ "nbformat_minor": 5
|
|
|
}
|
|
}
|