Skip to content

Commit

Permalink
connected python with DB
Browse files Browse the repository at this point in the history
  • Loading branch information
JAlcocerT committed Dec 7, 2024
1 parent 044a1a8 commit d9cfdbc
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 9 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=sk-proj-yourapikey
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

*_venv/

*.env

*.sql
*.sqlite
*.db
1 change: 1 addition & 0 deletions LangChain/ChatWithDB/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=sk-proj-yourapikey
33 changes: 29 additions & 4 deletions LangChain/ChatWithDB/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,41 @@ 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

<details>
<summary>Using Python Venv...👇</summary>
&nbsp;



```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
Expand All @@ -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 .
Expand Down
15 changes: 14 additions & 1 deletion LangChain/ChatWithDB/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
langchain
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
223 changes: 219 additions & 4 deletions LangChain/ChatWithDB/test_langchainChatDB.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -11,7 +11,7 @@
"2"
]
},
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -57,6 +57,13 @@
"print(sys.version_info)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DB Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"
},
Expand Down

0 comments on commit d9cfdbc

Please sign in to comment.