:param engine: str OpenAI API engine (e.g. davinci) :param truncate: bool Truncate input if too long (if False and input is too long, throw error)
(self, engine, truncate=False)
| 58 | REQ_CHUNK_SIZE = 20 |
| 59 | |
| 60 | def __init__(self, engine, truncate=False): |
| 61 | """ |
| 62 | |
| 63 | :param engine: str |
| 64 | OpenAI API engine (e.g. davinci) |
| 65 | :param truncate: bool |
| 66 | Truncate input if too long (if False and input is too long, throw error) |
| 67 | """ |
| 68 | super().__init__() |
| 69 | |
| 70 | import openai |
| 71 | |
| 72 | self.engine = engine |
| 73 | self.tokenizer = transformers.GPT2TokenizerFast.from_pretrained("gpt2") |
| 74 | |
| 75 | self.vocab_size = self.tokenizer.vocab_size |
| 76 | |
| 77 | # to make the annoying "Using pad_token, but it is not set yet." error go away |
| 78 | self.tokenizer.pad_token = "<|endoftext|>" |
| 79 | assert self.tokenizer.encode("hello\n\nhello") == [31373, 198, 198, 31373] |
| 80 | self.truncate = truncate |
| 81 | self.end_of_text_token_id = self.tokenizer.convert_tokens_to_ids( |
| 82 | ["<|endoftext|>"] |
| 83 | )[0] |
| 84 | |
| 85 | # Read from environment variable OPENAI_API_SECRET_KEY |
| 86 | openai.api_key = os.environ["OPENAI_API_SECRET_KEY"] |
| 87 | |
| 88 | @property |
| 89 | def eot_token_id(self): |
nothing calls this directly
no outgoing calls
no test coverage detected