Appended history.

This commit is contained in:
retoor 2025-03-17 01:02:46 +01:00
parent 60b89ebeaa
commit 0233814d55
2 changed files with 22 additions and 51 deletions

View File

@ -47,13 +47,14 @@ fn main() {
Ok(data) => { Ok(data) => {
let raw_text = String::from_utf8_lossy(&data); let raw_text = String::from_utf8_lossy(&data);
let mut lines = raw_text.lines(); let mut lines = raw_text.lines();
let mut assistant_response = String::new();
while let Some(line) = lines.next() { while let Some(line) = lines.next() {
match serde_json::from_str::<serde_json::Value>(line) { match serde_json::from_str::<serde_json::Value>(line) {
Ok(json_response) => { Ok(json_response) => {
if let Some(message) = json_response.get("message") { if let Some(message) = json_response.get("message") {
if let Some(content) = message.get("content") { if let Some(content) = message.get("content") {
if let Some(s) = content.as_str() { if let Some(s) = content.as_str() {
print!("{}", s); assistant_response.push_str(s);
} }
} }
} else { } else {
@ -65,6 +66,10 @@ fn main() {
} }
} }
} }
messages.push(json!({
"role": "assistant",
"content": assistant_response
}));
} }
Err(e) => { Err(e) => {
eprintln!("Error reading response: {:?}", e); eprintln!("Error reading response: {:?}", e);
@ -78,4 +83,4 @@ fn main() {
} }
} }
} }
} }

View File

@ -2,46 +2,14 @@ use wasm_bindgen::prelude::wasm_bindgen;
use reqwest::blocking::Client; use reqwest::blocking::Client;
use serde_json::json; use serde_json::json;
use std::env;
use std::fs;
use std::io; use std::io;
// Author: retoor@molodetz.nl
// Simple file description / purpose
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// ... (full license text)
#[wasm_bindgen] #[wasm_bindgen]
pub fn your_cool_function(input: &str) -> String { pub fn your_cool_function(input: &str) -> String {
// Your logic here
format!("Processed: {}", input) format!("Processed: {}", input)
} }
fn main() { fn main() {
let args: Vec<String> = env::args().collect();
let file_content = if args.len() > 1 {
let file_path = &args[1];
fs::read_to_string(file_path).unwrap_or_else(|_| {
eprintln!("Failed to read file: {}", file_path);
String::new()
})
} else {
String::new()
};
let client = Client::builder() let client = Client::builder()
.timeout(std::time::Duration::from_secs(600)) .timeout(std::time::Duration::from_secs(600))
.build() .build()
@ -49,6 +17,8 @@ fn main() {
println!("AI Chat! Type your message below (or type 'exit' to quit):"); println!("AI Chat! Type your message below (or type 'exit' to quit):");
let mut messages = Vec::new();
loop { loop {
let mut prompt = String::new(); let mut prompt = String::new();
io::stdin().read_line(&mut prompt).expect("Failed to read line"); io::stdin().read_line(&mut prompt).expect("Failed to read line");
@ -58,22 +28,16 @@ fn main() {
break; break;
} }
let message = if !file_content.is_empty() { messages.push(json!({
format!("{}\n{}", file_content, prompt) "role": "user",
} else { "content": prompt
prompt.to_string() }));
};
let response = client.post("https://ollama.molodetz.nl/api/chat") let response = client.post("https://ollama.molodetz.nl/api/chat")
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
.body(json!({ .body(json!({
"model": "qwen2.5:1.5b", "model": "qwen2.5:1.5b",
"messages": [ "messages": messages
{
"role": "user",
"content": message
}
]
}).to_string()) }).to_string())
.send(); .send();
@ -82,18 +46,16 @@ fn main() {
match resp.bytes() { match resp.bytes() {
Ok(data) => { Ok(data) => {
let raw_text = String::from_utf8_lossy(&data); let raw_text = String::from_utf8_lossy(&data);
//println!("Raw response: {}", raw_text);
let mut lines = raw_text.lines(); let mut lines = raw_text.lines();
let mut assistant_response = String::new();
while let Some(line) = lines.next() { while let Some(line) = lines.next() {
match serde_json::from_str::<serde_json::Value>(line) { match serde_json::from_str::<serde_json::Value>(line) {
Ok(json_response) => { Ok(json_response) => {
if let Some(message) = json_response.get("message") { if let Some(message) = json_response.get("message") {
if let Some(content) = message.get("content") { if let Some(content) = message.get("content") {
if let Some(s) = content.as_str() { if let Some(s) = content.as_str() {
assistant_response.push_str(s);
print!("{}", s);
} }
} }
} else { } else {
println!("No message found."); println!("No message found.");
@ -104,13 +66,17 @@ fn main() {
} }
} }
} }
messages.push(json!({
"role": "assistant",
"content": assistant_response
}));
println!("{}", assistant_response);
} }
Err(e) => { Err(e) => {
eprintln!("Error reading response: {:?}", e); eprintln!("Error reading response: {:?}", e);
} }
} }
println!("");
println!("");
} }
Err(e) => { Err(e) => {