Skip to content

Add support for multiple LLM providers (OpenAI & Gemini)#237

Open
subhranil2605 wants to merge 1 commit into
Capgemini:mainfrom
subhranil2605:feature/gemini-support
Open

Add support for multiple LLM providers (OpenAI & Gemini)#237
subhranil2605 wants to merge 1 commit into
Capgemini:mainfrom
subhranil2605:feature/gemini-support

Conversation

@subhranil2605

Copy link
Copy Markdown

Summary

Adds support for Google Gemini as an alternative LLM provider alongside OpenAI. Introduces a provider abstraction layer (llm-provider.ts) that allows switching between providers via environment configuration without changing application logic.

Key Changes

New Files

  • src/gemini.ts (318 lines) — Gemini API integration with structured output support

    • createFormWithGemini() — Generate form structure from descriptions
    • updateFormWithGemini() — Update forms with user feedback
    • generateSuggestionsWithGemini() — Suggest form improvements
    • judgeFormWithGemini() — Evaluate form quality
  • src/llm-provider.ts (107 lines) — Provider abstraction/router layer

    • Routes all LLM calls based on LLM_PROVIDER environment variable
    • Maintains provider-agnostic interface for routes
    • Extensible for future providers (Claude, etc.)
  • src/utils/nunjucks-setup.ts (72 lines) — Extracted nunjucks configuration utility

    • Consolidates template engine setup logic
    • Shared between main server and zip-download server

Modified Files

  • .env.example — Added LLM_PROVIDER=openai and GEMINI_API_KEY variables
  • src/validationSchemas/env.ts — Added provider and Gemini API key validation
  • src/routes/prototype-routes.ts — Updated 3 function calls to use llm-provider.ts
  • server.ts & data/zip-download/server.ts — Refactored to use shared nunjucks utility
  • package.json — Added @google/generative-ai dependency

Technical Details

Provider Abstraction Architecture

All LLM calls route through a single llm-provider.ts layer:
User Request

llm-provider.createForm(envVars, ...)

├─ If 'openai' → calls openai.ts functions
└─ If 'gemini' → calls gemini.ts functions

Returns JSON (same schema, any provider)

  • Both providers use identical JSON schemas from data/*-schema.json
  • No changes required to route handlers
  • Easy to extend with new providers

Configuration

Variable Default Required When
LLM_PROVIDER openai Never (defaults)
GEMINI_API_KEY When using Gemini

Supported Providers

  • OpenAI — GPT-4 mini, production-ready, ~$0.15 per 1M tokens input
  • Gemini — Gemini 2.0 Flash, cost-effective, ~$0.075 per 1M tokens input

Testing

  • ✅ All existing unit tests pass (628 tests)
  • ✅ All E2E tests pass (Playwright)
  • ✅ Provider abstraction maintains existing test coverage
  • ✅ New code compatible with mocked LLM providers

Test results: No breaking changes, all tests pass with provider abstraction

Migration Path

Fully backward compatible:

  • Default provider is OpenAI (no configuration changes needed)
  • To use Gemini:
    1. Set LLM_PROVIDER=gemini
    2. Set GEMINI_API_KEY=your-api-key
  1. Can rollback anytime by changing LLM_PROVIDER back to openai

Dependencies Added

  • @google/generative-ai — Official Google SDK for Gemini API

- Introduced new environment variables for Gemini API key and model ID in .env.example and validation schema.
- Created llm-provider.ts to handle form creation, updates, and suggestions using either Gemini or OpenAI.
- Refactored nunjucks setup into a separate utility file for better organization.
- Updated prototype routes to utilize the new LLM provider functions.
- Added new functions in gemini.ts for interacting with the Gemini API.
@subhranil2605 subhranil2605 requested a review from cmenon12 as a code owner June 8, 2026 15:42
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.

1 participant