11# Setup a REST API server on GCP for a Langroid script
22
3+ Ensure that your env secrets are in this folder as a ` .env ` file.
4+ We will of course * not* include it in the docker image
5+ (which is why it's in the ` .dockerignore ` file) but we will use it
6+ for local testing by passing this file in as an env file to the uvicorn server.
7+ (See the defn of ` make run ` in the Makefile.)
8+
39## local testing
410
5- Build server using ` make server ` , run it with ` make run `
11+ Build server using ` make server ` , run it with ` make run ` .
12+ See the definitions of these in the Makefile. Notice that
13+ we are passing in the env variables
614
715## Curl examples to test the server locally
816
@@ -25,6 +33,13 @@ curl -X POST \
2533```
2634(If you omit the ` -o out.txt ` part, the response will be printed to the terminal.)
2735
36+ ` process_file_and_data ` endpoint:
37+
38+ ``` bash
39+ curl -X POST " http://localhost:8000/process_file_and_data/" \
40+ -F " file=@/tmp/query.txt" \
41+ -F ' complex_data={"id": 1, "item": {"name": "Item Name", "description": "A description", "quantity": 10, "tags": ["tag1", "tag2"]}, "related_items": [{"name": "Related Item 1", "description": "Description 1", "quantity": 5, "tags": ["tag3", "tag4"]}, {"name": "Related Item 2", "description": "Description 2", "quantity": 3, "tags": ["tag5", "tag6"]}]}'
42+ ```
2843
2944## Deploy to google cloud run
3045
@@ -36,20 +51,12 @@ See other details here:
3651https://chat.openai.com/share/c34583c8-b88e-4a70-bf24-83229700c020
3752
3853
39- Run these from within the dir where the Dockerfile is located:
40-
41- ``` bash
42- gcloud auth configure-docker
43- docker build -t gcr.io/langroid/langroid-server:v1 .
44- docker push gcr.io/langroid/langroid-server:v1
45- ```
46-
47- The ` build ` and ` push ` cmds are also available via the Makefile
48- as ` make gbuild ` and ` make gpush ` respectively.
54+ Run ` make gserver ` , ` make gpush ` from within the dir where the Dockerfile is
55+ located.
4956
5057Go to Google Cloud Run Service and create a new service,
51- selecting the latest version of the pushed docker image above:
52- ` gcr.io/langroid/langroid-server:v1 `
58+ selecting the latest version of the pushed docker image above, e.g. :
59+ ` gcr.io/langroid/langroid-server:latest `
5360
5461When setting up the service:
5562- ensure you select the same port number as in the Dockerfile, e.g. 80.
@@ -61,6 +68,33 @@ If the service fails to start due to an error like `uvicorn: exec format error`,
6168then you may be able to fix it by explicitly choosing an architecture
6269in the Dockerfile, e.g. ` linux/amd64 ` (which we chose in the Dockerfile).
6370
71+ ### Creating secrets in google cloud
72+ Some commands for quick reference:
73+
74+ ``` bash
75+ gcloud secrets create openai-api-key --replication-policy=" automatic"
76+ echo -n " your-openai-api-key" | gcloud secrets versions add openai-api-key --data-file=-
77+ ```
78+
79+ After creating your secret and adding its value, you may need to set appropriate
80+ permissions for the secret. Use gcloud secrets add-iam-policy-binding to grant access
81+ to the secret:
82+
83+ ``` bash
84+ gcloud secrets add-iam-policy-binding openai-api-key \
85+ --member=" serviceAccount:langroid-docai-sa@langroid.iam.gserviceaccount.com" \
86+ --role=" roles/secretmanager.secretAccessor"
87+ ```
88+
89+ To expose one of these as environment var named ` OPENAI_API_KEY ` in the cloud run
90+ service:
91+
92+ ``` bash
93+ gcloud run services update langroid-server \
94+ --update-secrets OPENAI_API_KEY=openai-api-key:latest \
95+ --region=us-east4
96+ ```
97+
6498## Test GCP endpoints
6599
66100Same curl cmds as above, but use the endpoint url from the GCP Cloud Run service,
0 commit comments