Skip to content

Commit

Permalink
🐛 FIx chat validation about new chatgpt
Browse files Browse the repository at this point in the history
  • Loading branch information
luoshuijs committed Mar 27, 2024
1 parent 94cc8dc commit 45fdee7
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions crates/openai/src/serve/proxy/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::header_convert;
use super::toapi;
use crate::serve::error::{ProxyError, ResponseError};
use crate::serve::puid::{get_or_init, reduce_key};
use crate::URL_CHATGPT_API;

#[async_trait]
impl SendRequestExt for reqwest::Client {
Expand Down Expand Up @@ -119,6 +120,15 @@ async fn handle_conv_request(req: &mut RequestExt) -> Result<(), ResponseError>
}
}

let chat_requirements_token = create_chat_requirements_token(&token).await?;
if let Some(chat_requirements_token) = chat_requirements_token {
req.headers.insert(
header::HeaderName::from_static("openai-sentinel-chat-requirements-token"),
header::HeaderValue::from_str(chat_requirements_token.as_str())
.map_err(ResponseError::BadRequest)?,
);
}

// Parse model
let model = GPTModel::from_str(model).map_err(ResponseError::BadRequest)?;

Expand Down Expand Up @@ -194,3 +204,23 @@ async fn handle_dashboard_request(req: &mut RequestExt) -> Result<(), ResponseEr

Ok(())
}

async fn create_chat_requirements_token(token: &str) -> Result<Option<String>, ResponseError> {
let token = token.trim_start_matches("Bearer ");
let resp = with_context!(api_client)
.get(format!("{URL_CHATGPT_API}/backend-api/sentinel/chat-requirements"))
.bearer_auth(token)
.send()
.await
.map_err(ResponseError::InternalServerError)?
.error_for_status()
.map_err(ResponseError::BadRequest)?;
let body = resp.bytes().await?;
let json = serde_json::from_slice::<Value>(&body).map_err(ResponseError::BadRequest)?;
if let Some(token_value) = json.get("token") {
if let Some(token_str) = token_value.as_str() {
return Ok(Some(token_str.to_owned()));
}
}
Ok(None)
}

0 comments on commit 45fdee7

Please sign in to comment.