Skip to content

A lightweight Go microservice that generates 1536-dimensional text embeddings using either real OpenAI SmallEmbedding-3 or deterministic mock embeddings.

Notifications You must be signed in to change notification settings

ai-backend-course/ai-embedding-microservice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Embedding Microservice

A lightweight Go microservice that generates 1536-dimensional text embeddings using either real OpenAI SmallEmbedding-3 or deterministic mock embeddings.

Features

  • POST /embed -> returns an embedding vector
  • Real OpenAI mode (USE_MOCK_EMBEDDINGS=false)
  • Deterministic mock mode (USE_MOCK_EMBEDDINGS=true)
  • Go Fiber server
  • Zerolog request logging
  • Metrics middleware
  • Clean Dockerfile with CA certificates
  • Fly.io production-ready
  • .env.example (safe) + .env (local only)

Endpoint: POST /embed

Request Body

{
  "text": "My sample text to embed."
}

Example Response (Real OpenAI)

{
  "embedding": [0.0123, -0.9932, ... 1536 floats ...]
}

Example Response (Mock Mode)

{
  "embedding": [0.3321, 0.9922, 0.1010, ... deterministic ...]
}

Environment Variables

.env (local only — never committed)

OPENAI_API_KEY=your_real_key_here
USE_MOCK_EMBEDDINGS=true
PORT=8080

.env.example (safe to commit)

OPENAI_API_KEY=your_openai_api_key_here
USE_MOCK_EMBEDDINGS=false
PORT=8080

Run Locally

Install dependencies:

go mod tidy

Start the server:

go run .

Test:

curl -X POST http://localhost:8080/embed   -H "Content-Type: application/json"   -d '{"text":"embedding test"}'

Deploy to Fly.io

Set secrets:

flyctl secrets set OPENAI_API_KEY="your_real_key" -a ai-embedding-microservice
flyctl secrets set USE_MOCK_EMBEDDINGS=false -a ai-embedding-microservice

Deploy:

flyctl deploy -a ai-embedding-microservice

Project Structure

ai-embedding-microservice/
├── main.go
├── go.mod
├── .gitignore
├── .dockerignore
├── .env.example
├── Dockerfile
├── internal/
│   ├── ai/
│   │   ├── embeddings.go
│   │   └── select.go
│   ├── handlers/
│   │   └── embed_handler.go
│   └── middleware/
│       ├── logger.go
│       └── metrics.go

About

A lightweight Go microservice that generates 1536-dimensional text embeddings using either real OpenAI SmallEmbedding-3 or deterministic mock embeddings.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published