Quickstart

llamacodingAIvllmmachine-learningend-to-end-use-casesllama2LLMllama-cookbooktext2sqlPythonfinetuningpytorchlangchain

Open In Colab

Quick Demo of Text2SQL Using Llama 3.3

This demo shows how to use Llama 3.3 to answer questions about a SQLite DB.

We'll use LangChain and the Llama cloud provider 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).

[ ]
[2]

To recreate the nba_roster.db file, run the two commands below:

  • 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.
  • python csv2db.py to convert nba_roster.csv to nba_roster.db.
[ ]
[4]
[5]
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:
[6]
SELECT Team FROM nba_roster WHERE NAME = 'Stephen Curry'

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.

[7]
"[('Golden State Warriors',)]"
[8]
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 Llama, it doesn't know the answer. Let's try to fix it by adding context to the follow-up prompt.

[9]
Based on the table schema, question, SQL query, and SQL response below, write a new SQL response; be concise, just output the SQL response.

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's his salary?
SQL Query: What team is Stephen Curry on?
SQL Result: [('Golden State Warriors',)]

New SQL Response:

[10]
SELECT SALARY FROM nba_roster WHERE NAME = "Stephen Curry"

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.

[11]
"[('$51,915,615',)]"
[ ]