Skip to content

Commit

Permalink
fix: fix json_util
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 27, 2023
1 parent 25e396e commit 98f4b60
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "jarvis"
version = "1.1.6"
version = "1.1.7"
edition = "2021"
rust-version = "1.64"
description = ""
Expand Down
15 changes: 9 additions & 6 deletions src/json_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl RawJSONArray {

fn skip_space(&mut self) {
while self.offset < self.chars.len() {
if self.chars[self.offset].is_whitespace() {
if self.chars[self.offset].is_whitespace() || self.chars[self.offset].is_control() {
self.offset += 1;
} else {
break;
Expand All @@ -61,7 +61,7 @@ impl RawJSONArray {
fn skip_space_v(&self) -> usize {
let mut offset = self.offset;
while offset < self.chars.len() {
if self.chars[offset].is_whitespace() {
if self.chars[offset].is_whitespace() || self.chars[self.offset].is_control() {
offset += 1;
} else {
break;
Expand Down Expand Up @@ -234,7 +234,10 @@ impl RawJSONArray {
self.result.push(']');
}
_ => {
self.result.push(self.chars[self.offset]);
let c = self.chars[self.offset];
if !c.is_control() {
self.result.push(c);
}
self.offset += 1;
}
}
Expand Down Expand Up @@ -273,8 +276,8 @@ mod tests {
err: None,
},
Case {
input: r#"[" "]"#.to_string(),
output: r#"[" "]"#.to_string(),
input: r#"[" ❤️‍🔥🧑‍🤝‍🧑"]"#.to_string(),
output: r#"[" ❤️‍🔥🧑‍🤝‍🧑"]"#.to_string(),
err: None,
},
Case {
Expand Down Expand Up @@ -330,7 +333,7 @@ mod tests {
err: None,
},
Case {
input: r#"["\ "]"#.to_string(),
input: r#"["\ œ"]"#.to_string(), // with a control char
output: r#"["\\ "]"#.to_string(),
err: None,
},
Expand Down
34 changes: 9 additions & 25 deletions src/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reqwest::{header, Client, ClientBuilder, Identity, Response};
use serde::{de::DeserializeOwned, Serialize};
use std::{path::Path, str::FromStr, string::ToString};
use tiktoken_rs::{num_tokens_from_messages, ChatCompletionRequestMessage};
use tokio::time::Duration;
use tokio::time::{sleep, Duration};

use crate::conf::AI;
use crate::json_util::RawJSONArray;
Expand Down Expand Up @@ -61,8 +61,8 @@ impl AIModel {
// return (recommend, high)
pub fn translating_segment_tokens(&self) -> (usize, usize) {
match self {
AIModel::GPT3_5 => (3000, 3400),
AIModel::GPT4 => (3000, 3400),
AIModel::GPT3_5 => (3000, 3200),
AIModel::GPT4 => (3000, 3200),
}
}

Expand Down Expand Up @@ -260,19 +260,12 @@ impl OpenAI {
total_tokens: 0,
});

let real_tokens_rate: f32 = if usage.prompt_tokens > 1000 {
usage.completion_tokens as f32 / (usage.prompt_tokens as f32 - 90f32)
} else {
1.0f32
};

let elapsed = ctx.start.elapsed().as_millis() as u32;
ctx.set_kvs(vec![
("elapsed", elapsed.into()),
("prompt_tokens", usage.prompt_tokens.into()),
("completion_tokens", usage.completion_tokens.into()),
("total_tokens", usage.total_tokens.into()),
("real_tokens_rate", real_tokens_rate.into()),
("speed", (usage.total_tokens * 1000 / elapsed).into()),
])
.await;
Expand Down Expand Up @@ -523,6 +516,7 @@ impl OpenAI {
match Self::check_chat_response(res) {
Ok(rt) => Ok(rt),
Err(err) if err.code == 429 || err.code > 500 => {
sleep(Duration::from_secs(3)).await;
ctx.set("retry_because", err.to_string().into()).await;
rand_index += 1;
(api_url, headers) = self.get_params(&model_name, rand_index);
Expand Down Expand Up @@ -616,6 +610,7 @@ impl OpenAI {
match Self::check_chat_response(res) {
Ok(rt) => Ok(rt),
Err(err) if err.code == 429 || err.code > 500 => {
sleep(Duration::from_secs(3)).await;
ctx.set("retry_because", err.to_string().into()).await;
rand_index += 1;
(api_url, headers) = self.get_params(&model_name, rand_index);
Expand Down Expand Up @@ -654,11 +649,7 @@ impl OpenAI {
return Err(HTTPError {
code: 452,
message: "Content was triggered the filtering model".to_string(),
data: choice
.message
.content
.clone()
.map(serde_json::Value::String),
data: serde_json::to_value(rt).ok(),
});
}

Expand All @@ -667,23 +658,15 @@ impl OpenAI {
code: 422,
message: "Incomplete output due to max_tokens parameter"
.to_string(),
data: choice
.message
.content
.clone()
.map(serde_json::Value::String),
data: serde_json::to_value(rt).ok(),
})
}

reason => {
return Err(HTTPError {
code: 500,
message: format!("Unknown finish reason: {}", reason),
data: choice
.message
.content
.clone()
.map(serde_json::Value::String),
data: serde_json::to_value(rt).ok(),
});
}
}
Expand Down Expand Up @@ -752,6 +735,7 @@ impl OpenAI {
match Self::check_chat_response(res) {
Ok(rt) => Ok(rt),
Err(err) if err.code == 429 || err.code > 500 => {
sleep(Duration::from_secs(3)).await;
ctx.set("retry_because", err.to_string().into()).await;
rand_index += 1;
(api_url, headers) = self.get_params(&model_name, rand_index);
Expand Down

0 comments on commit 98f4b60

Please sign in to comment.