-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathOpenAI.py
246 lines (206 loc) · 8.4 KB
/
OpenAI.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
import os
import openai
from llmebench.models.model_base import ModelBase
class OpenAIModelBase(ModelBase):
"""
OpenAI Model interface. Can be used for models hosted on both OpenAI's platform and
on Azure.
Arguments
---------
api_type : str
Must be one of "openai" or "azure". If not provided, the implementation will try
to induce it from environment variables `OPEN_API_TYPE`, `AZURE_*` or default to
"openai"
api_base : str
URL where the model is hosted. Can be left as None for models hosted on OpenAI's
platform. If not provided, the implementation will look at environment variables
`OPENAI_API_BASE` or `AZURE_API_URL`
api_version : str
Version of the API to use. If not provided, the implementation will derive it
from environment variables `OPENAI_API_VERSION` or `AZURE_API_VERSION`. Must be
left as None for models hosted on OpenAI's platform
api_key : str
Authentication token for the API. If not provided, the implementation will derive it
from environment variables `OPENAI_API_KEY` or `AZURE_API_KEY`.
model_name : str
Name of the model to use. If not provided, the implementation will derive it from
environment variables `OPENAI_MODEL` or `AZURE_ENGINE_NAME`
engine_name : str
Alternative for `model_name`
temperature : float
Temperature value to use for the model. Defaults to zero for reproducibility.
top_p : float
Top P value to use for the model. Defaults to 0.95
max_tokens : int
Maximum number of tokens to pass to the model. Defaults to 800
frequency_penalty : float
Frequency Penalty to use for the model.
presence_penalty : float
Presence Penalty to use for the model.
"""
def __init__(
self,
api_type=None,
api_base=None,
api_version=None,
api_key=None,
model_name=None,
engine_name=None,
temperature=0,
top_p=0.95,
max_tokens=800,
frequency_penalty=0,
presence_penalty=0,
**kwargs
):
# API parameters
# Order of priority is:
# 1. arguments to the constructor
# 2. OPENAI_* env vars
# 3. AZURE_* env vars
azure_vars = self.read_azure_env_vars()
openai_vars = self.read_openai_env_vars()
api_type = (
api_type or openai_vars["api_type"] or azure_vars["api_type"] or "openai"
)
api_base = api_base or openai_vars["api_base"] or azure_vars["api_base"]
api_version = (
api_version or openai_vars["api_version"] or azure_vars["api_version"]
)
api_key = api_key or openai_vars["api_key"] or azure_vars["api_key"]
model_name = (
model_name or engine_name or openai_vars["model"] or azure_vars["model"]
)
openai.api_type = api_type
if api_type == "azure" and api_base is None:
raise Exception(
"API URL must be provided as model config or environment variable (`AZURE_API_BASE`)"
)
if api_base:
openai.api_base = api_base
if api_type == "azure" and api_version is None:
raise Exception(
"API version must be provided as model config or environment variable (`AZURE_API_VERSION`)"
)
if api_version:
openai.api_version = api_version
if api_key is None:
raise Exception(
"API Key must be provided as model config or environment variable (`OPENAI_API_KEY` or `AZURE_API_KEY`)"
)
openai.api_key = api_key
self.model_params = {}
if model_name is None:
raise Exception(
"Model/Engine must be provided as model config or environment variable `OPENAI_MODEL`/`AZURE_ENGINE_NAME`"
)
if api_type == "azure":
self.model_params["engine"] = model_name
else:
self.model_params["model"] = model_name
# GPT parameters
self.model_params["temperature"] = temperature
self.model_params["top_p"] = top_p
self.model_params["max_tokens"] = max_tokens
self.model_params["frequency_penalty"] = frequency_penalty
self.model_params["presence_penalty"] = presence_penalty
super(OpenAIModelBase, self).__init__(
retry_exceptions=(openai.error.Timeout, openai.error.RateLimitError),
**kwargs
)
@staticmethod
def read_azure_env_vars():
curr_api_type = None
if "AZURE_ENGINE_NAME" in os.environ or "ENGINE_NAME" in os.environ:
curr_api_type = "azure"
return {
"api_type": curr_api_type,
"api_version": os.getenv("AZURE_API_VERSION"),
"api_base": os.getenv("AZURE_API_URL"),
"api_key": os.getenv("AZURE_API_KEY"),
"model": os.getenv("AZURE_ENGINE_NAME", os.getenv("ENGINE_NAME")),
}
@staticmethod
def read_openai_env_vars():
return {
"api_type": os.getenv("OPEN_API_TYPE"),
"api_version": os.getenv("OPENAI_API_VERSION"),
"api_base": os.getenv("OPENAI_API_BASE"),
"api_key": os.getenv("OPENAI_API_KEY"),
"model": os.getenv("OPENAI_MODEL"),
}
class LegacyOpenAIModel(OpenAIModelBase):
# defining a function to create the prompt from the system and user messages
def create_prompt(self, system_message, messages):
system_message_template = "<|im_start|>system\n{}\n<|im_end|>"
message_template = "\n<|im_start|>{}\n{}\n<|im_end|>"
prompt = system_message_template.format(system_message)
for message in messages:
prompt += message_template.format(message["sender"], message["text"])
prompt += "\n<|im_start|>assistant\n"
return prompt
def summarize_response(self, response):
"""Returns the first reply, if available"""
if (
"choices" in response
and isinstance(response["choices"], list)
and len(response["choices"]) > 0
and "text" in response["choices"][0]
):
return response["choices"][0]["text"]
return response
def prompt(self, processed_input):
"""
OpenAI API Completion implementation
.. warning::
This implementation is deprecated and will be removed in future versions. Use
`OpenAIModel` instead.
Arguments
---------
processed_input : dict
Must be a dictionary with two keys; "system_message" with a string
value, and "messages" with a list value, where each element is a
dictionary with two string-valued keys, "sender" and "text".
Returns
-------
response : OpenAI API response
Response from the openai python library
"""
system_message = processed_input["system_message"]
messages = processed_input["messages"]
prompt = self.create_prompt(system_message, messages)
response = openai.Completion.create(
prompt=prompt, stop=["<|im_end|>"], **self.model_params
)
return response
class OpenAIModel(OpenAIModelBase):
def summarize_response(self, response):
"""Returns the first reply from the "assistant", if available"""
if (
"choices" in response
and isinstance(response["choices"], list)
and len(response["choices"]) > 0
and "message" in response["choices"][0]
and "content" in response["choices"][0]["message"]
and response["choices"][0]["message"]["role"] == "assistant"
):
return response["choices"][0]["message"]["content"]
return response
def prompt(self, processed_input):
"""
OpenAI API ChatCompletion implementation
Arguments
---------
processed_input : list
Must be list of dictionaries, where each dictionary has two keys;
"role" defines a role in the chat (e.g. "system", "user") and
"content" defines the actual message for that turn
Returns
-------
response : OpenAI API response
Response from the openai python library
"""
response = openai.ChatCompletion.create(
messages=processed_input, **self.model_params
)
return response