Skip to content

Commit

Permalink
feat: refactor demo frontend (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: Yuan <45984206+Yuan325@users.noreply.github.com>
  • Loading branch information
averikitsch and Yuan325 committed Oct 27, 2023
1 parent da374d9 commit 4245020
Show file tree
Hide file tree
Showing 13 changed files with 782 additions and 121 deletions.
104 changes: 76 additions & 28 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# DEVELOPER.md

## Pre-reqs

See [Pre-reqs](./cloudrun_instructions.md).

## Setup

We recommend using Python 3.11+ and installing the requirements into a virtualenv:
Expand All @@ -12,41 +16,85 @@ If you are developing or otherwise running tests, install the test requirements
pip install -r extension_service/requirements-test.txt -r langchain_tools_demo/requirements-test.txt
```

## Running the server
## Run the app locally
### Running the extension service

Create your database config:
```bash
cd extension_service
cp example-config.yml config.yml
```
1. Change into the service directory:

Add your values to `config.yml`
```bash
cd extension_service
```

Prepare the database:
```bash
python run_database_init.py
```
1. Create your database config:

To run the app using uvicorn, execute the following:
```bash
python run_app.py
```
```bash
cp example-config.yml config.yml
```

## Running the frontend
1. Add your database config to `config.yml`:

To run the app using streamlit, execute the following:
```bash
cd langchain_tools_demo
streamlit run run_app.py
```
1. Start the Cloud SQL Proxy or AlloyDB SSH tunnel.

1. To run the app using uvicorn, execute the following:

```bash
python run_app.py
```

### Running the frontend

1. [Optional] Set up [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials#GAC):

```bash
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json
```

1. Change into the demo directory:

```bash
cd langchain_tools_demo
```

1. Set the server port:

```bash
export PORT=9090
```

1. [Optional] Set `BASE_URL` environment variable:

```bash
export BASE_URL=<EXTENSION_SERVICE_URL>
```

1. [Optional] Set `DEBUG` environment variable:

```bash
export DEBUG=True
```

1. To run the app using uvicorn, execute the following:

```bash
python main.py
```

Note: for hot reloading of the app use: `uvicorn main:app --host 0.0.0.0 --port 9090 --reload`

1. View app at `http://localhost:9090/`

## Testing

Run pytest to automatically run all tests:
```bash
export DB_USER=""
export DB_PASS=""
export DB_NAME=""
pytest
```
1. Set environment variables:

```bash
export DB_USER=""
export DB_PASS=""
export DB_NAME=""
```

1. Run pytest to automatically run all tests:

```bash
pytest
```
119 changes: 119 additions & 0 deletions cloudrun_instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Deploy to Cloud Run

## Pre-reqs

* Google Cloud Project
* Enabled APIs:
* Cloud Run
* Vertex AI
* Cloud SQL or AlloyDB
* Compute
* Cloud Build
* Artifact Registry
* Service Networking
* Cloud SQL PostgreSQL instance or AlloyDB cluster and primary instance

## Datastore Setup


## Deployment

1. For easier deployment, set environment variables:

```bash
export PROJECT_ID=<YOUR_PROJECT_ID>
```

1. Create a backend service account:

```bash
gcloud iam service-accounts create extension-identity
```

1. Grant permissions to access Cloud SQL and/or AlloyDB:

```bash
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:extension-identity@$PROJECT_ID.iam.gserviceaccount.com \
--role roles/cloudsql.client
```

```bash
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:extension-identity@$PROJECT_ID.iam.gserviceaccount.com \
--role roles/alloydb.client
```

1. Change into the service directory:

```bash
cd extension_service
```

1. Deploy backend service to Cloud Run:

* For Cloud SQL:

```bash
gcloud run deploy extension-service \
--source . \
--no-allow-unauthenticated \
--service-account extension-identity \
--region us-central1 \
--add-cloudsql-instances <PROJECT_ID:REGION:CLOUD_SQL_INSTANCE_NAME>
```

* For AlloyDB:

```bash
gcloud alpha run deploy extension-service \
--source . \
--no-allow-unauthenticated \
--service-account extension-identity \
--region us-central1 \
--network=default \
--subnet=default
```

1. Retrieve extension URL:

```bash
export EXTENSION_URL=$(gcloud run services describe extension-service --format 'value(status.url)')
```

1. Create a frontend service account:

```bash
gcloud iam service-accounts create demo-identity
```

1. Grant the service account access to invoke the backend service and VertexAI API:

```bash
gcloud run services add-iam-policy-binding extension-service \
--member serviceAccount:demo-identity@$PROJECT_ID.iam.gserviceaccount.com \
--role roles/run.invoker
```
```bash
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member serviceAccount:demo-identity@$PROJECT_ID.iam.gserviceaccount.com \
--role roles/aiplatform.user
```

1. Change into the service directory:

```bash
cd langchain_tools-demos
```

1. Deploy to Cloud Run:

```bash
gcloud run deploy demo-service \
--source . \
--allow-unauthenticated \
--set-env-vars=BASE_URL=$EXTENSION_URL \
--service-account demo-identity
```

Note: Your organization may not allow unauthenticated requests. Deploy with `--no-allow-unauthenticated` and use the proxy to view the frontend: `gcloud run services proxy demo-service`.
2 changes: 1 addition & 1 deletion extension_service/app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def amenities_search(query: str, top_k: int, request: Request):
embed_service: Embeddings = request.app.state.embed_service
query_embedding = embed_service.embed_query(query)

results = await ds.amenities_search(query_embedding, 0.7, top_k)
results = await ds.amenities_search(query_embedding, 0.3, top_k)
return results


Expand Down
32 changes: 32 additions & 0 deletions langchain_tools_demo/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Use the official lightweight Python image.
# https://hub.docker.com/_/python
FROM python:3.11-slim

# Allow statements and log messages to immediately appear in the logs
ENV PYTHONUNBUFFERED True

WORKDIR /app

# Install production dependencies.
COPY ./requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy local code to the container image.
COPY . ./

# Run the web service on container startup.
CMD exec uvicorn main:app --host 0.0.0.0 --port $PORT
Loading

0 comments on commit 4245020

Please sign in to comment.