Skip to content

Commit

Permalink
feat: add ollama backend
Browse files Browse the repository at this point in the history
  • Loading branch information
arcuru committed Mar 19, 2024
1 parent 8f37588 commit c2c66e9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ anyhow = "1"
tokio = { version = "1.24.2", features = ["macros", "rt-multi-thread"] }
tracing-subscriber = "0.3.15"
matrix-sdk = "0.7.1"
ollama-rs = "0.1.0"
51 changes: 43 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use matrix_sdk::{
},
Client, Room, RoomState,
};
use ollama_rs::{generation::completion::request::GenerationRequest, Ollama};
use tokio::time::{sleep, Duration};

/// This is the starting point of the app. `main` is called by rust binaries to
Expand Down Expand Up @@ -162,16 +163,50 @@ async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
return;
};

// here comes the actual "logic": when the bot see's a `!party` in the message,
// it responds
if text_content.body.contains("!party") {
let content = RoomMessageEventContent::text_plain("🎉🎊🥳 let's PARTY!! 🥳🎊🎉");
// If we start with a single '!', interpret as a command
let text = text_content.body.trim_start();
if text.starts_with('!') && !text.starts_with("!!") {
// Interpret as a command
let command = text.split_whitespace().next();
if let Some(command) = command {
// Write a match statement to match the first word in the body
match &command[1..] {
"party" => {
let content =
RoomMessageEventContent::text_plain("🎉🎊🥳 let's PARTY!! 🥳🎊🎉");
// send our message to the room we found the "!party" command in
room.send(content).await.unwrap();
}
"ollama" => {
// Remove the command from the text
let input = text_content.body.trim_start_matches("!ollama").trim();

if let Ok(result) = send_to_ollama_server(input.to_string()).await {
let content = RoomMessageEventContent::text_plain(result);

room.send(content).await.unwrap();
}
}
_ => {
println!("Unknown command");
}
}
}
}
}

// Send the current conversation to the configured ollama server
async fn send_to_ollama_server(input: String) -> Result<String, ()> {
let ollama = Ollama::default();

println!("sending");
let model = "llama2:latest".to_string();
let prompt = input;

// send our message to the room we found the "!party" command in
room.send(content).await.unwrap();
let res = ollama.generate(GenerationRequest::new(model, prompt)).await;

println!("message sent");
if let Ok(res) = res {
// Strip leading and trailing whitespace from res.response
return Ok(res.response.trim().to_string());
}
Err(())
}

0 comments on commit c2c66e9

Please sign in to comment.