From 8d89a06d6c1abb0eede64589284e2dee39362d25 Mon Sep 17 00:00:00 2001 From: Eddie Date: Sun, 22 Dec 2024 15:58:27 -0300 Subject: [PATCH] added files to prompt --- .../generic_chain/generic_inference_chain.rs | 12 +++++++- .../chains/generic_chain/generic_prompts.rs | 11 +++++++ .../sheet_ui_inference_chain.rs | 7 +++++ .../v2_api/api_v2_commands_app_files.rs | 29 +++++++++++++++---- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/generic_chain/generic_inference_chain.rs b/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/generic_chain/generic_inference_chain.rs index c86c1ab6d..26669958f 100644 --- a/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/generic_chain/generic_inference_chain.rs +++ b/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/generic_chain/generic_inference_chain.rs @@ -11,6 +11,7 @@ use crate::managers::sheet_manager::SheetManager; use crate::managers::tool_router::{ToolCallFunctionResponse, ToolRouter}; use crate::network::agent_payments_manager::external_agent_offerings_manager::ExtAgentOfferingsManager; use crate::network::agent_payments_manager::my_agent_offerings_manager::MyAgentOfferingsManager; +use crate::utils::environment::{fetch_node_environment, NodeEnvironment}; use async_trait::async_trait; use shinkai_message_primitives::schemas::inbox_name::InboxName; use shinkai_message_primitives::schemas::job::{Job, JobLike}; @@ -83,6 +84,7 @@ impl InferenceChain for GenericInferenceChain { self.context.ext_agent_payments_manager.clone(), // self.context.sqlite_logger.clone(), self.context.llm_stopper.clone(), + fetch_node_environment(), ) .await?; Ok(response) @@ -122,6 +124,7 @@ impl GenericInferenceChain { ext_agent_payments_manager: Option>>, // sqlite_logger: Option>, llm_stopper: Arc, + node_env: NodeEnvironment, ) -> Result { shinkai_log( ShinkaiLogOption::JobExecution, @@ -327,6 +330,8 @@ impl GenericInferenceChain { Some(full_job.step_history.clone()), tools.clone(), None, + full_job.job_id.clone(), + node_env.clone(), ); let mut iteration_count = 0; @@ -394,7 +399,10 @@ impl GenericInferenceChain { // 6) Call workflow or tooling // Find the ShinkaiTool that has a tool with the function name - let shinkai_tool = tools.iter().find(|tool| tool.name() == function_call.name || tool.tool_router_key() == function_call.tool_router_key.clone().unwrap_or_default()); + let shinkai_tool = tools.iter().find(|tool| { + tool.name() == function_call.name + || tool.tool_router_key() == function_call.tool_router_key.clone().unwrap_or_default() + }); if shinkai_tool.is_none() { eprintln!("Function not found: {}", function_call.name); return Err(LLMProviderError::FunctionNotFound(function_call.name.clone())); @@ -443,6 +451,8 @@ impl GenericInferenceChain { Some(full_job.step_history.clone()), tools.clone(), Some(function_response), + full_job.job_id.clone(), + node_env.clone(), ); } else { // No more function calls required, return the final response diff --git a/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/generic_chain/generic_prompts.rs b/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/generic_chain/generic_prompts.rs index 72c68df67..cd911773a 100644 --- a/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/generic_chain/generic_prompts.rs +++ b/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/generic_chain/generic_prompts.rs @@ -2,6 +2,9 @@ use std::collections::HashMap; use crate::llm_provider::execution::prompts::general_prompts::JobPromptGenerator; use crate::managers::tool_router::ToolCallFunctionResponse; +use crate::network::v2_api::api_v2_commands_app_files::get_app_folder_path; +use crate::network::Node; +use crate::utils::environment::NodeEnvironment; use serde_json::json; use shinkai_message_primitives::schemas::job::JobStepResult; use shinkai_message_primitives::schemas::prompts::Prompt; @@ -23,6 +26,8 @@ impl JobPromptGenerator { job_step_history: Option>, tools: Vec, function_call: Option, + job_id: String, + node_env: NodeEnvironment, ) -> Prompt { let mut prompt = Prompt::new(); @@ -52,6 +57,12 @@ impl JobPromptGenerator { priority = priority.saturating_sub(1); } } + + let current_files = Node::v2_api_list_app_files_internal(get_app_folder_path(node_env, job_id)); + if let Ok(current_files) = current_files { + let content = format!("Current files: {:?}", current_files); + prompt.add_content(content, SubPromptType::ExtraContext, 97); + } } // Parses the retrieved nodes as individual sub-prompts, to support priority pruning diff --git a/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/sheet_ui_chain/sheet_ui_inference_chain.rs b/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/sheet_ui_chain/sheet_ui_inference_chain.rs index 847d90ab0..9cb967969 100644 --- a/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/sheet_ui_chain/sheet_ui_inference_chain.rs +++ b/shinkai-bin/shinkai-node/src/llm_provider/execution/chains/sheet_ui_chain/sheet_ui_inference_chain.rs @@ -12,6 +12,7 @@ use crate::managers::sheet_manager::SheetManager; use crate::managers::tool_router::{ToolCallFunctionResponse, ToolRouter}; use crate::network::agent_payments_manager::external_agent_offerings_manager::ExtAgentOfferingsManager; use crate::network::agent_payments_manager::my_agent_offerings_manager::MyAgentOfferingsManager; +use crate::utils::environment::{fetch_node_environment, NodeEnvironment}; use async_trait::async_trait; use shinkai_message_primitives::schemas::inbox_name::InboxName; use shinkai_message_primitives::schemas::job::{Job, JobLike}; @@ -79,6 +80,7 @@ impl InferenceChain for SheetUIInferenceChain { self.context.ext_agent_payments_manager.clone(), // self.context.sqlite_logger.clone(), self.context.llm_stopper.clone(), + fetch_node_environment(), ) .await?; let job_execution_context = self.context.execution_context.clone(); @@ -123,6 +125,7 @@ impl SheetUIInferenceChain { ext_agent_payments_manager: Option>>, // sqlite_logger: Option>, llm_stopper: Arc, + node_env: NodeEnvironment, ) -> Result { shinkai_log( ShinkaiLogOption::JobExecution, @@ -283,6 +286,8 @@ impl SheetUIInferenceChain { Some(full_job.step_history.clone()), tools.clone(), None, + full_job.job_id.clone(), + node_env.clone(), ); let mut iteration_count = 0; @@ -419,6 +424,8 @@ impl SheetUIInferenceChain { Some(full_job.step_history.clone()), tools.clone(), Some(function_response), + full_job.job_id.clone(), + node_env.clone(), ); } else { // No more function calls required, return the final response diff --git a/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_app_files.rs b/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_app_files.rs index c1020f522..9de40931c 100644 --- a/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_app_files.rs +++ b/shinkai-bin/shinkai-node/src/network/v2_api/api_v2_commands_app_files.rs @@ -12,7 +12,7 @@ use std::sync::Arc; use shinkai_http_api::node_api_router::APIError; -fn get_app_folder_path(node_env: NodeEnvironment, app_id: String) -> PathBuf { +pub fn get_app_folder_path(node_env: NodeEnvironment, app_id: String) -> PathBuf { let mut origin_path: PathBuf = PathBuf::from(node_env.node_storage_path.clone().unwrap_or_default()); origin_path.push("app_files"); origin_path.push(app_id); @@ -143,6 +143,24 @@ impl Node { return Ok(()); } let app_folder_path = get_app_folder_path(node_env, app_id); + let result = Self::v2_api_list_app_files_internal(app_folder_path); + match result { + Ok(file_list) => { + let _ = res + .send(Ok(Value::Array( + file_list.iter().map(|file| Value::String(file.clone())).collect(), + ))) + .await; + Ok(()) + } + Err(err) => { + let _ = res.send(Err(err)).await; + Ok(()) + } + } + } + + pub fn v2_api_list_app_files_internal(app_folder_path: PathBuf) -> Result, APIError> { let files = std::fs::read_dir(&app_folder_path); if let Err(err) = files { let api_error = APIError { @@ -150,19 +168,18 @@ impl Node { error: "Internal Server Error".to_string(), message: format!("Failed to read directory: {}", err), }; - let _ = res.send(Err(api_error)).await; - return Ok(()); + return Err(api_error); } let mut file_list = Vec::new(); + let files = files.unwrap(); for file in files { if let Ok(file) = file { let file_name = file.file_name().to_string_lossy().to_string(); - file_list.push(Value::String(file_name)); + file_list.push(file_name); } } - let _ = res.send(Ok(Value::Array(file_list))).await; - Ok(()) + Ok(file_list) } pub async fn v2_api_delete_app_file(