-
Notifications
You must be signed in to change notification settings - Fork 3
/
eval_utils.py
158 lines (140 loc) · 5.81 KB
/
eval_utils.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
import sys
import time
from functools import wraps
from typing import List
import openai
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential,
) # for exponential backoff
import json
import re
import spacy
nlp = None
def analyze_words(pos_words, sentence):
global nlp
if nlp is None:
nlp = spacy.load('en_core_web_lg')
doc = nlp(sentence)
# Prepare the inputs
input_dict = {word.split('_')[0]: word.split('_')[1] for word in pos_words}
# Lemmatization and POS matching
found_words = [tok.lemma_ for tok in doc if tok.lemma_ in input_dict.keys()]
found_pos_words = [tok.lemma_+"_"+tok.pos_ for tok in doc if tok.lemma_ in input_dict.keys() and input_dict[tok.lemma_].lower() in tok.pos_.lower()]
# print("Number of words used from the given list: ", len(found_words))
# print("Number of words with correct POS: ", len(found_pos_words))
# take unique items
found_words = list(set(found_words))
found_pos_words = list(set(found_pos_words))
return found_words, found_pos_words
@retry(wait=wait_random_exponential(min=1, max=30), stop=stop_after_attempt(30))
def completion_with_backoff(**kwargs):
completion = openai.ChatCompletion.create(**kwargs)
return completion.choices[0].message["content"].strip()
## V2
def retry_handler(retry_limit=10):
"""
This is an error handler for requests to OpenAI API.
If will retry for the request for `retry_limit` times if the error is not a rate limit error.
Otherwise, it will wait for the time specified in the error message and constantly retry.
You can add specific processing logic for different types of errors here.
Args:
retry_limit (int, optional): The number of times to retry. Defaults to 3.
Usage:
@retry_handler(retry_limit=3)
def call_openai_api():
pass
"""
def decorate(func):
@wraps(func)
def wrapper(*args, **kwargs):
retried = 0
while True:
try:
sys.stdout.flush()
return func(*args, **kwargs)
except Exception as e:
# if rate limit error, wait 2 seconds and retry
if isinstance(e, openai.error.RateLimitError):
words = str(e).split(' ')
try:
time_to_wait = int(words[words.index('after') + 1])
except ValueError:
time_to_wait = 5
# print("Rate limit error, waiting for {} seconds for another try..".format(time_to_wait))
time.sleep(time_to_wait) # wait 30 seconds
# print("Finished waiting for {} seconds. Start another try".format(time_to_wait))
elif isinstance(e, openai.error.APIError):
# this is because the prompt contains content that is filtered by OpenAI API
print("API error:", str(e))
if "Invalid" in str(e):
print("Invalid request, returning.")
raise e
else:
print(e.__class__.__name__+":", str(e))
if retried < retry_limit:
print(f"Retrying for the {retried + 1} time..")
else:
# finally failed
print("Retry limit reached. Saving the error message and returning.")
print(kwargs["prompt"])
raise e
retried += 1
return wrapper
return decorate
def openai_chat_request(
model: str=None,
engine: str=None,
temperature: float=0,
max_tokens: int=512,
top_p: float=1.0,
frequency_penalty: float=0,
presence_penalty: float=0,
prompt: str=None,
n: int=1,
messages: List[dict]=None,
stop: List[str]=None,
**kwargs,
) -> List[str]:
"""
Request the evaluation prompt from the OpenAI API in chat format.
Args:
prompt (str): The encoded prompt.
messages (List[dict]): The messages.
model (str): The model to use.
engine (str): The engine to use.
temperature (float, optional): The temperature. Defaults to 0.7.
max_tokens (int, optional): The maximum number of tokens. Defaults to 800.
top_p (float, optional): The top p. Defaults to 0.95.
frequency_penalty (float, optional): The frequency penalty. Defaults to 0.
presence_penalty (float, optional): The presence penalty. Defaults to 0.
stop (List[str], optional): The stop. Defaults to None.
Returns:
List[str]: The list of generated evaluation prompts.
"""
# Call openai api to generate aspects
assert prompt is not None or messages is not None, "Either prompt or messages should be provided."
if messages is None:
messages = [{"role":"system","content":"You are an AI assistant that helps people find information."},
{"role":"user","content": prompt}]
response = openai.ChatCompletion.create(
model=model,
engine=engine,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
n=n,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
stop=stop,
**kwargs,
)
contents = []
for choice in response['choices']:
# Check if the response is valid
if choice['finish_reason'] not in ['stop', 'length']:
raise ValueError(f"OpenAI Finish Reason Error: {choice['finish_reason']}")
contents.append(choice['message']['content'])
return contents