Skip to content

Commit

Permalink
new: centralized events and logging logic
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Jun 28, 2024
1 parent 74476b3 commit 5761b01
Show file tree
Hide file tree
Showing 20 changed files with 516 additions and 254 deletions.
41 changes: 36 additions & 5 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ memory-stats = "1.1.0"
sha256 = "1.5.0"
bitcode = { version = "0.6.0", features = ["serde"] }
intertrait = "0.2.2"
mini-rag = "0.2.1"
mini-rag = "0.2.2"
env_logger = "0.11.3"
log = "0.4.22"

[features]
default = ["ollama", "groq", "openai", "fireworks"]
Expand Down
6 changes: 6 additions & 0 deletions src/agent/events/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub(crate) type Sender = tokio::sync::mpsc::UnboundedSender<super::Event>;
pub(crate) type Receiver = tokio::sync::mpsc::UnboundedReceiver<super::Event>;

pub(crate) fn create_channel() -> (Sender, Receiver) {
tokio::sync::mpsc::unbounded_channel()
}
44 changes: 44 additions & 0 deletions src/agent/events/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use serde::{Deserialize, Serialize};

mod channel;

pub(crate) use channel::*;

use super::{
generator::Options,
state::{metrics::Metrics, storage::StorageType},
Invocation,
};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) enum Event {
MetricsUpdate(Metrics),
StorageUpdate {
storage_name: String,
storage_type: StorageType,
key: String,
prev: Option<String>,
new: Option<String>,
},
StateUpdate(Options),
EmptyResponse,
InvalidResponse(String),
InvalidAction {
invocation: Invocation,
error: Option<String>,
},
ActionTimeout {
invocation: Invocation,
elapsed: std::time::Duration,
},
ActionExecuted {
invocation: Invocation,
error: Option<String>,
result: Option<String>,
elapsed: std::time::Duration,
},
TaskComplete {
impossible: bool,
reason: Option<String>,
},
}
20 changes: 9 additions & 11 deletions src/agent/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::{fmt::Display, time::Duration};

use anyhow::Result;
use async_trait::async_trait;
use colored::Colorize;
use duration_string::DurationString;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};

use super::Invocation;

Expand All @@ -24,7 +24,7 @@ lazy_static! {
static ref CONN_RESET_PARSER: Regex = Regex::new(r"(?m)^.+onnection reset by peer.*").unwrap();
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Options {
pub system_prompt: String,
pub prompt: String,
Expand All @@ -41,7 +41,7 @@ impl Options {
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Message {
Agent(String, Option<Invocation>),
Feedback(String, Option<Invocation>),
Expand Down Expand Up @@ -86,9 +86,8 @@ pub trait Client: mini_rag::Embedder + Send + Sync {
}

if let Ok(retry_time) = retry_time_str.parse::<DurationString>() {
println!(
"{}: rate limit reached for this model, retrying in {} ...\n",
"WARNING".bold().yellow(),
log::warn!(
"rate limit reached for this model, retrying in {} ...",
retry_time,
);

Expand All @@ -99,16 +98,15 @@ pub trait Client: mini_rag::Embedder + Send + Sync {

return true;
} else {
eprintln!("can't parse '{}'", &retry_time_str);
log::error!("can't parse '{}'", &retry_time_str);
}
} else {
eprintln!("cap len wrong");
log::error!("cap len wrong");
}
} else if CONN_RESET_PARSER.captures_iter(error).next().is_some() {
let retry_time = Duration::from_secs(5);
println!(
"{}: connection reset by peer, retrying in {:?} ...\n",
"WARNING".bold().yellow(),
log::warn!(
"connection reset by peer, retrying in {:?} ...",
&retry_time,
);

Expand Down
2 changes: 1 addition & 1 deletion src/agent/generator/ollama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Client for OllamaClient {
if let Some(msg) = res.message {
Ok(msg.content)
} else {
println!("WARNING: model returned an empty message.");
log::warn!("model returned an empty message.");
Ok("".to_string())
}
}
Expand Down
Loading

0 comments on commit 5761b01

Please sign in to comment.