-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugins was compatible only with `text-generation-inference` based workers and therefore worked on Dragan's machines but did not work on OA prod. This resolves the incompatibility. --------- Co-authored-by: draganjovanovich <[email protected]>
- Loading branch information
1 parent
9bcc916
commit 5dd1025
Showing
9 changed files
with
173 additions
and
106 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
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,31 @@ | ||
import torch | ||
from tokenizers import Tokenizer | ||
from transformers import StoppingCriteria | ||
|
||
|
||
class SequenceStoppingCriteria(StoppingCriteria): | ||
def __init__( | ||
self, | ||
tokenizer: Tokenizer, | ||
stop_texts: list[str], | ||
input_prompt: str, | ||
*args, | ||
**kwargs, | ||
): | ||
super().__init__(*args, **kwargs) | ||
self.stop_texts = stop_texts | ||
self.tokenizer = tokenizer | ||
self.input_length = len(tokenizer.encode(input_prompt)) | ||
|
||
def __call__( | ||
self, | ||
input_ids: torch.LongTensor, | ||
scores: torch.FloatTensor, | ||
**kwargs, | ||
) -> bool: | ||
# Assumes batch size 1, sufficient for our use case | ||
generated_ids = input_ids[0, self.input_length :].tolist() | ||
# TODO: optimise this. Inefficient to decode whole sequence every time | ||
# but can't encode stop sequences as they don't always tokenize the same | ||
generated_text = self.tokenizer.decode(generated_ids) | ||
return any(text in generated_text for text in self.stop_texts) |
File renamed without changes.
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
Oops, something went wrong.