use super::Sink;
use crate::formatter::Format;
use log::Record;
use reqwest::blocking::Client;
use serde_json::json;
pub struct XGrokSink {
api_key: String,
client: Client,
}
impl XGrokSink {
pub fn new(api_key: impl Into<String>) -> Self {
Self {
api_key: api_key.into(),
client: Client::new(),
}
}
}
impl Sink for XGrokSink {
fn write(&self, record: &Record, formatter: &dyn Format) {
let formatted = formatter.format(record);
let payload = json!({
"model": "grok-2",
"messages": [
{
"role": "system",
"content": "You are a log analyzer. Please ingest this log silently."
},
{
"role": "user",
"content": formatted
}
]
});
let _ = self.client.post("https://api.x.ai/v1/chat/completions")
.header("Authorization", format!("Bearer {}", self.api_key))
.json(&payload)
.send();
}
fn flush(&self) {
}
}