openai-gemini

repository·main·Indexed 25 days ago

https://github.com/publicaffairs/openai-gemini

A serverless, OpenAI-compatible proxy for the Google Gemini API. It enables developers to use Gemini models with existing tools and software designed for OpenAI by providing compatible endpoints for /chat/completions, /embeddings, and /models. Supports deployment to Vercel, Netlify, Cloudflare Workers, and Deno, as well as local execution via Node.js, Deno, or Bun. Includes features such as web search tool integration, reasoning_effort mapping, and support for Gemini-specific configurations via extra_body.

Tokens
1.7K
Snippets
3
Records
15
Agent score
36%

What's inside openai-gemini

  1. Deploy openai-gemini to various providers

    main

    You can deploy this project as a serverless proxy to provide an OpenAI-compatible endpoint for the Gemini API. You will need a personal Google API key.

    Vercel

    • Use the Deploy with Vercel button.
    • Or use the CLI: vercel deploy.
    • Local development: vercel dev.

    Netlify

    • Use the Deploy to Netlify button.
    • Or use the CLI: netlify deploy.
    • Local development: netlify dev.
    • This provider offers two API bases:
      • /v1 (e.g., /v1/chat/completions)
      • /edge/v1

    Cloudflare

    Deno

  2. Configure OpenAI-compatible software to use the proxy

    main

    To use the proxy, point your software to your deployed API address and provide your Gemini API key. The API base should typically follow this format: https://your-proxy-address.vercel.app/v1

    For command-line tools, you may need to set one of the following environment variables:

    • OPENAI_BASE_URL
    • OPENAI_API_BASE
    OPENAI_BASE_URL="https://my-super-proxy.vercel.app/v1"
    # or
    OPENAI_API_BASE="https://my-super-proxy.vercel.app/v1"
  3. Serve openai-gemini locally

    main

    You can run the proxy locally using Node, Deno, or Bun.

    Node.js

    1. Install dependencies: npm install.
    2. Start the server: npm run start.
    3. For development mode (with watch): npm install --include=dev then npm run dev.

    Deno and Bun

    • Start the server: npm run start:deno or npm run start:bun.
    • Development mode: npm run dev:deno or npm run dev:bun.
    # Node.js example
    npm install
    npm run start
  4. Use Google Search tool in Chat Completions

    main

    You can trigger Google Search capabilities by using specific model suffixes or including the tool in your request.

    1. Model Suffix: Use a model name ending in :search (e.g., gemini-1.5-flash:search).
    2. Preview Models: Models containing -search-preview in their name will automatically have the googleSearch tool enabled.
    3. Explicit Tools: Pass tools in the request body containing { "googleSearch": {} }.
  5. Configure Chat Completions with extra Google features

    main

    When calling POST /chat/completions, you can pass Google-specific configurations using the extra_body.google field in your request. This allows access to features not standard in the OpenAI API.

    Supported extra_body.google keys:

    • safety_settings: Configure content filtering thresholds.
    • cached_content: Use context caching.
    • thinking_config: Configure reasoning/thinking behavior (for supported models).
  6. Supported chat/completions parameters

    main

    The chat/completions endpoint implements most parameters applicable to both APIs:

    • messages (including content, role [system, user, assistant, tool], and tool_calls)
    • model
    • frequency_penalty
    • max_tokens, max_completion_tokens
    • n (supports candidateCount < 8, not for streaming)
    • presence_penalty
    • reasoning_effort
    • response_format (json_object, json_schema, text)
    • seed
    • stop (string or array)
    • stream and stream_options (including include_usage)
    • temperature (Gemini supports up to infinity)
    • top_p
    • tools and tool_choice
    • extra_body (for Gemini-specific features)

    Not implemented: logit_bias, logprobs, top_logprobs, parallel_tool_calls (always active in Gemini), and completions endpoint.

  7. Generate embeddings via POST /embeddings

    main

    Generate vector embeddings for input text.

    Request Parameters:

    • model (string): The model to use (e.g., gemini-embedding-001). If not provided, defaults to gemini-embedding-001.
    • input (string | array<string>): The text to embed.
    • dimensions (number, optional): The output dimensionality.

    Response Format:

    {
      "object": "list",
      "data": [
        {
          "object": "embedding",
          "index": 0,
          "embedding": [0.1, 0.2, ...]
        }
      ],
      "model": "gemini-embedding-001"
    }
  8. List available models via GET /models

    main

    Retrieve a list of available models. The response follows the OpenAI format where model names are stripped of the models/ prefix.

    Response Format:

    {
      "object": "list",
      "data": [
        {
          "id": "gemini-1.5-flash",
          "object": "model",
          "created": 0,
          "owned_by": ""
        }
      ]
    }