mellea.stdlib.session
mellea.stdlib.session.backend_name_to_class(name: str)
mellea.stdlib.session.start_session(backend_name: Literal['ollama', 'hf', 'openai', 'watsonx'] = 'ollama', model_id: str | ModelIdentifier = IBM_GRANITE_3_3_8B, ctx: Context | None = SimpleContext(), model_options: dict | None = None, **backend_kwargs)
backend_name
: str
: ollama | hf | openaimodel_id
: ModelIdentifier
: a ModelIdentifier
from the mellea.backends.model_ids modulectx
: Optional[Context]
: If not provided, a LinearContext
is used.model_options
: Optional[dict]
: Backend will be instantiated with these as its default, if provided.backend_kwargs
: kwargs that will be passed to the backend for instantiation.class mellea.stdlib.session.MelleaSession(backend: Backend, ctx: Context | None = None)
m
convenience functions with NO special semantics.
Using a Mellea session is not required, but it does represent the “happy path” of Mellea programming. Some nice things about ussing a MelleaSession
:
Instruction
or Chat
object and then later calling backend.generate on the object.MelleaSession
s and managing your Context and Backend directly.
Note: we put the instruct
, validate
, and other convenience functions here instead of in Context
or Backend
to avoid import resolution issues.
backend
: Backend
: This is always required.ctx
: Context
: The way in which the model’s context will be managed. By default, each interaction with the model is a stand-alone interaction, so we use SimpleContext as the default.model_options
: Optional[dict]
: model options, which will upsert into the model/backend’s defaults.mellea.stdlib.session.MelleaSession._push_model_state(new_backend: Backend, new_model_opts: dict)
Context
can be temporarily changed. This method changes the model’s backend and model_opts, while saving the current settings in the self._backend_stack
.
Question: should this logic be moved into context? I really want to keep Session
as simple as possible… see true motivation in the docstring for the class.
mellea.stdlib.session.MelleaSession._pop_model_state()
Context
can be temporarily changed by pushing and popping from the model state.
This function restores the model’s previous backend and model_opts from the self._backend_stack
.
Question: should this logic be moved into context? I really want to keep Session
as simple as possible… see true motivation in the docstring for the class.
mellea.stdlib.session.MelleaSession.reset()
mellea.stdlib.session.MelleaSession.summarize()
mellea.stdlib.session.MelleaSession.instruct(description: str, requirements: list[Requirement | str] | None = None, icl_examples: list[str | CBlock] | None = None, grounding_context: dict[str, str | CBlock | Component] | None = None, user_variables: dict[str, str] | None = None, prefix: str | CBlock | None = None, output_prefix: str | CBlock | None = None, strategy: SamplingStrategy | None = None, return_sampling_results: bool = False, format: type[BaseModelSubclass] | None = None, model_options: dict | None = None, tool_calls: bool = False)
description
: The description of the instruction.requirements
: A list of requirements that the instruction can be validated against.icl_examples
: A list of in-context-learning examples that the instruction can be validated against.grounding_context
: A list of grounding contexts that the instruction can use. They can bind as variables using a (key
: str, value: str | ContentBlock) tuple.user_variables
: A dict of user-defined variables used to fill in Jinja placeholders in other parameters. This requires that all other provided parameters are provided as strings.prefix
: A prefix string or ContentBlock to use when generating the instruction.output_prefix
: A string or ContentBlock that defines a prefix for the output generation. Usually you do not need this.strategy
: A SamplingStrategy that describes the strategy for validating and repairing/retrying for the instruct-validate-repair pattern. None means that no particular sampling strategy is used.return_sampling_results
: attach the (successful and failed) sampling attempts to the results.format
: If set, the BaseModel to use for constrained decoding.model_options
: Additional model options, which will upsert into the model/backend’s defaults.tool_calls
: If true, tool calling is enabled.mellea.stdlib.session.MelleaSession.chat(content: str, role: Message.Role = 'user', user_variables: dict[str, str] | None = None, format: type[BaseModelSubclass] | None = None, model_options: dict | None = None, tool_calls: bool = False)
mellea.stdlib.session.MelleaSession.act(c: Component, tool_calls: bool = False)
mellea.stdlib.session.MelleaSession.validate(reqs: Requirement | list[Requirement], output: CBlock | None = None, return_full_validation_results: bool = False, format: type[BaseModelSubclass] | None = None, model_options: dict | None = None, generate_logs: list[GenerateLog] | None = None)
mellea.stdlib.session.MelleaSession.req(*args, **kwargs)
mellea.stdlib.session.MelleaSession.check(*args, **kwargs)
mellea.stdlib.session.MelleaSession.load_default_aloras()
mellea.stdlib.session.MelleaSession.genslot(gen_slot: Component, model_options: dict | None = None, format: type[BaseModelSubclass] | None = None, tool_calls: bool = False)
gen_slot
: GenerativeSlot Component
: A generative slotModelOutputThunk
: Output thunkmellea.stdlib.session.MelleaSession.query(obj: Any, query: str, format: type[BaseModelSubclass] | None = None, model_options: dict | None = None, tool_calls: bool = False)
obj
: The object to be queried. It should be an instance of MObject or can be converted to one if necessary.query
: The string representing the query to be executed against the object.format
: format for output parsing.model_options
: Model options to pass to the backend.tool_calls
: If true, the model may make tool calls. Defaults to False.ModelOutputThunk
: The result of the query as processed by the backend.mellea.stdlib.session.MelleaSession.transform(obj: Any, transformation: str, format: type[BaseModelSubclass] | None = None, model_options: dict | None = None)
obj
: The object to be queried. It should be an instance of MObject or can be converted to one if necessary.transformation
: The string representing the query to be executed against the object.mellea.stdlib.session.MelleaSession._call_tools(result: ModelOutputThunk)
mellea.stdlib.session.MelleaSession.last_prompt()