-
Notifications
You must be signed in to change notification settings - Fork 38
/
utils.py
218 lines (178 loc) · 6.24 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
import os
import re
import time
import json
import asyncio
import concurrent.futures
from functools import partial
from typing import Union, Dict, List, Type, Any, Callable
import httpx
import openai
import jsonref
import requests
from tqdm import tqdm
from openapi_spec_validator import validate_spec
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
retry_base,
)
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_key = os.getenv("OPENAI_API_KEY")
def create_retry_decorator(
max_retries: int = 5
) -> Callable[[Any], Any]:
errors = [
openai.error.Timeout,
openai.error.APIError,
openai.error.APIConnectionError,
openai.error.RateLimitError,
openai.error.ServiceUnavailableError,
]
min_seconds = 4
max_seconds = 10
# Wait 2^x * 1 second between each retry starting with
# 4 seconds, then up to 10 seconds, then 10 seconds afterwards
retry_instance: "retry_base" = retry_if_exception_type(errors[0])
for error in errors[1:]:
retry_instance = retry_instance | retry_if_exception_type(error)
return retry(
reraise=True,
stop=stop_after_attempt(max_retries),
wait=wait_exponential(multiplier=1, min=min_seconds, max=max_seconds),
retry=retry_instance
)
def openai_chat_completions(
messages: Union[List[Dict], List[List[Dict]]],
model=None,
temperature=1,
top_p=1,
n=1,
stop=None,
max_tokens=None,
max_retries=5,
num_workers=4,
**kwargs
):
if model is None:
model = os.getenv("OPENAI_DEFAULT_MODEL", "gpt-3.5-turbo")
retry_decorator = create_retry_decorator(max_retries)
@retry_decorator
def _completion_with_retry(messages, *args, **kwargs):
return openai.ChatCompletion.create(messages=messages, *args, **kwargs)
without_batch = len(messages) > 0 and isinstance(messages[0], dict)
if without_batch:
messages = [messages]
partial_func = partial(
_completion_with_retry,
model=model,
temperature=temperature,
top_p=top_p,
n=n,
stop=stop,
max_tokens=max_tokens,
**kwargs
)
with concurrent.futures.ThreadPoolExecutor(max_workers=num_workers) as executor:
all_completions = list(executor.map(partial_func, messages))
if without_batch:
all_completions = all_completions[0]
return all_completions
async def async_openai_chat_completions(
messages: Union[List[Dict], List[List[Dict]]],
model=None,
temperature=1,
top_p=1,
n=1,
stop=None,
max_tokens=None,
max_retries=5,
**kwargs
):
if model is None:
model = os.getenv("OPENAI_DEFAULT_MODEL", "gpt-3.5-turbo")
retry_decorator = create_retry_decorator(max_retries)
@retry_decorator
async def _completion_with_retry(*args, **kwargs):
return await openai.ChatCompletion.acreate(*args, **kwargs)
without_batch = len(messages) > 0 and isinstance(messages[0], dict)
if without_batch:
messages = [messages]
all_completions = []
for batch_id, batch in tqdm(enumerate(messages)):
data = {
"model": model,
"messages": batch,
"temperature": temperature,
"top_p": top_p,
"n": n,
"stop": stop,
"max_tokens": max_tokens,
**kwargs
}
all_completions.append(await _completion_with_retry(**data))
if without_batch:
all_completions = all_completions[0]
return all_completions
def validate_openapi_file(json_spec_str: str):
try:
if "Open AI Klarna product Api" in json_spec_str:
return "DO NOT copy the example!"
spec = json.loads(json_spec_str)
validate_spec(spec)
return None
except Exception as e:
if isinstance(e, KeyError):
print("KeyError: ", e)
return "{exception_name}: {exception_message}".format(
exception_name=type(e).__name__,
exception_message=str(e).split('\n\n')[0]
)
def load_openapi_spec(json_str: str, replace_refs=False):
openapi_spec = json.loads(json_str)
if replace_refs:
openapi_spec = jsonref.JsonRef.replace_refs(openapi_spec)
return openapi_spec
def escape(string):
return string.replace("{", "{{").replace("}", "}}")
def is_text_based(media_type):
return media_type.startswith("text/") or media_type == "application/json"
def analyze_openapi_spec(spec):
input_text_based = True
output_text_based = True
paths = spec.get('paths', {})
for path, methods in paths.items():
for method, operation in methods.items():
if method in ['get', 'post', 'put', 'delete', 'patch']:
content = operation.get('requestBody', {}).get('content', {})
if content:
input_text_based = any(is_text_based(media_type) for media_type in content.keys())
# Analyze output
responses = operation.get('responses', {})
for status, response in responses.items():
content = response.get('content', {})
if content:
output_text_based = any(is_text_based(media_type) for media_type in content.keys())
if not (input_text_based and output_text_based):
break
if not (input_text_based and output_text_based):
break
return input_text_based, output_text_based
def add_server_url_to_spec(api_data):
for api_info in api_data:
api = json.loads(api_info["Documentation"])
if not api.get("servers") or not api["servers"][0].get("url"):
api["servers"] = [{"url": api_info["Link"]}]
api_info["Documentation"] = json.dumps(api)
def parse_json_string(json_string, load=True):
json_start = min([idx for idx in (json_string.find('{'), json_string.find('['))
if idx != -1])
json_end = max([idx for idx in (json_string.rfind('}'), json_string.rfind(']'))
if idx != -1]) + 1
valid_json_string = json_string[json_start:json_end]
if load:
return json.loads(valid_json_string)
else:
return valid_json_string