-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
304 lines (259 loc) · 11 KB
/
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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# FIXME: ChatCompletion is not adapted for openai > 1.0.0
from collections import deque
from openai import ChatCompletion
import tiktoken
from .models.integration_pd import IntegrationModel
from .models.request_body import ChatCompletionRequestBody
from pylon.core.tools import log
# def init_openai(settings, project_id):
# import openai
# api_key = settings.api_token.unsecret(project_id)
# openai.api_key = api_key
# openai.api_type = settings.api_type
# openai.api_version = settings.api_version
# openai.api_base = settings.api_base
# return openai
def init_openai(settings, project_id):
return {
'api_key': settings.api_token.unsecret(project_id),
'api_type': settings.api_type,
'api_version': settings.api_version,
'api_base': settings.api_base
}
def num_tokens_from_messages(messages: list, model: str) -> int:
"""Return the number of tokens used by a list of messages.
See: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
"""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
log.warning("Warning: model not found. Using cl100k_base encoding.")
encoding = tiktoken.get_encoding("cl100k_base")
if model in {
"gpt-3.5-turbo-0613",
"gpt-3.5-turbo-16k-0613",
"gpt-4-0314",
"gpt-4-32k-0314",
"gpt-4-0613",
"gpt-4-32k-0613",
}:
tokens_per_message = 3
tokens_per_name = 1
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 "gpt-3.5-turbo" in model:
log.warning("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613")
elif "gpt-4" in model:
log.warning("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
return num_tokens_from_messages(messages, model="gpt-4-0613")
else:
tokens_per_message = 4
tokens_per_name = -1
num_tokens = 0
for message in messages:
num_tokens += tokens_per_message
for key, value in message.items():
if key != "custom_content":
num_tokens += len(encoding.encode(value))
if key == "name":
num_tokens += tokens_per_name
# num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens
def limit_conversation(
conversation: dict, model_name: str, max_response_tokens: int, token_limit: int
) -> list:
limited_conversation = []
remaining_tokens = token_limit - max_response_tokens
remaining_tokens -= 3 # every reply is primed with <|start|>assistant<|message|>
context_tokens = num_tokens_from_messages(conversation['context'], model_name)
remaining_tokens -= context_tokens
if remaining_tokens < 0:
raise Exception(
'There are no enough tokens to form messages for ChatCompletion. \
Try using a lower value for the token limit parameter.'
)
limited_conversation.extend(conversation['context'])
input_tokens = num_tokens_from_messages(conversation['input'], model_name)
remaining_tokens -= input_tokens
if remaining_tokens < 0:
return limited_conversation
final_examples = []
for example in conversation['examples']:
try:
example_tokens = num_tokens_from_messages([example], model_name)
remaining_tokens -= example_tokens
if remaining_tokens < 0:
if len(final_examples) % 2:
final_examples.pop() # remove incomplete example if present
return limited_conversation + final_examples + conversation['input']
final_examples.append(example)
except TypeError:
...
limited_conversation.extend(final_examples)
final_history = deque()
for message in reversed(conversation['chat_history']):
try:
message_tokens = num_tokens_from_messages([message], model_name)
remaining_tokens -= message_tokens
if remaining_tokens < 0:
return limited_conversation + list(final_history) + conversation['input']
final_history.appendleft(message)
except TypeError:
...
limited_conversation.extend(final_history)
limited_conversation.extend(conversation['input'])
return limited_conversation
def prepare_conversation_old(
prompt_struct: dict, model_name: str, max_response_tokens: int, token_limit: int,
check_limits: bool = True
) -> list:
conversation = {
'context': [],
'examples': [],
'chat_history': [],
'input': []
}
if prompt_struct.get('context'):
conversation['context'].append({
"role": "system",
"content": prompt_struct['context']
})
if prompt_struct.get('examples'):
for example in prompt_struct['examples']:
conversation['examples'].append({
"role": "user",
"name": "example_user",
"content": example['input']
})
if example.get("output", None):
conversation['examples'].append({
"role": "assistant",
"name": "example_assistant",
"content": example['output']
})
if prompt_struct.get('chat_history'):
for message in prompt_struct['chat_history']:
formatted_message = {
"role": "user" if message['role'] == 'user' else "assistant",
"content": message['content']
}
if 'custom_content' in message:
formatted_message['custom_content'] = message['custom_content']
# if 'name' in message:
# formatted_message['name'] = message['name']
conversation['chat_history'].append(formatted_message)
if prompt_struct.get('prompt'):
conversation['input'].append({
"role": "user",
"content": prompt_struct['prompt']
})
# conversation = context + examples + chat_history + input_
# conv_history_tokens = num_tokens_from_messages(conversation, model_name)
# while conv_history_tokens + max_response_tokens >= token_limit:
# if chat_history:
# del chat_history[0]
# elif examples:
# del examples[0:2]
# conversation = context + examples + chat_history + input_
# conv_history_tokens = num_tokens_from_messages(conversation, model_name)
if check_limits:
return limit_conversation(conversation, model_name, max_response_tokens, token_limit)
return conversation['context'] + conversation['examples'] + conversation['chat_history'] + conversation['input']
def limit_messages(messages: list, model_name: str, max_response_tokens: int, token_limit: int) -> list:
conversation = {
'context': [],
'examples': [],
'chat_history': [],
'input': []
}
for idx, message in enumerate(messages):
if message['role'] == 'system' and not message.get('name'):
conversation['context'].append(message)
if message.get("name") in ("example_user", "example_assistant"):
conversation['examples'].append(message)
if message['role'] == 'user' and idx != len(messages) - 1:
conversation['chat_history'].append(message)
if message['role'] == 'assistant':
conversation['chat_history'].append(message)
if messages[-1]['role'] == 'user':
conversation['input'].append(messages[-1])
return limit_conversation(conversation, model_name, max_response_tokens, token_limit)
def prepare_result(response: dict) -> dict:
messages = []
response_message: dict = response['choices'][0]['message']
custom_content = response_message.get('custom_content', {})
attachments = [
*response['choices'][0].get('custom_content', {}).get('attachments', []),
*custom_content.get('attachments', [])
]
if 'state' in custom_content:
messages.append({
'type': 'state',
'content': custom_content['state']
})
if 'content' in response_message:
messages.append({
'type': 'text',
'content': response_message['content']
})
for attachment in attachments:
if 'image' in attachment.get('type', ''):
messages.append({
'type': 'image',
'content': attachment
})
if 'text' in attachment.get('type', '') or not attachment.get('type'):
content = attachment['title'] + '\n\n' if attachment.get('title') else ''
content += attachment['data'] if attachment.get('data') else ''
content += '\n\n' + 'Reference URL: ' + attachment['reference_url'] if attachment.get(
'reference_url') else ''
messages.append({
'type': 'text',
'content': content
})
return {'messages': messages}
def predict_chat(
project_id: int, settings: dict,
prompt_struct: dict | list, format_response: bool = True,
from_legacy_api: bool = True, **kwargs
) -> dict:
settings = IntegrationModel.parse_obj(settings)
# openai = init_openai(settings, project_id)
init_settings = init_openai(settings, project_id)
token_limit = settings.token_limit
if from_legacy_api:
conversation = prepare_conversation_old(
prompt_struct, settings.model_name, settings.max_tokens, token_limit
)
else:
conversation = limit_messages(
prompt_struct, settings.model_name, settings.max_tokens, token_limit
)
# addons = prompt_struct.pop('addons', None)
# if addons:
# init_settings['addons'] = addons
response = ChatCompletion.create(
deployment_id=settings.model_name,
temperature=settings.temperature,
max_tokens=settings.max_tokens,
top_p=settings.top_p,
messages=conversation,
**init_settings
)
if format_response:
return prepare_result(response)
return dict(response)
def predict_chat_from_request(project_id: int, settings: dict, request_data: dict) -> str:
params = ChatCompletionRequestBody.validate(request_data).dict(exclude_unset=True)
settings = IntegrationModel.parse_obj(settings)
# openai = init_openai(settings, project_id)
init_settings = init_openai(settings, project_id)
token_limit = settings.get_token_limit(params['deployment_id'])
max_tokens = params.get('max_tokens', 0)
if params.get('messages'):
params['messages'] = limit_messages(
params['messages'], params['deployment_id'], max_tokens, token_limit
)
return ChatCompletion.create(**params, **init_settings)