openai_completions.py 16.6 KB
Newer Older
1
import copy
Jason Phang's avatar
gpt3  
Jason Phang committed
2
import os
lintangsutawika's avatar
lintangsutawika committed
3
import time
lintangsutawika's avatar
update  
lintangsutawika committed
4
from collections import defaultdict
5
from importlib.util import find_spec
6
from typing import List, Optional, Tuple
7

Leo Gao's avatar
Leo Gao committed
8
from tqdm import tqdm
lintangsutawika's avatar
update  
lintangsutawika committed
9

lintangsutawika's avatar
lintangsutawika committed
10
from lm_eval import utils
11
12
from lm_eval.api.model import LM
from lm_eval.api.registry import register_model
Leo Gao's avatar
Leo Gao committed
13

lintangsutawika's avatar
update  
lintangsutawika committed
14

Baber Abbasi's avatar
Baber Abbasi committed
15
def get_result(response, ctxlen: int) -> Tuple[float, bool]:
lintangsutawika's avatar
lintangsutawika committed
16
17
18
19
20
21
22
23
24
25
26
27
28
    """Process results from OpenAI API response.

    :param response: dict
        OpenAI API Response
    :param ctxlen: int
        Length of context (so we can slice them away and only keep the predictions)
    :return:
        continuation_logprobs: np.array
            Log probabilities of continuation tokens
        is_greedy: bool
            whether argmax matches given continuation exactly
    """
    is_greedy = True
Baber Abbasi's avatar
Baber Abbasi committed
29
    logprobs = response.logprobs.token_logprobs
lintangsutawika's avatar
lintangsutawika committed
30
31
    continuation_logprobs = sum(logprobs[ctxlen:])

Baber Abbasi's avatar
Baber Abbasi committed
32
33
34
    for i in range(ctxlen, len(response.logprobs.token_logprobs)):
        token = response.logprobs.token_logprobs[i]
        top_tokens = response.logprobs.top_logprobs[i]
lintangsutawika's avatar
lintangsutawika committed
35
36
37
38
39
40
41
42
43
44
45
46
47
        top_token = max(top_tokens.keys(), key=lambda x: top_tokens[x])
        if top_token != token:
            is_greedy = False
            break

    return continuation_logprobs, is_greedy


def oa_completion(**kwargs):
    """Query OpenAI API for completion.

    Retry with back-off until they respond
    """
48
    if not find_spec("openai") or not find_spec("tiktoken"):
lintangsutawika's avatar
lintangsutawika committed
49
        raise Exception(
50
51
            "attempted to use 'openai' LM type, but package `openai` or `tiktoken` are not installed. "
            "Please install these via `pip install lm-eval[openai]` or `pip install -e .[openai]`"
lintangsutawika's avatar
lintangsutawika committed
52
        )
53
54
    else:
        import openai
lintangsutawika's avatar
lintangsutawika committed
55
56
57
58

    backoff_time = 3
    while True:
        try:
59
60
            return openai.completions.create(**kwargs)
        except openai.OpenAIError:
lintangsutawika's avatar
lintangsutawika committed
61
62
63
64
65
66
67
            import traceback

            traceback.print_exc()
            time.sleep(backoff_time)
            backoff_time *= 1.5


68
@register_model("openai-completions")
lintangsutawika's avatar
lintangsutawika committed
69
70
class OpenaiCompletionsLM(LM):
    REQ_CHUNK_SIZE = 20
Baber Abbasi's avatar
Baber Abbasi committed
71
    _DEFAULT_MAX_LENGTH = 2048
lintangsutawika's avatar
lintangsutawika committed
72
73
74

    def __init__(
        self,
75
        model: str = "text-davinci-003",
lintangsutawika's avatar
lintangsutawika committed
76
        truncate: bool = False,
Baber Abbasi's avatar
Baber Abbasi committed
77
        max_gen_toks: int = 256,
lintangsutawika's avatar
lintangsutawika committed
78
        batch_size: int = 1,
Baber Abbasi's avatar
Baber Abbasi committed
79
80
        seed: int = 1234,
        max_length: Optional[int] = None,
lintangsutawika's avatar
lintangsutawika committed
81
82
83
84
85
86
87
88
89
    ) -> None:
        """

        :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)
        """
        super().__init__()
