Component
Invoker-based component templates used across Gen AI applications.
GenericLMComponent(lm_invoker, fallback_lms=None)
Bases: LMComponent
A general-purpose LM component.
Examples:
Basic usage
component = GenericLMComponent.from_config(model_id="openai/gpt-5.4-nano")
response = await component.run(query="What is the capital of France?")
With custom prompt templates
component = GenericLMComponent.from_config(
model_id="openai/gpt-5.4-nano",
system_template="Talk like a {role}.",
)
response = await component.run(query="What is the capital of France?", role="pirate")
Attributes:
| Name | Type | Description |
|---|---|---|
prompt_vars |
set[str]
|
Required prompt variables. |
default_system_template |
str
|
Default system template. |
default_user_template |
str
|
Default user template. |
invoke(query, history=None, extra_contents=None, hyperparameters=None, event_emitter=None, max_calls=1, **prompt_kwargs)
async
Invokes the language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The query to be passed to the language model. |
required |
history
|
list[Message] | None
|
Conversation history to include in the prompt. Defaults to None. |
None
|
extra_contents
|
list[MessageContent] | None
|
Extra user contents for prompt formatting. Defaults to None. |
None
|
hyperparameters
|
dict[str, Any] | None
|
Hyperparameters passed to the language model invocation. Defaults to None. |
None
|
event_emitter
|
EventEmitter | None
|
Event emitter for streaming outputs. Defaults to None. |
None
|
max_calls
|
int
|
The max number of times the language model can be invoked. Defaults to 1. |
1
|
**prompt_kwargs
|
Any
|
Additional prompt variables. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
LMOutput |
LMOutput
|
The output of the language model. |
LMComponent(lm_invoker, fallback_lms=None)
Bases: Component
A template component for LM-based components used across Gen AI applications.
Single-slot components keep the existing API: define class-level prompt fields and pass one invoker.
Multi-slot components declare lm_slots and pass a mapping of slot names to invokers.
Examples:
Define a single-slot LM component class
class MyLMComponent(LMComponent):
prompt_vars = {"context", "query"}
default_system_template = "Answer the query based on the following context: {context}"
default_user_template = "{query}"
Define a multi-slot LM component class
class MapReduceComponent(LMComponent):
lm_slots = {
"map": LMComponentSlot(
prompt_vars={"context", "query"},
default_system_template="Map the context for query: {query}",
default_user_template="{context}",
),
"reduce": LMComponentSlot(
prompt_vars={"context", "query"},
default_system_template="Reduce the mapped context for query: {query}",
default_user_template="{context}",
),
}
Init with LM invoker
component = MyLMComponent(lm_invoker=lm_invoker)
component = MapReduceComponent(lm_invoker={"map": map_invoker, "reduce": reduce_invoker})
Init with LM configuration
component = MyLMComponent.from_config(
model_id="openai/gpt-5.4-nano",
config={"response_schema": ResponseSchema},
)
Attributes:
| Name | Type | Description |
|---|---|---|
prompt_vars |
set[str]
|
Required prompt variables for the implicit default slot. |
default_system_template |
str
|
Default system template for the implicit default slot. |
default_user_template |
str
|
Default user template for the implicit default slot. |
lm_slots |
dict[str, LMComponentSlot] | None
|
Explicit named slots for multi-LM components. |
fallback_lms |
list[BaseLMInvoker] | dict[str, list[BaseLMInvoker]]
|
Ordered fallback invokers. |
Initializes LMComponent with one or more LM invokers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lm_invoker
|
BaseLMInvoker | dict[str, BaseLMInvoker]
|
The language model invoker or slot mapping. |
required |
fallback_lms
|
list[BaseLMInvoker] | dict[str, list[BaseLMInvoker]] | None
|
Ordered fallback invokers. Lists map to the implicit default slot. Dicts map to explicit slots. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
|
fallback_lms
property
Returns the fallback LMs.
Returns:
| Type | Description |
|---|---|
list[BaseLMInvoker] | dict[str, list[BaseLMInvoker]]
|
list[BaseLMInvoker] | dict[str, list[BaseLMInvoker]]: The fallback LMs. |
lm_invoker
property
Returns the default LM invoker.
Returns:
| Type | Description |
|---|---|
BaseLMInvoker | dict[str, BaseLMInvoker]
|
BaseLMInvoker | dict[str, BaseLMInvoker]: The default LM invoker. |
from_config(model_id, credentials=None, config=None, system_template=None, user_template=None, prompt_builder_kwargs=None, fallback_lms=None, **kwargs)
classmethod
Builds an LM invoker from config and initializes the component.
This method can be used to initialize the component with a model id and its configuration. Currently only supported for single-slot components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str | ModelId
|
The model ID. |
required |
credentials
|
str | dict[str, Any] | None
|
Model credentials. Defaults to None. |
None
|
config
|
dict[str, Any] | None
|
Model configuration. Defaults to None. |
None
|
system_template
|
str | None
|
System template. Defaults to None. |
None
|
user_template
|
str | None
|
User template. Defaults to None. |
None
|
prompt_builder_kwargs
|
dict[str, Any] | None
|
Prompt builder kwargs. Defaults to None. |
None
|
fallback_lms
|
list[BaseLMInvoker] | None
|
Ordered fallback invokers. Defaults to None. |
None
|
**kwargs
|
Any
|
Additional constructor kwargs for subclasses. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new LMComponent instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If prompt_builder_kwargs is provided without system_template or user_template. |
LMComponentSlot
Bases: BaseModel
Prompt contract for one named LM slot in an LMComponent.
Attributes:
| Name | Type | Description |
|---|---|---|
prompt_vars |
set[str]
|
Required prompt variables for the slot. |
default_system_template |
str
|
Default system template for the slot. |
default_user_template |
str
|
Default user template for the slot. |