LLM Integration Pipeline
LLM Integration Pipeline
Udility Diffuser leverages a multi-stage LLM pipeline to bridge the gap between abstract natural language descriptions and precise SVG (Scalable Vector Graphics) code. By utilizing high-parameter models via OpenRouter, the system ensures that generated illustrations are both syntactically correct and educationally relevant.
Core Model & Provider
The pipeline is powered by the Hermes-3-Llama-3.1-405B model, hosted via OpenRouter. This model was selected for its superior reasoning capabilities and its ability to follow complex structural instructions, which is critical for generating valid SVG markup.
- Provider: OpenRouter
- Model String:
nousresearch/hermes-3-llama-3.1-405b - Primary Engine: Meta Llama 3.1 (405B parameter version)
Orchestration Workflow
The integration follows a sequential two-step inference process to maximize the detail and accuracy of the output:
- Instruction Synthesis: The user's prompt is first sent to the LLM to generate a "visual blueprint." This stage expands a simple prompt (e.g., "Amoeba lifecycle") into a detailed set of technical instructions for drawing specific shapes, labels, and coordinates.
- SVG Code Generation: The visual blueprint is then fed back into the LLM with a specialized system prompt. This step forces the model to act as a code generator, outputting raw
<svg>code based on the synthesized instructions.
Public Interface
Users interact with the pipeline primarily through the generate_image_from_text function. This high-level wrapper manages the API calls to OpenRouter, processes the SVG output, and handles the conversion to a viewable image format.
generate_image_from_text
Generates an illustrative PNG image from a text-based prompt.
Parameters:
| Parameter | Type | Description |
| :--- | :--- | :--- |
| text_description | str | The educational or illustrative concept to visualize. |
| output_filename | str | (Optional) The name of the resulting PNG file. Defaults to output.png. |
Returns:
- Type:
None - Effect: Saves a PNG file to the local directory and displays it using
matplotlib.
Usage Example:
from Udility import diffuser
# The pipeline executes: Prompt -> Instructions -> SVG -> PNG
diffuser.generate_image_from_text(
"A diagram showing the water cycle with labels",
output_filename="water_cycle.png"
)
Configuration & Environment
To interface with the pipeline, the environment must be configured with a valid OpenRouter API key. This key is used to authenticate the OpenAI client instance that Udility uses internally.
import os
# Required for the LLM pipeline to authenticate
os.environ['OPENROUTER_API_KEY'] = 'your_api_key_here'
Internal Pipeline Components
While the following functions are called internally by the main diffuser, they represent the logic stages of the integration:
get_detailed_instructions(text_description): Interacts with the LLM to expand the user prompt into a technical drawing guide.generate_svg_from_instructions(detailed_instructions): Converts the drawing guide into valid, raw SVG XML code.
Error Handling
The pipeline includes built-in exception handling for common integration issues:
EnvironmentError: Raised if theOPENROUTER_API_KEYis missing.RuntimeError: Raised if the LLM fails to return a valid response or if the SVG conversion process (viacairosvg) encounters malformed code generated by the model.