Compare commits

...
Sign in to create a new pull request.

2 commits

Author SHA1 Message Date
Ishaan Jaff
a332cc1861 add support for using google ai sdk with litellm 2024-11-22 10:56:42 -08:00
Ishaan Jaff
d047a9b2c9 e2e testing for /gemini pass through 2024-11-22 10:47:55 -08:00
4 changed files with 31 additions and 2 deletions

View file

@ -2075,6 +2075,7 @@ class SpecialHeaders(enum.Enum):
openai_authorization = "Authorization"
azure_authorization = "API-Key"
anthropic_authorization = "x-api-key"
google_ai_studio_authorization = "x-goog-api-key"
class LitellmDataForBackendLLMCall(TypedDict, total=False):

View file

@ -95,6 +95,11 @@ anthropic_api_key_header = APIKeyHeader(
auto_error=False,
description="If anthropic client used.",
)
google_ai_studio_api_key_header = APIKeyHeader(
name=SpecialHeaders.google_ai_studio_authorization.value,
auto_error=False,
description="If google ai studio client used.",
)
def _get_bearer_token(
@ -197,6 +202,9 @@ async def user_api_key_auth( # noqa: PLR0915
anthropic_api_key_header: Optional[str] = fastapi.Security(
anthropic_api_key_header
),
google_ai_studio_api_key_header: Optional[str] = fastapi.Security(
google_ai_studio_api_key_header
),
) -> UserAPIKeyAuth:
from litellm.proxy.proxy_server import (
general_settings,
@ -233,6 +241,8 @@ async def user_api_key_auth( # noqa: PLR0915
api_key = azure_api_key_header
elif isinstance(anthropic_api_key_header, str):
api_key = anthropic_api_key_header
elif isinstance(google_ai_studio_api_key_header, str):
api_key = google_ai_studio_api_key_header
elif pass_through_endpoints is not None:
for endpoint in pass_through_endpoints:
if endpoint.get("path", "") == route:

View file

@ -61,10 +61,13 @@ async def gemini_proxy_route(
fastapi_response: Response,
):
## CHECK FOR LITELLM API KEY IN THE QUERY PARAMS - ?..key=LITELLM_API_KEY
api_key = request.query_params.get("key")
google_ai_studio_api_key = request.query_params.get("key") or request.headers.get(
"x-goog-api-key"
)
user_api_key_dict = await user_api_key_auth(
request=request, api_key="Bearer {}".format(api_key)
request=request,
google_ai_studio_api_key_header=google_ai_studio_api_key,
)
base_target_url = "https://generativelanguage.googleapis.com"

View file

@ -0,0 +1,15 @@
import google.generativeai as genai
import pytest
genai.configure(
api_key="sk-1234",
client_options={"api_endpoint": "http://0.0.0.0:4000/gemini"},
transport="rest",
)
def test_basic_non_streaming():
model = genai.GenerativeModel("gemini-1.5-flash")
response = model.generate_content("Explain how AI works")
print("response", response)
assert response.text is not None