Skip to content

AI copy generator — generate OG titles and descriptions #25

@anurag629

Description

@anurag629

Phase 3 — First AI Feature (1/2)

Implement the backend logic and prompt engineering for AI-powered OG copy generation.

Scope

System Prompts (`src/lib/ai/prompts.ts`)

Title generation prompt:
```
You are an expert copywriter specializing in social media and OG image text.
Generate {count} compelling, click-worthy title options for an OG image.

Rules:

  • Maximum 60 characters per title (OG images have limited space)
  • Be specific and intriguing, avoid generic phrases
  • Match the tone: {tone} (professional / casual / technical / creative)
  • Category: {category}
  • If context is provided, use it to make titles relevant

Return as a JSON array of strings. No explanation, just the array.
```

Description/subtitle generation prompt:
```
Generate {count} concise subtitle options for an OG image.

Rules:

  • Maximum 80 characters
  • Complement the title: "{title}"
  • Add context without repeating the title
  • Category: {category}

Return as a JSON array of strings. No explanation, just the array.
```

Generation Function (`src/lib/ai/generate.ts`)

  • `generateTitles(context): Promise<string[]>`
    • Inputs: category, topic/brief description, tone, count (default 3)
    • Returns: array of title suggestions
  • `generateSubtitles(context): Promise<string[]>`
    • Inputs: category, title (to complement), tone, count (default 3)
    • Returns: array of subtitle suggestions
  • Parse AI response (handle JSON in markdown code blocks, plain text fallbacks)
  • Retry once on parse failure with a "please return valid JSON" follow-up

Response Parsing

  • AI responses may come wrapped in ```json ... ``` blocks — strip those
  • Validate response is an array of strings
  • Filter out any results exceeding character limits
  • Fallback: if parsing completely fails, return the raw text split by newlines

Acceptance Criteria

  • Title generation returns 3 relevant, correctly-sized suggestions
  • Subtitle generation complements the given title
  • Prompts are category-aware (blog vs product vs event tone)
  • JSON response parsing handles edge cases (code blocks, trailing commas)
  • Works with all 5 providers (same prompts, different API formats)
  • Character limits are enforced on results

Metadata

Metadata

Assignees

No one assigned

    Labels

    aiAI-powered featuresfeatureNew feature requestphase-3First AI feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions