diff --git a/README.md b/README.md index 75e581f..2e39748 100644 --- a/README.md +++ b/README.md @@ -34,3 +34,8 @@ To integrate Explorer Proxy with your AI agent, you’ll need to modify how your ### **🔹 Anthropic Integration** Coming Soon! + +### Run +docker compose -f docker-compose.local.yml down +&& docker compose -f docker-compose.local.yml up -d --build +&& docker logs explorer-proxy-explorer-proxy-1 -f \ No newline at end of file diff --git a/proxy/routes/anthropic.py b/proxy/routes/anthropic.py index 6e1f61b..544a6cb 100644 --- a/proxy/routes/anthropic.py +++ b/proxy/routes/anthropic.py @@ -1,5 +1,43 @@ """Proxy service to forward requests to the Anthropic APIs""" -from fastapi import APIRouter +from fastapi import APIRouter, Header, HTTPException, Depends, Request proxy = APIRouter() + +ALLOWED_ANTHROPIC_ENDPOINTS = {"v1/messages"} +MISSING_INVARIANT_AUTH_HEADER = "Missing invariant-authorization header" +MISSING_AUTH_HEADER = "Missing authorization header" +NOT_SUPPORTED_ENDPOINT = "Not supported OpenAI endpoint" +FAILED_TO_PUSH_TRACE = "Failed to push trace to the dataset: " + + +def validate_headers( + invariant_authorization: str = Header(None), authorization: str = Header(None) +): + """Require the invariant-authorization and authorization headers to be present""" + if invariant_authorization is None: + raise HTTPException(status_code=400, detail=MISSING_INVARIANT_AUTH_HEADER) + # if authorization is None: + # raise HTTPException(status_code=400, detail=MISSING_AUTH_HEADER) + +@proxy.post( + "/{dataset_name}/anthropic/{endpoint:path}", + dependencies=[Depends(validate_headers)], +) +async def anthropic_proxy( + dataset_name: str, + endpoint: str, + request: Request, +): + """Proxy calls to the Anthropic APIs""" + if endpoint not in ALLOWED_ANTHROPIC_ENDPOINTS: + raise HTTPException(status_code=404, detail=NOT_SUPPORTED_ENDPOINT) + + headers = { + k: v for k, v in request.headers.items() + } + headers["accept-encoding"] = "identity" + + request_body = await request.body() + + print("request_body", request_body) diff --git a/proxy/routes/open_ai.py b/proxy/routes/open_ai.py index 883bdfc..76b061d 100644 --- a/proxy/routes/open_ai.py +++ b/proxy/routes/open_ai.py @@ -56,6 +56,8 @@ async def openai_proxy( request_body = await request.body() + print("request_body", request_body) + async with httpx.AsyncClient() as client: open_ai_request = client.build_request( "POST",