use http_req::{
request::{Method, Request},
uri::Uri,
};
use serde::Deserialize;
use std::fmt;
use urlencoding::encode;
use crate::Retry;
#[derive(Debug, Deserialize)]
pub struct ChatResponse {
pub restarted: bool,
pub choice: String,
}
impl Default for ChatResponse {
fn default() -> ChatResponse {
ChatResponse {
restarted: true,
choice: String::new(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum ChatModel {
GPT4_32K,
GPT4,
GPT35Turbo,
}
impl fmt::Display for ChatModel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ChatModel::GPT4_32K => write!(f, "gpt-4-32k"),
ChatModel::GPT4 => write!(f, "gpt-4"),
ChatModel::GPT35Turbo => write!(f, "gpt-3.5-turbo"),
}
}
}
impl Default for ChatModel {
fn default() -> ChatModel {
ChatModel::GPT35Turbo
}
}
#[derive(Debug, Default)]
pub struct ChatOptions<'a> {
pub model: ChatModel,
pub restart: bool,
pub system_prompt: Option<&'a str>,
}
impl crate::OpenAIFlows {
pub async fn chat_completion(
&self,
conversation_id: &str,
sentence: &str,
options: &ChatOptions<'_>,
) -> Result<ChatResponse, String> {
self.keey_trying(|account| {
chat_completion_inner(account, conversation_id, sentence, options)
})
}
}
fn chat_completion_inner(
account: &str,
conversation_id: &str,
sentence: &str,
options: &ChatOptions,
) -> Retry<ChatResponse> {
unsafe {
let mut flows_user = Vec::<u8>::with_capacity(100);
let c = crate::get_flows_user(flows_user.as_mut_ptr());
flows_user.set_len(c as usize);
let flows_user = String::from_utf8(flows_user).unwrap();
let mut flow_id = Vec::<u8>::with_capacity(100);
let c = crate::get_flow_id(flow_id.as_mut_ptr());
if c == 0 {
panic!("Failed to get flow id");
}
flow_id.set_len(c as usize);
let flow_id = String::from_utf8(flow_id).unwrap();
let mut writer = Vec::new();
let uri = format!(
"{}/{}/{}/chat_completion?account={}&conversation={}&model={}&restart={}",
crate::OPENAI_API_PREFIX.as_str(),
flows_user,
flow_id,
encode(account),
encode(conversation_id),
options.model,
options.restart,
);
let uri = Uri::try_from(uri.as_str()).unwrap();
let body = serde_json::to_vec(&serde_json::json!({
"sentence": sentence,
"system_prompt": options.system_prompt
}))
.unwrap_or_default();
match Request::new(&uri)
.method(Method::POST)
.header("Content-Type", "application/json")
.header("Content-Length", &body.len())
.body(&body)
.send(&mut writer)
{
Ok(res) => {
match res.status_code().is_success() {
true => Retry::No(
serde_json::from_slice::<ChatResponse>(&writer)
.or(Err(String::from("Unexpected error"))),
),
false => {
match res.status_code().into() {
409 | 429 | 503 => {
Retry::Yes(String::from_utf8_lossy(&writer).into_owned())
}
_ => Retry::No(Err(String::from_utf8_lossy(&writer).into_owned())),
}
}
}
}
Err(e) => Retry::No(Err(e.to_string())),
}
}
}