Skip to content

Commit

Permalink
feat: Connect to external database
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanpooniwala committed Apr 17, 2023
1 parent 4f7fdb8 commit 00bff69
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ docker build . -f Dockerfile -t chainfury:latest
docker run --env OPENAI_API_KEY=<your_key_here> -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://<user>:<password>@127.0.0.1:3306/<database>" -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:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
fastapi
langchain==0.0.131
langchain==0.0.141
openai
sqlalchemy
pyjwt[crypto]
jinja2
langflow==0.0.54
black
passlib
pymysql
30 changes: 30 additions & 0 deletions scripts/build_and_copy.sh
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 6 additions & 1 deletion server/commons/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 00bff69

Please sign in to comment.