Skip to content

ai-backend-course/ai-generative-service

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Generative AI Service

A lightweight, production-ready Generative AI microservice built in Go. This service exposes a simple, prompt-driven API for generating text using an LLM, designed to act as a foundational building block in a larger AI backend system.

This service intentionally focuses on generation only — it does not perform retrieval, orchestration, evaluation, or task-specific transformations. Those responsibilities live in other services.


Purpose

This service exists to provide a clean, reusable interface for text generation, suitable for:

  • Explanations and drafting
  • General-purpose completions
  • Backend-driven AI features
  • Use by RAG pipelines, workflows, or automation tools

It reflects how production AI backends typically isolate LLM access behind a dedicated service.


What Makes This Generative

  • Prompt-driven (no hard-coded task assumptions)
  • Produces new text, not transformations
  • Provider-agnostic via an LLM client interface
  • Reusable across many contexts

Architecture (High-Level)

Client
  |
  | POST /generate
  v
Generative AI Service
  |
  |-- LLM Client Interface
        |
        |-- OpenAI (real)
        |-- Mock (local/testing)

The HTTP layer is completely decoupled from the LLM provider.


API

POST /generate

Generates text based on a prompt and optional system context.

Request

{
  "system": "You are a helpful backend tutor.",
  "prompt": "Explain pgvector in simple terms.",
  "max_tokens": 300,
  "temperature": 0.7
}

Response

{
  "output": "pgvector is a Postgres extension that allows...",
  "model": "gpt-4o-mini",
  "tokens_used": 279,
  "request_id": "fc2c9a9e-e950-4526-a3f0-05994b5dc44e"
}

Configuration

Environment variables:

PORT=8080
OPENAI_API_KEY=your_api_key_here
OPENAI_MODEL=gpt-4o-mini
LLM_MODE=real   # or mock

LLM_MODE

  • real → Uses OpenAI
  • mock → Returns deterministic mock output (no API calls)

Local Development

Mock mode

LLM_MODE=mock go run .
curl -X POST http://localhost:8080/generate \
  -H "Content-Type: application/json" \
  -d '{"prompt":"Explain pgvector simply"}'

Real mode

LLM_MODE=real OPENAI_API_KEY=sk-... go run .

How This Fits Into the AI Backend Stack

This service is designed to be called by other services, including:

  • RAG pipelines (for answer generation)
  • Workflow / orchestration services
  • Automation tools (n8n, Zapier, internal systems)

It intentionally avoids embedding higher-level logic so it remains composable and reusable.


Tech Stack

  • Go
  • Fiber v2
  • OpenAI API
  • Docker
  • Fly.io (deployment)

Design Philosophy

  • Clear responsibility boundaries
  • Simple, inspectable APIs
  • Provider abstraction
  • Production-minded defaults
  • Easy to rebuild from memory

Possible Extensions

  • Streaming responses
  • Model switching
  • Prompt templates
  • Usage metrics
  • Centralized AI gateway

Final Note

This project prioritizes clarity and correctness over complexity. It represents how generative AI is typically exposed in real backend systems — as a focused service that other components build upon.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors