diff --git a/.env.api.example b/.env.api.example index eaf06982..dcd8ea1d 100644 --- a/.env.api.example +++ b/.env.api.example @@ -2,4 +2,4 @@ API_PORT = "42170" # Port for the API server API_HOST = "0.0.0.0" # Host for the API server SCORING_KEY = "123" # The scoring key for the validator (must match the scoring key in the .env.validator file) SCORE_ORGANICS = True # Whether to score organics -VALIDATOR_API = "0.0.0.0:8094" # The validator API to forward responses to for scoring \ No newline at end of file +VALIDATOR_API = "0.0.0.0:8094" # The validator API to forward responses to for scoring diff --git a/api_keys.json b/api_keys.json index 9e26dfee..0967ef42 100644 --- a/api_keys.json +++ b/api_keys.json @@ -1 +1 @@ -{} \ No newline at end of file +{} diff --git a/shared/logging.py b/shared/logging.py index f2fdbd15..6da5e1de 100644 --- a/shared/logging.py +++ b/shared/logging.py @@ -223,6 +223,7 @@ def log_event(event: BaseEvent): reinit_wandb() unpacked_event = unpack_events(event) unpacked_event = convert_arrays_to_lists(unpacked_event) + logger.debug(f"""LOGGING WANDB EVENT: {unpacked_event}""") wandb.log(unpacked_event) diff --git a/validator_api/gpt_endpoints.py b/validator_api/gpt_endpoints.py index 34681f0e..3614e865 100644 --- a/validator_api/gpt_endpoints.py +++ b/validator_api/gpt_endpoints.py @@ -1,6 +1,7 @@ +import json import random -from fastapi import APIRouter, Request +from fastapi import APIRouter, Depends, Header, HTTPException, Request from loguru import logger from starlette.responses import StreamingResponse @@ -9,9 +10,19 @@ router = APIRouter() +# load api keys from api_keys.json +with open("api_keys.json", "r") as f: + _keys = json.load(f) + + +def validate_api_key(api_key: str = Header(...)): + if api_key not in _keys: + raise HTTPException(status_code=403, detail="Invalid API key") + return _keys[api_key] + @router.post("/v1/chat/completions") -async def completions(request: Request): +async def completions(request: Request, api_key: str = Depends(validate_api_key)): """Main endpoint that handles both regular and mixture of miners chat completion.""" try: body = await request.json()