-
Notifications
You must be signed in to change notification settings - Fork 535
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dispatch
cfg
based on the model passed by the user
- Loading branch information
Showing
3 changed files
with
41 additions
and
17 deletions.
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
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,39 @@ | ||
from functools import singledispatch | ||
from typing import List, Optional, Union | ||
|
||
from outlines.fsm.fsm import CFGFSM | ||
from outlines.generate.api import SequenceGenerator | ||
from outlines.generate.samplers import Sampler, multinomial | ||
from outlines.models import OpenAI | ||
|
||
|
||
@singledispatch | ||
def cfg( | ||
model, | ||
cfg_str: str, | ||
max_tokens: Optional[int] = None, | ||
stop_at: Optional[Union[str, List[str]]] = None, | ||
sampler: Sampler = multinomial, | ||
): | ||
fsm = CFGFSM(cfg_str, model.tokenizer) | ||
|
||
device = model.device | ||
generator = SequenceGenerator( | ||
fsm, model, sampler, device, max_tokens=max_tokens, stop_at=stop_at | ||
) | ||
|
||
return generator | ||
|
||
|
||
@cfg.register(OpenAI) | ||
def cfg_openai( | ||
model, | ||
cfg_str: str, | ||
max_tokens: Optional[int] = None, | ||
stop_at: Optional[Union[str, List[str]]] = None, | ||
sampler: Sampler = multinomial, | ||
): | ||
raise NotImplementedError( | ||
"Cannot use grammar-structured generation with an OpenAI model" | ||
+ "due to the limitations of the OpenAI API." | ||
) |