-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate text using any
interegular
FSM
- Loading branch information
1 parent
9c4e7f4
commit 7a21043
Showing
5 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Custom FSM Operations | ||
|
||
```RegexFSM.from_interegular_fsm``` leverages the flexibility of ```interegular.FSM``` to use the available operations in ```interegular```. | ||
|
||
## Examples | ||
|
||
### ```difference``` | ||
|
||
Returns an FSM which recognises only the strings recognised by the first FSM in the list, but none of the others. | ||
|
||
```python | ||
list_of_strings_pattern = """\["[^"\s]*"(?:,"[^"\s]*")*\]""" | ||
pink_elephant_pattern = """.*(pink|elephant).*""" | ||
|
||
list_of_strings_fsm = interegular.parse_pattern(list_of_strings_pattern).to_fsm() | ||
pink_elephant_fsm = interegular.parse_pattern(pink_elephant_pattern).to_fsm() | ||
|
||
list_of_strings_fsm.accepts('["a","pink","elephant"]') | ||
# True | ||
|
||
difference_fsm = list_of_strings_fsm - pink_elephant_fsm | ||
|
||
difference_fsm_fsm.accepts('["a","pink","elephant"]') | ||
# False | ||
difference_fsm_fsm.accepts('["a","blue","donkey"]') | ||
# True | ||
``` | ||
|
||
### ```union``` | ||
|
||
Returns a finite state machine which accepts any sequence of symbols that is accepted by either self or other. | ||
|
||
```python | ||
list_of_strings_pattern = """\["[^"\s]*"(?:,"[^"\s]*")*\]""" | ||
tuple_of_strings_pattern = """\("[^"\s]*"(?:,"[^"\s]*")*\)""" | ||
|
||
list_of_strings_fsm = interegular.parse_pattern(list_of_strings_pattern).to_fsm() | ||
tuple_of_strings_fsm = interegular.parse_pattern(tuple_of_strings_pattern).to_fsm() | ||
|
||
list_of_strings_fsm.accepts('("a","pink","elephant")') | ||
# False | ||
|
||
union_fsm = list_of_strings_fsm|tuple_of_strings_fsm | ||
|
||
union_fsm.accepts('["a","pink","elephant"]') | ||
# True | ||
union_fsm.accepts('("a","blue","donkey")') | ||
# True | ||
``` | ||
|
||
### ```intersection``` | ||
|
||
Returns an FSM which accepts any sequence of symbols that is accepted by both of the original FSMs. | ||
|
||
```python | ||
list_of_strings_pattern = """\["[^"\s]*"(?:,"[^"\s]*")*\]""" | ||
pink_elephant_pattern = """.*(pink|elephant).*""" | ||
|
||
list_of_strings_fsm = interegular.parse_pattern(list_of_strings_pattern).to_fsm() | ||
pink_elephant_fsm = interegular.parse_pattern(pink_elephant_pattern).to_fsm() | ||
|
||
list_of_strings_fsm.accepts('["a","blue","donkey"]') | ||
# True | ||
|
||
intersection_fsm = list_of_strings_fsm & pink_elephant_fsm | ||
|
||
intersection_fsm.accepts('["a","pink","elephant"]') | ||
# True | ||
intersection_fsm.accepts('["a","blue","donkey"]') | ||
# False | ||
``` | ||
|
||
_There are more operations available, we refer to https://github.com/MegaIng/interegular/blob/master/interegular/fsm.py._ | ||
|
||
# Loading Custom FSM | ||
|
||
```python | ||
import outlines | ||
|
||
generator = outlines.generate.fsm(model, custom_fsm) | ||
|
||
response = generator(prompt) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import interegular | ||
|
||
from outlines.fsm.fsm import RegexFSM | ||
from outlines.generate.api import SequenceGenerator | ||
from outlines.samplers import Sampler, multinomial | ||
|
||
|
||
def fsm( | ||
model, fsm: interegular.fsm.FSM, sampler: Sampler = multinomial() | ||
) -> SequenceGenerator: | ||
fsm = RegexFSM.from_interegular_fsm(fsm, model.tokenizer) | ||
device = model.device | ||
generator = SequenceGenerator(fsm, model, sampler, device) | ||
return generator |