"<a href=\"https://colab.research.google.com/github/meta-llama/llama-recipes/blob/main/recipes/use_cases/text2sql/StructuredLlama.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a> \n",
"<a href=\"https://colab.research.google.com/github/meta-llama/llama-recipes/blob/main/recipes/use_cases/coding/text2sql/quickstart.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a> \n",
"\n",
"\n",
"## Use Llama 3 to chat about structured data\n",
"## Quick Demo of Text2SQL Using Llama 3.3\n",
"This demo shows how to use LangChain with Llama 3 to query structured data, the 2023-24 NBA roster info, stored in a SQLite DB, and how to ask Llama 3 follow up question about the DB."
]
},
{
"cell_type": "markdown",
"id": "f839d07d",
"metadata": {},
"source": [
"We start by installing the necessary packages:\n",
"- [Replicate](https://replicate.com/) to host the Llama 3 model\n",
"- [langchain](https://python.langchain.com/docs/get_started/introduction) provides necessary RAG tools for this demo\n",
"\n",
"\n",
"**Note** We will be using [Replicate](https://replicate.com/meta/meta-llama-3-8b-instruct) to run the examples here. You will need to first sign in with Replicate with your github account, then create a free API token [here](https://replicate.com/account/api-tokens) that you can use for a while. You can also use other Llama 3 cloud providers such as [Groq](https://console.groq.com/), [Together](https://api.together.xyz/playground/language/meta-llama/Llama-3-8b-hf), or [Anyscale](https://app.endpoints.anyscale.com/playground) - see Section 2 of the Getting to Know Llama [notebook](https://github.com/meta-llama/llama-recipes/blob/main/recipes/quickstart/Getting_to_know_Llama.ipynb) for more information.\n",
"This demo shows how to use Llama 3.3 to answer questions about a SQLite DB. \n",
"\n",
"\n",
"If you'd like to run Llama 3 locally for the benefits of privacy, no cost or no rate limit (some Llama 3 hosting providers set limits for free plan of queries or tokens per second or minute), see [Running Llama Locally](https://github.com/meta-llama/llama-recipes/blob/main/recipes/quickstart/Running_Llama2_Anywhere/Running_Llama_on_Mac_Windows_Linux.ipynb)."
"We'll use LangChain and the Llama cloud provider [Together.ai](https://api.together.ai/), where you can easily get a free API key (or you can use any other Llama cloud provider or even Ollama running Llama locally - see [here](https://github.com/meta-llama/llama-recipes/tree/main/recipes/quickstart) for examples)."
"Next we call the Llama 3 8b chat model from Replicate. You can also use Llama 3 70b model by replacing the `model` name with \"meta/meta-llama-3-70b-instruct\"."
"To recreate the `nba_roster.db` file, run the two commands below:\n",
"- `python txt2csv.py` to convert the `nba.txt` file to `nba_roster.csv`. The `nba.txt` file was created by scraping the NBA roster info from the web.\n",
"- `python csv2db.py` to convert `nba_roster.csv` to `nba_roster.db`."
"To recreate the `nba_roster.db` file, run the two commands below:\n",
"- `python txt2csv.py` to convert the `nba.txt` file to `nba_roster.csv`. The `nba.txt` file was created by scraping the NBA roster info from the web.\n",
"- `python csv2db.py` to convert `nba_roster.csv` to `nba_roster.db`."
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "3bb99f39-cd7a-4db6-91dd-02f3bf80347c",
"id": "3bb99f39-cd7a-4db6-91dd-02f3bf80347c",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [],
...
@@ -99,12 +81,38 @@
...
@@ -99,12 +81,38 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "8d793ce7-324b-4861-926c-54973d7c9b43",
"id": "8d793ce7-324b-4861-926c-54973d7c9b43",
"metadata": {},
"metadata": {},
"outputs": [],
"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"
]
}
],
"source": [
"source": [
"question = \"What team is Klay Thompson 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",
"\n",
"\n",
"Scheme:\n",
"Scheme:\n",
...
@@ -119,12 +127,20 @@
...
@@ -119,12 +127,20 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "70776558",
"id": "70776558",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SELECT Team FROM nba_roster WHERE NAME = 'Stephen Curry'\n"
]
}
],
"source": [
"source": [
"answer = llm.invoke(prompt)\n",
"answer = llm.invoke(prompt).content\n",
"print(answer)"
"print(answer)"
]
]
},
},
...
@@ -133,30 +149,50 @@
...
@@ -133,30 +149,50 @@
"id": "afcf423a",
"id": "afcf423a",
"metadata": {},
"metadata": {},
"source": [
"source": [
"If you don't have the \"just return the SQL query and nothing else\" in the prompt above, or even with it but asking Llama 2 which doesn't follow instructions as well as Llama 3, you'll likely get more text other than the SQL query back in the answer."
"***Note:*** If you don't have the \"just return the SQL query and nothing else\" in the prompt above, you'll likely get more text other than the SQL query back in the answer, making some extra post-processing necessary before running the db query below."
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "62472ce6-794b-4a61-b88c-a1e031e28e4e",
"id": "62472ce6-794b-4a61-b88c-a1e031e28e4e",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"\"[('Golden State Warriors',)]\""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"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)"
"result = db.run(answer)\n",
"result"
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "39ed4bc3",
"id": "39ed4bc3",
"metadata": {},
"metadata": {},
"outputs": [],
"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"
]
}
],
"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",
"print(llm.invoke(follow_up))"
"print(llm.invoke(follow_up).content)"
]
]
},
},
{
{
...
@@ -164,17 +200,44 @@
...
@@ -164,17 +200,44 @@
"id": "98b2c523",
"id": "98b2c523",
"metadata": {},
"metadata": {},
"source": [
"source": [
"Since we did not pass any context along with the follow-up to the model it did not know who \"his\" is and just picked LeBron James.\n",
"Since we did not pass any context along with the follow-up to Llama, it doesn't know the answer. Let's try to fix it by adding context to the follow-up prompt."
"\n",
"Let's try to fix it by adding context to the follow-up prompt."
]
]
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"id": "0c305278-29d2-4e88-9b3d-ad67c94ce0f2",
"id": "0c305278-29d2-4e88-9b3d-ad67c94ce0f2",
"metadata": {},
"metadata": {},
"outputs": [],
"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"
]
}
],
"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",
...
@@ -192,12 +255,20 @@
...
@@ -192,12 +255,20 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "03739b96-e607-4fa9-bc5c-df118198dc7f",
"id": "03739b96-e607-4fa9-bc5c-df118198dc7f",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SELECT SALARY FROM nba_roster WHERE NAME = \"Stephen Curry\"\n"
]
}
],
"source": [
"source": [
"new_answer = llm.invoke(prompt)\n",
"new_answer = llm.invoke(prompt).content\n",
"print(new_answer)"
"print(new_answer)"
]
]
},
},
...
@@ -211,13 +282,32 @@
...
@@ -211,13 +282,32 @@
},
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "6ecfca53-be7e-4668-bad1-5ca7571817d7",
"id": "6ecfca53-be7e-4668-bad1-5ca7571817d7",
"metadata": {},
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"\"[('$51,915,615',)]\""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"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": {
...
@@ -236,7 +326,7 @@
...
@@ -236,7 +326,7 @@
"name": "python",
"name": "python",
"nbconvert_exporter": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.10.14"
}
}
},
},
"nbformat": 4,
"nbformat": 4,
...
...
%% Cell type:markdown id:e8cba0b6 tags:
%% Cell type:markdown id:e8cba0b6 tags:
<ahref="https://colab.research.google.com/github/meta-llama/llama-recipes/blob/main/recipes/use_cases/text2sql/StructuredLlama.ipynb"target="_parent"><imgsrc="https://colab.research.google.com/assets/colab-badge.svg"alt="Open In Colab"/></a>
<ahref="https://colab.research.google.com/github/meta-llama/llama-recipes/blob/main/recipes/use_cases/coding/text2sql/quickstart.ipynb"target="_parent"><imgsrc="https://colab.research.google.com/assets/colab-badge.svg"alt="Open In Colab"/></a>
## Use Llama 3 to chat about structured data
## Quick Demo of Text2SQL Using Llama 3.3
This demo shows how to use LangChain with Llama 3 to query structured data, the 2023-24 NBA roster info, stored in a SQLite DB, and how to ask Llama 3 follow up question about the DB.
%% Cell type:markdown id:f839d07d tags:
This demo shows how to use Llama 3.3 to answer questions about a SQLite DB.
We start by installing the necessary packages:
We'll use LangChain and the Llama cloud provider [Together.ai](https://api.together.ai/), where you can easily get a free API key (or you can use any other Llama cloud provider or even Ollama running Llama locally - see [here](https://github.com/meta-llama/llama-recipes/tree/main/recipes/quickstart) for examples).
-[Replicate](https://replicate.com/) to host the Llama 3 model
-[langchain](https://python.langchain.com/docs/get_started/introduction) provides necessary RAG tools for this demo
**Note** We will be using [Replicate](https://replicate.com/meta/meta-llama-3-8b-instruct) to run the examples here. You will need to first sign in with Replicate with your github account, then create a free API token [here](https://replicate.com/account/api-tokens) that you can use for a while. You can also use other Llama 3 cloud providers such as [Groq](https://console.groq.com/), [Together](https://api.together.xyz/playground/language/meta-llama/Llama-3-8b-hf), or [Anyscale](https://app.endpoints.anyscale.com/playground) - see Section 2 of the Getting to Know Llama [notebook](https://github.com/meta-llama/llama-recipes/blob/main/recipes/quickstart/Getting_to_know_Llama.ipynb) for more information.
If you'd like to run Llama 3 locally for the benefits of privacy, no cost or no rate limit (some Llama 3 hosting providers set limits for free plan of queries or tokens per second or minute), see [Running Llama Locally](https://github.com/meta-llama/llama-recipes/blob/main/recipes/quickstart/Running_Llama2_Anywhere/Running_Llama_on_Mac_Windows_Linux.ipynb).
Next we call the Llama 3 8b chat model from Replicate. You can also use Llama 3 70b model by replacing the `model` name with "meta/meta-llama-3-70b-instruct".
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.
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.
Scheme:
Scheme:
{get_schema()}
{get_schema()}
Question: {question}
Question: {question}
SQL Query:"""
SQL Query:"""
print(prompt)
print(prompt)
```
```
%% Output
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.
Scheme:
CREATE TABLE nba_roster (
"Team" TEXT,
"NAME" TEXT,
"Jersey" TEXT,
"POS" TEXT,
"AGE" INTEGER,
"HT" TEXT,
"WT" TEXT,
"COLLEGE" TEXT,
"SALARY" TEXT
)
Question: What team is Stephen Curry on?
SQL Query:
%% Cell type:code id:70776558 tags:
%% Cell type:code id:70776558 tags:
``` python
``` python
answer=llm.invoke(prompt)
answer=llm.invoke(prompt).content
print(answer)
print(answer)
```
```
%% Output
SELECT Team FROM nba_roster WHERE NAME = 'Stephen Curry'
%% Cell type:markdown id:afcf423a tags:
%% Cell type:markdown id:afcf423a tags:
If you don't have the "just return the SQL query and nothing else" in the prompt above, or even with it but asking Llama 2 which doesn't follow instructions as well as Llama 3, you'll likely get more text other than the SQL query back in the answer.
***Note:***If you don't have the "just return the SQL query and nothing else" in the prompt above, you'll likely get more text other than the SQL query back in the answer, making some extra post-processing necessary before running the db query below.
# note this is a dangerous operation and for demo purpose only; in production app you'll need to safe-guard any DB operation
# note this is a dangerous operation and for demo purpose only; in production app you'll need to safe-guard any DB operation
result=db.run(answer)
result=db.run(answer)
result
```
```
%% Output
"[('Golden State Warriors',)]"
%% Cell type:code id:39ed4bc3 tags:
%% Cell type:code id:39ed4bc3 tags:
``` python
``` python
# how about a follow up question
# how about a follow up question
follow_up="What's his salary?"
follow_up="What's his salary?"
print(llm.invoke(follow_up))
print(llm.invoke(follow_up).content)
```
```
%% Cell type:markdown id:98b2c523 tags:
%% Output
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?
Since we did not pass any context along with the follow-up to the model it did not know who "his" is and just picked LeBron James.
%% Cell type:markdown id:98b2c523 tags:
Let's try to fix it by adding context to the follow-up prompt.
Since we did not pass any context along with the follow-up to Llama, it doesn't know the answer. Let's try to fix it by adding context to the follow-up prompt.
Because we have "be concise, just output the SQL response", Llama 3 is able to just generate the SQL statement; otherwise output parsing will be needed.
Because we have "be concise, just output the SQL response", Llama 3 is able to just generate the SQL statement; otherwise output parsing will be needed.