Implementation:AIScientists Dev WorldSeed generate gazette
| Knowledge Sources | |
|---|---|
| Domains | LLM, Content_Generation |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete LLM gazette generator using LiteLLM + Instructor with JSON mode structured output, returning parsed GazetteContent and generation metadata including token counts and cost estimation.
Description
The generate_gazette function calls the LLM with structured output via Instructor's `Mode.JSON`. It takes a context string (user message) and system prompt, sends them to the specified model, and returns a `GazetteContent` Pydantic model with: edition_title, breaking_banner, lead_story, secondary_stories, editorials, ticker, pull_quote.
The function also extracts token usage from the raw response and computes cost via LiteLLM's price database. Returns `(GazetteContent, metadata_dict)` where metadata includes model, elapsed_s, tokens_in, tokens_out, cost_usd.
A companion estimate_tokens function estimates input tokens and cost without making an LLM call.
Usage
Import this function to generate gazette content. Called by the gazette API route handler.
Code Reference
Source Location
- Repository: WorldSeed
- File: src/worldseed/gazette/generator.py
Signature
async def generate_gazette(
context: str,
system_prompt: str,
model: str,
timeout: float = 120.0,
max_retries: int = 2,
) -> tuple[GazetteContent, dict[str, Any]]:
"""Call LLM to generate gazette content.
Returns (parsed_content, metadata) where metadata includes
model, elapsed_s, tokens_in, tokens_out, cost_usd.
"""
def estimate_tokens(context: str, system_prompt: str, model: str) -> dict[str, Any]: ...
Import
from worldseed.gazette.generator import generate_gazette, estimate_tokens
Usage Examples
import asyncio
from worldseed.gazette.generator import generate_gazette
from worldseed.gazette.context import load_run_data, build_context, build_system_prompt
data = load_run_data("run_001")
context = build_context(data, "en")
system = build_system_prompt("en")
content, meta = asyncio.run(generate_gazette(
context=context,
system_prompt=system,
model="openai/gpt-4o",
timeout=120.0,
))
print(f"Edition: {content.edition_title}")
print(f"Lead: {content.lead_story.headline}")
print(f"Cost: ${meta['cost_usd']:.4f}")
Related Pages
Implements Principle
- Principle:AIScientists_Dev_WorldSeed_Structured_LLM_Output
- Principle:AIScientists_Dev_WorldSeed_Gazette_Context_Building