use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize)]
struct ChatMessage {
role: String,
content: String,
}
#[derive(Serialize)]
struct ChatRequest {
model: String,
messages: Vec<ChatMessage>,
temperature: f32,
max_tokens: Option<u32>,
}
#[derive(Deserialize)]
struct ChatChoice {
message: ChatMessage,
}
#[derive(Deserialize)]
struct ChatResponse {
choices: Vec<ChatChoice>,
}
pub async fn call_llm(prompt: &str, temperature: f32, max_tokens: Option<u32>) -> Result<String, String> {
let client = Client::new();
let request = ChatRequest {
model: "local".to_string(),
messages: vec![ChatMessage {
role: "user".to_string(),
content: prompt.to_string(),
}],
temperature,
max_tokens,
};
let response = client
.post("http://localhost:8080/v1/chat/completions")
.json(&request)
.send()
.await
.map_err(|e| e.to_string())?
.json::<ChatResponse>()
.await
.map_err(|e| e.to_string())?;
response.choices
.first()
.map(|c| c.message.content.clone())
.ok_or_else(|| "No response from LLM".to_string())
}