A Python API to serve data from the knowledge graph for the Sustainable Energy Academy.
Warning
The package is currently undergoing a major revamp. Some features may be missing or not working as intended. Feel free to open an issue.
Follow the steps below to run the API locally.
- Clone the repository and navigate to the project folder.
- Create and activate a virtual environment.
- Create and populate the
.envfile based on.env.example. - Run
make installto install project dependencies. - To launch the API, run
make run. The API will be running at http://127.0.0.1:8000.
git clone https://github.com/UNDP-Data/dsc-sea-ai-api
cd dsc-sea-ai-api
python -m venv .venv
source .venv/bin/activate
make install
make run
# INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)For Azure Blob auth you can use either:
STORAGE_SAS_URL(full SAS URL), orSTORAGE_ACCOUNT_NAMEwithSTORAGE_ACCOUNT_KEY/STORAGE_SAS_TOKEN.
Optional local tester env (separate tester app):
KG_TESTER_API_KEY(or fallbackAPI_KEY) for tester proxy authentication to backend.KG_TESTER_LOCAL_API_BASE_URLfor local backend target (defaulthttp://127.0.0.1:8000).KG_TESTER_REMOTE_API_BASE_URLto prefill remote backend target (defaulthttps://sea-ai-api.azurewebsites.net).
All protected endpoints require X-Api-Key.
GET /graph?query=<text>[&hops=<int>]- Response:
nodes[]withname,description,neighbourhood,weight,colouredges[]withsubject,predicate,object,description,weight
GET /graph/v2?query=<text>- Response:
nodes[]withname,description,tier,weight,colourtieris one of:central,secondary,periphery
edges[]withsubject,predicate,object,description,weightlevelis not returned in the API payload.
POST /model[?graph_version=v1|v2]graph_versiondefault isv2.- Request body must include at least one message; empty lists return
400. - Response includes
X-Request-Idheader for request correlation. - The stream is NDJSON; graph and text generation run in parallel.
- Chunk order is not fixed. The graph chunk usually arrives early, but clients must handle it arriving before, during, or after text deltas.
- To avoid stalled streams under slow storage/model conditions,
/modelapplies:MODEL_GRAPH_TIMEOUT_SECONDS(graph build timeout),MODEL_STREAM_IDLE_TIMEOUT_SECONDS(max idle gap between streamed chunks),MODEL_TOOLS_PREP_TIMEOUT_SECONDS(SQL/RAG tool preparation timeout),MODEL_STREAM_WATCHDOG_SECONDS(overall no-progress watchdog for stream completion),RETRIEVE_CHUNKS_TIMEOUT_SECONDS(RAG chunk retrieval timeout).
- This controls the schema of
graphreturned in streamed/modelchunks:v1: legacy graph schema (neighbourhoodon nodes,levelon edges)v2: staged graph schema (tieron nodes, no edgelevel)
Knowledge graph logic is versioned under:
src/kg/v1.pyfor V1 graph assemblysrc/kg/v2.pyfor V2 staged graph assemblysrc/kg/types.pyfor V2 response models
Compatibility adapters are retained:
src/entities_v2.pysrc/graph_v2.py
The KG tester is now a separate frontend app in frontend/. The backend API deployment
does not include the tester route.
- Start the API backend locally (
make run). - Start the tester frontend app (
make run-tester). - Open http://127.0.0.1:8010/kg-tester.
- Enter:
- target (
Local serverorRemote API), - graph version (
default,v1, orv2, sent asgraph_versionto/modelwhen selected), - a graph
query(for exampleclimate change mitigation), - remote API base URL (only when target is
Remote API).
- target (
- Submit to call
/modeland view:- streamed answer text (delta chunks),
- query ideas (clickable chips that trigger a new query),
- graph payload returned in the stream,
- an interactive D3 force graph,
- the raw JSON response payload.
Interaction shortcuts:
- Press
Enterin the form to run the current query. - Click a graph node to run a follow-up query (
tell me more about <node>). - Click an idea chip to run that suggestion immediately.
Notes:
- The tester proxy uses
KG_TESTER_API_KEY(orAPI_KEY) from environment and does not expose API keys in browser JavaScript. - The tester proxy forwards
X-Request-Id(or generates one) to simplify backend trace correlation. - The tester app calls backend APIs server-to-server, so browser CORS is not required for remote targets.
- No extra CORS proxy is needed when using the standalone tester app.
- The tester app is local-only by design (loopback clients only).
Example local startup:
# terminal 1
make run
# terminal 2
export KG_TESTER_API_KEY="$API_KEY"
make run-testermake run-tester binds to 127.0.0.1:8010 by default.
Pre-commit validation:
python3 -m py_compile main.py src/security.py src/database.py src/genai.py src/entities.py src/kg/__init__.py src/kg/v1.py src/kg/v2.py frontend/kg_tester_app.py tests/test_model.py tests/test_genai.py
make test # requires dev dependencies installedTo evaluate V2 output quality over a representative query set:
python3 scripts/evaluate_graph_v2.py --api-key "$API_KEY"Optional flags:
--queries-file path/to/queries.txtfor custom query sets--output /tmp/graph_v2_eval.jsonto persist full metrics
The project is hooked up to CI/CD via GitHub Actions.
- Workflow:
.github/workflows/azure-webapps-python.yml - Azure Web App name:
sea-ai-api - A push to
maintriggers deployment. - The
frontend/KG tester app is intentionally separate and is not required for API deployment.
All contributions must follow Conventional Commits.
The codebase is formatted with black and isort. Use the provided Makefile for these
routine operations.
- Clone or fork the repository
- Create a new branch (
git checkout -b feature-branch) - Make your changes
- Ensure your code is properly formatted (
make format) - Commit your changes (
git commit -m 'Add some feature') - Push to the branch (
git push origin feature-branch) - Open a pull request
This project is licensed under the BSD 3-Clause License. See the LICENSE file.