Skip to content
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

Hotfix/check api key #516

Merged
merged 7 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.api.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
VALIDATOR_API = "0.0.0.0:8094" # The validator API to forward responses to for scoring
2 changes: 1 addition & 1 deletion api_keys.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{}
{}
1 change: 1 addition & 0 deletions shared/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
15 changes: 13 additions & 2 deletions validator_api/gpt_endpoints.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
Expand Down
Loading