From 00bff698608fe01a68763f7d47797a8d18ea5cb3 Mon Sep 17 00:00:00 2001 From: Rohan Pooniwala Date: Mon, 17 Apr 2023 13:57:09 +0530 Subject: [PATCH] feat: Connect to external database --- README.md | 8 ++++++++ requirements.txt | 3 ++- scripts/build_and_copy.sh | 30 ++++++++++++++++++++++++++++++ server/commons/config.py | 7 ++++++- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100755 scripts/build_and_copy.sh diff --git a/README.md b/README.md index 87f2b1b..52161cb 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,14 @@ docker build . -f Dockerfile -t chainfury:latest docker run --env OPENAI_API_KEY= -p 8000:8000 chainfury:latest ``` +You can also pass a Database URL to the docker container using the `DATABASE_URL` environment variable. If you do not pass a database URL, ChainFury will use a SQLite database. + +Example: + +```bash +docker run -it -E DATABASE_URL="mysql+pymysql://:@127.0.0.1:3306/" -p 8000:8000 chainfury +``` + ### **Method 2: Manual** For this, you will need to build the frontend and and then run the backend. The frontend can be built using the following command: diff --git a/requirements.txt b/requirements.txt index 03e1610..e9a76c7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ fastapi -langchain==0.0.131 +langchain==0.0.141 openai sqlalchemy pyjwt[crypto] @@ -7,3 +7,4 @@ jinja2 langflow==0.0.54 black passlib +pymysql diff --git a/scripts/build_and_copy.sh b/scripts/build_and_copy.sh new file mode 100755 index 0000000..929f496 --- /dev/null +++ b/scripts/build_and_copy.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# This script builds the project and copies the resulting ./dist folder to the server + +# check if working in root directory, both client and server folders should be present +if [ ! -d "client" ] || [ ! -d "server" ]; then + echo "Please run this script from the root directory of the Repo" + exit 1 +fi + +# build the project +# Go into the client folder and build the project using yarn +cd client +yarn build + +# Go back to the root directory +cd .. + +# copy the dist folder to the server +# Go into the server folder, remove the old static folder and copy the new dist folder, copy index.html to templates +cd server +rm -rf static +cp -r ../client/dist static +cp ../client/dist/index.html templates + +# Go back to the root directory +cd .. + +# Done +echo "Done" \ No newline at end of file diff --git a/server/commons/config.py b/server/commons/config.py index 59b727d..6bf29a1 100644 --- a/server/commons/config.py +++ b/server/commons/config.py @@ -17,8 +17,13 @@ def get_logger(name): logger = get_logger(__name__) + DATABASE = "sqlite:///./chain.db" -engine = create_engine(DATABASE, connect_args={"check_same_thread": False}) +if os.environ.get("DATABASE_URL", None) is not None: + logger.info("Using DATABASE_URL") + DATABASE = os.environ.get("DATABASE_URL") + +engine = create_engine(DATABASE) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) logger.info("Database opened successfully")