Baber Abbasi's avatar
Baber Abbasi committed
90
        self.seed = seed
lintangsutawika's avatar
lintangsutawika committed
91
        try:
92
93
            import openai  # noqa: E401
            import tiktoken
lintangsutawika's avatar
lintangsutawika committed
94
95
96
97
98
        except ModuleNotFoundError:
            raise Exception(
                "attempted to use 'openai' LM type, but package `openai` or `tiktoken` are not installed. \
    please install these via `pip install lm-eval[openai]` or `pip install -e .[openai]`",
            )
Baber Abbasi's avatar
Baber Abbasi committed
99
        self.model = model
100
        self.tokenizer = tiktoken.encoding_for_model(self.model)
lintangsutawika's avatar
lintangsutawika committed
101
102
103
        self.vocab_size = self.tokenizer.n_vocab
        self.truncate = truncate
        self.end_of_text_token_id = self.tokenizer.eot_token
Baber Abbasi's avatar
Baber Abbasi committed
104
105
        self._max_gen_toks = max_gen_toks
        self._max_length = max_length
lintangsutawika's avatar
lintangsutawika committed
106

107
        # Read from environment variable OPENAI_API_KEY
Baber Abbasi's avatar
Baber Abbasi committed
108
        openai.api_key = os.environ["OPENAI_API_KEY"]
lintangsutawika's avatar
lintangsutawika committed
109
110
111
112
113
114
115

    @property
    def eot_token_id(self):
        return self.end_of_text_token_id

    @property
    def max_length(self) -> int:
Baber Abbasi's avatar
Baber Abbasi committed
116
117
118
119
        if self._max_length:
            return self._max_length
        else:
            return self._DEFAULT_MAX_LENGTH
lintangsutawika's avatar
lintangsutawika committed
120
121
122

    @property
    def max_gen_toks(self) -> int:
Baber Abbasi's avatar
Baber Abbasi committed
123
        return self._max_gen_toks
lintangsutawika's avatar
lintangsutawika committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

    @property
    def batch_size(self):
        # Isn't used because we override _loglikelihood_tokens
        raise NotImplementedError()

    @property
    def device(self):
        # Isn't used because we override _loglikelihood_tokens
        raise NotImplementedError()

    def tok_encode(self, string: str) -> List[int]:
        return self.tokenizer.encode(string)

    def tok_decode(self, tokens: List[int]) -> str:
        return self.tokenizer.decode(tokens)

    def _encode_pair(
        self, context: str, continuation: str
    ) -> Tuple[List[int], List[int]]:
        n_spaces = len(context) - len(context.rstrip())
        if n_spaces > 0:
            continuation = context[-n_spaces:] + continuation
            context = context[:-n_spaces]
        whole_enc = self.tok_encode(context + continuation)
        context_enc = self.tok_encode(context)
        context_enc_len = len(context_enc)
        continuation_enc = whole_enc[context_enc_len:]
        return context_enc, continuation_enc

    def loglikelihood(self, requests) -> List[Tuple[float, bool]]:
        new_reqs = []
        for context, continuation in [req.args for req in requests]:
            if context == "":
                # end of text as context
159
160
161
                context_enc, continuation_enc = (
                    [self.eot_token_id],
                    self.tok_encode(continuation),
lintangsutawika's avatar
lintangsutawika committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
                )
            else:
                context_enc, continuation_enc = self._encode_pair(context, continuation)

            new_reqs.append(((context, continuation), context_enc, continuation_enc))

        return self._loglikelihood_tokens(new_reqs)

    def _loglikelihood_tokens(
        self, requests, disable_tqdm: bool = False
    ) -> List[Tuple[float, bool]]:
        res = []

        def _collate(x):
            # this doesn't efficiently handle last-token differences yet, but those are kinda annoying because
            # it's not guaranteed that the 100 or so logprobs we get to see actually contain all the continuations
            # we care about, and so we need some kind of backup for when it isn't
            toks = x[1] + x[2]
            return -len(toks), tuple(toks)

        re_ord = utils.Reorderer(requests, _collate)

        for chunk in tqdm(
            list(utils.chunks(re_ord.get_reordered(), self.REQ_CHUNK_SIZE)),
            disable=disable_tqdm,
        ):
            inps = []
            ctxlens = []
            for cache_key, context_enc, continuation_enc in chunk:
                # max_length+1 because the API takes up to 2049 tokens, including the first context token
                inp = (context_enc + continuation_enc)[-(self.max_length + 1) :]
                # TODO: the logic is much simpler if we just look at the length of continuation tokens
                ctxlen = len(context_enc) - max(
                    0, len(context_enc) + len(continuation_enc) - (self.max_length + 1)
                )

                inps.append(inp)
                ctxlens.append(ctxlen)

            response = oa_completion(
Baber Abbasi's avatar
Baber Abbasi committed
202
                model=self.model,
lintangsutawika's avatar
lintangsutawika committed
203
204
205
206
207
                prompt=inps,
                echo=True,
                max_tokens=0,
                temperature=0.0,
                logprobs=10,
Baber Abbasi's avatar
Baber Abbasi committed
208
                seed=self.seed,
lintangsutawika's avatar
lintangsutawika committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
            )

            for resp, ctxlen, (cache_key, context_enc, continuation_enc) in zip(
                response.choices, ctxlens, chunk
            ):
                answer = get_result(resp, ctxlen)

                res.append(answer)

                # partial caching
                if cache_key is not None:
                    self.cache_hook.add_partial("loglikelihood", cache_key, answer)
        return re_ord.get_original(res)

    def generate_until(self, requests) -> List[str]:
        if not requests:
            return []
        res = []
        requests = [req.args for req in requests]

        def _collate(x):
            toks = self.tok_encode(x[0])
            return len(toks), x[0]

        re_ord = utils.Reorderer(requests, _collate)

        def sameuntil_chunks(xs, size):
            ret = []
            lastuntil = xs[0][1]
            for x in xs:
                if len(ret) >= size or x[1] != lastuntil:
                    yield ret, lastuntil
                    ret = []
                    lastuntil = x[1]
                ret.append(x)

            if ret:
                yield ret, lastuntil

        # todo: more intelligent batching for heterogeneous `until`
        for chunk, request_args in tqdm(
            list(sameuntil_chunks(re_ord.get_reordered(), self.REQ_CHUNK_SIZE))
        ):
            inps = []
            for context, _ in chunk:
                context_enc = self.tok_encode(context)
                inp = context_enc[-(self.max_length - self.max_gen_toks) :]
                inps.append(inp)

Baber Abbasi's avatar
Baber Abbasi committed
258
259
260
            until = request_args.pop("until", ["<|endoftext|>"])
            request_args.pop("do_sample", None)
            request_args["temperature"] = request_args.get("temperature", 0)
lintangsutawika's avatar
lintangsutawika committed
261
262

            response = oa_completion(
263
                model=self.model,
lintangsutawika's avatar
lintangsutawika committed
264
265
266
                prompt=inps,
                max_tokens=self.max_gen_toks,
                stop=until,
Baber Abbasi's avatar
Baber Abbasi committed
267
268
                seed=self.seed,
                **request_args,
lintangsutawika's avatar
lintangsutawika committed
269
270
            )
            for resp, (context, args_) in zip(response.choices, chunk):
Baber Abbasi's avatar
Baber Abbasi committed
271
                s = getattr(resp, "text")
lintangsutawika's avatar
lintangsutawika committed
272

Baber Abbasi's avatar
Baber Abbasi committed
273
                until_ = until
lintangsutawika's avatar
lintangsutawika committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

                for term in until_:
                    if len(term) > 0:
                        s = s.split(term)[0]

                # partial caching
                self.cache_hook.add_partial(
                    "generate_until", (context, {"until": until_}), s
                )

                res.append(s)
        return re_ord.get_original(res)

    def _model_call(self, inps):
        # Isn't used because we override _loglikelihood_tokens
        raise NotImplementedError()

    def _model_generate(self, context, max_length, eos_token_id):
        # Isn't used because we override generate_until
        raise NotImplementedError()

    def loglikelihood_rolling(self, requests) -> List[float]:
        loglikelihoods = []

        for (string,) in tqdm([req.args for req in requests]):
            rolling_token_windows = list(
                map(
                    utils.make_disjoint_window,
                    utils.get_rolling_token_windows(
                        token_list=self.tok_encode(string),
                        prefix_token=self.eot_token_id,
                        max_seq_len=self.max_length,
                        context_len=1,
                    ),
                )
            )

            # TODO: Right now, we pass single EOT token to the Encoder and the full context to the decoder, in seq2seq case
            rolling_token_windows = [(None,) + x for x in rolling_token_windows]

            string_nll = self._loglikelihood_tokens(
                rolling_token_windows,
                disable_tqdm=True,
            )

            # discard is_greedy
            string_nll = [x[0] for x in string_nll]

            string_nll = sum(string_nll)
            loglikelihoods.append(string_nll)
        return loglikelihoods


327
def oa_chat_completion(client, **kwargs):
328
329
330
331
    """Query OpenAI API for chat completion.

    Retry with back-off until they respond
    """
332
    if not find_spec("openai") or not find_spec("tiktoken"):
333
        raise Exception(
334
335
            "attempted to use 'openai' LM type, but package `openai` or `tiktoken` are not installed. "
            "Please install these via `pip install lm-eval[openai]` or `pip install -e .[openai]`"
336
        )
337
338
    else:
        import openai
339

340
341
342
343
    async def _get_completions(**kwargs):
        chat_completions = await client.chat.completions.create(**kwargs)
        return chat_completions

344
345
346
    backoff_time = 3
    while True:
        try:
lintangsutawika's avatar
lintangsutawika committed
347
348
            return client.chat.completions.create(**kwargs)
        except openai.OpenAIError:
349
350
351
352
353
354
355
            import traceback

            traceback.print_exc()
            time.sleep(backoff_time)
            backoff_time *= 1.5


356
@register_model("openai-chat-completions", "local-chat-completions")
357
class OpenaiChatCompletionsLM(LM):
358
    def __init__(
359
360
361
362
363
        self,
        model: str = "gpt-3.5-turbo",  # GPT model or Local model using HuggingFace model paths
        base_url: str = None,
        truncate: bool = False,
        **kwargs,
364
    ) -> None:
365
366
        """

lintangsutawika's avatar
lintangsutawika committed
367
        :param model: str
368
369
370
            Implements an OpenAI-style chat completion API for
            accessing both OpenAI OR locally-hosted models using
            HuggingFace Tokenizer
lintangsutawika's avatar
lintangsutawika committed
371
            OpenAI API model (e.g. gpt-3.5-turbo)
372
            using the **gen_kwargs passed on init
373
374
375
376
377
        :param truncate: bool
            Truncate input if too long (if False and input is too long, throw error)
        """
        super().__init__()
        try:
378
            import openai  # noqa: E401
379
380
381
382
383
        except ModuleNotFoundError:
            raise Exception(
                "attempted to use 'openai' LM type, but package `openai` or `tiktoken` are not installed. \
    please install these via `pip install lm-eval[openai]` or `pip install -e .[openai]`",
            )
lintangsutawika's avatar
lintangsutawika committed
384
        self.model = model
385
        self.base_url = base_url
386
        self.truncate = truncate
387

388
        # Read from environment variable OPENAI_API_KEY
