Skip to content

Commit 8ebc9f1

Browse files
authored
Remove flask API from firebase functions (#1998)
* Remove flask API from firebase functions * Add top-level comment to main.py
1 parent f3e9376 commit 8ebc9f1

3 files changed

Lines changed: 7 additions & 98 deletions

File tree

llm/main.py

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
from flask import Flask, jsonify, abort, request
2-
from llm_functions import get_summary_api_function, get_tags_api_function
3-
import json
1+
"""
2+
The module contains all of the Python based Firebase functions
3+
4+
* add_summary_on_document_created - when new bills are written to the database, this function will trigger via on_document_created
5+
"""
6+
47
from firebase_admin import initialize_app
5-
from firebase_functions import https_fn, options
8+
from firebase_functions import options
69
import os
710
from firebase_functions.firestore_fn import (
811
on_document_created,
@@ -12,7 +15,6 @@
1215
import bill_on_document_created
1316

1417
initialize_app()
15-
app = Flask(__name__)
1618

1719

1820
def is_intersection(keys, required_keys):
@@ -29,56 +31,6 @@ def set_openai_api_key():
2931
os.environ["OPENAI_API_KEY"] = os.environ["OPENAI_DEV"]
3032

3133

32-
@app.route("/summary", methods=["POST"])
33-
def summary():
34-
set_openai_api_key()
35-
body = json.loads(request.data)
36-
# We require bill_id, bill_title, bill_text to exist as keys in the POST
37-
if not is_intersection(body.keys(), {"bill_id", "bill_title", "bill_text"}):
38-
abort(404, description="requires bill_id, bill_title, and bill_text")
39-
40-
summary = get_summary_api_function(
41-
body["bill_id"], body["bill_title"], body["bill_text"]
42-
)
43-
44-
if summary["status"] in [-1, -2]:
45-
abort(500, description="Unable to generate summary")
46-
47-
return jsonify(summary["summary"])
48-
49-
50-
@app.route("/tags", methods=["POST"])
51-
def tags():
52-
set_openai_api_key()
53-
body = json.loads(request.data)
54-
# We require bill_id, bill_title, bill_text to exist as keys in the POST
55-
# Note: & is essentially set intersection
56-
if not is_intersection(body.keys(), {"bill_id", "bill_title", "bill_text"}):
57-
abort(404, description="requires bill_id, bill_title, and bill_text")
58-
59-
tags = get_tags_api_function(body["bill_id"], body["bill_title"], body["bill_text"])
60-
61-
if tags["status"] in [-1, -2]:
62-
abort(500, description="Unable to generate tags")
63-
64-
return jsonify(tags["tags"])
65-
66-
67-
@app.route("/ready", methods=["GET"])
68-
def ready():
69-
return ""
70-
71-
72-
@https_fn.on_request(
73-
secrets=["OPENAI_DEV", "OPENAI_PROD"],
74-
timeout_sec=300,
75-
memory=options.MemoryOption.GB_1,
76-
)
77-
def httpsflaskexample(req: https_fn.Request) -> https_fn.Response:
78-
with app.request_context(req.environ):
79-
return app.full_dispatch_request()
80-
81-
8234
@on_document_created(
8335
secrets=["OPENAI_DEV", "OPENAI_PROD"],
8436
timeout_sec=300,

llm/readme.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,30 +170,14 @@ in the emulator after running `yarn dev:up`.
170170
# had a number of problems with authorization
171171
GOOGLE_APPLICATION_CREDENTIALS=/path/to/application_default_credentials.json \
172172
firebase deploy --only functions:maple-llm --debug
173-
174-
# Hit the function in production
175-
curl \
176-
-X POST \
177-
-H "Content-Type: application/json" \
178-
-H "Authorization: Bearer $(gcloud auth print-identity-token)" \
179-
-d '{"bill_id": "1234","bill_title": "A title","bill_text": "Some bill text"}' \
180-
https://httpsflaskexample-ke6znoupgq-uc.a.run.app/summary
181173
```
182174

183175
## Future work
184176

185-
Currently, we are just using the built-in Flask server. We should switch to a
186-
production WSGI server, like `gunicorn`.
187-
188177
The local emulator installation process is quite cumbersome and ideally the
189178
virtual environment was built during container instantiation instead of from
190179
within the Docker container (i.e. the "Deploying locally" docs above).
191180

192-
The current API is a little wonky because we take `bill_id` **and** `bill_text`.
193-
We could just look up the `bill_text` via the `bill_id` using the Firestore API.
194-
It might make sense to avoid the HTTP wrapper all-together and figure out how
195-
JS <-> Python communication works without an HTTP layer.
196-
197181
## Document triggers
198182

199183
The first trigger is an `@on_document_created` trigger in

llm/test_api.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)