Implementation:AIScientists Dev WorldSeed LiteLLMDMProvider judge
| Knowledge Sources | |
|---|---|
| Domains | LLM, API_Integration |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete DM provider that wraps LiteLLM multi-provider LLM calls with Instructor's Pydantic-validated structured output, using function-calling mode for DM judgments with fallback model support and token/cost tracking.
Description
The LiteLLMDMProvider class is the production DM implementation. It uses:
- LiteLLM: Unified LLM API client supporting 100+ providers. Model format: `"provider/model-name"`.
- Instructor: Wraps LLM calls with Pydantic validation via `Mode.TOOLS` (function calling). The `DMJudgment` Pydantic model defines the expected output: `narrative` (str) and `effects` (list[EffectConfig]).
Key features:
- Lazy initialization: The Instructor client is created on first `judge()` call, not at construction.
- Fallback model: If a `fallback_model` is configured, the provider tries the primary model first, then the fallback on failure.
- Token tracking: Extracts `prompt_tokens` and `completion_tokens` from the raw response.
- Call counting: Tracks `call_count` and `total_tokens` for the session.
The `DMProvider` is defined as a `Protocol` — any object with an async `judge()` method satisfies it, enabling mock providers for testing.
Usage
Import this class to create a DM provider. Pass to `WorldEngine.__init__()` as the `dm_provider` parameter.
Code Reference
Source Location
- Repository: WorldSeed
- File: src/worldseed/dm/providers/llm.py
Signature
class LiteLLMDMProvider:
def __init__(
self,
model: str,
timeout: float = 10.0,
max_retries: int = 2,
fallback_model: str | None = None,
) -> None: ...
async def judge(self, context: DMContext) -> DMResponse:
"""Call LLM to get DM judgment with structured output."""
class DMProvider(Protocol):
async def judge(self, context: DMContext) -> DMResponse: ...
Import
from worldseed.dm.providers.llm import LiteLLMDMProvider
from worldseed.dm.providers.base import DMProvider # Protocol
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | Yes | LLM model ID (e.g. "openai/gpt-4o") |
| timeout | float | No | Request timeout (default 10.0s) |
| max_retries | int | No | Instructor retry count (default 2) |
| fallback_model | str | No | Secondary model on primary failure |
Outputs
| Name | Type | Description |
|---|---|---|
| judge() returns | DMResponse | narrative (str), effects (list[EffectConfig]), tokens_in, tokens_out |
Usage Examples
Basic DM Provider
from worldseed.dm.providers.llm import LiteLLMDMProvider
from worldseed.world import WorldEngine
dm = LiteLLMDMProvider(model="openai/gpt-4o", timeout=15.0)
engine = WorldEngine(config_path="configs/teahouse.yaml", dm_provider=dm)
With Fallback Model
dm = LiteLLMDMProvider(
model="openai/gpt-4o",
fallback_model="anthropic/claude-3-5-sonnet",
timeout=15.0,
max_retries=3,
)
Direct Judge Call
import asyncio
from worldseed.dm.builder import DMContextBuilder
builder = DMContextBuilder(store, event_log, config)
ctx = builder.build(action, dm_config, tick=1)
response = asyncio.run(dm.judge(ctx))
print(f"Narrative: {response.narrative}")
print(f"Effects: {len(response.effects)}")
print(f"Tokens: {response.tokens_in}/{response.tokens_out}")
Related Pages
Implements Principle
- Principle:AIScientists_Dev_WorldSeed_Structured_LLM_Output
- Principle:AIScientists_Dev_WorldSeed_DM_Resolution
Requires Environment
- Environment:AIScientists_Dev_WorldSeed_Python_3_11_Engine
- Environment:AIScientists_Dev_WorldSeed_LiteLLM_DM_Provider