Files
worldcoder/src/llm_integration/content_llm.rs
T
2025-07-24 00:18:09 +01:00

80 lines
2.1 KiB
Rust

use serde::{Deserialize, Serialize};
pub fn continue_content(
context: &str,
instruction: &str,
_max_tokens: usize,
) -> Result<String, Box<dyn std::error::Error>> {
let client = reqwest::blocking::Client::new();
let messages = vec![
Message {
role: "system".to_string(),
content: "
Please generate content that is a direct continuation of the given text.
Your response should be a logical next step in the content and should not repeat any of the text from the instruction or the content.
Do not generate any text that is not a direct continuation of the content.
if extra instructions are provided, follow them exactly, otherwise continue the text in a logical way.
".to_string(),
},
Message {
role: "user".to_string(),
content: context.to_string(),
},
Message {
role: "user".to_string(),
content: format!("Instructions: {instruction}"),
},
];
let request = ChatRequest {
messages,
temperature: 0.7,
};
let response = client
.post("http://localhost:1234/v1/chat/completions")
.json(&request)
.send()?;
if !response.status().is_success() {
return Err(format!("Request failed: {}", response.text()?).into());
}
let response: ChatResponse = response.json()?;
if let Some(choice) = response.choices.into_iter().next() {
Ok(choice.message.content)
} else {
Err("No response from model".into())
}
}
pub fn ai_enabled() -> bool {
let client = reqwest::blocking::Client::new();
client.get("http://localhost:1234/v1/models").send().is_ok()
}
// Simple request structure
#[derive(Serialize)]
struct ChatRequest {
messages: Vec<Message>,
temperature: f32,
}
#[derive(Serialize, Deserialize, Debug)]
struct Message {
role: String,
content: String,
}
#[derive(Deserialize, Debug)]
struct ChatResponse {
choices: Vec<Choice>,
}
#[derive(Deserialize, Debug)]
struct Choice {
message: Message,
}