-
Notifications
You must be signed in to change notification settings - Fork 54
/
rank_gpt.py
284 lines (239 loc) · 10.9 KB
/
rank_gpt.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
159
160
161
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
202
203
204
205
206
207
208
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import copy
from tqdm import tqdm
import time
import openai
import json
import tiktoken
try:
from litellm import completion
except:
completion = openai.ChatCompletion.create
class SafeOpenai:
def __init__(self, model=None, keys=None, start_id=None, proxy=None):
if isinstance(keys, str):
keys = [keys]
if keys is None:
raise "Please provide OpenAI Key."
self.key = keys
self.key_id = start_id or 0
self.key_id = self.key_id % len(self.key)
openai.proxy = proxy
openai.api_key = self.key[self.key_id % len(self.key)]
self.api_key = self.key[self.key_id % len(self.key)]
def chat(self, *args, return_text=False, reduce_length=False, **kwargs):
while True:
try:
model = args[0] if len(args) > 0 else kwargs["model"]
if "gpt" in model:
completion = openai.ChatCompletion.create(*args, **kwargs, timeout=30)
elif model in litellm.model_list:
completion = completion(*args, **kwargs, api_key=self.api_key, force_timeout=30)
break
except Exception as e:
print(str(e))
if "This model's maximum context length is" in str(e):
print('reduce_length')
return 'ERROR::reduce_length'
self.key_id = (self.key_id + 1) % len(self.key)
openai.api_key = self.key[self.key_id]
time.sleep(0.1)
if return_text:
completion = completion['choices'][0]['message']['content']
return completion
def text(self, *args, return_text=False, reduce_length=False, **kwargs):
while True:
try:
completion = openai.Completion.create(*args, **kwargs)
break
except Exception as e:
print(e)
if "This model's maximum context length is" in str(e):
print('reduce_length')
return 'ERROR::reduce_length'
self.key_id = (self.key_id + 1) % len(self.key)
openai.api_key = self.key[self.key_id]
time.sleep(0.1)
if return_text:
completion = completion['choices'][0]['text']
return completion
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301"):
"""Returns the number of tokens used by a list of messages."""
if model == "gpt-3.5-turbo":
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0301")
elif model == "gpt-4":
return num_tokens_from_messages(messages, model="gpt-4-0314")
elif model == "gpt-3.5-turbo-0301":
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
tokens_per_name = -1 # if there's a name, the role is omitted
elif model == "gpt-4-0314":
tokens_per_message = 3
tokens_per_name = 1
else:
tokens_per_message, tokens_per_name = 0, 0
try:
encoding = tiktoken.get_encoding(model)
except:
encoding = tiktoken.get_encoding("cl100k_base")
num_tokens = 0
if isinstance(messages, list):
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += tokens_per_name
else:
num_tokens += len(encoding.encode(messages))
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens
def max_tokens(model):
if 'gpt-4' in model:
return 8192
else:
return 4096
def run_retriever(topics, searcher, qrels=None, k=100, qid=None):
ranks = []
if isinstance(topics, str):
hits = searcher.search(topics, k=k)
ranks.append({'query': topics, 'hits': []})
rank = 0
for hit in hits:
rank += 1
content = json.loads(searcher.doc(hit.docid).raw())
if 'title' in content:
content = 'Title: ' + content['title'] + ' ' + 'Content: ' + content['text']
else:
content = content['contents']
content = ' '.join(content.split())
ranks[-1]['hits'].append({
'content': content,
'qid': qid, 'docid': hit.docid, 'rank': rank, 'score': hit.score})
return ranks[-1]
for qid in tqdm(topics):
if qid in qrels:
query = topics[qid]['title']
ranks.append({'query': query, 'hits': []})
hits = searcher.search(query, k=k)
rank = 0
for hit in hits:
rank += 1
content = json.loads(searcher.doc(hit.docid).raw())
if 'title' in content:
content = 'Title: ' + content['title'] + ' ' + 'Content: ' + content['text']
else:
content = content['contents']
content = ' '.join(content.split())
ranks[-1]['hits'].append({
'content': content,
'qid': qid, 'docid': hit.docid, 'rank': rank, 'score': hit.score})
return ranks
def get_prefix_prompt(query, num):
return [{'role': 'system',
'content': "You are RankGPT, an intelligent assistant that can rank passages based on their relevancy to the query."},
{'role': 'user',
'content': f"I will provide you with {num} passages, each indicated by number identifier []. \nRank the passages based on their relevance to query: {query}."},
{'role': 'assistant', 'content': 'Okay, please provide the passages.'}]
def get_post_prompt(query, num):
return f"Search Query: {query}. \nRank the {num} passages above based on their relevance to the search query. The passages should be listed in descending order using identifiers. The most relevant passages should be listed first. The output format should be [] > [], e.g., [1] > [2]. Only response the ranking results, do not say any word or explain."
def create_permutation_instruction(item=None, rank_start=0, rank_end=100, model_name='gpt-3.5-turbo'):
query = item['query']
num = len(item['hits'][rank_start: rank_end])
max_length = 300
while True:
messages = get_prefix_prompt(query, num)
rank = 0
for hit in item['hits'][rank_start: rank_end]:
rank += 1
content = hit['content']
content = content.replace('Title: Content: ', '')
content = content.strip()
# For Japanese should cut by character: content = content[:int(max_length)]
content = ' '.join(content.split()[:int(max_length)])
messages.append({'role': 'user', 'content': f"[{rank}] {content}"})
messages.append({'role': 'assistant', 'content': f'Received passage [{rank}].'})
messages.append({'role': 'user', 'content': get_post_prompt(query, num)})
if num_tokens_from_messages(messages, model_name) <= max_tokens(model_name) - 200:
break
else:
max_length -= 1
return messages
def run_llm(messages, openai_key=None, model_name="gpt-3.5-turbo"):
agent = SafeOpenai(openai_key)
response = agent.chat(model=model_name, messages=messages, temperature=0, return_text=True)
return response
def clean_response(response: str):
new_response = ''
for c in response:
if not c.isdigit():
new_response += ' '
else:
new_response += c
new_response = new_response.strip()
return new_response
def remove_duplicate(response):
new_response = []
for c in response:
if c not in new_response:
new_response.append(c)
return new_response
def receive_permutation(item, permutation, rank_start=0, rank_end=100):
response = clean_response(permutation)
response = [int(x) - 1 for x in response.split()]
response = remove_duplicate(response)
cut_range = copy.deepcopy(item['hits'][rank_start: rank_end])
original_rank = [tt for tt in range(len(cut_range))]
response = [ss for ss in response if ss in original_rank]
response = response + [tt for tt in original_rank if tt not in response]
for j, x in enumerate(response):
item['hits'][j + rank_start] = copy.deepcopy(cut_range[x])
if 'rank' in item['hits'][j + rank_start]:
item['hits'][j + rank_start]['rank'] = cut_range[j]['rank']
if 'score' in item['hits'][j + rank_start]:
item['hits'][j + rank_start]['score'] = cut_range[j]['score']
return item
def permutation_pipeline(item=None, rank_start=0, rank_end=100, model_name='gpt-3.5-turbo', api_key=None): # change to `api_key` from `openai_key` to make it more generic
messages = create_permutation_instruction(item=item, rank_start=rank_start, rank_end=rank_end,
model_name=model_name) # chan
permutation = run_llm(messages, openai_key=api_key, model_name=model_name)
item = receive_permutation(item, permutation, rank_start=rank_start, rank_end=rank_end)
return item
def sliding_windows(item=None, rank_start=0, rank_end=100, window_size=20, step=10, model_name='gpt-3.5-turbo',
api_key=None): # change to `api_key` from `openai_key` to make it more generic
item = copy.deepcopy(item)
end_pos = rank_end
start_pos = rank_end - window_size
while start_pos >= rank_start:
start_pos = max(start_pos, rank_start)
item = permutation_pipeline(item, start_pos, end_pos, model_name=model_name, openai_key=api_key)
end_pos = end_pos - step
start_pos = start_pos - step
return item
def write_eval_file(rank_results, file):
with open(file, 'w') as f:
for i in range(len(rank_results)):
rank = 1
hits = rank_results[i]['hits']
for hit in hits:
f.write(f"{hit['qid']} Q0 {hit['docid']} {rank} {hit['score']} rank\n")
rank += 1
return True
def main():
from pyserini.search import LuceneSearcher
from pyserini.search import get_topics, get_qrels
import tempfile
openai_key = None # Your openai key
searcher = LuceneSearcher.from_prebuilt_index('msmarco-v1-passage')
topics = get_topics('dl19-passage')
qrels = get_qrels('dl19-passage')
rank_results = run_retriever(topics, searcher, qrels, k=100)
new_results = []
for item in tqdm(rank_results):
new_item = permutation_pipeline(item, rank_start=0, rank_end=20, model_name='gpt-3.5-turbo',
openai_key=openai_key)
new_results.append(new_item)
temp_file = tempfile.NamedTemporaryFile(delete=False).name
write_eval_file(new_results, temp_file)
from trec_eval import EvalFunction
EvalFunction.eval(['-c', '-m', 'ndcg_cut.10', 'dl19-passage', temp_file])
if __name__ == '__main__':
main()