Formatron v0.4.9
Formatron empowers everyone to control the output format of language models with minimal overhead.
Loading...
Searching...
No Matches
formatron.formatter.FormatterBase Class Reference

An abstract Formatter that enforces a format on the string generated by a language model. More...

Inheritance diagram for formatron.formatter.FormatterBase:
formatron.formatter.Formatter

Public Member Functions

 accept_token (self, int token_id)
 Accept a token from the language model.
 
None accept_bytes (self, bytes _bytes)
 Accept a bytes object from the language model.
 
None compute_allowed_tokens (self)
 Compute the allowed tokens based on the current state.
 
typing.Any mask_logits (self, logits)
 Mask the logits based on the current state.
 
typing.Sequence[int] get_allowed_tokens_since_last_computation (self)
 Get the allowed tokens since the last computation(in other words, the last call to compute_allowed_tokens).
 
bool is_completed (self)
 Check if the generated string satisfies the format and hence the generation is completed.
 
dict[str, typing.Any|None] captures (self)
 
None reset (self)
 Reset the formatter to the initial state.
 

Protected Member Functions

None _on_completion (self, str generated_output)
 Perform actions when the generation is completed.
 

Detailed Description

An abstract Formatter that enforces a format on the string generated by a language model.

Definition at line 24 of file formatter.py.

Member Function Documentation

◆ _on_completion()

None formatron.formatter.FormatterBase._on_completion ( self,
str generated_output )
protected

Perform actions when the generation is completed.

Reimplemented in formatron.formatter.Formatter.

Definition at line 80 of file formatter.py.

◆ accept_bytes()

None formatron.formatter.FormatterBase.accept_bytes ( self,
bytes _bytes )

Accept a bytes object from the language model.

Parameters
_bytesThe bytes object.

Reimplemented in formatron.formatter.Formatter.

Definition at line 42 of file formatter.py.

◆ accept_token()

formatron.formatter.FormatterBase.accept_token ( self,
int token_id )

Accept a token from the language model.

Parameters
token_idThe token ID.
Returns
The result of accepting the token.

Reimplemented in formatron.formatter.Formatter.

Definition at line 34 of file formatter.py.

◆ captures()

dict[str, typing.Any|None] formatron.formatter.FormatterBase.captures ( self)

Reimplemented in formatron.formatter.Formatter.

Definition at line 98 of file formatter.py.

◆ compute_allowed_tokens()

None formatron.formatter.FormatterBase.compute_allowed_tokens ( self)

Compute the allowed tokens based on the current state.

Reimplemented in formatron.formatter.Formatter.

Definition at line 48 of file formatter.py.

◆ get_allowed_tokens_since_last_computation()

typing.Sequence[int] formatron.formatter.FormatterBase.get_allowed_tokens_since_last_computation ( self)

Get the allowed tokens since the last computation(in other words, the last call to compute_allowed_tokens).

Returns
The allowed tokens.

Reimplemented in formatron.formatter.Formatter.

Definition at line 66 of file formatter.py.

◆ is_completed()

bool formatron.formatter.FormatterBase.is_completed ( self)

Check if the generated string satisfies the format and hence the generation is completed.

Reimplemented in formatron.formatter.Formatter.

Definition at line 72 of file formatter.py.

◆ mask_logits()

typing.Any formatron.formatter.FormatterBase.mask_logits ( self,
logits )

Mask the logits based on the current state.

Parameters
logitsThe logits to mask.
Returns
The masked logits.

Reimplemented in formatron.formatter.Formatter.

Definition at line 58 of file formatter.py.

◆ reset()

None formatron.formatter.FormatterBase.reset ( self)

Reset the formatter to the initial state.

Reimplemented in formatron.formatter.Formatter.

Definition at line 104 of file formatter.py.


The documentation for this class was generated from the following file: