Skip to content

Llm flask api #1789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft

Conversation

chiroptical
Copy link
Collaborator

@chiroptical chiroptical commented Apr 9, 2025

DO NOT MERGE

This PR requires an update to firebase-tools in package.json. It at least needs to be "firebase-tools": "^13.23.0",.

Summary

In this PR, I introduce a wrapper for our LLM functions via an HTTP firebase function. It contains a bunch of infrastructure updates to support deploying the new Python firebase functions. The only very tricky part is deploying via our local docker compose pipeline and I tried to document the deficiencies in the process.

Checklist

  • On the frontend, I've made my strings translate-able.
  • If I've added shared components, I've added a storybook story.
  • I've made pages responsive and look good on mobile.

Not a FE change.

Screenshots

N/A

Known issues

If you've run against limitations or caveats, include them here. Include follow-up issues as well.

Steps to test/reproduce

For each feature or bug fix, create a step by step list for how a reviewer can test it out. E.g.:

  1. Go to the home page
  2. Click on a testimony
  3. See that it's loaded with a loading spinner

Copy link

vercel bot commented Apr 9, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
maple-dev ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 0:11am
maple-prod 🔄 Building (Inspect) Visit Preview 💬 Add feedback Jun 25, 2025 0:11am

@Mephistic
Copy link
Collaborator

Nathan's comments from the Zoom on cutting down dependency:

I expect the imports here are sufficient to run the python llm functions:

import json

There are some fairly hefty ones there that are needed... langchain, sklearn, chromadb

We can probably cut sklearn if we find easy replacement for the small number of functions we are importing from it.
Also, I noticed streamlit is imported there, and that is actually totally unecessary! It is vestigial IUUC. I don't see it actually referenced anywhere in the script.
Removing streamlit could be a quick win.

Comment on lines +26 to +27
if summary["status"] in [-1, -2]:
abort(500, description="Unable to generate summary")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status field returns either -1 or -2 if it fails... This is probably somewhat unfortunate here because if this API ever changes I will not be able to know about it.

I'll add a note to get_summary_api_function but we don't really have types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, maybe I should just check that this value is negative? technically the return type is int

Comment on lines +42 to +43
if tags["status"] in [-1, -2]:
abort(500, description="Unable to generate tags")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably check for negative return type? See above https://github.com/codeforboston/maple/pull/1789/files#r2055086855

Add a GET /ready endpoint for testing

Add some command examples to readme.md
"predeploy": [". llm/venv/bin/activate && python3 -m pip install -r llm/requirements.txt"],
"source": "llm",
"codebase": "maple-llm",
"runtime": "python311"
Copy link
Collaborator Author

@chiroptical chiroptical Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the version running inside Dockerfile.firebase, and this is actually required to build the virtual environment see llm/readme.md for more information.

@@ -1,183 +1,16 @@
absl-py==2.1.0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimized and simplified the requirements file.

@@ -4,11 +4,20 @@
"cleanUrls": true,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this PR requires an update to firebase-tools in package.json!


# Running the API

Set up a virtual environment and run the Flask app
Copy link
Collaborator Author

@chiroptical chiroptical Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually, there are a few ways you can run this application.

  1. Locally with no dependencies on firebase, that is the first set of notes
  2. Deployed as a function with firebase locally, that is the second set of notes
  3. Deploying to firebase directly

"2." is by far the hardest because we directly overlay /app into the Docker.firebase container in docker-compose.yaml. So, we need the virtual env and python version when running inside the container to match! Therefore, either we stop overlaying /app or we build the virtual environment from inside the container. We chose the latter because it is a simpler change.

@@ -278,6 +278,7 @@ def set_my_llm_cache(cache_file: Path=LLM_CACHE) -> SQLiteCache:
Set an LLM cache, which allows for previously executed completions to be
loaded from disk instead of repeatedly queried.
"""
cache_file.parent.mkdir(exist_ok=True)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was required because the container doesn't have this path and we need it to build the LLM cache

@@ -1,7 +1,7 @@
FROM andreysenov/firebase-tools:latest-node-18

USER root
RUN apt update && apt install -y curl
RUN apt update && apt install -y curl python3 python3-pip python3-venv
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need python in this container to build the virtual environments, see readme.md



def set_openai_api_key():
match os.environ.get("MAPLE_DEV"):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT, we use this to deploy dev versus prod. I re-use it here to select an environment. IIUC, we have a lost less dollars in OPENAI_DEV so it seems like a safe default.

Comment on lines +11 to +12
"deploy:backend:dev": "firebase --project=default deploy --only firestore,functions:maple,storage",
"deploy:backend:prod": "firebase --project=prod deploy --only firestore,functions:maple,storage",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we also have Python, and it is a little janky right now, just deploy the maple functions and skip the python ones.

Copy link
Collaborator Author

@chiroptical chiroptical Jun 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It kind of sucks that this is a JSON file... Obviously, not something you can control.

Comment on lines +61 to +63
@app.route("/ready", methods=["GET"])
def ready():
return ""
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not be necessary...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants