Update the format where the user passes the invariant key in the llm provider key header. (#18)

This commit is contained in:
Hemang Sarkar
2025-03-06 10:57:32 +01:00
committed by GitHub
parent 73f42e7938
commit adfabd5b12
5 changed files with 11 additions and 11 deletions
+3 -3
View File
@@ -177,7 +177,7 @@ https://explorer.invariantlabs.ai/api/v1/gateway/{add-your-dataset-name-here}/op
Set the API Key using the following format:
```text
{your-llm-api-key}|invariant-auth: {your-invariant-api-key}
{your-llm-api-key};invariant-auth={your-invariant-api-key}
```
> **Note:** Do not include the curly braces `{}`.
@@ -209,8 +209,8 @@ Run `sweagent` with the following flag:
Instead of setting your API Key normally, modify the environment variable as follows:
```bash
export OPENAI_API_KEY={your-openai-api-key}|invariant-auth: {your-invariant-api-key}
export ANTHROPIC_API_KEY={your-anthropic-api-key}|invariant-auth: {your-invariant-api-key}
export OPENAI_API_KEY={your-openai-api-key};invariant-auth={your-invariant-api-key}
export ANTHROPIC_API_KEY={your-anthropic-api-key};invariant-auth={your-invariant-api-key}
```
> **Note:** Do not include the curly braces `{}`.
+3 -3
View File
@@ -66,12 +66,12 @@ async def anthropic_v1_messages_gateway(
# In such cases, the Invariant API Key is passed as part of the
# x-api-key header with the Anthropic API key.
# The header in that case becomes:
# "x-api-key": "<Anthropic API Key>|invariant-auth: <Invariant API Key>"
# "x-api-key": "<Anthropic API Key>;invariant-auth=<Invariant API Key>"
invariant_authorization = None
if dataset_name:
if request.headers.get(
INVARIANT_AUTHORIZATION_HEADER
) is None and "|invariant-auth:" not in request.headers.get(
) is None and ";invariant-auth=" not in request.headers.get(
ANTHROPIC_AUTHORIZATION_HEADER
):
raise HTTPException(status_code=400, detail=MISSING_INVARIANT_AUTH_API_KEY)
@@ -81,7 +81,7 @@ async def anthropic_v1_messages_gateway(
)
else:
header_value = request.headers.get(ANTHROPIC_AUTHORIZATION_HEADER)
api_keys = header_value.split("|invariant-auth: ")
api_keys = header_value.split(";invariant-auth=")
invariant_authorization = f"Bearer {api_keys[1].strip()}"
# Update the authorization header to pass the Anthropic API Key
headers[ANTHROPIC_AUTHORIZATION_HEADER] = f"{api_keys[0].strip()}"
+3 -3
View File
@@ -63,12 +63,12 @@ async def openai_chat_completions_gateway(
# In such cases, the Invariant API Key is passed as part of the
# authorization header with the OpenAI API key.
# The header in that case becomes:
# "authorization": "<OpenAI API Key>|invariant-auth: <Invariant API Key>"
# "authorization": "<OpenAI API Key>;invariant-auth=<Invariant API Key>"
invariant_authorization = None
if dataset_name:
if request.headers.get(
INVARIANT_AUTHORIZATION_HEADER
) is None and "|invariant-auth:" not in request.headers.get(
) is None and ";invariant-auth=" not in request.headers.get(
OPENAI_AUTHORIZATION_HEADER
):
raise HTTPException(status_code=400, detail=MISSING_INVARIANT_AUTH_API_KEY)
@@ -79,7 +79,7 @@ async def openai_chat_completions_gateway(
)
else:
header_value = request.headers.get(OPENAI_AUTHORIZATION_HEADER)
api_keys = header_value.split("|invariant-auth: ")
api_keys = header_value.split(";invariant-auth=")
invariant_authorization = f"Bearer {api_keys[1].strip()}"
# Update the authorization header to pass the OpenAI API Key to the OpenAI API
headers[OPENAI_AUTHORIZATION_HEADER] = f"{api_keys[0].strip()}"
@@ -31,7 +31,7 @@ async def test_gateway_with_invariant_key_in_anthropic_key_header(
os.environ,
{
"ANTHROPIC_API_KEY": anthropic_api_key
+ "|invariant-auth: <not needed for test>"
+ ";invariant-auth=<not needed for test>"
},
):
client = anthropic.Anthropic(
@@ -183,7 +183,7 @@ async def test_chat_completion_with_invariant_key_in_openai_key_header(
openai_api_key = os.getenv("OPENAI_API_KEY")
with patch.dict(
os.environ,
{"OPENAI_API_KEY": openai_api_key + "|invariant-auth: <not needed for test>"},
{"OPENAI_API_KEY": openai_api_key + ";invariant-auth=<not needed for test>"},
):
client = OpenAI(
http_client=Client(),