mirror of
				https://github.com/hwchase17/langchain.git
				synced 2025-11-03 17:54:10 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			130 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			130 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
{
 | 
						|
 "cells": [
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "id": "0ed6aab1",
 | 
						|
   "metadata": {
 | 
						|
    "pycharm": {
 | 
						|
     "name": "#%% md\n"
 | 
						|
    }
 | 
						|
   },
 | 
						|
   "source": [
 | 
						|
    "# SQLite example\n",
 | 
						|
    "\n",
 | 
						|
    "This example showcases hooking up an LLM to answer questions over a database."
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "markdown",
 | 
						|
   "id": "b2f66479",
 | 
						|
   "metadata": {
 | 
						|
    "pycharm": {
 | 
						|
     "name": "#%% md\n"
 | 
						|
    }
 | 
						|
   },
 | 
						|
   "source": [
 | 
						|
    "This uses the example Chinook database.\n",
 | 
						|
    "To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the `.db` file in a notebooks folder at the root of this repository."
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 1,
 | 
						|
   "id": "d0e27d88",
 | 
						|
   "metadata": {
 | 
						|
    "pycharm": {
 | 
						|
     "name": "#%%\n"
 | 
						|
    }
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "from langchain import OpenAI, SQLDatabase, SQLDatabaseChain"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 2,
 | 
						|
   "id": "72ede462",
 | 
						|
   "metadata": {
 | 
						|
    "pycharm": {
 | 
						|
     "name": "#%%\n"
 | 
						|
    }
 | 
						|
   },
 | 
						|
   "outputs": [],
 | 
						|
   "source": [
 | 
						|
    "db = SQLDatabase.from_uri(\"sqlite:///../../../notebooks/Chinook.db\")\n",
 | 
						|
    "llm = OpenAI(temperature=0)\n",
 | 
						|
    "db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": 3,
 | 
						|
   "id": "15ff81df",
 | 
						|
   "metadata": {
 | 
						|
    "pycharm": {
 | 
						|
     "name": "#%%\n"
 | 
						|
    }
 | 
						|
   },
 | 
						|
   "outputs": [
 | 
						|
    {
 | 
						|
     "name": "stdout",
 | 
						|
     "output_type": "stream",
 | 
						|
     "text": [
 | 
						|
      "\n",
 | 
						|
      "\n",
 | 
						|
      "\u001b[1m> Entering new chain...\u001b[0m\n",
 | 
						|
      "How many employees are there?\n",
 | 
						|
      "SQLQuery:\u001b[102m SELECT COUNT(*) FROM Employee\u001b[0m\n",
 | 
						|
      "SQLResult: \u001b[103m[(8,)]\u001b[0m\n",
 | 
						|
      "Answer:\u001b[102m 8\u001b[0m\n",
 | 
						|
      "\u001b[1m> Finished chain.\u001b[0m\n"
 | 
						|
     ]
 | 
						|
    },
 | 
						|
    {
 | 
						|
     "data": {
 | 
						|
      "text/plain": [
 | 
						|
       "' 8'"
 | 
						|
      ]
 | 
						|
     },
 | 
						|
     "execution_count": 3,
 | 
						|
     "metadata": {},
 | 
						|
     "output_type": "execute_result"
 | 
						|
    }
 | 
						|
   ],
 | 
						|
   "source": [
 | 
						|
    "db_chain.run(\"How many employees are there?\")"
 | 
						|
   ]
 | 
						|
  },
 | 
						|
  {
 | 
						|
   "cell_type": "code",
 | 
						|
   "execution_count": null,
 | 
						|
   "id": "61d91b85",
 | 
						|
   "metadata": {},
 | 
						|
   "outputs": [],
 | 
						|
   "source": []
 | 
						|
  }
 | 
						|
 ],
 | 
						|
 "metadata": {
 | 
						|
  "kernelspec": {
 | 
						|
   "display_name": "Python 3 (ipykernel)",
 | 
						|
   "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.7.6"
 | 
						|
  }
 | 
						|
 },
 | 
						|
 "nbformat": 4,
 | 
						|
 "nbformat_minor": 5
 | 
						|
}
 |