Implementation:ALwrity ALwrity llm text gen
| Knowledge Sources | |
|---|---|
| Domains | NLP, LLM, Text_Generation |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Concrete tool for provider-agnostic LLM text generation provided by the ALwrity backend, routing to Gemini, HuggingFace, OpenAI, or WaveSpeed based on environment and tenant configuration.
Description
The llm_text_gen function is the central text-generation entry point of the ALwrity backend. It abstracts away the choice of LLM provider by inspecting the GPT_PROVIDER environment variable, per-tenant provider configuration, and optional caller-supplied hints (preferred_provider, preferred_hf_models). When a structured JSON response is required, callers pass a json_struct schema and the function delegates to the provider's structured-output mode (e.g. gemini_structured_json_response).
It is used by nearly every content-generating workflow in the repository — Blog Writer, LinkedIn, Podcast, Image Studio prompts, Daily Workflow agents, SEO insights, and onboarding persona generation — making it the single choke point through which all LLM traffic flows.
Usage
Import this function whenever a workflow needs to generate text from a prompt and the choice of provider should be configurable at deploy or tenant time:
- Blog / LinkedIn / Podcast content generation — pass flow_type=premium_tool.
- SIF agent / committee-based daily workflow generation — pass preferred_hf_models and flow_type=sif_agent.
- Structured schema-bound generation (site briefs, personas, SEO insights) — pass json_struct.
- Per-tenant provider override — pass user_id so tenant config can be resolved.
Code Reference
Source Location
- Repository: ALwrity
- File: backend/services/llm_providers/main_text_generation.py
Signature
def llm_text_gen(
prompt: str,
system_prompt: Optional[str] = None,
json_struct: Optional[Dict[str, Any]] = None,
user_id: str = None,
preferred_hf_models: Optional[List[str]] = None,
preferred_provider: Optional[str] = None,
flow_type: Optional[str] = None,
max_tokens: Optional[int] = None,
temperature: Optional[float] = None,
) -> str:
"""
Generate text using a Language Model (LLM) based on the provided prompt.
Args:
prompt: The prompt to generate text from.
system_prompt: Custom system prompt to use instead of the default.
json_struct: JSON schema structure for structured responses.
user_id: Clerk user ID for subscription checking (required).
preferred_hf_models: Preferred HuggingFace models.
preferred_provider: Preferred provider (google, huggingface, wavespeed, openai).
flow_type: Flow type for logging (e.g., 'sif_agent', 'premium_tool').
max_tokens: Max tokens for response. If None, provider default is used.
temperature: Temperature for generation (0.0-1.0). If None, defaults to 0.7.
Returns:
str: Generated text based on the prompt.
Raises:
RuntimeError: If subscription limits are exceeded or user_id is missing.
"""
Import
from services.llm_providers.main_text_generation import llm_text_gen
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| prompt | str | Yes | The generation prompt |
| system_prompt | Optional[str] | No | Custom system prompt |
| json_struct | Optional[Dict[str, Any]] | No | JSON schema for structured output |
| user_id | str | Yes | Clerk user ID for subscription/provider routing |
| preferred_hf_models | Optional[List[str]] | No | Preferred HuggingFace model IDs |
| preferred_provider | Optional[str] | No | Provider hint (google/huggingface/wavespeed/openai) |
| flow_type | Optional[str] | No | sif_agent or premium_tool (logging) |
| max_tokens | Optional[int] | No | Max response tokens (provider default if None) |
| temperature | Optional[float] | No | Sampling temperature (default 0.7) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | str | Generated text; when json_struct is supplied the string is JSON-parseable to the schema |
| side effect | env / tenant config | Reads GPT_PROVIDER, TEXTGEN_AI_MODELS env vars and per-tenant provider config |
Usage Examples
Basic free-text generation
from services.llm_providers.main_text_generation import llm_text_gen
# Generate a blog section from a prompt
text = llm_text_gen(
prompt="Write a 300-word introduction to fine-tuning LLMs.",
user_id="clerk_user_123",
flow_type="premium_tool",
)
print(text)
Structured JSON generation (site brief)
from services.llm_providers.main_text_generation import llm_text_gen
schema = {
"type": "object",
"properties": {
"business_name": {"type": "string"},
"tagline": {"type": "string"},
},
"required": ["business_name", "tagline"],
}
result = llm_text_gen(
prompt="Generate a site brief from the intake data.",
json_struct=schema,
user_id="clerk_user_123",
)
import json
brief = json.loads(result)
print(brief["business_name"])
SIF agent generation with HuggingFace routing
from services.llm_providers.main_text_generation import llm_text_gen
# Committee-based daily workflow generation uses HF models
tasks_json = llm_text_gen(
prompt=plan_prompt,
user_id="clerk_user_123",
preferred_hf_models=["openai/gpt-oss-120b:cerebras"],
flow_type="sif_agent",
)