Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: llm based automatic function calling #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,291 changes: 1,276 additions & 15 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,22 @@ clap_complete = "4.5.7"
config = "0.14.0"
dirs = "5.0.1"
inquire = { version = "0.7.5", features = ["date"] }
ollama-rs = { version = "0.2.0", features = [
"chat-history",
"function-calling",
"tokio",
"tokio-stream",
] }
serde = "1.0.203"
serde_json = "1"
tokio = { version = "1.38.0", features = ["full"] }
tokio-stream = { version = "0.1.15" }
async-trait = { version = "0.1.73" }
url = "2.5.2"

[features]
default = []
chat-history = []

[profile.dev]
debug = "line-tables-only"
Expand Down
6 changes: 6 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
debug = 0
chat_model = "elvee/hermes-2-pro-llama-3:8b-Q5_K_M"

[database]
name = "mindmap"
host = "localhost"
password = "password"
port = 5432
username = "postgres"

[ollama]
ollama_scheme = "http"
ollama_host = "localhost"
ollama_port = 11434
61 changes: 47 additions & 14 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils, flakebox }:
flake-utils.lib.eachDefaultSystem (system:
outputs =
{
self,
nixpkgs,
flake-utils,
flakebox,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
projectName = "mindmap";

pkgs = nixpkgs.legacyPackages.${system};
openssl = pkgs.openssl;
libldap = pkgs.openldap;

flakeboxLib = flakebox.lib.${system} {
config = {
github.ci.buildOutputs = [ ".#ci.${projectName}" ];
Expand All @@ -33,26 +44,48 @@
paths = buildPaths;
};

multiBuild =
(flakeboxLib.craneMultiBuild { }) (craneLib':
let
craneLib = (craneLib'.overrideArgs {
multiBuild = (flakeboxLib.craneMultiBuild { }) (
craneLib':
let
craneLib = (
craneLib'.overrideArgs {
pname = projectName;
src = buildSrc;
nativeBuildInputs = [ ];
});
in
{
mindmap = craneLib.buildPackage { meta.mainProgram = "mindmap"; };
todo = craneLib.buildPackage { meta.mainProgram = "todo"; };
});
nativeBuildInputs = [
pkgs.pkg-config
openssl
libldap
];
buildInputs = [
openssl
libldap
];
cargoBuildOptions = [ "--features=vendored" ];
}
);
in
{
mindmap = craneLib.buildPackage { meta.mainProgram = "mindmap"; };
todo = craneLib.buildPackage { meta.mainProgram = "todo"; };
}
);
in
{
packages.default = multiBuild.${projectName};

legacyPackages = multiBuild;

devShells = flakeboxLib.mkShells { };
devShells = flakeboxLib.mkShells {
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [
openssl
libldap
];
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
openssl
libldap
];
};
}
);
}
37 changes: 37 additions & 0 deletions src/bin/todo/auto.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::sync::Arc;

use clap::Parser;
use inquire::prompt_text;
use mindmap::model::get_ai;
use ollama_rs::generation::chat::ChatMessage;
use ollama_rs::generation::functions::{FunctionCallRequest, NousFunctionCall};

use crate::{create, list};

#[derive(Parser)]
pub struct Args {}

pub async fn command(_args: &Args) {
let (mut ollama, model) = get_ai().await.unwrap();
let create = Arc::new(create::Args::new());
let list = Arc::new(list::Args::new());
let parser = Arc::new(NousFunctionCall::new());

let response = ollama
.send_function_call_with_history(
FunctionCallRequest::new(
model.clone(),
vec![create, list],
vec![ChatMessage::user(
prompt_text("Prompt:").expect("An error occurred!"),
)],
),
parser,
"default".to_string(),
)
.await
.unwrap();

let assistant_message = response.message.unwrap().content;
println!("{}", assistant_message);
}
141 changes: 118 additions & 23 deletions src/bin/todo/create.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,126 @@
use chrono::{Local, Weekday};
use std::error::Error;

use async_trait::async_trait;
use chrono::{Local, NaiveDate, Weekday};
use clap::Parser;
use inquire::{DateSelect, Select};
use mindmap::{Difficulty, Priority, Task};
use ollama_rs::generation::functions::tools::Tool;
use serde_json::{json, Value};

#[derive(Parser)]
pub struct Args {}

pub fn command(_args: &Args) {
let task = Task {
description: inquire::prompt_text("Description").expect("An error occurred!"),
difficulty: Select::new(
"Difficulty",
vec![Difficulty::Low, Difficulty::Medium, Difficulty::High],
)
.prompt_skippable()
.expect("An error occurred!"),
priority: Select::new(
"Priority",
vec![Priority::Low, Priority::Medium, Priority::High],
)
.prompt_skippable()
.expect("An error occurred!"),
deadline: DateSelect::new("Deadline")
.with_min_date(Local::now().date_naive())
.with_week_start(Weekday::Mon)
.prompt_skippable()
.expect("An error occurred!"),
};
println!("Task \"{}\" created successfully!", task.description);
pub fn command(args: &Args) {
println!("{}", args.create_task(None, None, None, None));
}
impl Args {
pub fn new() -> Self {
Args {}
}
pub fn create_task(
&self,
description_input: Option<&str>,
difficulty_input: Option<Difficulty>,
priority_input: Option<Priority>,
deadline_input: Option<NaiveDate>,
) -> String {
let today = Local::now().date_naive();
let task = Task {
description: match description_input {
Some(description) => description.to_string(),
None => inquire::prompt_text("Description").expect("An error occurred!"),
},
difficulty: difficulty_input.or_else(|| {
Select::new(
"Difficulty",
vec![Difficulty::Low, Difficulty::Medium, Difficulty::High],
)
.prompt_skippable()
.expect("An error occurred!")
}),
priority: priority_input.or_else(|| {
Select::new(
"Priority",
vec![Priority::Low, Priority::Medium, Priority::High],
)
.prompt_skippable()
.expect("An error occurred!")
}),
deadline: match deadline_input {
Some(date) if date >= today => Some(date),
Some(_) | None => DateSelect::new("Deadline")
.with_min_date(today)
.with_week_start(Weekday::Mon)
.prompt_skippable()
.expect("An error occurred!"),
},
};
format!("Task \"{}\" created successfully!", task)
}
}

impl Default for Args {
fn default() -> Self {
Args::new()
}
}

#[async_trait]
impl Tool for Args {
fn name(&self) -> String {
"Create Task".to_string()
}

fn description(&self) -> String {
"Create a new task for the current user.".to_string()
}

fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"description": {
"type": "string",
"description": "The description of the task."
},
"difficulty": {
"type": "string",
"enum": ["low", "medium", "high"],
"description": "The difficulty of the task."
},
"priority": {
"type": "string",
"enum": ["low", "medium", "high"],
"description": "The priority of the task."
},
"deadline": {
"type": "string",
"format": "date",
"description": format!("Today is {},deadline of the task should be equal or greater than today.",Local::now().date_naive())
}
},
"required": []
})
}

async fn run(&self, input: Value) -> Result<String, Box<dyn Error>> {
let description_input = input["description"].as_str();
let difficulty_input = input["difficulty"]
.as_str()
.and_then(|s| s.parse::<Difficulty>().ok());
let priority_input = input["priority"]
.as_str()
.and_then(|s| s.parse::<Priority>().ok());
let deadline_input = input["deadline"]
.as_str()
.and_then(|d| NaiveDate::parse_from_str(d, "%Y-%m-%d").ok());

Ok(self.create_task(
description_input,
difficulty_input,
priority_input,
deadline_input,
))
}
}
38 changes: 36 additions & 2 deletions src/bin/todo/list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
use std::error::Error;

use async_trait::async_trait;
use clap::Parser;
use ollama_rs::generation::functions::tools::Tool;
use serde_json::Value;

#[derive(Parser)]
pub struct Args {}

pub fn command(_args: &Args) {
println!("Tasks due today:");
pub fn command(args: &Args) {
println!("{}", args.list());
}

impl Args {
pub fn new() -> Self {
Args {}
}
pub fn list(&self) -> String {
String::from("Tasks due today:")
}
}
impl Default for Args {
fn default() -> Self {
Self::new()
}
}

#[async_trait]
impl Tool for Args {
fn name(&self) -> String {
"List Tasks".to_string()
}

fn description(&self) -> String {
"Displays all the tasks pending for the current user.".to_string()
}

async fn run(&self, _input: Value) -> Result<String, Box<dyn Error>> {
Ok(self.list())
}
}
7 changes: 6 additions & 1 deletion src/bin/todo/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::{Parser, Subcommand};

mod auto;
mod completion;
mod create;
mod delete;
Expand Down Expand Up @@ -28,9 +29,12 @@ enum Commands {
Delete(delete::Args),
/// Generate shell completion scripts
Completion(completion::Args),
/// Automatically run the desired command
Auto(auto::Args),
}

fn main() {
#[tokio::main]
async fn main() {
let cli = Args::parse();

match &cli.command {
Expand All @@ -40,5 +44,6 @@ fn main() {
Commands::Update(args) => update::command(args),
Commands::Delete(args) => delete::command(args),
Commands::Completion(args) => completion::command(args),
Commands::Auto(args) => auto::command(args).await,
}
}
Loading
Loading