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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use crate::{
constants::{OPENAI_API_URL, OPENAI_BASE_INSTRUCTIONS, OPENAI_FUNCTION_INSTRUCTIONS},
domain::{OpenAIRateLimit, OpenAPIChatResponse, OpenAPICompletionsResponse},
};
#[derive(Deserialize, Serialize, Debug, Clone)]
pub enum OpenAIModels {
Gpt3_5Turbo,
Gpt3_5Turbo0613,
Gpt3_5Turbo16k,
Gpt4,
Gpt4_32k,
TextDavinci003,
Gpt4Turbo,
}
impl OpenAIModels {
pub fn as_str(&self) -> &'static str {
match self {
//In an API call, you can describe functions to gpt-3.5-turbo-0613 and gpt-4-0613
//On June 27, 2023 the stable gpt-3.5-turbo will be automatically upgraded to gpt-3.5-turbo-0613
OpenAIModels::Gpt3_5Turbo => "gpt-3.5-turbo",
OpenAIModels::Gpt3_5Turbo0613 => "gpt-3.5-turbo-0613",
OpenAIModels::Gpt3_5Turbo16k => "gpt-3.5-turbo-16k",
OpenAIModels::Gpt4 => "gpt-4-0613",
OpenAIModels::Gpt4_32k => "gpt-4-32k",
OpenAIModels::TextDavinci003 => "text-davinci-003",
OpenAIModels::Gpt4Turbo => "gpt-4-1106-preview",
}
}
pub fn default_max_tokens(&self) -> usize {
//OpenAI documentation: https://platform.openai.com/docs/models/gpt-3-5
//This is the max tokens allowed between prompt & response
match self {
OpenAIModels::Gpt3_5Turbo => 4096,
OpenAIModels::Gpt3_5Turbo0613 => 4096,
OpenAIModels::Gpt3_5Turbo16k => 16384,
OpenAIModels::Gpt4 => 8192,
OpenAIModels::Gpt4_32k => 32768,
OpenAIModels::TextDavinci003 => 4097,
OpenAIModels::Gpt4Turbo => 128_000,
}
}
pub(crate) fn get_endpoint(&self) -> String {
//OpenAI documentation: https://platform.openai.com/docs/models/model-endpoint-compatibility
match self {
OpenAIModels::Gpt3_5Turbo
| OpenAIModels::Gpt3_5Turbo0613
| OpenAIModels::Gpt3_5Turbo16k
| OpenAIModels::Gpt4
| OpenAIModels::Gpt4Turbo
| OpenAIModels::Gpt4_32k => {
format!(
"{OPENAI_API_URL}/v1/chat/completions",
OPENAI_API_URL = *OPENAI_API_URL
)
}
OpenAIModels::TextDavinci003 => format!(
"{OPENAI_API_URL}/v1/completions",
OPENAI_API_URL = *OPENAI_API_URL
),
}
}
pub(crate) fn get_base_instructions(&self, function_call: Option<bool>) -> String {
let function_call = function_call.unwrap_or_else(|| self.function_call_default());
match function_call {
true => OPENAI_FUNCTION_INSTRUCTIONS.to_string(),
false => OPENAI_BASE_INSTRUCTIONS.to_string(),
}
}
pub(crate) fn function_call_default(&self) -> bool {
//OpenAI documentation: https://platform.openai.com/docs/guides/gpt/function-calling
match self {
OpenAIModels::TextDavinci003 | OpenAIModels::Gpt3_5Turbo | OpenAIModels::Gpt4_32k => {
false
}
OpenAIModels::Gpt3_5Turbo0613
| OpenAIModels::Gpt3_5Turbo16k
| OpenAIModels::Gpt4
| OpenAIModels::Gpt4Turbo => true,
}
}
//This method prepares the body of the API call for different models
pub(crate) fn get_body(
&self,
instructions: &str,
json_schema: &Value,
function_call: bool,
max_tokens: &usize,
temperature: &u32,
) -> serde_json::Value {
match self {
//https://platform.openai.com/docs/api-reference/completions/create
//For DaVinci model all text goes into the 'prompt' filed of the body
OpenAIModels::TextDavinci003 => {
let schema_string = serde_json::to_string(json_schema).unwrap_or_default();
let base_instructions = self.get_base_instructions(Some(function_call));
json!({
"model": self.as_str(),
"max_tokens": max_tokens,
"temperature": temperature,
"prompt": format!(
"{base_instructions}\n\n
Output Json schema:\n
{schema_string}\n\n
{instructions}",
),
})
}
OpenAIModels::Gpt3_5Turbo
| OpenAIModels::Gpt3_5Turbo0613
| OpenAIModels::Gpt3_5Turbo16k
| OpenAIModels::Gpt4
| OpenAIModels::Gpt4Turbo
| OpenAIModels::Gpt4_32k => {
let base_instructions = self.get_base_instructions(Some(function_call));
let system_message = json!({
"role": "system",
"content": base_instructions,
});
match function_call {
//If we choose to use function calling
//https://platform.openai.com/docs/guides/gpt/function-calling
true => {
let user_message = json!({
"role": "user",
"content": instructions,
});
let function = json!({
"name": "analyze_data",
"description": "Use this function to compute the answer based on input data, instructions and your language model. Output should be a fully formed JSON object.",
"parameters": json_schema,
});
let function_call = json!({
"name": "analyze_data"
});
//For ChatGPT we ignore max_tokens. It will default to 'inf'
json!({
"model": self.as_str(),
"temperature": temperature,
"messages": vec![
system_message,
user_message,
],
"functions": vec![
function,
],
//This forces ChatGPT to use the function definition
"function_call": function_call,
})
}
//https://platform.openai.com/docs/guides/chat/introduction
false => {
let schema_string = serde_json::to_string(json_schema).unwrap_or_default();
let user_message = json!({
"role": "user",
"content": format!(
"Output Json schema:\n
{schema_string}\n\n
{instructions}"
),
});
//For ChatGPT we ignore max_tokens. It will default to 'inf'
json!({
"model": self.as_str(),
"temperature": temperature,
"messages": vec![
system_message,
user_message,
],
})
}
}
}
}
}
//This method attempts to convert the provided API response text into the expected struct and extracts the data from the response
pub(crate) fn get_data(&self, response_text: &str, function_call: bool) -> Result<String> {
match self {
//https://platform.openai.com/docs/api-reference/completions/create
OpenAIModels::TextDavinci003 => {
//Convert API response to struct representing expected response format
let completions_response: OpenAPICompletionsResponse =
serde_json::from_str(response_text)?;
//Extract data part
match completions_response.choices {
Some(choices) => Ok(choices.into_iter().filter_map(|item| item.text).collect()),
None => Err(anyhow!(
"Unable to retrieve response from OpenAI Completions API"
)),
}
}
//https://platform.openai.com/docs/guides/chat/introduction
OpenAIModels::Gpt3_5Turbo
| OpenAIModels::Gpt3_5Turbo0613
| OpenAIModels::Gpt3_5Turbo16k
| OpenAIModels::Gpt4
| OpenAIModels::Gpt4Turbo
| OpenAIModels::Gpt4_32k => {
//Convert API response to struct representing expected response format
let chat_response: OpenAPIChatResponse = serde_json::from_str(response_text)?;
//Extract data part
match chat_response.choices {
Some(choices) => Ok(choices
.into_iter()
.filter_map(|item| {
//For function_call the response is in arguments, and for regular call in content
match function_call {
true => item
.message
.function_call
.map(|function_call| function_call.arguments),
false => item.message.content,
}
})
.collect()),
None => Err(anyhow!("Unable to retrieve response from OpenAI Chat API")),
}
}
}
}
//This function allows to check the rate limits for different models
fn get_rate_limit(&self) -> OpenAIRateLimit {
//OpenAI documentation: https://platform.openai.com/account/rate-limits
//This is the max tokens allowed between prompt & response
match self {
OpenAIModels::Gpt3_5Turbo => OpenAIRateLimit {
tpm: 90_000,
rpm: 3_500,
},
OpenAIModels::Gpt3_5Turbo0613 => OpenAIRateLimit {
tpm: 90_000,
rpm: 3_500,
},
OpenAIModels::Gpt3_5Turbo16k => OpenAIRateLimit {
tpm: 180_000,
rpm: 3_500,
},
OpenAIModels::Gpt4 => OpenAIRateLimit {
tpm: 10_000,
rpm: 200,
},
OpenAIModels::Gpt4Turbo => OpenAIRateLimit {
tpm: 10_000,
rpm: 200,
},
OpenAIModels::Gpt4_32k => OpenAIRateLimit {
tpm: 10_000,
rpm: 200,
},
OpenAIModels::TextDavinci003 => OpenAIRateLimit {
tpm: 250_000,
rpm: 3_000,
},
}
}
//This function checks how many requests can be sent to an OpenAI model within a minute
pub fn get_max_requests(&self) -> usize {
let rate_limit = self.get_rate_limit();
//Check max requests based on rpm
let max_requests_from_rpm = rate_limit.rpm;
//Double check max number of requests based on tpm
//Assume we will use ~50% of allowed tokens per request (for prompt + response)
let max_tokens_per_minute = rate_limit.tpm;
let tpm_per_request = (self.default_max_tokens() as f64 * 0.5).ceil() as usize;
//Then check how many requests we can process
let max_requests_from_tpm = max_tokens_per_minute / tpm_per_request;
//To be safe we go with smaller of the numbers
std::cmp::min(max_requests_from_rpm, max_requests_from_tpm)
}
}
#[cfg(test)]
mod tests {
use crate::models::OpenAIModels;
use crate::utils::get_tokenizer;
#[test]
fn it_computes_gpt3_5_tokenization() {
let bpe = get_tokenizer(&OpenAIModels::Gpt4_32k).unwrap();
let tokenized: Result<Vec<_>, _> = bpe
.split_by_token_iter("This is a test with a lot of spaces", true)
.collect();
let tokenized = tokenized.unwrap();
assert_eq!(
tokenized,
vec!["This", " is", " a", " test", " ", " with", " a", " lot", " of", " spaces"]
);
}
// Tests for calculating max requests per model
#[test]
fn test_gpt3_5turbo_max_requests() {
let model = OpenAIModels::Gpt3_5Turbo;
let max_requests = model.get_max_requests();
let expected_max = std::cmp::min(3500, 90000 / ((4096_f64 * 0.5).ceil() as usize));
assert_eq!(max_requests, expected_max);
}
#[test]
fn test_gpt3_5turbo0613_max_requests() {
let model = OpenAIModels::Gpt3_5Turbo0613;
let max_requests = model.get_max_requests();
let expected_max = std::cmp::min(3500, 90000 / ((4096_f64 * 0.5).ceil() as usize));
assert_eq!(max_requests, expected_max);
}
#[test]
fn test_gpt3_5turbo16k_max_requests() {
let model = OpenAIModels::Gpt3_5Turbo16k;
let max_requests = model.get_max_requests();
let expected_max = std::cmp::min(3500, 180000 / ((16384_f64 * 0.5).ceil() as usize));
assert_eq!(max_requests, expected_max);
}
#[test]
fn test_gpt4_max_requests() {
let model = OpenAIModels::Gpt4;
let max_requests = model.get_max_requests();
let expected_max = std::cmp::min(200, 10000 / ((8192_f64 * 0.5).ceil() as usize));
assert_eq!(max_requests, expected_max);
}
}