use std::collections::HashMap;
use futures_util::StreamExt;
use serde::{Deserialize, Serialize};
use serde_json::Value;
pub const DEFAULT_BASE_URL: &str = "https://gate.nabuxai.com/v1";
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("nabugate: transport error: {0}")]
Transport(#[from] reqwest::Error),
#[error("nabugate: request failed ({status}): {body}")]
Api { status: u16, body: String },
#[error("nabugate: could not decode the response: {0}")]
Decode(#[from] serde_json::Error),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
pub role: String,
pub content: Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_call_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Value>,
}
impl Message {
fn text(role: &str, content: impl Into<String>) -> Self {
Self {
role: role.to_string(),
content: Value::String(content.into()),
name: None,
tool_call_id: None,
tool_calls: None,
}
}
pub fn system(content: impl Into<String>) -> Self {
Self::text("system", content)
}
pub fn user(content: impl Into<String>) -> Self {
Self::text("user", content)
}
pub fn assistant(content: impl Into<String>) -> Self {
Self::text("assistant", content)
}
pub fn as_text(&self) -> &str {
self.content.as_str().unwrap_or_default()
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct ChatRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub model: Option<String>,
pub messages: Vec<Message>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub max_tokens: Option<u32>,
#[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 seed: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tools: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_choice: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub conversation_id: Option<String>,
#[serde(flatten)]
pub extra: HashMap<String, Value>,
}
impl ChatRequest {
pub fn new(messages: Vec<Message>) -> Self {
Self {
messages,
..Default::default()
}
}
pub fn model(mut self, model: impl Into<String>) -> Self {
self.model = Some(model.into());
self
}
pub fn temperature(mut self, temperature: f32) -> Self {
self.temperature = Some(temperature);
self
}
pub fn max_tokens(mut self, max_tokens: u32) -> Self {
self.max_tokens = Some(max_tokens);
self
}
pub fn param(mut self, key: impl Into<String>, value: Value) -> Self {
self.extra.insert(key.into(), value);
self
}
}
#[derive(Debug, Clone, Default, Deserialize)]
pub struct Usage {
#[serde(default)]
pub prompt_tokens: u32,
#[serde(default)]
pub completion_tokens: u32,
#[serde(default)]
pub total_tokens: u32,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ChatChoice {
#[serde(default)]
pub index: u32,
pub message: Message,
#[serde(default)]
pub finish_reason: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ChatResponse {
#[serde(default)]
pub id: String,
#[serde(default)]
pub model: String,
pub choices: Vec<ChatChoice>,
#[serde(default)]
pub usage: Usage,
}
impl ChatResponse {
pub fn text(&self) -> &str {
self.choices
.first()
.map(|c| c.message.as_text())
.unwrap_or_default()
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct StreamChunk {
#[serde(default)]
pub id: String,
#[serde(default)]
pub choices: Vec<StreamChoice>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct StreamChoice {
#[serde(default)]
pub delta: Delta,
#[serde(default)]
pub finish_reason: Option<String>,
}
#[derive(Debug, Clone, Default, Deserialize)]
pub struct Delta {
#[serde(default)]
pub role: Option<String>,
#[serde(default)]
pub content: Option<String>,
#[serde(default)]
pub tool_calls: Option<Value>,
}
#[derive(Debug, Clone, Serialize)]
pub struct EmbeddingsRequest {
pub model: String,
pub input: Value,
#[serde(skip_serializing_if = "Option::is_none")]
pub dimensions: Option<u32>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Embedding {
#[serde(default)]
pub index: u32,
pub embedding: Vec<f32>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct EmbeddingsResponse {
#[serde(default)]
pub model: String,
pub data: Vec<Embedding>,
#[serde(default)]
pub usage: Usage,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ModelInfo {
pub id: String,
#[serde(default)]
pub object: String,
#[serde(default)]
pub owned_by: String,
}
#[derive(Clone)]
pub struct NabuGateClient {
api_key: String,
base_url: String,
default_model: String,
http: reqwest::Client,
}
impl NabuGateClient {
pub fn new(api_key: impl Into<String>) -> Self {
Self {
api_key: api_key.into(),
base_url: DEFAULT_BASE_URL.to_string(),
default_model: "nabu-smart".to_string(),
http: reqwest::Client::new(),
}
}
pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = base_url.into().trim_end_matches('/').to_string();
self
}
pub fn default_model(mut self, model: impl Into<String>) -> Self {
self.default_model = model.into();
self
}
pub fn http_client(mut self, http: reqwest::Client) -> Self {
self.http = http;
self
}
pub async fn chat(&self, request: ChatRequest) -> Result<ChatResponse, Error> {
let body = self.chat_body(request, false)?;
self.post_json("/chat/completions", &body).await
}
pub async fn complete_text(&self, request: ChatRequest) -> Result<String, Error> {
Ok(self.chat(request).await?.text().trim().to_string())
}
pub async fn stream<F>(&self, request: ChatRequest, mut on_chunk: F) -> Result<(), Error>
where
F: FnMut(StreamChunk) -> bool,
{
let body = self.chat_body(request, true)?;
let response = self.send("/chat/completions", &body).await?;
let mut stream = response.bytes_stream();
let mut buffer = String::new();
while let Some(bytes) = stream.next().await {
buffer.push_str(&String::from_utf8_lossy(&bytes?));
while let Some(newline) = buffer.find('\n') {
let line = buffer[..newline].trim().to_string();
buffer.drain(..=newline);
let Some(payload) = line.strip_prefix("data:") else {
continue;
};
let payload = payload.trim();
if payload == "[DONE]" {
return Ok(());
}
if let Ok(chunk) = serde_json::from_str::<StreamChunk>(payload) {
if !on_chunk(chunk) {
return Ok(());
}
}
}
}
Ok(())
}
pub async fn stream_text<F>(&self, request: ChatRequest, mut on_text: F) -> Result<(), Error>
where
F: FnMut(&str) -> bool,
{
self.stream(request, |chunk| {
for choice in &chunk.choices {
if let Some(content) = &choice.delta.content {
if !content.is_empty() && !on_text(content) {
return false;
}
}
}
true
})
.await
}
pub async fn embeddings(
&self,
input: Value,
model: Option<&str>,
dimensions: Option<u32>,
) -> Result<EmbeddingsResponse, Error> {
let request = EmbeddingsRequest {
model: model.unwrap_or("nabu-embed").to_string(),
input,
dimensions,
};
self.post_json("/embeddings", &request).await
}
pub async fn images(&self, prompt: &str, model: Option<&str>) -> Result<Value, Error> {
let body = serde_json::json!({
"model": model.unwrap_or("nabu-image"),
"prompt": prompt,
});
self.post_json("/images/generations", &body).await
}
pub async fn speech(
&self,
input: &str,
model: Option<&str>,
voice: Option<&str>,
) -> Result<Vec<u8>, Error> {
let mut body = serde_json::json!({
"model": model.unwrap_or("nabu-voice"),
"input": input,
});
if let Some(voice) = voice {
body["voice"] = Value::String(voice.to_string());
}
let response = self.send("/audio/speech", &body).await?;
Ok(response.bytes().await?.to_vec())
}
pub async fn models(&self) -> Result<Vec<ModelInfo>, Error> {
#[derive(Deserialize)]
struct Catalogue {
#[serde(default)]
data: Vec<ModelInfo>,
}
let catalogue: Catalogue = self.get_json("/models").await?;
Ok(catalogue.data)
}
pub async fn usage(&self) -> Result<Value, Error> {
self.get_json("/usage").await
}
fn chat_body(&self, mut request: ChatRequest, stream: bool) -> Result<Value, Error> {
if request.model.is_none() {
request.model = Some(self.default_model.clone());
}
let mut body = serde_json::to_value(request)?;
if let Some(map) = body.as_object_mut() {
map.insert("stream".to_string(), Value::Bool(stream));
}
Ok(body)
}
async fn send<B: Serialize>(&self, path: &str, body: &B) -> Result<reqwest::Response, Error> {
let response = self
.http
.post(format!("{}{}", self.base_url, path))
.bearer_auth(&self.api_key)
.json(body)
.send()
.await?;
Self::check(response).await
}
async fn check(response: reqwest::Response) -> Result<reqwest::Response, Error> {
let status = response.status();
if status.is_success() {
return Ok(response);
}
let body = response.text().await.unwrap_or_default();
Err(Error::Api {
status: status.as_u16(),
body,
})
}
async fn post_json<B: Serialize, T: for<'de> Deserialize<'de>>(
&self,
path: &str,
body: &B,
) -> Result<T, Error> {
let text = self.send(path, body).await?.text().await?;
Ok(serde_json::from_str(&text)?)
}
async fn get_json<T: for<'de> Deserialize<'de>>(&self, path: &str) -> Result<T, Error> {
let response = self
.http
.get(format!("{}{}", self.base_url, path))
.bearer_auth(&self.api_key)
.send()
.await?;
let text = Self::check(response).await?.text().await?;
Ok(serde_json::from_str(&text)?)
}
}