Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:4thfever Cultivation world simulator call llm with task name

From Leeroopedia
Knowledge Sources
Domains LLM_Applications, Infrastructure
Last Updated 2026-10-22 14:00 GMT

Overview

Concrete tool for dispatching LLM calls by task name with automatic mode selection, test-mode fallback, template rendering, and JSON response parsing, provided by the Cultivation World Simulator unified LLM client.

Description

The call_llm_with_task_name function is the primary LLM dispatch point in the application. It:

  1. Checks if test mode is enabled; if so, returns `resolve_test_mode_task(task_name, infos)` — a deterministic, rule-based fallback
  2. Looks up the LLM mode (NORMAL, FAST, etc.) for the task name from config
  3. Loads the template file, fills it with `infos` dict
  4. Calls the LLM API and parses the JSON response

This centralizes all LLM calls, ensuring test mode never hits a real provider, retry logic is handled at the lowest level, and errors are classified consistently.

Usage

Import and call this function when:

  • AI decisions: `LLMAI._decide` calls it with `task_name="action_decision"`
  • Story generation: `StoryTeller.tell_story` calls it with `task_name="story_teller"`
  • World lore rewriting: WorldLoreRewriteRunner uses it for lore rewrite tasks
  • Any LLM task: new tasks must be registered in config and have a test-mode fallback

Code Reference

Source Location

Signature

async def call_llm_with_task_name(
    task_name: str,
    template_path: Path | str,
    infos: dict,
    max_retries: int | None = None
) -> dict:
    """
    根据任务名称自动选择 LLM 模式并调用
    
    Args:
        task_name: 任务名称,用于在 config.yml 中查找对应的模式
        template_path: 模板路径
        infos: 模板参数
        max_retries: 最大重试次数
        
    Returns:
        dict: LLM 返回的 JSON 数据
    """

Import

from src.utils.llm import call_llm_with_task_name

I/O Contract

Inputs

Name Type Required Description
task_name str Yes Task key for mode lookup (e.g., "action_decision", "story_teller")
template_path str Yes Path to locale-specific template file
infos dict Yes Template variable dict
max_retries None No Override retry count (default: per config)

Outputs

Name Type Description
return dict Parsed JSON response from LLM (or test-mode fallback)

Usage Examples

Call LLM for AI Decision

from src.utils.llm import call_llm_with_task_name
from src.utils.config import CONFIG

template_path = CONFIG.paths.templates / "ai.txt"
result = await call_llm_with_task_name(
    "action_decision",
    template_path,
    {
        "avatar_name": "张三",
        "avatar_info": avatar_info,
        "world_info": world_info,
        "general_action_infos": action_catalog,
    }
)
print(result["avatar_name"])  # "张三"
print(result["action_name_params_pairs"])  # [["Respire", {}], ...]

Test Mode (Automatic)

# When test_mode is enabled via llm_test_mode_scope(True),
# call_llm_with_task_name automatically returns resolve_test_mode_task()
# without hitting a real LLM provider.
with llm_test_mode_scope(True):
    result = await call_llm_with_task_name("action_decision", path, info)
    # result is deterministic, rule-based

Related Pages

Implements Principle

Required Environments

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment