Skip to main content

Documentation Index

Fetch the complete documentation index at: https://simplellmfunc.cn/llms.txt

Use this file to discover all available pages before exploring further.

Decorators API Reference

@llm_function

from SimpleLLMFunc import llm_function

@llm_function(
    llm_interface: LLM_Interface,
    toolkit: Optional[List[Tool]] = None,
    max_tool_calls: Optional[int] = None,
    system_prompt_template: Optional[str] = None,
    user_prompt_template: Optional[str] = None,
    **llm_kwargs: Any,
)
Transforms an async function into an LLMFunction callable instance. await fn(...) returns the parsed typed result; fn.stream(...) yields ReactOutput.

Parameters

ParameterTypeDefaultDescription
llm_interfaceLLM_InterfacerequiredModel to call
toolkitList[Tool] | NoneNoneTools available to the model
max_tool_callsint | NoneNoneMaximum tool calls before forcing final answer. None = unlimited
system_prompt_templatestr | NoneNoneOverride the system prompt template
user_prompt_templatestr | NoneNoneOverride the user prompt template
**llm_kwargsAnyForwarded to LLM (temperature, max_tokens, etc.)

Special Call-Time Parameters

ParameterTypeDescription
_template_paramsDict[str, Any]Template values for docstring {placeholders}
_abort_signalAbortSignalCancellation signal

Example

from pydantic import BaseModel, Field
from SimpleLLMFunc import llm_function, OpenAICompatible

llm = OpenAICompatible.load_from_json_file("provider.json")["openrouter"]["gpt-4o"]

class Sentiment(BaseModel):
    label: str = Field(description="positive, negative, or neutral")
    score: float = Field(description="confidence 0.0–1.0")

@llm_function(llm_interface=llm)
async def classify(text: str) -> Sentiment:
    """Classify the sentiment of the text."""
    pass

# Usage
result = await classify("I love this!")
print(result)  # Sentiment(label="positive", score=0.95)

@llm_chat

from SimpleLLMFunc import llm_chat

@llm_chat(
    llm_interface: LLM_Interface,
    toolkit: Optional[List[Tool]] = None,
    max_tool_calls: Optional[int] = DEFAULT_MAX_TOOL_CALLS,
    stream: bool = False,
    strict_signature: bool = False,
    self_reference: Optional[SelfReference] = None,
    self_reference_key: Optional[str] = None,
    **llm_kwargs: Any,
)
Creates an LLMChat callable instance. Calling it yields AsyncGenerator[ReactOutput, None] and gives SelfRef a stable agent identity.

Parameters

ParameterTypeDefaultDescription
llm_interfaceLLM_InterfacerequiredModel to call
toolkitList[Tool] | NoneNoneAvailable tools
max_tool_callsint | Noneframework defaultTool call limit per invocation
streamboolFalseEnable streaming chunks
strict_signatureboolFalseEnforce strict parameter validation
self_referenceSelfReference | NoneNoneExplicit SelfReference backend
self_reference_keystr | NoneNoneAuto-create SelfReference for this key
**llm_kwargsAnyForwarded to LLM

Special Call-Time Parameters

ParameterTypeDescription
_template_paramsDict[str, Any]Template values
_abort_signalAbortSignalCancellation signal
_too_long_to_fileboolTruncate long tool results to temp file

History Parameter

The function must have a parameter named history or chat_history (type: list | None). The framework uses it to carry conversation state.

Example

from SimpleLLMFunc import llm_chat, tool

@tool
async def search(query: str) -> str:
    """Search for information."""
    return f"Result for: {query}"

@llm_chat(llm_interface=llm, toolkit=[search], stream=True)
async def agent(message: str, history: list | None = None):
    """A helpful research assistant."""
    pass

# Usage
history = []
async for output in agent("What is SimpleLLMFunc?", history):
    if is_response_yield(output):
        print(output.response)
        history = output.messages

@tool

from SimpleLLMFunc import tool

@tool(
    name: Optional[str] = None,
    description: Optional[str] = None,
    best_practices: Optional[Sequence[str]] = None,
    prompt_injection_builder: Optional[Callable] = None,
    too_long_to_file: bool = False,
)
Registers an async function as a tool callable by the LLM. Can be used with or without arguments.

Parameters

ParameterTypeDefaultDescription
namestr | Nonefunction nameOverride tool name
descriptionstr | Nonefrom docstringOverride tool description
best_practicesSequence[str] | Nonefrom docstringOverride best practices list
prompt_injection_builderCallable | NoneNoneDynamic system prompt injection
too_long_to_fileboolFalseWrite results > 20k tokens to temp file

Requirements

  • Decorated function must be async def
  • Docstring should include Args, Returns, and Best Practices sections
  • Parameter types become the tool’s JSON schema

Example

from SimpleLLMFunc import tool

@tool
async def calculate(expression: str) -> float:
    """
    Evaluate a math expression.

    Args:
        expression: Python math expression (e.g., "2 ** 10").

    Returns:
        Numeric result.

    Best Practices:
        - Use Python syntax.
        - Keep expressions simple.
    """
    return float(eval(expression))

# Used without parentheses (simple form)
@tool
async def simple_tool(x: str) -> str:
    """Do something."""
    return x.upper()

Tool Class

from SimpleLLMFunc import Tool

tool_instance = Tool(
    name: str,
    description: str,
    func: Callable[..., Awaitable[Any]],
    best_practices: Optional[Sequence[str]] = None,
    prompt_injection_builder: Optional[Callable] = None,
    too_long_to_file: bool = False,
)
For programmatic tool creation without the decorator.

Methods

MethodReturnsDescription
.tool_spec()DictTool specification dict
.to_openai_tool()DictOpenAI-format tool definition
.serialize_tools(tools)List[Dict]Class method: serialize tool list
.build_system_prompt_injection(context)str | NoneDynamic prompt injection

Properties

PropertyTypeDescription
.namestrTool name
.descriptionstrTool description
.parametersList[Parameter]Extracted parameters
.funcCallableThe underlying async function