Skip to content

Conversation

dataopsnick
Copy link
Owner

Description

This pull request extends the existing Lambda proxy to support the Google Gemini API alongside the current OpenAI functionality. The implementation adheres to the provided gold standard for Gemini API interaction and ensures that existing OpenAI features remain unaffected.

Changes Implemented

  • Installed @google/generative-ai: Added the necessary library for Gemini API communication.
  • Updated openai_servers.yaml: Added a gemini configuration entry, including API token and model specification, while omitting the URL as per client library behavior.
  • Created src/gemini_settings.ts: Introduced a GeminiSettings interface for type-safe Gemini configurations.
  • Modified src/llm_client.ts:
    • Updated the constructor to accept OpenAiSettings | GeminiSettings.
    • Differentiated client initialization based on settings type (url presence for OpenAI).
    • Implemented chatCompletionStreaming for Gemini, precisely mimicking the provided gold standard example (using getGenerativeModel, startChat, and sendMessageStream).
    • Preserved existing OpenAI streaming and non-streaming functionality.
  • Modified src/llm_proxy.ts:
    • Adapted the transformGenerator and formatChunk functions to handle stream data from both OpenAI and Gemini.
    • Updated getLlmClient to correctly instantiate LlmClient with either OpenAiSettings or GeminiSettings.
    • Changed the constructor to accept OpenAiServerSettings | Record<string, GeminiSettings>.
  • Modified src/app_settings.ts:
    • Replaced getOpenAiServerSettings with getAllServerSettings.
    • Updated getAllServerSettings to correctly parse and differentiate between OpenAI and Gemini configurations from openai_servers.yaml based on the presence of the url key.
  • Modified src/index.ts:
    • Updated to use the new getAllServerSettings function.
  • Updated src/tests/index.test.ts:
    • Added new unit and integration tests specifically for the Gemini API streaming functionality.
    • Ensured existing tests for OpenAI/Replicate continue to pass.
    • Updated test setup to use getAllServerSettings.
  • Updated package.json:
    • Added @google/generative-ai and web-streams-polyfill to dependencies.
    • Modified the build script in package.json to mark @google/genai as an external dependency for esbuild.
  • Updated package-lock.json: Reflects new dependencies.

How to Test

  1. Ensure your openai_servers.yaml has a valid gemini entry with an API key and model.
  2. Deploy the Lambda function.
  3. Send requests to the /gemini/v1/chat/completions endpoint.
  4. Verify that streaming responses are received correctly from the Gemini API.
  5. Verify that existing OpenAI endpoints continue to function as expected.
  6. Run npm test to ensure all unit and integration tests pass.

This implementation fulfills the objective of integrating Gemini support while maintaining existing functionalities.

@dataopsnick dataopsnick linked an issue May 31, 2025 that may be closed by this pull request
@dataopsnick dataopsnick merged commit 85a2876 into main May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gemini API not supported
1 participant