This commit is contained in:
2025-10-04 14:32:13 +01:00
parent 1cfc5774ad
commit 7efac1ae33
10 changed files with 1114 additions and 94 deletions
+48
View File
@@ -0,0 +1,48 @@
// src/llm.rs
use serde::{Deserialize, Serialize};
#[derive(Serialize)]
struct LlmRequest {
model: String,
messages: Vec<Message>,
}
#[derive(Serialize, Deserialize)]
struct Message {
role: String, // "user" or "assistant"
content: String,
}
pub async fn query_llm(text: &str) -> Result<String, String> {
let client = reqwest::Client::new();
// Build the request body
let payload = LlmRequest {
model: "gemma2-9b-it".into(), // whatever model you run locally
messages: vec![Message {
role: "user".into(),
content: text.into(),
}],
};
// POST to lmstudio (default 127.0.0.1:1234)
let resp = client
.post("http://127.0.0.1:1234/v1/chat/completions")
.json(&payload)
.send()
.await
.unwrap();
// The API returns a JSON with `choices[].message.content`
#[derive(Deserialize)]
struct LlmResponse {
choices: Vec<Choice>,
}
#[derive(Deserialize)]
struct Choice {
message: Message,
}
let llm_resp: LlmResponse = resp.json().await.unwrap();
Ok(llm_resp.choices[0].message.content.clone())
}