File size: 9,009 Bytes
762d748 | 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 | """Integration with OpenAI's API."""
import copy
import functools
from dataclasses import asdict, dataclass, field, replace
from typing import Callable, Dict, List, Optional, Tuple, Union
import numpy as np
from outlines.base import vectorize
from outlines.caching import cache
__all__ = ["OpenAI", "openai", "azure_openai"]
@dataclass(frozen=True)
class OpenAIConfig:
"""Represents the parameters of the OpenAI API.
The information was last fetched on 2023/11/20. We document below the
properties that are specific to the OpenAI API. Not all these properties are
supported by Outlines.
Properties
----------
model
The name of the model. Available models can be found on OpenAI's website.
frequence_penalty
Number between 2.0 and -2.0. Positive values penalize new tokens based on
their existing frequency in the text,
logit_bias
Modifies the likelihood of specified tokens to appear in the completion.
Number between -100 (forbid) and +100 (only allows).
n
The number of completions to return for each prompt.
presence_penalty
Similar to frequency penalty.
response_format
Specifies the format the model must output. `{"type": "json_object"}`
enables JSON mode.
seed
Two completions with the same `seed` value should return the same
completion. This is however not guaranteed.
stop
Up to 4 words where the API will stop the completion.
temperature
Number between 0 and 2. Higher values make the output more random, while
lower values make it more deterministic.
top_p
Number between 0 and 1. Parameter for nucleus sampling.
user
A unique identifier for the end-user.
"""
model: str = ""
frequency_penalty: float = 0
logit_bias: Dict[int, int] = field(default_factory=dict)
max_tokens: Optional[int] = None
n: int = 1
presence_penalty: float = 0
response_format: Optional[Dict[str, str]] = None
seed: Optional[int] = None
stop: Optional[Union[str, List[str]]] = None
temperature: float = 1.0
top_p: int = 1
user: str = field(default_factory=str)
class OpenAI:
"""An object that represents the OpenAI API."""
def __init__(
self,
client,
config,
system_prompt: Optional[str] = None,
):
"""Create an `OpenAI` instance.
This class supports the standard OpenAI API, the Azure OpeanAI API as
well as compatible APIs that rely on the OpenAI client.
Parameters
----------
client
An instance of the API's async client.
config
An instance of `OpenAIConfig`. Can be useful to specify some
parameters that cannot be set by calling this class' methods.
"""
self.client = client
self.config = config
# We count the total number of prompt and generated tokens as returned
# by the OpenAI API, summed over all the requests performed with this
# model instance.
self.prompt_tokens = 0
self.completion_tokens = 0
self.format_sequence = lambda seq: seq
def __call__(
self,
prompt: Union[str, List[str]],
max_tokens: Optional[int] = None,
stop_at: Optional[Union[List[str], str]] = None,
*,
system_prompt: Optional[str] = None,
temperature: Optional[float] = None,
samples: Optional[int] = None,
) -> np.ndarray:
"""Call the OpenAI API to generate text.
Parameters
----------
prompt
A string or list of strings that will be used to prompt the model
max_tokens
The maximum number of tokens to generate
stop_at
A string or array of strings which, such that the generation stops
when they are generated.
system_prompt
The content of the system message that precedes the user's prompt.
temperature
The value of the temperature used to sample tokens
samples
The number of completions to generate for each prompt
stop_at
Up to 4 words where the API will stop the completion.
"""
if max_tokens is None:
max_tokens = self.config.max_tokens
if stop_at is None:
stop_at = self.config.stop
if temperature is None:
temperature = self.config.temperature
if samples is None:
samples = self.config.n
config = replace(self.config, max_tokens=max_tokens, temperature=temperature, n=samples, stop=stop_at) # type: ignore
response, prompt_tokens, completion_tokens = generate_chat(
prompt, system_prompt, self.client, config
)
self.prompt_tokens += prompt_tokens
self.completion_tokens += completion_tokens
return self.format_sequence(response)
def stream(self, *args, **kwargs):
raise NotImplementedError(
"Streaming is currently not supported for the OpenAI API"
)
def new_with_replacements(self, **kwargs):
new_instance = copy.copy(self)
new_instance.config = replace(new_instance.config, **kwargs)
return new_instance
def __str__(self):
return self.__class__.__name__ + " API"
def __repr__(self):
return str(self.config)
@functools.partial(vectorize, signature="(),(),(),()->(s),(),()")
async def generate_chat(
prompt: str,
system_prompt: Union[str, None],
client,
config: OpenAIConfig,
) -> Tuple[np.ndarray, int, int]:
"""Call OpenAI's Chat Completion API.
Parameters
----------
prompt
The prompt we use to start the generation. Passed to the model
with the "user" role.
system_prompt
The system prompt, passed to the model with the "system" role
before the prompt.
client
The API client
config
An `OpenAIConfig` instance.
Returns
-------
A tuple that contains the model's response(s) and usage statistics.
"""
@error_handler
@cache()
async def call_api(prompt, system_prompt, config):
responses = await client.chat.completions.create(
messages=system_message + user_message,
**asdict(config), # type: ignore
)
return responses.model_dump()
system_message = (
[{"role": "system", "content": system_prompt}] if system_prompt else []
)
user_message = [{"role": "user", "content": prompt}]
responses = await call_api(prompt, system_prompt, config)
results = np.array(
[responses["choices"][i]["message"]["content"] for i in range(config.n)]
)
usage = responses["usage"]
return results, usage["prompt_tokens"], usage["completion_tokens"]
def error_handler(api_call_fn: Callable) -> Callable:
"""Handle OpenAI API errors and missing API key."""
def call(*args, **kwargs):
import openai
try:
return api_call_fn(*args, **kwargs)
except (
openai.APITimeoutError,
openai.InternalServerError,
openai.RateLimitError,
) as e:
raise OSError(f"Could not connect to the OpenAI API: {e}")
except (
openai.AuthenticationError,
openai.BadRequestError,
openai.ConflictError,
openai.PermissionDeniedError,
openai.NotFoundError,
openai.UnprocessableEntityError,
) as e:
raise e
return call
@functools.singledispatch
def openai(model_or_client, *args, **kwargs):
return OpenAI(model_or_client, *args, **kwargs)
@openai.register(str)
def openai_model(
model_name: str,
config: Optional[OpenAIConfig] = None,
**openai_client_params,
):
try:
from openai import AsyncOpenAI
except ImportError:
raise ImportError(
"The `openai` library needs to be installed in order to use Outlines' OpenAI integration."
)
if config is not None:
config = replace(config, model=model_name) # type: ignore
else:
config = OpenAIConfig(model=model_name)
client = AsyncOpenAI(**openai_client_params)
return OpenAI(client, config)
def azure_openai(
deployment_name: str,
model_name: Optional[str] = None,
config: Optional[OpenAIConfig] = None,
**azure_openai_client_params,
):
try:
from openai import AsyncAzureOpenAI
except ImportError:
raise ImportError(
"The `openai` library needs to be installed in order to use Outlines' Azure OpenAI integration."
)
if config is not None:
config = replace(config, model=deployment_name) # type: ignore
if config is None:
config = OpenAIConfig(model=deployment_name)
client = AsyncAzureOpenAI(**azure_openai_client_params)
return OpenAI(client, config)
|