yf176 commited on
Commit ·
118def6
1
Parent(s): 2dd4f3d
fix: keep Gemini credentials out of request URLs
Browse files
src/polyhedra/services/llm_service.py
CHANGED
|
@@ -301,7 +301,7 @@ class GeminiAdapter(LLMAdapter):
|
|
| 301 |
try:
|
| 302 |
response = await client.post(
|
| 303 |
url,
|
| 304 |
-
|
| 305 |
json=request,
|
| 306 |
)
|
| 307 |
response.raise_for_status()
|
|
|
|
| 301 |
try:
|
| 302 |
response = await client.post(
|
| 303 |
url,
|
| 304 |
+
headers={"x-goog-api-key": self.api_key},
|
| 305 |
json=request,
|
| 306 |
)
|
| 307 |
response.raise_for_status()
|
tests/test_services/test_llm_service.py
CHANGED
|
@@ -242,7 +242,7 @@ class TestGeminiAdapter:
|
|
| 242 |
request = httpx.Request(
|
| 243 |
"POST",
|
| 244 |
"https://generativelanguage.googleapis.com/v1beta/models/test"
|
| 245 |
-
":generateContent
|
| 246 |
)
|
| 247 |
response = httpx.Response(503, request=request)
|
| 248 |
mock_client = AsyncMock()
|
|
@@ -258,6 +258,9 @@ class TestGeminiAdapter:
|
|
| 258 |
|
| 259 |
assert str(exc_info.value) == "Gemini API returned HTTP 503"
|
| 260 |
assert "secret-test-key" not in str(exc_info.value)
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
@pytest.mark.asyncio
|
| 263 |
async def test_structured_completion_uses_native_json_mode(self):
|
|
@@ -279,6 +282,10 @@ class TestGeminiAdapter:
|
|
| 279 |
assert request["generationConfig"] == {
|
| 280 |
"responseMimeType": "application/json",
|
| 281 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
def test_calculate_cost_gpt4_turbo(self):
|
| 284 |
"""Test cost calculation for GPT-4 Turbo."""
|
|
|
|
| 242 |
request = httpx.Request(
|
| 243 |
"POST",
|
| 244 |
"https://generativelanguage.googleapis.com/v1beta/models/test"
|
| 245 |
+
":generateContent",
|
| 246 |
)
|
| 247 |
response = httpx.Response(503, request=request)
|
| 248 |
mock_client = AsyncMock()
|
|
|
|
| 258 |
|
| 259 |
assert str(exc_info.value) == "Gemini API returned HTTP 503"
|
| 260 |
assert "secret-test-key" not in str(exc_info.value)
|
| 261 |
+
call = mock_client.post.call_args
|
| 262 |
+
assert "key" not in call.kwargs.get("params", {})
|
| 263 |
+
assert call.kwargs["headers"] == {"x-goog-api-key": "secret-test-key"}
|
| 264 |
|
| 265 |
@pytest.mark.asyncio
|
| 266 |
async def test_structured_completion_uses_native_json_mode(self):
|
|
|
|
| 282 |
assert request["generationConfig"] == {
|
| 283 |
"responseMimeType": "application/json",
|
| 284 |
}
|
| 285 |
+
assert mock_client.post.call_args.kwargs["headers"] == {
|
| 286 |
+
"x-goog-api-key": "test-key",
|
| 287 |
+
}
|
| 288 |
+
assert "params" not in mock_client.post.call_args.kwargs
|
| 289 |
|
| 290 |
def test_calculate_cost_gpt4_turbo(self):
|
| 291 |
"""Test cost calculation for GPT-4 Turbo."""
|