diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..a46ed08 --- /dev/null +++ b/.env.sample @@ -0,0 +1 @@ +OPENAI_API_KEY=sk-proj-yourapikey \ No newline at end of file diff --git a/.gitignore b/.gitignore index ebe7e0f..c56419b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ *_venv/ +*.env + *.sql *.sqlite *.db \ No newline at end of file diff --git a/LangChain/ChatWithDB/.env.sample b/LangChain/ChatWithDB/.env.sample new file mode 100644 index 0000000..a46ed08 --- /dev/null +++ b/LangChain/ChatWithDB/.env.sample @@ -0,0 +1 @@ +OPENAI_API_KEY=sk-proj-yourapikey \ No newline at end of file diff --git a/LangChain/ChatWithDB/readme.md b/LangChain/ChatWithDB/readme.md index fee1477..e1efeb9 100644 --- a/LangChain/ChatWithDB/readme.md +++ b/LangChain/ChatWithDB/readme.md @@ -9,6 +9,8 @@ Follow any of [these Python environment setup](https://jalcocert.github.io/JAlco For the `ipynb` vscode will ak you for the `ipykernel` module. +Then you will be asked to **select the Python env**. + You will need : https://platform.openai.com/api-keys
@@ -16,10 +18,32 @@ You will need : https://platform.openai.com/api-keys   + ```sh -pip install langchain +python3 -m venv datachat_venv #create the venv Linux +#python -m venv datachat_venv #create the venv W + +#datachat_venv\Scripts\activate #activate venv (windows) +source datachat_venv/bin/activate #(linux) +``` + +Set **API credentials**: + +```sh +export OPENAI_API_KEY=YOUR_API_KEY +$env:OPENAI_API_KEY="YOUR_API_KEY" +set OPENAI_API_KEY=YOUR_API_KEY +``` + +Install the libraries one by one... + +```sh +pip install langchain==0.1.7 mysql-connector-python ``` + +Or with `requirements.txt`: + ```sh #sudo apt install python3.12-venv python3 -m venv langchainChatDB_venv @@ -29,15 +53,16 @@ python3 -m venv langchainChatDB_venv source langchainChatDB_venv/bin/activate #.\langchainChatDB_venv\Scripts\activate #Windows -pip install -r requirements.txt - - source .env #export OPENAI_API_KEY="your-api-key-here" #set OPENAI_API_KEY=your-api-key-here #$env:OPENAI_API_KEY="your-api-key-here" echo $OPENAI_API_KEY +cd ./LangChain/ChatWithDB +pip install -r requirements.txt + + streamlit run langchain_chat_db.py # git add . diff --git a/LangChain/ChatWithDB/requirements.txt b/LangChain/ChatWithDB/requirements.txt index e7f08b2..3f953e7 100644 --- a/LangChain/ChatWithDB/requirements.txt +++ b/LangChain/ChatWithDB/requirements.txt @@ -1 +1,14 @@ -langchain \ No newline at end of file +langchain + +openai==1.56.0 #1.52.2 #https://pypi.org/project/openai/ +python-dotenv==1.0.1 + +langchain #==0.1.7 +langchain-community #==0.0.20 +langchain-core #==0.1.23 +langchain-openai #==0.0.6 + +mysql-connector-python==8.3.0 + +#pip install -r requirements.txt +#pip freeze | grep langchain \ No newline at end of file diff --git a/LangChain/ChatWithDB/test_langchainChatDB.ipynb b/LangChain/ChatWithDB/test_langchainChatDB.ipynb index fc3f273..da2bcbe 100644 --- a/LangChain/ChatWithDB/test_langchainChatDB.ipynb +++ b/LangChain/ChatWithDB/test_langchainChatDB.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -11,7 +11,7 @@ "2" ] }, - "execution_count": 1, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -22,7 +22,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -57,6 +57,13 @@ "print(sys.version_info)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## DB Setup" + ] + }, { "cell_type": "code", "execution_count": null, @@ -119,6 +126,13 @@ "# SHOW FULL TABLES;" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can render the mermaid **diagram of a DB Schema** as per [this post](https://jalcocert.github.io/JAlcocerT/how-to-chat-with-your-data/#chat-with-a-db-with-langchain)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -147,11 +161,212 @@ "# ORDER BY \n", "# TABLE_NAME, ORDINAL_POSITION;\n" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Python BackEnd Setup" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "#source .env\n", + "\n", + "#export OPENAI_API_KEY=\"your-api-key-here\"\n", + "#set OPENAI_API_KEY=your-api-key-here\n", + "#$env:OPENAI_API_KEY=\"your-api-key-here\"\n", + "\n", + "#echo $OPENAI_API_KEY" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from dotenv import load_dotenv\n", + "from openai import OpenAI" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "# Load environment variables from the .env file\n", + "load_dotenv()\n", + "\n", + "# Get the OpenAI API key from the environment variables\n", + "api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "#print(api_key)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#pip install langchain==0.1.7" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_core.prompts import ChatPromptTemplate\n", + "\n", + "template = \"\"\"Based on the table schema below, write a SQL query that would answer the user's question:\n", + "{schema}\n", + "\n", + "Question: {question}\n", + "SQL Query:\"\"\"\n", + "prompt = ChatPromptTemplate.from_template(template)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Human: Based on the table schema below, write a SQL query that would answer the user's question:\\nmy schema\\n\\nQuestion: how many users are there?\\nSQL Query:\"" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prompt.format(schema=\"my schema\", question=\"how many users are there?\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#pip install mysql-connector-python==8.3.0\n", + "#pip freeze | grep sql" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#sudo systemctl status mysql\n", + "#sudo mysql -u root -padmin\n", + "\n", + "#SHOW DATABASES;\n", + "#SHOW VARIABLES LIKE 'port';\n", + "#SHOW VARIABLES LIKE 'socket';" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#sudo ss -tuln | grep 127.0.0.1\n", + "#sudo ss -tuln | grep 3306\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Update the **root user’s authentication method** to use the `mysql_native_password` plugin (which allows password-based login):" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#sudo mysql -u root\n", + "#USE mysql;\n", + "#ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_password';\n", + "#FLUSH PRIVILEGES;\n", + "#EXIT\n", + "\n", + "#mysql -u root -p\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_community.utilities import SQLDatabase\n", + "\n", + "# if you are using SQLite\n", + "#sqlite_uri = 'sqlite:///./Chinook.db' \n", + "\n", + "# if you are using MySQL\n", + "#mysql_uri = 'mysql+mysqlconnector://root:colerany$iM92@localhost:3306/test_db'\n", + "mysql_uri = 'mysql+mysqlconnector://root:your_password@localhost:3306/Chinook' #PASS WAS SETUP JUST ABOVE\n", + "\n", + "#db = SQLDatabase.from_uri(sqlite_uri)\n", + "db = SQLDatabase.from_uri(mysql_uri)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we can **QUERY THE DB FROM PYTHON**" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"[(1, 'For Those About To Rock We Salute You', 1), (2, 'Balls to the Wall', 2), (3, 'Restless and Wild', 2), (4, 'Let There Be Rock', 1), (5, 'Big Ones', 3)]\"" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "db.run(\"Select * FROM Album LIMIT 5\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "datachat_venv", + "display_name": "langchainChatDB_venv", "language": "python", "name": "python3" },