-
Notifications
You must be signed in to change notification settings - Fork 0
/
perplexity.py
63 lines (48 loc) · 2.3 KB
/
perplexity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from openai import OpenAI
# model = 'openai'
# client = OpenAI(api_key='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')
# openai_model = 'gpt-3.5-turbo'
# import numpy as np
# import torch
import numpy as np
# # assert openai_key is not None, "Must provide OpenAI API key as --openai_key"
client = OpenAI(api_key='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')
def get_ll(text):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
,logprobs = True
)
result = response.choices[0]
tokens, logprobs = result.message.content, result.logprobs
all_log_prob = [item.logprob for item in logprobs.content]
final_perplexity = np.mean(all_log_prob)
# print(result.logprobs)
print(tokens)
print(final_perplexity)
# if model == 'openai':
# #kwargs = { "model": openai_model, "temperature": 0, "max_tokens": 0, "echo": True, "logprobs": 0}
# r = client.completions.create(model= openai_model,
# messages=[
# {"role": "system", "content": "You are a helpful assistant."},
# {"role": "user", "content": "Who won the world series in 2020?"},
# {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
# {"role": "user", "content": "Where was it played?"}
# ])
# result = r.choices[0]
# tokens, logprobs = result["logprobs"]["tokens"][1:], result["logprobs"]["token_logprobs"][1:]
# assert len(tokens) == len(logprobs), f"Expected {len(tokens)} logprobs, got {len(logprobs)}"
# return np.mean(logprobs)
# else:
# with torch.no_grad():
# tokenized = base_tokenizer(text, return_tensors="pt").to(DEVICE)
# labels = tokenized.input_ids
# return -base_model(**tokenized, labels=labels).loss.item()
get_ll("hi hello")
from openai import OpenAI
client = OpenAI(api_key='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')