Skip to content

Commit 9275393

Browse files
committed
Add both the /summary and /tags functions
- Add basic incomplete post body testing. Running them for real would require OpenAI calls
1 parent 5dd2c19 commit 9275393

File tree

4 files changed

+60
-12
lines changed

4 files changed

+60
-12
lines changed

llm/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
llm-api/
22
__pycache__/
3+
databases/

llm/api.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
1-
from flask import Flask, jsonify, abort
2-
from llm_functions import get_summaries_and_tags_api_function
1+
from flask import Flask, jsonify, abort, request
2+
from llm_functions import get_summary_api_function, get_tags_api_function
3+
import json
34

45
app = Flask(__name__)
56

6-
@app.route('/summary', methods=['POST'])
7+
8+
def is_intersection(keys, required_keys):
9+
return (keys & required_keys) == required_keys
10+
11+
12+
@app.route("/summary", methods=["POST"])
713
def summary():
814
body = json.loads(request.data)
915
# We require bill_id, bill_title, bill_text to exist as keys in the POST
10-
# Note: & is essentially set intersection
11-
if not (body.keys() & {"bill_id", "bill_title", "bill_text"}):
16+
if not is_intersection(body.keys(), {"bill_id", "bill_title", "bill_text"}):
1217
abort(404, description="requires bill_id, bill_title, and bill_text")
1318

14-
summary = get_summaries_and_tags_api_function(
15-
body["bill_id"],
16-
body["bill_title"],
17-
body["bill_text"]
19+
summary = get_summary_api_function(
20+
body["bill_id"], body["bill_title"], body["bill_text"]
1821
)
1922

2023
if summary["status"] in [-1, -2]:
21-
abort(404, description="unable to generate summary or tags")
22-
23-
return jsonify(summary)
24+
abort(500, description="Unable to generate summary")
25+
26+
return jsonify(summary["summary"])
27+
28+
29+
@app.route("/tags", methods=["POST"])
30+
def tags():
31+
body = json.loads(request.data)
32+
# We require bill_id, bill_title, bill_text to exist as keys in the POST
33+
# Note: & is essentially set intersection
34+
if not is_intersection(body.keys(), {"bill_id", "bill_title", "bill_text"}):
35+
abort(404, description="requires bill_id, bill_title, and bill_text")
36+
37+
tags = get_tags_api_function(body["bill_id"], body["bill_title"], body["bill_text"])
38+
39+
if tags["status"] in [-1, -2]:
40+
abort(500, description="Unable to generate tags")
41+
42+
return jsonify(tags["tags"])

llm/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,4 @@ zipp==3.19.1
184184
Flask==3.1.0
185185
itsdangerous==2.2.0
186186
Werkzeug==3.1.3
187+
pytest==8.3.5

llm/test_api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from api import app
3+
4+
5+
@pytest.fixture
6+
def client():
7+
return app.test_client()
8+
9+
10+
def test_summary_returns_404_with_empty_body(client):
11+
response = client.post("/summary", json={})
12+
assert response.status_code == 404
13+
14+
15+
def test_summary_returns_404_with_partial_body(client):
16+
response = client.post("/summary", json={"bill_id": "bill"})
17+
assert response.status_code == 404
18+
19+
20+
def test_tags_returns_404_with_empty_body(client):
21+
response = client.post("/tags", json={})
22+
assert response.status_code == 404
23+
24+
25+
def test_tags_returns_404_with_partial_body(client):
26+
response = client.post("/tags", json={"bill_id": "bill"})
27+
assert response.status_code == 404

0 commit comments

Comments
 (0)