rigging.completion
Completions work with isolated strings of text pre and post generation.
Completion(text: str, generated: str, generator: t.Optional[Generator] = None, **kwargs: t.Any)
#
Bases: BaseModel
Represents a completed text generation.
Parameters:
-
text
(str
) –The original text.
-
generated
(str
) –The generated text.
-
generator
(Optional[Generator]
, default:None
) –The generator associated with this completion.
-
**kwargs
(Any
, default:{}
) –Additional keyword arguments (typically used for serialization).
Source code in rigging/completion.py
all: str
property
#
Returns both the text and the generation.
error: t.Optional[Exception] = Field(None, exclude=True, repr=False)
class-attribute
instance-attribute
#
Holds any exception that was caught during the generation pipeline.
extra: dict[str, t.Any] = Field(default_factory=dict, repr=False)
class-attribute
instance-attribute
#
Any additional information from the generation.
failed: bool = Field(False, exclude=False, repr=False)
class-attribute
instance-attribute
#
Indicates whether conditions during generation were not met. This is typically used for graceful error handling when parsing.
generated: str
instance-attribute
#
The generated text.
generator: t.Optional[Generator] = Field(None, exclude=True, repr=False)
class-attribute
instance-attribute
#
The generator associated with the completion.
generator_id: str | None
property
#
The identifier of the generator used to create the completion
metadata: dict[str, t.Any] = Field(default_factory=dict)
class-attribute
instance-attribute
#
Additional metadata for the completion.
params: t.Optional[GenerateParams] = Field(None, exclude=True, repr=False)
class-attribute
instance-attribute
#
Any additional generation params used for this completion.
stop_reason: StopReason = Field(default='unknown')
class-attribute
instance-attribute
#
The reason the generation stopped.
text: str
instance-attribute
#
The original text.
timestamp: datetime = Field(default_factory=datetime.now, repr=False)
class-attribute
instance-attribute
#
The timestamp when the completion was created.
usage: t.Optional[Usage] = Field(None, repr=False)
class-attribute
instance-attribute
#
The usage statistics for the generation if available.
uuid: UUID = Field(default_factory=uuid4)
class-attribute
instance-attribute
#
The unique identifier.
clone(*, only_messages: bool = False) -> Completion
#
Creates a deep copy of the completion.
Source code in rigging/completion.py
continue_(text: str) -> CompletionPipeline
#
Alias for the rigging.completion.Completion.fork with include_all=True
.
fork(text: str, *, include_all: bool = False) -> CompletionPipeline
#
Forks the completion by creating calling rigging.completion.Completion.restart and appends the specified text.
Parameters:
-
text
(str
) –The text to append.
Returns:
-
CompletionPipeline
–A new instance of the pipeline with the specified messages added.
Source code in rigging/completion.py
meta(**kwargs: t.Any) -> Completion
#
Updates the metadata of the completion with the provided key-value pairs.
Parameters:
-
**kwargs
(Any
, default:{}
) –Key-value pairs representing the metadata to be updated.
Returns:
-
Completion
–The updated completion object.
Source code in rigging/completion.py
restart(*, generator: t.Optional[Generator] = None, include_all: bool = False) -> CompletionPipeline
#
Attempt to convert back to a CompletionPipeline for further generation.
Parameters:
-
generator
(Optional[Generator]
, default:None
) –The generator to use for the restarted completion. Otherwise the generator from the original CompletionPipeline will be used.
-
include_all
(bool
, default:False
) –Whether to include the generation before the next round.
Returns: The restarted completion.
Raises:
-
ValueError
–If the completion was not created with a CompletionPipeline and no generator is provided.
Source code in rigging/completion.py
CompletionPipeline(generator: Generator, text: str, *, params: t.Optional[GenerateParams] = None, watch_callbacks: t.Optional[list[WatchCompletionCallback]] = None)
#
Pipeline to manipulate and produce completions.
Source code in rigging/completion.py
errors_to_fail_on: set[type[Exception]] = set()
instance-attribute
#
The list of exceptions to catch during generation if you are including or skipping failures.
ExhuastedMaxRounds is implicitly included.
generator: Generator = generator
instance-attribute
#
The generator object responsible for generating the completion.
metadata: dict[str, t.Any] = {}
instance-attribute
#
Additional metadata associated with the completion.
on_failed: FailMode = 'raise'
instance-attribute
#
How to handle failures in the pipeline unless overriden in calls.
params = params
instance-attribute
#
The parameters for generating the completion.
text = text
instance-attribute
#
The text to be completed.
add(text: str) -> CompletionPipeline
#
Appends new text to the internal text before generation.
Parameters:
-
text
(str
) –The text to be added to the completion.
Returns:
-
CompletionPipeline
–The updated CompletionPipeline object.
Source code in rigging/completion.py
apply(**kwargs: str) -> CompletionPipeline
#
Applies keyword arguments to the text using string template substitution.
Note
This produces a clone of the CompletionPipeline, leaving the original unchanged.
Parameters:
-
**kwargs
(str
, default:{}
) –Keyword arguments to be applied to the text.
Returns:
-
CompletionPipeline
–A new instance of CompletionPipeline with the applied arguments.
Source code in rigging/completion.py
catch(*errors: type[Exception], on_failed: FailMode | None = None) -> CompletionPipeline
#
Adds exceptions to catch during generation when including or skipping failures.
Parameters:
-
*errors
(type[Exception]
, default:()
) –The exception types to catch.
-
on_failed
(FailMode | None
, default:None
) –How to handle failures in the pipeline unless overriden in calls.
Returns:
-
CompletionPipeline
–The updated ChatPipeline object.
Source code in rigging/completion.py
clone(*, only_text: bool = False) -> CompletionPipeline
#
Creates a clone of the current CompletionPipeline
instance.
Parameters:
-
only_text
(bool
, default:False
) –If True, only the text will be cloned. If False (default), the entire
CompletionPipeline
instance will be cloned including until callbacks, types, and metadata.
Returns:
-
CompletionPipeline
–A new instance of
CompletionPipeline
that is a clone of the current instance.
Source code in rigging/completion.py
fork(text: str) -> CompletionPipeline
#
Creates a new instance of CompletionPipeline
by forking the current completion and adding the specified text.
This is a convenience method for calling clone().add(text)
.
Parameters:
-
text
(str
) –The text to be added to the new completion.
Returns:
-
CompletionPipeline
–A new instance of
CompletionPipeline
with the specified text added.
Source code in rigging/completion.py
map(callback: MapCompletionCallback) -> CompletionPipeline
#
Registers a callback to be executed after the generation process completes.
Note
You must return a list of completion objects from the callback which will represent the state of completions for the remainder of the callbacks and return.
async def process(completions: list[Completion]) -> list[Completion]:
...
await pipeline.map(process).run()
Parameters:
-
callback
(MapCompletionCallback
) –The callback function to be executed.
Returns:
-
CompletionPipeline
–The current instance of the completion.
Source code in rigging/completion.py
meta(**kwargs: t.Any) -> CompletionPipeline
#
Updates the metadata of the completion with the provided key-value pairs.
Parameters:
-
**kwargs
(Any
, default:{}
) –Key-value pairs representing the metadata to be updated.
Returns:
-
CompletionPipeline
–The updated completion object.
Source code in rigging/completion.py
run(*, allow_failed: bool = False, on_failed: FailMode | None = None) -> Completion
async
#
Execute the generation process to produce the final chat.
Parameters:
-
allow_failed
(bool
, default:False
) –Ignore any errors and potentially return the chat in a failed state.
-
on_failed
(FailMode | None
, default:None
) –The behavior when a message fails to generate. (this is used as an alternative to allow_failed)
Returns:
-
Completion
–The generated Chat.
Source code in rigging/completion.py
run_batch(many: t.Sequence[str], params: t.Sequence[t.Optional[GenerateParams]] | None = None, *, on_failed: FailMode = 'raise') -> list[Completion]
async
#
Executes the generation process accross multiple input messages.
Note
Anything already in this pending completion will be prepended to the text.
Parameters:
-
many
(Sequence[str]
) –A sequence of texts to generate with.
-
params
(Sequence[Optional[GenerateParams]] | None
, default:None
) –A sequence of parameters to be used for each text.
-
on_failed
(FailMode
, default:'raise'
) –How to handle failures in the pipeline unless overriden in calls.
Returns:
-
list[Completion]
–A list of generatated Completions.
Source code in rigging/completion.py
run_many(count: int, *, params: t.Sequence[t.Optional[GenerateParams]] | None = None, on_failed: FailMode | None = None) -> list[Completion]
async
#
Executes the generation process multiple times with the same inputs.
Parameters:
-
count
(int
) –The number of times to execute the generation process.
-
params
(Sequence[Optional[GenerateParams]] | None
, default:None
) –A sequence of parameters to be used for each execution.
-
on_failed
(FailMode | None
, default:None
) –How to handle failures in the pipeline unless overriden in calls.
Returns:
-
list[Completion]
–A list of generatated Completions.
Source code in rigging/completion.py
run_over(*generators: Generator | str, include_original: bool = True, on_failed: FailMode | None = None) -> list[Completion]
async
#
Executes the generation process across multiple generators.
For each generator, this pipeline is cloned and the generator is replaced before the run call. All callbacks and parameters are preserved.
Parameters:
-
*generators
(Generator | str
, default:()
) –A sequence of generators to be used for the generation process.
-
include_original
(bool
, default:True
) –Whether to include the original generator in the list of runs.
-
on_failed
(FailMode | None
, default:None
) –The behavior when a message fails to generate.
Returns:
-
list[Completion]
–A list of generatated Chats.
Source code in rigging/completion.py
then(callback: ThenCompletionCallback) -> CompletionPipeline
#
Registers a callback to be executed after the generation process completes.
Note
Returning a Completion object from the callback will replace the current completion.
for the remainder of the callbacks + return value of run()
.
async def process(completion: Completion) -> Completion | None:
...
await pipeline.then(process).run()
Parameters:
-
callback
(ThenCompletionCallback
) –The callback function to be executed.
Returns:
-
CompletionPipeline
–The current instance of the pipeline.
Source code in rigging/completion.py
until(callback: UntilCompletionCallback, *, use_all_text: bool = False, max_rounds: int = DEFAULT_MAX_ROUNDS) -> CompletionPipeline
#
Registers a callback to participate in validating the generation process.
# Takes the generated text, and returns whether or not to retry generation.
def callback(text: str) -> bool:
if is_valid(text):
return False
else:
return True
await pipeline.until(callback).run()
Parameters:
-
callback
(UntilCompletionCallback
) –The callback function to be executed.
-
use_all_text
(bool
, default:False
) –Whether to pass the entire text (including prompt) to the callback.
-
max_rounds
(int
, default:DEFAULT_MAX_ROUNDS
) –The maximum number of rounds to attempt generation + callbacks before giving up.
Returns:
-
CompletionPipeline
–The current instance of the completion.
Source code in rigging/completion.py
until_parsed_as(*types: type[ModelT], use_all_text: bool = False, max_rounds: int = DEFAULT_MAX_ROUNDS) -> CompletionPipeline
#
Adds the specified types to the list of types which should successfully parse before the generation process completes.
Parameters:
-
*types
(type[ModelT]
, default:()
) –The type or types of models to wait for.
-
use_all_text
(bool
, default:False
) –Whether to pass the entire text (including prompt) to the parser.
-
max_rounds
(int
, default:DEFAULT_MAX_ROUNDS
) –The maximum number of rounds to try to parse successfully.
Returns:
-
CompletionPipeline
–The updated CompletionPipeline object.
Source code in rigging/completion.py
watch(*callbacks: WatchCompletionCallback, allow_duplicates: bool = False) -> CompletionPipeline
#
Registers a callback to monitor any completions produced.
Parameters:
-
*callbacks
(WatchCompletionCallback
, default:()
) –The callback functions to be executed.
-
allow_duplicates
(bool
, default:False
) –Whether to allow (seemingly) duplicate callbacks to be added.
Returns:
-
CompletionPipeline
–The current instance.
Source code in rigging/completion.py
with_(params: t.Optional[GenerateParams] = None, **kwargs: t.Any) -> CompletionPipeline
#
Assign specific generation parameter overloads for this completion.
Note
This will trigger a clone
if overload params have already been set.
Parameters:
-
params
(Optional[GenerateParams]
, default:None
) –The parameters to set for the completion.
-
**kwargs
(Any
, default:{}
) –An alternative way to pass parameters as keyword arguments.
Returns:
-
CompletionPipeline
–The current (or cloned) instance of the completion.
Source code in rigging/completion.py
wrap(func: t.Callable[[CallableT], CallableT]) -> CompletionPipeline
#
Helper for rigging.generator.base.Generator.wrap.
Parameters:
-
func
(Callable[[CallableT], CallableT]
) –The function to wrap the calls with.
Returns:
-
CompletionPipeline
–The current instance of the pipeline.
Source code in rigging/completion.py
MapCompletionCallback
#
Bases: Protocol
__call__(completions: list[Completion]) -> t.Awaitable[list[Completion]]
#
Passed a finalized completion to process.
This callback can replace, remove, or extend completions in the pipeline.
ThenCompletionCallback
#
Bases: Protocol
__call__(completion: Completion) -> t.Awaitable[Completion | None]
#
Passed a finalized completion to process and can return a new completion to replace it.
UntilCompletionCallback
#
Bases: Protocol
__call__(text: str) -> bool
#
A callback function that takes the generated text and returns whether or not to retry generation.