mirror of https://github.com/sigoden/aichat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
5.7 KiB
Rust
205 lines
5.7 KiB
Rust
use super::{
|
|
extract_sytem_message, json_stream, message::*, Client, CohereClient, ExtraConfig, Model,
|
|
ModelConfig, PromptType, ReplyHandler, SendData,
|
|
};
|
|
|
|
use crate::utils::PromptKind;
|
|
|
|
use anyhow::{bail, Result};
|
|
use async_trait::async_trait;
|
|
use reqwest::{Client as ReqwestClient, RequestBuilder};
|
|
use serde::Deserialize;
|
|
use serde_json::{json, Value};
|
|
|
|
const API_URL: &str = "https://api.cohere.ai/v1/chat";
|
|
|
|
const MODELS: [(&str, usize, &str); 2] = [
|
|
// https://docs.cohere.com/docs/command-r
|
|
("command-r", 128000, "text"),
|
|
("command-r-plus", 128000, "text"),
|
|
];
|
|
|
|
#[derive(Debug, Clone, Deserialize, Default)]
|
|
pub struct CohereConfig {
|
|
pub name: Option<String>,
|
|
pub api_key: Option<String>,
|
|
#[serde(default)]
|
|
pub models: Vec<ModelConfig>,
|
|
pub extra: Option<ExtraConfig>,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Client for CohereClient {
|
|
client_common_fns!();
|
|
|
|
async fn send_message_inner(&self, client: &ReqwestClient, data: SendData) -> Result<String> {
|
|
let builder = self.request_builder(client, data)?;
|
|
send_message(builder).await
|
|
}
|
|
|
|
async fn send_message_streaming_inner(
|
|
&self,
|
|
client: &ReqwestClient,
|
|
handler: &mut ReplyHandler,
|
|
data: SendData,
|
|
) -> Result<()> {
|
|
let builder = self.request_builder(client, data)?;
|
|
send_message_streaming(builder, handler).await
|
|
}
|
|
}
|
|
|
|
impl CohereClient {
|
|
list_models_fn!(CohereConfig, &MODELS);
|
|
config_get_fn!(api_key, get_api_key);
|
|
|
|
pub const PROMPTS: [PromptType<'static>; 1] =
|
|
[("api_key", "API Key:", false, PromptKind::String)];
|
|
|
|
fn request_builder(&self, client: &ReqwestClient, data: SendData) -> Result<RequestBuilder> {
|
|
let api_key = self.get_api_key().ok();
|
|
|
|
let body = build_body(data, &self.model)?;
|
|
|
|
let url = API_URL;
|
|
|
|
debug!("Cohere Request: {url} {body}");
|
|
|
|
let mut builder = client.post(url).json(&body);
|
|
if let Some(api_key) = api_key {
|
|
builder = builder.bearer_auth(api_key);
|
|
}
|
|
|
|
Ok(builder)
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn send_message(builder: RequestBuilder) -> Result<String> {
|
|
let res = builder.send().await?;
|
|
let status = res.status();
|
|
let data: Value = res.json().await?;
|
|
if status != 200 {
|
|
catch_error(&data, status.as_u16())?;
|
|
}
|
|
let output = extract_text(&data)?;
|
|
Ok(output.to_string())
|
|
}
|
|
|
|
pub(crate) async fn send_message_streaming(
|
|
builder: RequestBuilder,
|
|
handler: &mut ReplyHandler,
|
|
) -> Result<()> {
|
|
let res = builder.send().await?;
|
|
let status = res.status();
|
|
if status != 200 {
|
|
let data: Value = res.json().await?;
|
|
catch_error(&data, status.as_u16())?;
|
|
} else {
|
|
let handle = |value: &str| -> Result<()> {
|
|
let value: Value = serde_json::from_str(value)?;
|
|
if let Some("text-generation") = value["event_type"].as_str() {
|
|
handler.text(extract_text(&value)?)?;
|
|
}
|
|
Ok(())
|
|
};
|
|
json_stream(res.bytes_stream(), handle).await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn build_body(data: SendData, model: &Model) -> Result<Value> {
|
|
let SendData {
|
|
mut messages,
|
|
temperature,
|
|
top_p,
|
|
stream,
|
|
} = data;
|
|
|
|
let system_message = extract_sytem_message(&mut messages);
|
|
|
|
let mut image_urls = vec![];
|
|
let mut messages: Vec<Value> = messages
|
|
.into_iter()
|
|
.map(|message| {
|
|
let role = match message.role {
|
|
MessageRole::User => "USER",
|
|
_ => "CHATBOT",
|
|
};
|
|
match message.content {
|
|
MessageContent::Text(text) => json!({
|
|
"role": role,
|
|
"message": text,
|
|
}),
|
|
MessageContent::Array(list) => {
|
|
let list: Vec<String> = list
|
|
.into_iter()
|
|
.filter_map(|item| match item {
|
|
MessageContentPart::Text { text } => Some(text),
|
|
MessageContentPart::ImageUrl {
|
|
image_url: ImageUrl { url },
|
|
} => {
|
|
image_urls.push(url.clone());
|
|
None
|
|
}
|
|
})
|
|
.collect();
|
|
json!({ "role": role, "message": list.join("\n\n") })
|
|
}
|
|
}
|
|
})
|
|
.collect();
|
|
|
|
if !image_urls.is_empty() {
|
|
bail!("The model does not support images: {:?}", image_urls);
|
|
}
|
|
let message = messages.pop().unwrap();
|
|
let message = message["message"].as_str().unwrap_or_default();
|
|
|
|
let mut body = json!({
|
|
"model": &model.name,
|
|
"message": message,
|
|
});
|
|
|
|
if let Some(preamble) = system_message {
|
|
body["preamble"] = preamble.into();
|
|
}
|
|
|
|
if let Some(max_tokens) = model.max_output_tokens {
|
|
body["max_tokens"] = max_tokens.into();
|
|
}
|
|
|
|
if !messages.is_empty() {
|
|
body["chat_history"] = messages.into();
|
|
}
|
|
|
|
if let Some(temperature) = temperature {
|
|
body["temperature"] = temperature.into();
|
|
}
|
|
if let Some(top_p) = top_p {
|
|
body["p"] = top_p.into();
|
|
}
|
|
if stream {
|
|
body["stream"] = true.into();
|
|
}
|
|
|
|
Ok(body)
|
|
}
|
|
|
|
fn catch_error(data: &Value, status: u16) -> Result<()> {
|
|
debug!("Invalid response, status: {status}, data: {data}");
|
|
|
|
if let Some(message) = data["message"].as_str() {
|
|
bail!("{message}");
|
|
} else {
|
|
bail!("Invalid response, status: {status}, data: {data}");
|
|
}
|
|
}
|
|
|
|
fn extract_text(data: &Value) -> Result<&str> {
|
|
match data["text"].as_str() {
|
|
Some(text) => Ok(text),
|
|
None => {
|
|
bail!("Invalid response data: {data}")
|
|
}
|
|
}
|
|
}
|