|
1 | 1 | # Custom FSM Operations |
2 | 2 |
|
3 | | -```RegexFSM.from_interegular_fsm``` leverages the flexibility of ```interegular.FSM``` to use the available operations in ```interegular```. |
| 3 | +Outlines is fast because it compiles regular expressions into an index ahead of inference. To do so we use the equivalence between regular expressions and Finite State Machines (FSMs), and the library [interegular](https://github.com/MegaIng/interegular) to perform the translation. |
4 | 4 |
|
5 | | -## Examples |
| 5 | +Alternatively, one can pass a FSM built using `integular` directly to structure the generation. |
6 | 6 |
|
7 | | -### ```difference``` |
| 7 | +## Example |
8 | 8 |
|
9 | | -Returns an FSM which recognises only the strings recognised by the first FSM in the list, but none of the others. |
| 9 | +### Using the `difference` operation |
| 10 | + |
| 11 | +In the following example we build a fsm which recognizes only the strings valid to the first regular expression but not the second. In particular, it will prevent the words "pink" and "elephant" from being generated: |
10 | 12 |
|
11 | 13 | ```python |
| 14 | +import interegular |
| 15 | +from outlines import models, generate |
| 16 | + |
| 17 | + |
12 | 18 | list_of_strings_pattern = """\["[^"\s]*"(?:,"[^"\s]*")*\]""" |
13 | 19 | pink_elephant_pattern = """.*(pink|elephant).*""" |
14 | 20 |
|
15 | 21 | list_of_strings_fsm = interegular.parse_pattern(list_of_strings_pattern).to_fsm() |
16 | 22 | pink_elephant_fsm = interegular.parse_pattern(pink_elephant_pattern).to_fsm() |
17 | 23 |
|
18 | | -list_of_strings_fsm.accepts('["a","pink","elephant"]') |
19 | | -# True |
20 | | - |
21 | 24 | difference_fsm = list_of_strings_fsm - pink_elephant_fsm |
22 | 25 |
|
23 | 26 | difference_fsm_fsm.accepts('["a","pink","elephant"]') |
24 | 27 | # False |
25 | 28 | difference_fsm_fsm.accepts('["a","blue","donkey"]') |
26 | 29 | # True |
27 | | -``` |
28 | | - |
29 | | -### ```union``` |
30 | | - |
31 | | -Returns a finite state machine which accepts any sequence of symbols that is accepted by either self or other. |
32 | | - |
33 | | -```python |
34 | | -list_of_strings_pattern = """\["[^"\s]*"(?:,"[^"\s]*")*\]""" |
35 | | -tuple_of_strings_pattern = """\("[^"\s]*"(?:,"[^"\s]*")*\)""" |
36 | | - |
37 | | -list_of_strings_fsm = interegular.parse_pattern(list_of_strings_pattern).to_fsm() |
38 | | -tuple_of_strings_fsm = interegular.parse_pattern(tuple_of_strings_pattern).to_fsm() |
39 | | - |
40 | | -list_of_strings_fsm.accepts('("a","pink","elephant")') |
41 | | -# False |
42 | 30 |
|
43 | | -union_fsm = list_of_strings_fsm|tuple_of_strings_fsm |
44 | 31 |
|
45 | | -union_fsm.accepts('["a","pink","elephant"]') |
46 | | -# True |
47 | | -union_fsm.accepts('("a","blue","donkey")') |
48 | | -# True |
| 32 | +model = models.transformers("mistralai/Mistral-7B-Instruct-v0.2") |
| 33 | +generator = generate.fsm(model, difference_fsm) |
| 34 | +response = generator("Don't talk about pink elephants") |
49 | 35 | ``` |
50 | 36 |
|
51 | | -### ```intersection``` |
52 | | - |
53 | | -Returns an FSM which accepts any sequence of symbols that is accepted by both of the original FSMs. |
54 | | - |
55 | | -```python |
56 | | -list_of_strings_pattern = """\["[^"\s]*"(?:,"[^"\s]*")*\]""" |
57 | | -pink_elephant_pattern = """.*(pink|elephant).*""" |
58 | | - |
59 | | -list_of_strings_fsm = interegular.parse_pattern(list_of_strings_pattern).to_fsm() |
60 | | -pink_elephant_fsm = interegular.parse_pattern(pink_elephant_pattern).to_fsm() |
61 | | - |
62 | | -list_of_strings_fsm.accepts('["a","blue","donkey"]') |
63 | | -# True |
64 | | - |
65 | | -intersection_fsm = list_of_strings_fsm & pink_elephant_fsm |
66 | | - |
67 | | -intersection_fsm.accepts('["a","pink","elephant"]') |
68 | | -# True |
69 | | -intersection_fsm.accepts('["a","blue","donkey"]') |
70 | | -# False |
71 | | -``` |
72 | | - |
73 | | -_There are more operations available, we refer to https://github.com/MegaIng/interegular/blob/master/interegular/fsm.py._ |
74 | | - |
75 | | -# Loading Custom FSM |
76 | | - |
77 | | -```python |
78 | | -import outlines |
79 | | - |
80 | | -generator = outlines.generate.fsm(model, custom_fsm) |
81 | | - |
82 | | -response = generator(prompt) |
83 | | -``` |
| 37 | +To see the other operations available, consult [interegular's documentation](https://github.com/MegaIng/interegular/blob/master/interegular/fsm.py). |
0 commit comments