389
390
391
392
393
        # Set to EMPTY for local
        if self.base_url:
            self.client = openai.OpenAI(base_url=self.base_url)
        else:
            self.client = openai.OpenAI()  # openai.AsyncOpenAI()
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

    @property
    def max_length(self) -> int:
        # Note: the OpenAI API supports up to 2049 tokens, with the first token being the first input token
        return 2048

    @property
    def max_gen_toks(self) -> int:
        return 256

    @property
    def batch_size(self):
        # Isn't used because we override _loglikelihood_tokens
        raise NotImplementedError()

    @property
    def device(self):
        # Isn't used because we override _loglikelihood_tokens
        raise NotImplementedError()

414
    def generate_until(self, requests) -> List[str]:
lintangsutawika's avatar
update  
lintangsutawika committed
415
416
        res = defaultdict(list)
        re_ords = {}
417

lintangsutawika's avatar
update  
lintangsutawika committed
418
419
420
421
422
423
        # we group requests by their generation_kwargs,
        # so that we don't try to execute e.g. greedy sampling and temp=0.8 sampling
        # in the same batch.
        grouper = utils.Grouper(requests, lambda x: str(x.args[1]))
        for key, reqs in grouper.get_grouped().items():
            # within each set of reqs for given kwargs, we reorder by token length, descending.
424
425
426
            re_ords[key] = utils.Reorderer(
                [req.args for req in reqs], lambda x: (-len(x[0]), x[0])
            )
427

lintangsutawika's avatar
update  
lintangsutawika committed
428
429
        pbar = tqdm(total=len(requests), disable=(self.rank != 0))
        for key, re_ord in re_ords.items():
430
431
            # n needs to be 1 because messages in
            # chat completion are not batch but
432
433
            # is regarded as a single conversation.
            chunks = utils.chunks(re_ord.get_reordered(), n=1)
lintangsutawika's avatar
update  
lintangsutawika committed
434
435
436
437
            for chunk in chunks:
                contexts, all_gen_kwargs = zip(*chunk)
                inps = [{"role": "user", "content": context} for context in contexts]

438
439
440
441
                gen_kwargs = all_gen_kwargs[0]
                until = None
                if isinstance(gen_kwargs, dict):
                    kwargs = copy.deepcopy(gen_kwargs)  # edge case for repeats > 1
442
443
                    if "do_sample" in kwargs.keys():
                        kwargs.pop("do_sample")
444
445
446
447
448
449
                    if "until" in kwargs.keys():
                        until = kwargs.pop("until")
                        if isinstance(until, str):
                            until = [kwargs]
                        elif not isinstance(until, list):
                            raise ValueError(
450
                                f"Expected repr(kwargs['until']) to be of type Union[str, list] but got {until}"
451
452
453
                            )
                else:
                    raise ValueError(
454
                        f"Expected repr(kwargs) to be of type repr(dict) but got {kwargs}"
455
456
457
                    )

                response = oa_chat_completion(
458
                    client=self.client, messages=inps, model=self.model, **kwargs
lintangsutawika's avatar
update  
lintangsutawika committed
459
                )
460

461
462
                for resp, (context, args_) in zip(response.choices, chunk):
                    s = resp.message.content
463

464
465
466
467
                    if until is not None:
                        for term in until:
                            if len(term) > 0:
                                s = s.split(term)[0]
lintangsutawika's avatar
update  
lintangsutawika committed
468

469
                    res[key].append(s)
lintangsutawika's avatar
update  
lintangsutawika committed
470

471
472
473
474
475
                    self.cache_hook.add_partial(
                        "generate_until", (context, {"until": until}), s
                    )
                    pbar.update(1)
            # reorder this group of results back to original unsorted form
lintangsutawika's avatar
update  
lintangsutawika committed
476
477
478
            res[key] = re_ord.get_original(res[key])

        pbar.close()
479

lintangsutawika's avatar
update  
lintangsutawika committed
480
        return grouper.get_original(res)
481
482
483
484
485
486

    def loglikelihood(self, requests):
        raise NotImplementedError("No support for logits.")

    def loglikelihood_rolling(self, requests):
        raise NotImplementedError("No support for logits.")