megacommit

This commit is contained in:
2025-10-07 22:48:08 +01:00
parent 7efac1ae33
commit 3e83f31c27
24 changed files with 2735 additions and 192 deletions
+49 -32
View File
@@ -1,6 +1,8 @@
// src/llm.rs
use serde::{Deserialize, Serialize};
use crate::messages::ChatMsg;
#[derive(Serialize)]
struct LlmRequest {
model: String,
@@ -13,36 +15,51 @@ struct Message {
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())
pub struct LlmWorker {
uri: String,
}
impl LlmWorker {
pub fn new(uri: String) -> Self {
Self { uri }
}
pub async fn query(&self, message: &ChatMsg) -> Result<ChatMsg, 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: message.text.clone().into(),
}],
};
// POST to lmstudio (default 127.0.0.1:1234)
let resp = client
.post(self.uri.clone())
.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(ChatMsg {
user_id: 0,
text: llm_resp.choices[0].message.content.clone(),
timestamp: chrono::Local::now().timestamp() as usize,
})
}
}