use wasm_bindgen::prelude::wasm_bindgen;
use reqwest::blocking::Client;
use serde_json::json;
use std::io;
#[wasm_bindgen]
pub fn your_cool_function(input: &str) -> String {
format!("Processed: {}", input)
}
fn main() {
let client = Client::builder()
.timeout(std::time::Duration::from_secs(600))
.build()
.unwrap();
println!("AI Chat! Type your message below (or type 'exit' to quit):");
let mut messages = Vec::new();
loop {
let mut prompt = String::new();
io::stdin().read_line(&mut prompt).expect("Failed to read line");
let prompt = prompt.trim();
if prompt.eq_ignore_ascii_case("exit") {
println!("Goodbye!");
break;
}
messages.push(json!({
"role": "user",
"content": prompt
}));
let response = client.post("https://ollama.molodetz.nl/api/chat")
.header("Content-Type", "application/json")
.body(json!({
"model": "qwen2.5:1.5b",
"messages": messages
}).to_string())
.send();
match response {
Ok(resp) => {
match resp.bytes() {
Ok(data) => {
let raw_text = String::from_utf8_lossy(&data);
let mut lines = raw_text.lines();
let mut assistant_response = String::new();
while let Some(line) = lines.next() {
match serde_json::from_str::<serde_json::Value>(line) {
Ok(json_response) => {
if let Some(message) = json_response.get("message") {
if let Some(content) = message.get("content") {
if let Some(s) = content.as_str() {
assistant_response.push_str(s);
}
}
} else {
println!("No message found.");
}
}
Err(e) => {
println!("Failed to parse JSON: {:?}", e);
}
}
}
messages.push(json!({
"role": "assistant",
"content": assistant_response
}));
}
Err(e) => {
eprintln!("Error reading response: {:?}", e);
}
}
println!("");
}
Err(e) => {
eprintln!("Error: {:?}", e);
}
}
}
}