|
1 |
| - |
2 | 1 | import os
|
3 | 2 | import uuid
|
4 | 3 | import copy
|
@@ -51,7 +50,6 @@ def __init__(self,
|
51 | 50 | self.app.add_middleware(AxonMiddleware, axon=self)
|
52 | 51 |
|
53 | 52 |
|
54 |
| - |
55 | 53 | class CortexAxonMiddleware(BaseHTTPMiddleware):
|
56 | 54 | """
|
57 | 55 | The `AxonMiddleware` class is a key component in the Axon server, responsible for processing all incoming requests.
|
@@ -85,7 +83,7 @@ def __init__(self, app: "AxonMiddleware", axon: "bittensor.axon"):
|
85 | 83 | self.axon = axon
|
86 | 84 |
|
87 | 85 | async def dispatch(
|
88 |
| - self, request: Request, call_next: RequestResponseEndpoint |
| 86 | + self, request: Request, call_next: RequestResponseEndpoint |
89 | 87 | ) -> Response:
|
90 | 88 | """
|
91 | 89 | Asynchronously processes incoming HTTP requests and returns the corresponding responses. This
|
@@ -115,6 +113,21 @@ async def dispatch(
|
115 | 113 | # Records the start time of the request processing.
|
116 | 114 | start_time = time.time()
|
117 | 115 |
|
| 116 | + if "v1/chat/completions" in request.url.path: |
| 117 | + if request.method == "OPTIONS": |
| 118 | + return await call_next(request) |
| 119 | + try: |
| 120 | + api_key = request.headers.get("Authorization").split(" ")[1] |
| 121 | + if not api_key or api_key not in VALID_API_KEYS: |
| 122 | + return JSONResponse( |
| 123 | + {"detail": "Invalid or missing API Key"}, status_code=401 |
| 124 | + ) |
| 125 | + return await call_next(request) |
| 126 | + except Exception: |
| 127 | + return JSONResponse( |
| 128 | + {"detail": "Invalid or missing API Key"}, status_code=401 |
| 129 | + ) |
| 130 | + |
118 | 131 | try:
|
119 | 132 | # Set up the synapse from its headers.
|
120 | 133 | synapse: bittensor.Synapse = await self.preprocess(request)
|
@@ -410,7 +423,7 @@ async def priority(self, synapse: bittensor.Synapse):
|
410 | 423 | priority_fn = self.axon.priority_fns.get(str(synapse.name), None)
|
411 | 424 |
|
412 | 425 | async def submit_task(
|
413 |
| - executor: PriorityThreadPoolExecutor, priority: float |
| 426 | + executor: PriorityThreadPoolExecutor, priority: float |
414 | 427 | ) -> Tuple[float, Any]:
|
415 | 428 | """
|
416 | 429 | Submits the given priority function to the specified executor for asynchronous execution.
|
@@ -455,10 +468,10 @@ async def submit_task(
|
455 | 468 | raise PriorityException(f"Response timeout after: {synapse.timeout}s")
|
456 | 469 |
|
457 | 470 | async def run(
|
458 |
| - self, |
459 |
| - synapse: bittensor.Synapse, |
460 |
| - call_next: RequestResponseEndpoint, |
461 |
| - request: Request, |
| 471 | + self, |
| 472 | + synapse: bittensor.Synapse, |
| 473 | + call_next: RequestResponseEndpoint, |
| 474 | + request: Request, |
462 | 475 | ) -> Response:
|
463 | 476 | """
|
464 | 477 | Executes the requested function as part of the request processing pipeline. This method calls
|
@@ -499,7 +512,7 @@ async def run(
|
499 | 512 | return response
|
500 | 513 |
|
501 | 514 | async def postprocess(
|
502 |
| - self, synapse: bittensor.Synapse, response: Response, start_time: float |
| 515 | + self, synapse: bittensor.Synapse, response: Response, start_time: float |
503 | 516 | ) -> Response:
|
504 | 517 | """
|
505 | 518 | Performs the final processing on the response before sending it back to the client. This method
|
|
0 commit comments