From 2a865946f3ed83872edddff0d6609a70149605c0 Mon Sep 17 00:00:00 2001 From: Oleg Klimov Date: Sun, 12 Jan 2025 09:34:50 +0100 Subject: [PATCH 1/6] now sets only max_completion_tokens, not max_tokens --- src/forward_to_openai_endpoint.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/forward_to_openai_endpoint.rs b/src/forward_to_openai_endpoint.rs index 3cffbe90a..f2206c498 100644 --- a/src/forward_to_openai_endpoint.rs +++ b/src/forward_to_openai_endpoint.rs @@ -39,7 +39,6 @@ pub async fn forward_to_openai_style_endpoint( } if model_name != "o1-mini" { data["temperature"] = serde_json::Value::from(sampling_parameters.temperature); - data["max_tokens"] = serde_json::Value::from(sampling_parameters.max_new_tokens); data["max_completion_tokens"] = serde_json::Value::from(sampling_parameters.max_new_tokens); } else { data["max_completion_tokens"] = serde_json::Value::from(sampling_parameters.max_new_tokens); @@ -104,7 +103,6 @@ pub async fn forward_to_openai_style_endpoint_streaming( "model": model_name, "stream": true, "temperature": sampling_parameters.temperature, - "max_tokens": sampling_parameters.max_new_tokens, "max_completion_tokens": sampling_parameters.max_new_tokens, "stream_options": {"include_usage": true}, // "stop": sampling_parameters.stop, // openai does not like stop: [] From ad8a8a53805b40e44d1093c1ba66631302477381 Mon Sep 17 00:00:00 2001 From: V4LER11 Date: Mon, 23 Dec 2024 13:09:42 +0000 Subject: [PATCH 2/6] fixed potential critical place: if there are many images in one directory, any cat wants to expect whole directory, it could exceed token limit and lead to financial losses --- src/tools/tool_cat.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/tools/tool_cat.rs b/src/tools/tool_cat.rs index eab123933..6ae986fa0 100644 --- a/src/tools/tool_cat.rs +++ b/src/tools/tool_cat.rs @@ -21,6 +21,9 @@ use image::{ImageFormat, ImageReader}; pub struct ToolCat; +const CAT_MAX_IMAGES_CNT: usize = 1; + + #[async_trait] impl Tool for ToolCat { fn as_any(&self) -> &dyn std::any::Any { self } @@ -81,7 +84,7 @@ impl Tool for ToolCat { } } if !not_found_messages.is_empty() { - content.push_str(&format!("Path problems:\n\n{}\n\n", not_found_messages.join("\n\n"))); + content.push_str(&format!("Problems:\n\n{}\n\n", not_found_messages.join("\n\n"))); corrections = true; } @@ -260,6 +263,7 @@ pub async fn paths_and_symbols_to_cat( .filter_map(|x| if let ContextEnum::ContextFile(cf) = x { Some(cf.file_name.clone()) } else { None }) .collect::>(); + let mut image_counter = 0; for p in unique_paths.iter().filter(|x|!filenames_got_symbols_for.contains(x)) { // don't have symbols for these, so we need to mention them as files, without a symbol, analog of @file let f_type = get_file_type(&PathBuf::from(p)); @@ -267,6 +271,11 @@ pub async fn paths_and_symbols_to_cat( if f_type.starts_with("image/") { match load_image(p, &f_type).await { Ok(mm) => { + if image_counter == CAT_MAX_IMAGES_CNT { + not_found_messages.push(format!("{}: cat can show only 1 image per call", p)); + continue; + } + image_counter += 1; filenames_present.push(p.clone()); multimodal.push(mm); }, From 9740efa11507365fd0e09018f6f7e963431d497d Mon Sep 17 00:00:00 2001 From: V4LER11 Date: Tue, 24 Dec 2024 12:57:34 +0000 Subject: [PATCH 3/6] a single warn message about attaching multiple messages --- src/tools/tool_cat.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tools/tool_cat.rs b/src/tools/tool_cat.rs index 6ae986fa0..82af40155 100644 --- a/src/tools/tool_cat.rs +++ b/src/tools/tool_cat.rs @@ -272,10 +272,12 @@ pub async fn paths_and_symbols_to_cat( match load_image(p, &f_type).await { Ok(mm) => { if image_counter == CAT_MAX_IMAGES_CNT { - not_found_messages.push(format!("{}: cat can show only 1 image per call", p)); - continue; + not_found_messages.push("cat can show only 1 image per call. cat(image_path) in parallel if need to show all images".to_string()); } image_counter += 1; + if image_counter > CAT_MAX_IMAGES_CNT { + continue + } filenames_present.push(p.clone()); multimodal.push(mm); }, From a58b0a3eded3b3bddf00dd0f320857bf096571f0 Mon Sep 17 00:00:00 2001 From: Oleg Klimov Date: Sun, 12 Jan 2025 14:15:44 +0100 Subject: [PATCH 4/6] integr_shell improvement: explicit STDOUT STDERR --- src/integrations/integr_cmdline.rs | 4 ++-- src/integrations/integr_shell.rs | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/integrations/integr_cmdline.rs b/src/integrations/integr_cmdline.rs index ab04c625d..13dd9a9a4 100644 --- a/src/integrations/integr_cmdline.rs +++ b/src/integrations/integr_cmdline.rs @@ -113,7 +113,7 @@ pub fn format_output(stdout_out: &str, stderr_out: &str) -> String { let mut out = String::new(); if !stdout_out.is_empty() && stderr_out.is_empty() { // special case: just clean output, nice - out.push_str(&format!("{}\n", stdout_out)); + out.push_str(&format!("{}\n\n", stdout_out)); } else { if !stdout_out.is_empty() { out.push_str(&format!("STDOUT\n```\n{}```\n\n", stdout_out)); @@ -195,7 +195,7 @@ pub async fn execute_blocking_command( let mut out = format_output(&stdout, &stderr); let exit_code = output.status.code().unwrap_or_default(); - out.push_str(&format!("command was running {:.3}s, finished with exit code {exit_code}\n", duration.as_secs_f64())); + out.push_str(&format!("The command was running {:.3}s, finished with exit code {exit_code}\n", duration.as_secs_f64())); Ok(out) }; diff --git a/src/integrations/integr_shell.rs b/src/integrations/integr_shell.rs index 8fc8486a8..c0ac366c6 100644 --- a/src/integrations/integr_shell.rs +++ b/src/integrations/integr_shell.rs @@ -212,12 +212,17 @@ pub async fn execute_shell_command( } cmd.arg(shell_arg).arg(command); - cmd.stdout(Stdio::piped()).stderr(Stdio::piped()); + cmd.stdout(Stdio::piped()); + cmd.stderr(Stdio::piped()); + let t0 = tokio::time::Instant::now(); + tracing::info!("SHELL: running command directory {}\n{:?}", workdir, command); let output = tokio::time::timeout(tokio::time::Duration::from_secs(timeout), cmd.output()) .await .map_err(|_| format!("Command timed out after {} seconds", timeout))? .map_err(|e| format!("Failed to execute command: {}", e))?; + let duration = t0.elapsed(); + tracing::info!("SHELL: /finished in {:.3}s", duration.as_secs_f64()); let stdout = String::from_utf8_lossy(&output.stdout).to_string(); let stderr = String::from_utf8_lossy(&output.stderr).to_string(); @@ -225,11 +230,10 @@ pub async fn execute_shell_command( let filtered_stdout = crate::postprocessing::pp_command_output::output_mini_postprocessing(output_filter, &stdout); let filtered_stderr = crate::postprocessing::pp_command_output::output_mini_postprocessing(output_filter, &stderr); - if !filtered_stderr.is_empty() { - return Err(filtered_stderr); - } - - Ok(filtered_stdout) + let mut out = crate::integrations::integr_cmdline::format_output(&filtered_stdout, &filtered_stderr); + let exit_code = output.status.code().unwrap_or_default(); + out.push_str(&format!("The command was running {:.3}s, finished with exit code {exit_code}\n", duration.as_secs_f64())); + Ok(out) } fn parse_args(args: &HashMap) -> Result<(String, Option), String> { From 2678ef3d970956993269ec6b2daac10ccbc0ae78 Mon Sep 17 00:00:00 2001 From: Oleg Klimov Date: Sun, 12 Jan 2025 14:43:17 +0100 Subject: [PATCH 5/6] oops --- src/integrations/integr_shell.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/integrations/integr_shell.rs b/src/integrations/integr_shell.rs index c0ac366c6..ec818dd04 100644 --- a/src/integrations/integr_shell.rs +++ b/src/integrations/integr_shell.rs @@ -216,7 +216,7 @@ pub async fn execute_shell_command( cmd.stderr(Stdio::piped()); let t0 = tokio::time::Instant::now(); - tracing::info!("SHELL: running command directory {}\n{:?}", workdir, command); + tracing::info!("SHELL: running command directory {:?}\n{:?}", workdir_maybe, command); let output = tokio::time::timeout(tokio::time::Duration::from_secs(timeout), cmd.output()) .await .map_err(|_| format!("Command timed out after {} seconds", timeout))? From 0576b1e4de569d4afef092b0cecf06a9d4e6601f Mon Sep 17 00:00:00 2001 From: Oleg Klimov Date: Sun, 12 Jan 2025 14:44:16 +0100 Subject: [PATCH 6/6] fix cat() for images --- src/tools/tool_cat.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/tools/tool_cat.rs b/src/tools/tool_cat.rs index 82af40155..36ddd7237 100644 --- a/src/tools/tool_cat.rs +++ b/src/tools/tool_cat.rs @@ -27,7 +27,7 @@ const CAT_MAX_IMAGES_CNT: usize = 1; #[async_trait] impl Tool for ToolCat { fn as_any(&self) -> &dyn std::any::Any { self } - + async fn tool_execute( &mut self, ccx: Arc>, @@ -84,7 +84,7 @@ impl Tool for ToolCat { } } if !not_found_messages.is_empty() { - content.push_str(&format!("Problems:\n\n{}\n\n", not_found_messages.join("\n\n"))); + content.push_str(&format!("Problems:\n{}\n\n", not_found_messages.join("\n\n"))); corrections = true; } @@ -269,16 +269,16 @@ pub async fn paths_and_symbols_to_cat( let f_type = get_file_type(&PathBuf::from(p)); if f_type.starts_with("image/") { + filenames_present.push(p.clone()); + if image_counter == CAT_MAX_IMAGES_CNT { + not_found_messages.push("Cat() shows only 1 image per call to avoid token overflow, call several cat() in parallel to see more images.".to_string()); + } + image_counter += 1; + if image_counter > CAT_MAX_IMAGES_CNT { + continue + } match load_image(p, &f_type).await { Ok(mm) => { - if image_counter == CAT_MAX_IMAGES_CNT { - not_found_messages.push("cat can show only 1 image per call. cat(image_path) in parallel if need to show all images".to_string()); - } - image_counter += 1; - if image_counter > CAT_MAX_IMAGES_CNT { - continue - } - filenames_present.push(p.clone()); multimodal.push(mm); }, Err(e) => { not_found_messages.push(format!("{}: {}", p, e)); }