use crate::LLMApi;
use crate::Retry;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use urlencoding::encode;
#[derive(Debug, Deserialize)]
pub struct ChatResponse {
pub choice: String,
}
impl Default for ChatResponse {
fn default() -> ChatResponse {
ChatResponse {
choice: String::new(),
}
}
}
#[derive(Debug, Default, Serialize)]
pub struct ChatOptions<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub model: Option<&'a str>,
pub token_limit: u32,
pub restart: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub system_prompt: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pre_prompt: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub post_prompt: Option<&'a str>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub top_p: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stop: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
pub presence_penalty: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub frequency_penalty: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub logit_bias: Option<HashMap<String, i8>>,
}
impl LLMApi for (&str, &str, &ChatOptions<'_>) {
type Output = ChatResponse;
async fn api(&self, endpoint: &str, api_key: &str) -> Retry<Self::Output> {
chat_completion_inner(endpoint, api_key, self.0, self.1, self.2).await
}
}
impl<'a> crate::LLMServiceFlows<'a> {
pub async fn chat_completion(
&self,
conversation_id: &str,
sentence: &str,
options: &ChatOptions<'_>,
) -> Result<ChatResponse, String> {
self.keep_trying((conversation_id, sentence, options)).await
}
}
async fn chat_completion_inner(
endpoint: &str,
api_key: &str,
conversation_id: &str,
sentence: &str,
options: &ChatOptions<'_>,
) -> Retry<ChatResponse> {
let flows_user = unsafe { crate::_get_flows_user() };
let flow_id = unsafe { crate::_get_flow_id() };
let uri = format!(
"{}/{}/{}/chat_completion?endpoint={}&api_key={}&conversation={}",
crate::LLM_API_PREFIX.as_str(),
flows_user,
flow_id,
encode(endpoint),
encode(api_key),
encode(conversation_id),
);
let body = serde_json::to_vec(&serde_json::json!({
"sentence": sentence,
"params": options
}))
.unwrap_or_default();
match reqwest::Client::new()
.post(uri)
.header("Content-Type", "application/json")
.header("Content-Length", body.len())
.body(body)
.send()
.await
{
Ok(res) => {
let status = res.status();
let body = res.bytes().await.unwrap();
match status.is_success() {
true => Retry::No(
serde_json::from_slice::<ChatResponse>(body.as_ref())
.or(Err(String::from("Unexpected error"))),
),
false => {
match status.into() {
409 | 429 | 503 => {
Retry::Yes(String::from_utf8_lossy(body.as_ref()).into_owned())
}
_ => Retry::No(Err(String::from_utf8_lossy(body.as_ref()).into_owned())),
}
}
}
}
Err(e) => Retry::No(Err(e.to_string())),
}
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ChatRole {
User,
Assistant,
}
#[derive(Debug, Deserialize)]
pub struct ChatMessage {
pub role: ChatRole,
pub content: String,
}
pub async fn chat_history(conversation_id: &str, limit: u8) -> Option<Vec<ChatMessage>> {
let flows_user = unsafe { crate::_get_flows_user() };
let flow_id = unsafe { crate::_get_flow_id() };
let uri = format!(
"{}/{}/{}/chat_history?conversation={}&limit={}",
crate::LLM_API_PREFIX.as_str(),
flows_user,
flow_id,
encode(conversation_id),
limit
);
match reqwest::get(&uri).await {
Ok(res) => match res.status().is_success() {
true => {
serde_json::from_slice::<Vec<ChatMessage>>(&res.bytes().await.unwrap().as_ref())
.ok()
}
false => None,
},
Err(_) => None,
}
}