1c9a7c0b05
Co-authored-by: Michael Neale <michael.neale@gmail.com> Co-authored-by: Wendy Tang <wendytang@squareup.com> Co-authored-by: Jarrod Sibbison <72240382+jsibbison-square@users.noreply.github.com> Co-authored-by: Alex Hancock <alex.hancock@example.com> Co-authored-by: Alex Hancock <alexhancock@block.xyz> Co-authored-by: Lifei Zhou <lifei@squareup.com> Co-authored-by: Wes <141185334+wesrblock@users.noreply.github.com> Co-authored-by: Max Novich <maksymstepanenko1990@gmail.com> Co-authored-by: Zaki Ali <zaki@squareup.com> Co-authored-by: Salman Mohammed <smohammed@squareup.com> Co-authored-by: Kalvin C <kalvinnchau@users.noreply.github.com> Co-authored-by: Alec Thomas <alec@swapoff.org> Co-authored-by: lily-de <119957291+lily-de@users.noreply.github.com> Co-authored-by: kalvinnchau <kalvin@block.xyz> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Rizel Scarlett <rizel@squareup.com> Co-authored-by: bwrage <bwrage@squareup.com> Co-authored-by: Kalvin Chau <kalvin@squareup.com> Co-authored-by: Alice Hau <110418948+ahau-square@users.noreply.github.com> Co-authored-by: Alistair Gray <ajgray@stripe.com> Co-authored-by: Nahiyan Khan <nahiyan.khan@gmail.com> Co-authored-by: Alex Hancock <alexhancock@squareup.com> Co-authored-by: Nahiyan Khan <nahiyan@squareup.com> Co-authored-by: marcelle <1852848+laanak08@users.noreply.github.com> Co-authored-by: Yingjie He <yingjiehe@block.xyz> Co-authored-by: Yingjie He <yingjiehe@squareup.com> Co-authored-by: Lily Delalande <ldelalande@block.xyz> Co-authored-by: Adewale Abati <acekyd01@gmail.com> Co-authored-by: Ebony Louis <ebony774@gmail.com> Co-authored-by: Angie Jones <jones.angie@gmail.com> Co-authored-by: Ebony Louis <55366651+EbonyLouis@users.noreply.github.com>
146 lines
4.8 KiB
Rust
146 lines
4.8 KiB
Rust
use super::errors::ProviderError;
|
|
use crate::message::Message;
|
|
use crate::model::ModelConfig;
|
|
use crate::providers::base::{ConfigKey, Provider, ProviderMetadata, ProviderUsage};
|
|
use crate::providers::formats::openai::{create_request, get_usage, response_to_message};
|
|
use crate::providers::utils::get_model;
|
|
use anyhow::Result;
|
|
use async_trait::async_trait;
|
|
use mcp_core::Tool;
|
|
use reqwest::{Client, StatusCode};
|
|
use serde_json::Value;
|
|
use std::time::Duration;
|
|
|
|
pub const GROQ_API_HOST: &str = "https://api.groq.com";
|
|
pub const GROQ_DEFAULT_MODEL: &str = "llama-3.3-70b-versatile";
|
|
pub const GROQ_KNOWN_MODELS: &[&str] = &["gemma2-9b-it", "llama-3.3-70b-versatile"];
|
|
|
|
pub const GROQ_DOC_URL: &str = "https://console.groq.com/docs/models";
|
|
|
|
#[derive(serde::Serialize)]
|
|
pub struct GroqProvider {
|
|
#[serde(skip)]
|
|
client: Client,
|
|
host: String,
|
|
api_key: String,
|
|
model: ModelConfig,
|
|
}
|
|
|
|
impl Default for GroqProvider {
|
|
fn default() -> Self {
|
|
let model = ModelConfig::new(GroqProvider::metadata().default_model);
|
|
GroqProvider::from_env(model).expect("Failed to initialize Groq provider")
|
|
}
|
|
}
|
|
|
|
impl GroqProvider {
|
|
pub fn from_env(model: ModelConfig) -> Result<Self> {
|
|
let config = crate::config::Config::global();
|
|
let api_key: String = config.get_secret("GROQ_API_KEY")?;
|
|
let host: String = config
|
|
.get("GROQ_HOST")
|
|
.unwrap_or_else(|_| GROQ_API_HOST.to_string());
|
|
|
|
let client = Client::builder()
|
|
.timeout(Duration::from_secs(600))
|
|
.build()?;
|
|
|
|
Ok(Self {
|
|
client,
|
|
host,
|
|
api_key,
|
|
model,
|
|
})
|
|
}
|
|
|
|
async fn post(&self, payload: Value) -> anyhow::Result<Value, ProviderError> {
|
|
let url = format!(
|
|
"{}/openai/v1/chat/completions",
|
|
self.host.trim_end_matches('/')
|
|
);
|
|
|
|
let response = self
|
|
.client
|
|
.post(&url)
|
|
.header("Authorization", format!("Bearer {}", self.api_key))
|
|
.json(&payload)
|
|
.send()
|
|
.await?;
|
|
|
|
let status = response.status();
|
|
let payload: Option<Value> = response.json().await.ok();
|
|
|
|
match status {
|
|
StatusCode::OK => payload.ok_or_else( || ProviderError::RequestFailed("Response body is not valid JSON".to_string()) ),
|
|
StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => {
|
|
Err(ProviderError::Authentication(format!("Authentication failed. Please ensure your API keys are valid and have the required permissions. \
|
|
Status: {}. Response: {:?}", status, payload)))
|
|
}
|
|
StatusCode::PAYLOAD_TOO_LARGE => {
|
|
Err(ProviderError::ContextLengthExceeded(format!("{:?}", payload)))
|
|
}
|
|
StatusCode::TOO_MANY_REQUESTS => {
|
|
Err(ProviderError::RateLimitExceeded(format!("{:?}", payload)))
|
|
}
|
|
StatusCode::INTERNAL_SERVER_ERROR | StatusCode::SERVICE_UNAVAILABLE => {
|
|
Err(ProviderError::ServerError(format!("{:?}", payload)))
|
|
}
|
|
_ => {
|
|
tracing::debug!(
|
|
"{}", format!("Provider request failed with status: {}. Payload: {:?}", status, payload)
|
|
);
|
|
Err(ProviderError::RequestFailed(format!("Request failed with status: {}", status)))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Provider for GroqProvider {
|
|
fn metadata() -> ProviderMetadata {
|
|
ProviderMetadata::new(
|
|
"groq",
|
|
"Groq",
|
|
"Fast inference with Groq hardware",
|
|
GROQ_DEFAULT_MODEL,
|
|
GROQ_KNOWN_MODELS.iter().map(|&s| s.to_string()).collect(),
|
|
GROQ_DOC_URL,
|
|
vec![
|
|
ConfigKey::new("GROQ_API_KEY", true, true, None),
|
|
ConfigKey::new("GROQ_HOST", false, false, Some(GROQ_API_HOST)),
|
|
],
|
|
)
|
|
}
|
|
|
|
fn get_model_config(&self) -> ModelConfig {
|
|
self.model.clone()
|
|
}
|
|
|
|
#[tracing::instrument(
|
|
skip(self, system, messages, tools),
|
|
fields(model_config, input, output, input_tokens, output_tokens, total_tokens)
|
|
)]
|
|
async fn complete(
|
|
&self,
|
|
system: &str,
|
|
messages: &[Message],
|
|
tools: &[Tool],
|
|
) -> anyhow::Result<(Message, ProviderUsage), ProviderError> {
|
|
let payload = create_request(
|
|
&self.model,
|
|
system,
|
|
messages,
|
|
tools,
|
|
&super::utils::ImageFormat::OpenAi,
|
|
)?;
|
|
|
|
let response = self.post(payload.clone()).await?;
|
|
|
|
let message = response_to_message(response.clone())?;
|
|
let usage = get_usage(&response)?;
|
|
let model = get_model(&response);
|
|
super::utils::emit_debug_trace(self, &payload, &response, &usage);
|
|
Ok((message, ProviderUsage::new(model, usage)))
|
|
}
|
|
}
|