-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82e84a9
commit c646b82
Showing
32 changed files
with
308 additions
and
471 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
LANGUAGE_MODEL=AgentPublic/llama3-instruct-8b | ||
EMBEDDINGS_MODEL=BAAI/bge-m3 | ||
ALBERT_BASE_URL=https://albert.api.dev.etalab.gouv.fr/v1 | ||
#ALBERT_BASE_URL=http://localhost:8080/v1 | ||
#ALBERT_API_KEY=spp-prod-V3QLrqTTmzd5jrZSiAAdHnFw9ijyc7m2DZDK97nAq4md34DMpvQYmCZj7wQYwkta | ||
ALBERT_API_KEY=leo-qQVK5cFW4R7QbCxx5V33gKE9qzER32tesNb4DTrPeg7sqVrRsUdprJfArwMtAxui | ||
REDIS_HOST=albert.bdd.001.etalab.gouv.fr | ||
REDIS_PORT=36379 | ||
REDIS_PASSWORD=gaYauVqErKgFtAgDZrgJt4ZKohjoJ7FXkgQAEU3gMVSAwHwY2TqeaeTwofroeJnk | ||
COLLECTION_ID=5080b4bc-71a3-49af-acfc-c27f56079f0c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
# Albert - Services Publics Plus | ||
|
||
1. Envoi du prompt au modèle | ||
|
||
```sh | ||
curl -XPOST https://spp.etalab.gouv.fr/api/spp/anonymize -H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer $API_KEY" \ | ||
-d '{"id":"123", "text":"Merci pour service"}' | ||
``` | ||
```sh | ||
curl -XPOST https://spp.etalab.gouv.fr/api/spp/anonymize -H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer $API_KEY" \ | ||
-d '{"id":"123", "text":"Merci pour service."}' | ||
``` | ||
|
||
2. Récupération de la réponse du modèle | ||
|
||
```sh | ||
curl -XPOST https://spp.etalab.gouv.fr/api/spp/prod/run/ditp-get-data -H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer $API_KEY" \ | ||
-d '{"id":"123"}' | ||
``` | ||
```sh | ||
curl -XPOST https://spp.etalab.gouv.fr/api/spp/prod/run/ditp-get-data -H "Content-Type: application/json" \ | ||
-H "Authorization: Bearer $API_KEY" \ | ||
-d '{"id":"123"}' | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
FROM python:3.12-slim | ||
|
||
RUN groupadd --gid 1100 albert | ||
RUN useradd --home /home/albert --gid 1100 --uid 1100 albert | ||
USER albert | ||
|
||
WORKDIR /home/albert | ||
ADD ./pyproject.toml ./pyproject.toml | ||
RUN pip install . | ||
ADD ./app /home/albert/app | ||
ENV PYTHONPATH="/home/albert/app:${PYTHONPATH}" | ||
Check warning on line 11 in app/Dockerfile GitHub Actions / Build and push from main/c646b82e5868b09a4f6c551e9c6c1933b42d63c7Variables should be defined before their use
|
||
ENV PATH="/home/albert/.local/bin:${PATH}" |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from contextlib import asynccontextmanager | ||
import datetime as dt | ||
import json | ||
from typing import List, Union | ||
import uuid | ||
|
||
from fastapi import Body, Depends, FastAPI, HTTPException, Response, Security | ||
from redis import Redis | ||
import requests | ||
from starlette.middleware.cors import CORSMiddleware | ||
|
||
from app.config import ( | ||
ALBERT_API_KEY, | ||
ALBERT_BASE_URL, | ||
APP_NAME, | ||
APP_VERSION, | ||
EMBEDDINGS_MODEL, | ||
ENV, | ||
LANGUAGE_MODEL, | ||
) | ||
from app.deps import get_redis | ||
from app.schemas import ExpId, ExpIdWithText | ||
from app.security import check_api_key | ||
from app.subscriptions import Listener | ||
|
||
|
||
def init_redis(r: Redis): | ||
app.state.listener = Listener(r, ["spp-exp-channel"]) | ||
app.state.listener.start() | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
# Startup code | ||
if ENV != "unittest": | ||
r = next(get_redis(finally_close=False)) | ||
init_redis(r) | ||
|
||
request = requests.get(f"{ALBERT_BASE_URL}/models", headers={"Authorization": f"Bearer {ALBERT_API_KEY}"}) | ||
request.raise_for_status() | ||
models = [model["id"] for model in request.json()["data"]] | ||
assert LANGUAGE_MODEL in models, f"Model {LANGUAGE_MODEL} not found" | ||
assert EMBEDDINGS_MODEL in models, f"Model {EMBEDDINGS_MODEL} not found" | ||
|
||
yield | ||
|
||
# Shutdown code | ||
app.state.listener.stop() | ||
|
||
|
||
app = FastAPI(title=APP_NAME, version=APP_VERSION, lifespan=lifespan) | ||
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"]) | ||
|
||
|
||
@app.get("/health") | ||
def healt() -> dict[str, str]: | ||
return Response(status_code=200) | ||
|
||
|
||
@app.post("/anonymize") | ||
def anonymize( | ||
form_data: Union[ExpIdWithText, List[ExpIdWithText]] = Body(...), | ||
redis: Redis = Depends(get_redis), | ||
api_key: str = Security(check_api_key), | ||
): | ||
if not isinstance(form_data, list): | ||
form_data = [form_data] | ||
|
||
for data in form_data: | ||
if not data.id: | ||
# see https://tchap.gouv.fr/#/room/!ZyhOfCwElHmyNMSlcw:agent.dinum.tchap.gouv.fr/$XMeXbIDhGtXBycZu-9Px2frsczn_iU7xiJ5xvjbs-pQ?via=agent.dinum.tchap.gouv.fr&via=agent.externe.tchap.gouv.fr&via=agent.tchap.gouv.fr | ||
data.id = str(uuid.uuid4()) | ||
|
||
data = data.model_dump() | ||
|
||
data["time"] = dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%d %H:%M:%S.%f%z") | ||
print(f"anonymize - {data["id"]}: {data["time"]}") # TODO: replace with logger later | ||
redis.publish("spp-exp-channel", json.dumps(data)) | ||
|
||
if len(form_data) == 1: | ||
responseOutput = {"id": form_data[0].id} | ||
else: | ||
# The spec is ill-defined !! | ||
responseOutput = [{"id": x.id} for x in form_data] | ||
|
||
return {"body": responseOutput} | ||
|
||
|
||
@app.post("/prod/run/ditp-get-data") | ||
def ditp_get_data(form_data: Union[ExpId, List[ExpId]] = Body(...), redis: Redis = Depends(get_redis), api_key: str = Security(check_api_key)): | ||
if not isinstance(form_data, list): | ||
form_data = [form_data] | ||
|
||
answers = [] | ||
for data in form_data: | ||
data = data.model_dump() | ||
answer = redis.get(data["id"]) | ||
answers.append(answer) | ||
|
||
if len(form_data) == 1: | ||
if answers[0] is None: | ||
raise HTTPException(status_code=400, detail="ID not found") | ||
responseOutput = {"generated_answer": answers[0]} | ||
else: | ||
# The spec is ill-defined !! | ||
responseOutput = [{"generated_answer": x} for x in answers] | ||
|
||
return {"body": responseOutput} |
File renamed without changes.
Oops, something went wrong.