From 5055744ff4dba4a4a837552b4ea25378e857dab0 Mon Sep 17 00:00:00 2001 From: Jeff Tang <jeff.x.tang@gmail.com> Date: Wed, 11 Oct 2023 12:50:12 -0700 Subject: [PATCH] removed system prompt and notebook update StructuredLlama.ipynb --- llama-demo-apps/StructuredLlama.ipynb | 522 +++++--------------------- 1 file changed, 96 insertions(+), 426 deletions(-) diff --git a/llama-demo-apps/StructuredLlama.ipynb b/llama-demo-apps/StructuredLlama.ipynb index ddc3aa29..ff3b69a0 100644 --- a/llama-demo-apps/StructuredLlama.ipynb +++ b/llama-demo-apps/StructuredLlama.ipynb @@ -5,7 +5,7 @@ "id": "e8cba0b6", "metadata": {}, "source": [ - "## This demo shows how to use LangChain's SQLDatabaseChain with Llama2 to chat about structured data stored in a SQL DB. \n", + "## This demo shows how to use LangChain's SQLDatabaseChain with Llama2 to query about structured data stored in a SQL DB. \n", "* As the 2023-24 NBA season is around the corner, we use the NBA roster info saved in a SQLite DB to show you how to ask Llama2 questions about your favorite teams or players. \n", "* Because the SQLDatabaseChain API implementation is still in the langchain_experimental package, you'll see more issues that come with using the cutting edge experimental features, and how we succeed resolving some of the issues but fail on some others." ] @@ -57,18 +57,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "9dcd744c", "metadata": {}, "outputs": [], "source": [ "llama2_13b_chat = \"meta/llama-2-13b-chat:f4e2de70d66816a838a89eeeb621910adffb0dd0baba3976c96980970978018d\"\n", "\n", - "# the reason we set system_prompt below is to ask Llama to generate only the SQL statement, instead of being wordy and adding something like\n", - "# \"Sure! Here's the SQL query for the given input question: \" before the SQL query; otherwise custom parsing will be needed.\n", "llm = Replicate(\n", " model=llama2_13b_chat,\n", - " model_kwargs={\"temperature\": 0.01, \"top_p\": 1, \"max_new_tokens\":500, \"system_prompt\": \"Given an input question, convert it to a SQL query. No pre-amble.\"},\n", + " model_kwargs={\"temperature\": 0.01, \"top_p\": 1, \"max_new_tokens\":500}\n", ")" ] }, @@ -79,18 +77,16 @@ "metadata": {}, "outputs": [], "source": [ + "# to see how the db file was created, check the README file\n", "db = SQLDatabase.from_uri(\"sqlite:///nba_roster.db\", sample_rows_in_table_info= 0)\n", "\n", - "# customize the default sqlite prompt defined in the link above\n", "PROMPT_SUFFIX = \"\"\"\n", "Only use the following tables:\n", "{table_info}\n", "\n", "Question: {input}\"\"\"\n", "\n", - "# if return_sql is set to False, then calling db_chain.run will make two calls to llm: first get the SQL query statement, \n", - "# then pass the result of running the SQL query along with the original question and the query to llm for NL answer\n", - "db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, return_sql=False, \n", + "db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, return_sql=True, \n", " prompt=PromptTemplate(input_variables=[\"input\", \"table_info\"], \n", " template=PROMPT_SUFFIX))" ] @@ -98,7 +94,7 @@ { "cell_type": "code", "execution_count": 5, - "id": "239fb5a6", + "id": "9319b42e-0c04-4b60-95b3-6c7758ba9cf8", "metadata": {}, "outputs": [ { @@ -125,12 +121,12 @@ " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: How many unique teams are there?\\nSQLQuery:\"\n", " ]\n", "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [2.63s] Exiting LLM run with output:\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [3.90s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", - " \"text\": \" SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\",\n", + " \"text\": \" Sure thing! Here's the answer to your question using the provided table structure:\\n\\nTo find out how many unique teams there are in the `nba_roster` table, we can use the `COUNT` function and the `DISTINCT` keyword to count the number of distinct values in the `Team` column.\\n\\nHere's the SQL query:\\n```sql\\nSELECT COUNT(DISTINCT Team) AS num_teams\\nFROM nba_roster;\\n```\\nWhen I run this query, I get the result:\\n\\n| num_teams |\\n| --- |\\n| 30 |\\n\\nThere are 30 unique teams in the `nba_roster` table.\",\n", " \"generation_info\": null\n", " }\n", " ]\n", @@ -138,303 +134,39 @@ " \"llm_output\": null,\n", " \"run\": null\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [2.63s] Exiting Chain run with output:\n", - "\u001b[0m{\n", - " \"text\": \" SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\"\n", - "}\n", - "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 4:chain:LLMChain] Entering Chain run with input:\n", - "\u001b[0m{\n", - " \"input\": \"How many unique teams are there?\\nSQLQuery:SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\\nSQLResult: [(30,)]\\nAnswer:\",\n", - " \"top_k\": \"5\",\n", - " \"dialect\": \"sqlite\",\n", - " \"table_info\": \"\\nCREATE 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", - " \"stop\": [\n", - " \"\\nSQLResult:\"\n", - " ]\n", - "}\n", - "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 4:chain:LLMChain > 5:llm:Replicate] Entering LLM run with input:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [3.90s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"prompts\": [\n", - " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: How many unique teams are there?\\nSQLQuery:SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\\nSQLResult: [(30,)]\\nAnswer:\"\n", - " ]\n", + " \"text\": \" Sure thing! Here's the answer to your question using the provided table structure:\\n\\nTo find out how many unique teams there are in the `nba_roster` table, we can use the `COUNT` function and the `DISTINCT` keyword to count the number of distinct values in the `Team` column.\\n\\nHere's the SQL query:\\n```sql\\nSELECT COUNT(DISTINCT Team) AS num_teams\\nFROM nba_roster;\\n```\\nWhen I run this query, I get the result:\\n\\n| num_teams |\\n| --- |\\n| 30 |\\n\\nThere are 30 unique teams in the `nba_roster` table.\"\n", "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 4:chain:LLMChain > 5:llm:Replicate] [2.06s] Exiting LLM run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [3.90s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"generations\": [\n", - " [\n", - " {\n", - " \"text\": \" Sure! Here's the answer to your question:\\n\\nThere are 30 unique teams in the NBA.\",\n", - " \"generation_info\": null\n", - " }\n", - " ]\n", - " ],\n", - " \"llm_output\": null,\n", - " \"run\": null\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 4:chain:LLMChain] [2.06s] Exiting Chain run with output:\n", - "\u001b[0m{\n", - " \"text\": \" Sure! Here's the answer to your question:\\n\\nThere are 30 unique teams in the NBA.\"\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [4.70s] Exiting Chain run with output:\n", - "\u001b[0m{\n", - " \"result\": \"Sure! Here's the answer to your question:\\n\\nThere are 30 unique teams in the NBA.\"\n", + " \"result\": \"Sure thing! Here's the answer to your question using the provided table structure:\\n\\nTo find out how many unique teams there are in the `nba_roster` table, we can use the `COUNT` function and the `DISTINCT` keyword to count the number of distinct values in the `Team` column.\\n\\nHere's the SQL query:\\n```sql\\nSELECT COUNT(DISTINCT Team) AS num_teams\\nFROM nba_roster;\\n```\\nWhen I run this query, I get the result:\\n\\n| num_teams |\\n| --- |\\n| 30 |\\n\\nThere are 30 unique teams in the `nba_roster` table.\"\n", "}\n" ] + }, + { + "data": { + "text/plain": [ + "\"Sure thing! Here's the answer to your question using the provided table structure:\\n\\nTo find out how many unique teams there are in the `nba_roster` table, we can use the `COUNT` function and the `DISTINCT` keyword to count the number of distinct values in the `Team` column.\\n\\nHere's the SQL query:\\n```sql\\nSELECT COUNT(DISTINCT Team) AS num_teams\\nFROM nba_roster;\\n```\\nWhen I run this query, I get the result:\\n\\n| num_teams |\\n| --- |\\n| 30 |\\n\\nThere are 30 unique teams in the `nba_roster` table.\"" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "# turn on the LangChain debug to see what exactly happens behind the scenes in terms of the inputs and outputs of calling Llama\n", + "# turn on the debug of LangChain so we can see how many calls to Llama are made and exactly what are inputs and outputs\n", "import langchain\n", "langchain.debug = True\n", - "answer = db_chain.run(\"How many unique teams are there?\")\n", "\n", - "# Two Llama calls happen below \n", - "# Note the difference of the first and second inputs for the Llama calls - before \"llm:Replicate] Entering LLM run with input\"\n", - "# About the output, for the first llm call, both \"llm:Replicate] [2.10s] Exiting LLM run with output\" and \n", - "# \"Exiting Chain run with output\" return correctly the text of SQL statement \"SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\"\n", - "# In the second llm call, \"Exiting LLM run with output\" also shows the correct result:\n", - "# \"Sure! Here's the answer to your question:\\n\\nThere are 30 unique teams in the NBA.\"" + "# first question\n", + "db_chain.run(\"How many unique teams are there?\")" ] }, { "cell_type": "code", "execution_count": 6, - "id": "eef91aea", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Sure! Here's the answer to your question:\n", - "\n", - "There are 30 unique teams in the NBA.\n" - ] - } - ], - "source": [ - "# just show the answer without all the debug info \n", - "print(answer)" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "e3b444aa", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] Entering Chain run with input:\n", - "\u001b[0m{\n", - " \"query\": \"Which team is Klay Thompson in?\"\n", - "}\n", - "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] Entering Chain run with input:\n", - "\u001b[0m{\n", - " \"input\": \"Which team is Klay Thompson in?\\nSQLQuery:\",\n", - " \"top_k\": \"5\",\n", - " \"dialect\": \"sqlite\",\n", - " \"table_info\": \"\\nCREATE 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", - " \"stop\": [\n", - " \"\\nSQLResult:\"\n", - " ]\n", - "}\n", - "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] Entering LLM run with input:\n", - "\u001b[0m{\n", - " \"prompts\": [\n", - " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: Which team is Klay Thompson in?\\nSQLQuery:\"\n", - " ]\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [2.13s] Exiting LLM run with output:\n", - "\u001b[0m{\n", - " \"generations\": [\n", - " [\n", - " {\n", - " \"text\": \" SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\",\n", - " \"generation_info\": null\n", - " }\n", - " ]\n", - " ],\n", - " \"llm_output\": null,\n", - " \"run\": null\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [2.14s] Exiting Chain run with output:\n", - "\u001b[0m{\n", - " \"text\": \" SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"\n", - "}\n", - "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 4:chain:LLMChain] Entering Chain run with input:\n", - "\u001b[0m{\n", - " \"input\": \"Which team is Klay Thompson in?\\nSQLQuery:SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\\nSQLResult: [('Golden State Warriors',)]\\nAnswer:\",\n", - " \"top_k\": \"5\",\n", - " \"dialect\": \"sqlite\",\n", - " \"table_info\": \"\\nCREATE 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", - " \"stop\": [\n", - " \"\\nSQLResult:\"\n", - " ]\n", - "}\n", - "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 4:chain:LLMChain > 5:llm:Replicate] Entering LLM run with input:\n", - "\u001b[0m{\n", - " \"prompts\": [\n", - " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: Which team is Klay Thompson in?\\nSQLQuery:SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\\nSQLResult: [('Golden State Warriors',)]\\nAnswer:\"\n", - " ]\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 4:chain:LLMChain > 5:llm:Replicate] [2.68s] Exiting LLM run with output:\n", - "\u001b[0m{\n", - " \"generations\": [\n", - " [\n", - " {\n", - " \"text\": \" Sure! Here's the SQL query for the given input question:\\n\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\",\n", - " \"generation_info\": null\n", - " }\n", - " ]\n", - " ],\n", - " \"llm_output\": null,\n", - " \"run\": null\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 4:chain:LLMChain] [2.68s] Exiting Chain run with output:\n", - "\u001b[0m{\n", - " \"text\": \" Sure! Here's the SQL query for the given input question:\\n\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [4.82s] Exiting Chain run with output:\n", - "\u001b[0m{\n", - " \"result\": \"Sure! Here's the SQL query for the given input question:\\n\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"\n", - "}\n" - ] - } - ], - "source": [ - "# try another question and start seeing why cutting edge may be inconsistent and cut and hurt \n", - "answer = db_chain.run(\"Which team is Klay Thompson in?\")\n", - "\n", - "# the output of the first llm call is correct: SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"\n", - "# the second \"Entering LLM run with input\" also has correct input, containing \"SQLResult: [('Golden State Warriors',)]\"\n", - "# and yet the second \"Exiting Chain run with output\" insists generating the result:\n", - "# \"Sure! Here's the SQL query for the given input question:\\n\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "222169a7", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Sure! Here's the SQL query for the given input question:\n", - "\n", - "SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\n" - ] - } - ], - "source": [ - "# just to see how unbelievable it truly is\n", - "print(answer)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "03d22e2c-24c1-4063-9556-37f48c202a84", - "metadata": {}, - "outputs": [], - "source": [ - "# one way to fix this is to set return_sql to be True, so each db_chain.run call will only make one llm call, and we'll\n", - "# manually process each llm output\n", - "db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True, return_sql=True, \n", - " prompt=PromptTemplate(input_variables=[\"input\", \"table_info\"], \n", - " template=PROMPT_SUFFIX))" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "9319b42e-0c04-4b60-95b3-6c7758ba9cf8", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] Entering Chain run with input:\n", - "\u001b[0m{\n", - " \"query\": \"How many unique teams are there?\"\n", - "}\n", - "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] Entering Chain run with input:\n", - "\u001b[0m{\n", - " \"input\": \"How many unique teams are there?\\nSQLQuery:\",\n", - " \"top_k\": \"5\",\n", - " \"dialect\": \"sqlite\",\n", - " \"table_info\": \"\\nCREATE 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", - " \"stop\": [\n", - " \"\\nSQLResult:\"\n", - " ]\n", - "}\n", - "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] Entering LLM run with input:\n", - "\u001b[0m{\n", - " \"prompts\": [\n", - " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: How many unique teams are there?\\nSQLQuery:\"\n", - " ]\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [2.20s] Exiting LLM run with output:\n", - "\u001b[0m{\n", - " \"generations\": [\n", - " [\n", - " {\n", - " \"text\": \" SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\",\n", - " \"generation_info\": null\n", - " }\n", - " ]\n", - " ],\n", - " \"llm_output\": null,\n", - " \"run\": null\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [2.20s] Exiting Chain run with output:\n", - "\u001b[0m{\n", - " \"text\": \" SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\"\n", - "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [2.20s] Exiting Chain run with output:\n", - "\u001b[0m{\n", - " \"result\": \"SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\"\n", - "}\n", - "SELECT COUNT(DISTINCT Team) AS num_teams FROM nba_roster;\n" - ] - } - ], - "source": [ - "# let's go back to the question that works before to make sure the change above doesn't break it\n", - "# because return_sql is True, and system_prompt is set to \"Given an input question, convert it to a SQL query. No pre-amble.\" when \n", - "# creating the llm instance, the db_chain's run will make just one Llama call and ask it to return the SQL statement only, \n", - "# instead of making two Llama calls and returning the answer it can't do in the above case.\n", - "sql = db_chain.run(\"How many unique teams are there?\")\n", - "print(sql)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "35cba597-5774-472a-a464-41a2eba062ab", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[{'num_teams': 30}]\n" - ] - } - ], - "source": [ - "print(db._execute(sql))\n", - "# the right answer is generated" - ] - }, - { - "cell_type": "code", - "execution_count": 12, "id": "fb38a462-e23a-45b3-8379-7900d07751bf", "metadata": {}, "outputs": [ @@ -462,12 +194,12 @@ " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: Which team is Klay Thompson in?\\nSQLQuery:\"\n", " ]\n", "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [2.11s] Exiting LLM run with output:\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [7.14s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", - " \"text\": \" SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\",\n", + " \"text\": \" Sure thing! I'd be happy to help you with that question. Here's the SQL query to find out which team Klay Thompson is on based on the `nba_roster` table:\\n```sql\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\\n```\\nAnd here's the result:\\n```\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson'\\n -> \\\"Team\\\": \\\"Golden State Warriors\\\"\\n```\\nSo, Klay Thompson is in the Golden State Warriors team!\",\n", " \"generation_info\": null\n", " }\n", " ]\n", @@ -475,46 +207,35 @@ " \"llm_output\": null,\n", " \"run\": null\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [2.11s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [7.14s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"text\": \" SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"\n", + " \"text\": \" Sure thing! I'd be happy to help you with that question. Here's the SQL query to find out which team Klay Thompson is on based on the `nba_roster` table:\\n```sql\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\\n```\\nAnd here's the result:\\n```\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson'\\n -> \\\"Team\\\": \\\"Golden State Warriors\\\"\\n```\\nSo, Klay Thompson is in the Golden State Warriors team!\"\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [2.11s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [7.15s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"result\": \"SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"\n", - "}\n", - "SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\n" + " \"result\": \"Sure thing! I'd be happy to help you with that question. Here's the SQL query to find out which team Klay Thompson is on based on the `nba_roster` table:\\n```sql\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\\n```\\nAnd here's the result:\\n```\\nSELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson'\\n -> \\\"Team\\\": \\\"Golden State Warriors\\\"\\n```\\nSo, Klay Thompson is in the Golden State Warriors team!\"\n", + "}\n" ] - } - ], - "source": [ - "# let's try the query that fails\n", - "sql = db_chain.run(\"Which team is Klay Thompson in?\")\n", - "print(sql)" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "6aef6ae7-b14c-46d6-b4a4-3563abbd3391", - "metadata": {}, - "outputs": [ + }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "[{'Team': 'Golden State Warriors'}]\n" - ] + "data": { + "text/plain": [ + "'Sure thing! I\\'d be happy to help you with that question. Here\\'s the SQL query to find out which team Klay Thompson is on based on the `nba_roster` table:\\n```sql\\nSELECT Team FROM nba_roster WHERE NAME = \\'Klay Thompson\\';\\n```\\nAnd here\\'s the result:\\n```\\nSELECT Team FROM nba_roster WHERE NAME = \\'Klay Thompson\\'\\n -> \"Team\": \"Golden State Warriors\"\\n```\\nSo, Klay Thompson is in the Golden State Warriors team!'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "# the sql result looks good. let's run it to get the answer\n", - "print(db._execute(sql))" + "# let's try another query\n", + "db_chain.run(\"Which team is Klay Thompson in?\")" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 7, "id": "39ed4bc3", "metadata": {}, "outputs": [ @@ -542,12 +263,12 @@ " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: What's his salary?\\nSQLQuery:\"\n", " ]\n", "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [2.73s] Exiting LLM run with output:\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [2.03s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", - " \"text\": \" Sure! Here is the SQL query based on the given question and table structure:\\n\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\",\n", + " \"text\": \" Sure thing! To find out what LeBron James' salary is, we can query the `nba_roster` table like this:\\n```\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\\n```\\nThis will return the salary for LeBron James, which should be around $30 million per year.\",\n", " \"generation_info\": null\n", " }\n", " ]\n", @@ -555,80 +276,42 @@ " \"llm_output\": null,\n", " \"run\": null\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [2.73s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [2.03s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"text\": \" Sure! Here is the SQL query based on the given question and table structure:\\n\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\"\n", + " \"text\": \" Sure thing! To find out what LeBron James' salary is, we can query the `nba_roster` table like this:\\n```\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\\n```\\nThis will return the salary for LeBron James, which should be around $30 million per year.\"\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [2.73s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [2.03s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"result\": \"Sure! Here is the SQL query based on the given question and table structure:\\n\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\"\n", + " \"result\": \"Sure thing! To find out what LeBron James' salary is, we can query the `nba_roster` table like this:\\n```\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\\n```\\nThis will return the salary for LeBron James, which should be around $30 million per year.\"\n", "}\n" ] + }, + { + "data": { + "text/plain": [ + "\"Sure thing! To find out what LeBron James' salary is, we can query the `nba_roster` table like this:\\n```\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\\n```\\nThis will return the salary for LeBron James, which should be around $30 million per year.\"" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ "# how about a follow up question\n", - "sql = db_chain.run(\"What's his salary?\")" + "db_chain.run(\"What's his salary?\")" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 8, "id": "0c305278-29d2-4e88-9b3d-ad67c94ce0f2", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Sure! Here is the SQL query based on the given question and table structure:\n", - "\n", - "SELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\n" - ] - } - ], - "source": [ - "# two issues here: the llm output is wordy again, even though we use the system prompt to ask it not to be; \n", - "# the other issue makes sense: we didn't pass the context along with the follow-up to llm so it didn't know what \"his\" is.\n", - "print(sql)" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "670bc858", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "SELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\n", - "[{'SALARY': '$47,607,350'}]\n" - ] - } - ], - "source": [ - "# let's fix the first issue by custom parsing, which is typical in real LLM apps\n", - "# use regular expression to extract only the SQL SELECT statement\n", - "import re\n", - "\n", - "pattern = r'SELECT[\\s\\S]*?;'\n", - "match = re.search(pattern, sql)\n", - "if match:\n", - " sql = match.group()\n", - " print(sql)\n", - "print(db._execute(sql))" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "id": "0bfa3d34", - "metadata": {}, "outputs": [], "source": [ - "# now let's try to fix the issue that the context (the previous question and answer) was not sent to LLM along with the new question\n", + "# since we didn't pass the context along with the follow-up to llm so it didn't know what \"his\" is and just picked LeBron James\n", + "\n", + "# let's try to fix the issue that the context (the previous question and answer) was not sent to LLM along with the new question\n", "# SQLDatabaseChain.from_llm has a parameter \"memory\" which can be set to a ConversationBufferMemory instance, which looks promising.\n", "from langchain.memory import ConversationBufferMemory\n", "\n", @@ -641,7 +324,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 9, "id": "d12b50e7", "metadata": {}, "outputs": [ @@ -671,12 +354,12 @@ " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: Which team is Klay Thompson in\\nSQLQuery:\"\n", " ]\n", "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [2.11s] Exiting LLM run with output:\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [6.52s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", - " \"text\": \" SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\",\n", + " \"text\": \" Sure thing! Based on the information provided in the `nba_roster` table, Klay Thompson is in the Golden State Warriors. Here's the SQL query to retrieve that information:\\n```sql\\nSELECT * FROM nba_roster WHERE Team = 'Golden State Warriors';\\n```\\nThis will return all rows where the `Team` column matches \\\"Golden State Warriors\\\", which should only have one row with Klay Thompson's information.\",\n", " \"generation_info\": null\n", " }\n", " ]\n", @@ -684,47 +367,32 @@ " \"llm_output\": null,\n", " \"run\": null\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [2.11s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [6.52s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"text\": \" SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"\n", + " \"text\": \" Sure thing! Based on the information provided in the `nba_roster` table, Klay Thompson is in the Golden State Warriors. Here's the SQL query to retrieve that information:\\n```sql\\nSELECT * FROM nba_roster WHERE Team = 'Golden State Warriors';\\n```\\nThis will return all rows where the `Team` column matches \\\"Golden State Warriors\\\", which should only have one row with Klay Thompson's information.\"\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [2.11s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [6.52s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"result\": \"SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\"\n", + " \"result\": \"Sure thing! Based on the information provided in the `nba_roster` table, Klay Thompson is in the Golden State Warriors. Here's the SQL query to retrieve that information:\\n```sql\\nSELECT * FROM nba_roster WHERE Team = 'Golden State Warriors';\\n```\\nThis will return all rows where the `Team` column matches \\\"Golden State Warriors\\\", which should only have one row with Klay Thompson's information.\"\n", "}\n", - "SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\n" + "Sure thing! Based on the information provided in the `nba_roster` table, Klay Thompson is in the Golden State Warriors. Here's the SQL query to retrieve that information:\n", + "```sql\n", + "SELECT * FROM nba_roster WHERE Team = 'Golden State Warriors';\n", + "```\n", + "This will return all rows where the `Team` column matches \"Golden State Warriors\", which should only have one row with Klay Thompson's information.\n" ] } ], "source": [ "# use the db_chain_memory to run the original question again\n", "question = \"Which team is Klay Thompson in\"\n", - "sql = db_chain_memory.run(question)\n", - "print(sql)" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "15cb95bf-73fe-404f-9b30-79dd6a53e369", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[{'Team': 'Golden State Warriors'}]\n" - ] - } - ], - "source": [ - "answer = db._execute(sql)\n", + "answer = db_chain_memory.run(question)\n", "print(answer)" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 10, "id": "fee65d5f", "metadata": { "scrolled": true @@ -737,7 +405,7 @@ "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] Entering Chain run with input:\n", "\u001b[0m{\n", " \"query\": \"What's his salary\",\n", - " \"history\": \"Human: Which team is Klay Thompson in\\nAI: SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\\nHuman: Which team is Klay Thompson in\\nAI: [{\\\"Team\\\": \\\"Golden State Warriors\\\"}]\"\n", + " \"history\": \"Human: Which team is Klay Thompson in\\nAI: Sure thing! Based on the information provided in the `nba_roster` table, Klay Thompson is in the Golden State Warriors. Here's the SQL query to retrieve that information:\\n```sql\\nSELECT * FROM nba_roster WHERE Team = 'Golden State Warriors';\\n```\\nThis will return all rows where the `Team` column matches \\\"Golden State Warriors\\\", which should only have one row with Klay Thompson's information.\\nHuman: Which team is Klay Thompson in\\nAI: \\\"Sure thing! Based on the information provided in the `nba_roster` table, Klay Thompson is in the Golden State Warriors. Here's the SQL query to retrieve that information:\\\\n```sql\\\\nSELECT * FROM nba_roster WHERE Team = 'Golden State Warriors';\\\\n```\\\\nThis will return all rows where the `Team` column matches \\\\\\\"Golden State Warriors\\\\\\\", which should only have one row with Klay Thompson's information.\\\"\"\n", "}\n", "\u001b[32;1m\u001b[1;3m[chain/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] Entering Chain run with input:\n", "\u001b[0m{\n", @@ -748,7 +416,7 @@ " \"stop\": [\n", " \"\\nSQLResult:\"\n", " ],\n", - " \"history\": \"Human: Which team is Klay Thompson in\\nAI: SELECT Team FROM nba_roster WHERE NAME = 'Klay Thompson';\\nHuman: Which team is Klay Thompson in\\nAI: [{\\\"Team\\\": \\\"Golden State Warriors\\\"}]\"\n", + " \"history\": \"Human: Which team is Klay Thompson in\\nAI: Sure thing! Based on the information provided in the `nba_roster` table, Klay Thompson is in the Golden State Warriors. Here's the SQL query to retrieve that information:\\n```sql\\nSELECT * FROM nba_roster WHERE Team = 'Golden State Warriors';\\n```\\nThis will return all rows where the `Team` column matches \\\"Golden State Warriors\\\", which should only have one row with Klay Thompson's information.\\nHuman: Which team is Klay Thompson in\\nAI: \\\"Sure thing! Based on the information provided in the `nba_roster` table, Klay Thompson is in the Golden State Warriors. Here's the SQL query to retrieve that information:\\\\n```sql\\\\nSELECT * FROM nba_roster WHERE Team = 'Golden State Warriors';\\\\n```\\\\nThis will return all rows where the `Team` column matches \\\\\\\"Golden State Warriors\\\\\\\", which should only have one row with Klay Thompson's information.\\\"\"\n", "}\n", "\u001b[32;1m\u001b[1;3m[llm/start]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] Entering LLM run with input:\n", "\u001b[0m{\n", @@ -756,12 +424,12 @@ " \"Only use the following tables:\\n\\nCREATE 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\\nQuestion: What's his salary\\nSQLQuery:\"\n", " ]\n", "}\n", - "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [2.75s] Exiting LLM run with output:\n", + "\u001b[36;1m\u001b[1;3m[llm/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain > 3:llm:Replicate] [5.16s] Exiting LLM run with output:\n", "\u001b[0m{\n", " \"generations\": [\n", " [\n", " {\n", - " \"text\": \" Sure! Here is the SQL query based on the given question and table structure:\\n\\nSELECT SALARY FROM nba_roster WHERE NAME = 'Lebron James';\",\n", + " \"text\": \" Sure thing! To find out what LeBron James' salary is, we can run a SQL query like this:\\n```\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\\n```\\nThis will return the salary for LeBron James, which should be around $30 million per year.\",\n", " \"generation_info\": null\n", " }\n", " ]\n", @@ -769,17 +437,19 @@ " \"llm_output\": null,\n", " \"run\": null\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [2.75s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain > 2:chain:LLMChain] [5.16s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"text\": \" Sure! Here is the SQL query based on the given question and table structure:\\n\\nSELECT SALARY FROM nba_roster WHERE NAME = 'Lebron James';\"\n", + " \"text\": \" Sure thing! To find out what LeBron James' salary is, we can run a SQL query like this:\\n```\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\\n```\\nThis will return the salary for LeBron James, which should be around $30 million per year.\"\n", "}\n", - "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [2.75s] Exiting Chain run with output:\n", + "\u001b[36;1m\u001b[1;3m[chain/end]\u001b[0m \u001b[1m[1:chain:SQLDatabaseChain] [5.16s] Exiting Chain run with output:\n", "\u001b[0m{\n", - " \"result\": \"Sure! Here is the SQL query based on the given question and table structure:\\n\\nSELECT SALARY FROM nba_roster WHERE NAME = 'Lebron James';\"\n", + " \"result\": \"Sure thing! To find out what LeBron James' salary is, we can run a SQL query like this:\\n```\\nSELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\\n```\\nThis will return the salary for LeBron James, which should be around $30 million per year.\"\n", "}\n", - "Sure! Here is the SQL query based on the given question and table structure:\n", - "\n", - "SELECT SALARY FROM nba_roster WHERE NAME = 'Lebron James';\n" + "Sure thing! To find out what LeBron James' salary is, we can run a SQL query like this:\n", + "```\n", + "SELECT SALARY FROM nba_roster WHERE NAME = 'LeBron James';\n", + "```\n", + "This will return the salary for LeBron James, which should be around $30 million per year.\n" ] } ], @@ -801,10 +471,10 @@ "id": "d23d47e9", "metadata": {}, "source": [ - "Looks like we hit a [known issue](https://github.com/langchain-ai/langchain/issues/6918#issuecomment-1632932653) with adding memory to SQLDatabaseChain, even after the [PR](https://github.com/langchain-ai/langchain/pull/7546) that's supposed to fix it was merged. We'll leave the notebook using the experimental SQLDatabaseChain as is. To find a better solution to using SQLDatabaseChain, we can:\n", - "1. Play with the prompt engineering further;\n", - "2. Look into the SQLDatabaseChain's implementation and the use of ConversationBufferMemory to fix the memory issue;\n", - "3. Wait till SQLDatabaseChain prompted from the langchain_experimental package to langchain." + "Looks like we hit a [known issue](https://github.com/langchain-ai/langchain/issues/6918#issuecomment-1632932653) with adding memory to SQLDatabaseChain, even after the [PR](https://github.com/langchain-ai/langchain/pull/7546) that's supposed to fix it was merged. We'll leave the notebook using the experimental SQLDatabaseChain with memory as is. To find a better solution to using SQLDatabaseChain, we can:\n", + "1. Look into the SQLDatabaseChain's implementation and the use of ConversationBufferMemory to fix the memory issue;\n", + "2. Wait till the SQLDatabaseChain memory issue is really fixed.\n", + "We'll update this demo when a good solution is found." ] } ], -- GitLab