diff --git a/proxy/routes/anthropic.py b/proxy/routes/anthropic.py index d4a8c26..a5882b7 100644 --- a/proxy/routes/anthropic.py +++ b/proxy/routes/anthropic.py @@ -53,7 +53,6 @@ async def anthropic_proxy( k: v for k, v in request.headers.items() if k.lower() not in IGNORED_HEADERS } headers["accept-encoding"] = "identity" - if request.headers.get( "invariant-authorization" ) is None and "|invariant-auth:" not in request.headers.get(HEADER_AUTHORIZATION): diff --git a/tests/anthropic/test_anthropic_header_with_invariant_key.py b/tests/anthropic/test_anthropic_header_with_invariant_key.py new file mode 100644 index 0000000..20040d8 --- /dev/null +++ b/tests/anthropic/test_anthropic_header_with_invariant_key.py @@ -0,0 +1,64 @@ +from unittest.mock import patch +import os +import anthropic +from httpx import Client +import datetime + +import pytest +import sys +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) + +from util import * # needed for pytest fixtures + +pytest_plugins = ("pytest_asyncio") +@pytest.mark.skipif(not os.getenv("ANTHROPIC_API_KEY"), reason="No ANTHROPIC_API_KEY set") +async def test_header( + context, proxy_url, explorer_api_url +): + anthropic_api_key = os.getenv("ANTHROPIC_API_KEY") + dataset_name = "claude_header_test" + str( + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ) + print("before patch:",anthropic_api_key) + with patch.dict(os.environ, {"ANTHROPIC_API_KEY": anthropic_api_key + "|invariant-auth: "}): + print("after patch:",os.environ.get("ANTHROPIC_API_KEY")) + client = anthropic.Anthropic( + http_client=Client(), + base_url = f"{proxy_url}/api/v1/proxy/{dataset_name}/anthropic", + ) + response = client.messages.create( + model="claude-3-5-sonnet-20241022", + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Give me an introduction to Zurich within 200 words." + } + ] + ) + + assert response is not None + response_text = response.content[0].text + assert "zurich" in response_text.lower() + + traces_response = await context.request.get( + f"{explorer_api_url}/api/v1/dataset/byuser/developer/{dataset_name}/traces" + ) + traces = await traces_response.json() + assert len(traces) == 1 + + trace_id = traces[0]["id"] + get_trace_response = await context.request.get( + f"{explorer_api_url}/api/v1/trace/{trace_id}" + ) + trace = await get_trace_response.json() + assert trace["messages"] == [ + { + "role": "user", + "content": "Give me an introduction to Zurich within 200 words." + }, + { + "role": "assistant", + "content": response_text + } + ] \ No newline at end of file diff --git a/tests/anthropic/test_anthropic_with_tool_call.py b/tests/anthropic/test_anthropic_with_tool_call.py index 56be242..30ecf37 100644 --- a/tests/anthropic/test_anthropic_with_tool_call.py +++ b/tests/anthropic/test_anthropic_with_tool_call.py @@ -156,7 +156,7 @@ class WeatherAgent: return response @pytest.mark.skipif(not os.getenv("ANTHROPIC_API_KEY"), reason="No ANTHROPIC_API_KEY set") -async def test_chat_completion_without_streaming( +async def test_response_with_toolcall( context, explorer_api_url, proxy_url ): """Test the chat completion without streaming for the weather agent.""" @@ -213,7 +213,7 @@ async def test_chat_completion_without_streaming( @pytest.mark.skipif(not os.getenv("ANTHROPIC_API_KEY"), reason="No ANTHROPIC_API_KEY set") -async def test_chat_completion_with_streaming( +async def test_streaming_response_with_toolcall( context, explorer_api_url, proxy_url ): """Test the chat completion with streaming for the weather agent.""" diff --git a/tests/anthropic/test_anthropic_without_tool_call.py b/tests/anthropic/test_anthropic_without_tool_call.py index 91995f6..a2d05f3 100644 --- a/tests/anthropic/test_anthropic_without_tool_call.py +++ b/tests/anthropic/test_anthropic_without_tool_call.py @@ -10,7 +10,7 @@ from util import * # needed for pytest fixtures pytest_plugins = ("pytest_asyncio") @pytest.mark.skipif(not os.getenv("ANTHROPIC_API_KEY"), reason="No ANTHROPIC_API_KEY set") -async def test_chat_completion_without_streaming( +async def test_response_without_toolcall( context, explorer_api_url,proxy_url ): dataset_name = "claude_streaming_response_without_toolcall_test" + str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")) @@ -71,7 +71,8 @@ async def test_chat_completion_without_streaming( async def test_streaming_response_without_toolcall( context, explorer_api_url, - proxy_url): + proxy_url + ): dataset_name = "claude_streaming_response_without_toolcall_test" + str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")) invariant_api_key = os.environ.get("INVARIANT_API_KEY","None")