Skip to content

Commit

Permalink
Further cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robkorn committed Oct 11, 2023
1 parent 637df20 commit c107d04
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/agent/execution/chains/qa_inference_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl AgentManager {
// Inference the agent's LLM with the prompt. If it has an answer, the chain
// is finished and so just return the answer response as a cleaned String
let response_json = self.inference_agent(agent.clone(), filled_prompt).await?;
if let Ok(answer_str) = self.extract_inference_json_response(response_json, "answer") {
if let Ok(answer_str) = self.extract_inference_json_response(response_json.clone(), "answer") {
let cleaned_answer = Self::ending_stripper(&answer_str);
println!("QA Chain Final Answer: {:?}", cleaned_answer);
return Ok(cleaned_answer);
Expand Down
42 changes: 16 additions & 26 deletions src/agent/execution/job_execution_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,9 @@ impl AgentManager {
// TODO: later we should able to grab errors and return them to the user
let new_scope_entries = match agent_found.clone() {
Some(agent) => {
let resp = AgentManager::process_files_inbox(
self.db.clone(),
agent,
job_message.files_inbox.clone(),
profile,
)
.await?;
let resp = self
.process_files_inbox(self.db.clone(), agent, job_message.files_inbox.clone(), profile)
.await?;
resp
}
None => {
Expand Down Expand Up @@ -201,6 +197,7 @@ impl AgentManager {
/// If save_to_db_directly == true, the files will save to the DB and be returned as `DBScopeEntry`s.
/// Else, the files will be returned as `LocalScopeEntry`s and thus held inside.
pub async fn process_files_inbox(
&self,
db: Arc<Mutex<ShinkaiDB>>,
agent: Arc<Mutex<Agent>>,
files_inbox: String,
Expand All @@ -209,55 +206,48 @@ impl AgentManager {
let _bert_process = BertCPPProcess::start(); // Gets killed if out of scope
let mut shinkai_db = db.lock().await;
let files_result = shinkai_db.get_all_files_from_inbox(files_inbox.clone());

// Check if there was an error getting the files
let files = match files_result {
Ok(files) => files,
Err(e) => return Err(AgentError::ShinkaiDB(e)),
};

let mut files_map: HashMap<String, LocalScopeEntry> = HashMap::new();

// Create the RemoteEmbeddingGenerator instance
let generator = Arc::new(RemoteEmbeddingGenerator::new_default());
let mut files_map: HashMap<String, LocalScopeEntry> = HashMap::new();

// Start processing the files
for (filename, content) in files.into_iter() {
eprintln!("Iterating over file: {}", filename);
if filename.ends_with(".pdf") {
eprintln!("Processing PDF file: {}", filename);
// Prepare description
let pdf_overview = FileParser::parse_pdf_for_keywords_and_description(&content, 3, 200)?;

let agent_clone = agent.clone();
let grouped_text_list_clone = pdf_overview.grouped_text_list.clone();
let description_response = tokio::spawn(async move {
let mut agent = agent_clone.lock().await;
let prompt = JobPromptGenerator::simple_doc_description(grouped_text_list_clone);
agent.inference(prompt).await
})
.await?;

// TODO: Maybe add: "\nKeywords: keywords_generated_by_RAKE"?
eprintln!("description_response: {:?}", description_response);
let prompt = JobPromptGenerator::simple_doc_description(grouped_text_list_clone);
let description_response = self
.inference_agent_and_extract(agent.clone(), prompt, "answer")
.await?;

let vrsource = Self::create_vrsource(
&filename,
SourceFileType::Document(SourceDocumentType::Pdf),
Some(pdf_overview.blake3_hash),
);
eprintln!("vrsource: {:?}", vrsource);
let doc = FileParser::parse_pdf(
&content,
150,
&*generator,
&filename,
Some(&"".to_string()),
Some(&description_response),
vrsource,
&vec![],
)?;

// TODO: Maybe add: "\nKeywords: keywords_generated_by_RAKE"?
eprintln!("description_response: {:?}", description_response);
eprintln!("vrsource: {:?}", vrsource);

let resource = BaseVectorResource::from(doc.clone());
// eprintln!("resource: {:?}", resource);
eprintln!("profile: {:?}", profile);
shinkai_db.init_profile_resource_router(&profile)?;
shinkai_db.save_resource(resource, &profile).unwrap();

Expand Down
4 changes: 3 additions & 1 deletion src/agent/execution/job_prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,13 @@ impl JobPromptGenerator {
r#"You are an autoregressive language model that has been fine-tuned with instruction-tuning and RLHF. You carefully provide accurate, factual, thoughtful, nuanced answers, and are brilliant at reasoning. If you think there might not be a correct answer, you say so. Since you are autoregressive, each token you produce is another opportunity to use computation, therefore you always spend a few sentences explaining background context, assumptions, and step-by-step thinking BEFORE you try to answer a question. Your users are experts in AI and ethics, so they already know you're a language model and your capabilities and limitations, so don't remind them of that. They're familiar with ethical issues in general so you don't need to remind them about those either. Don't be verbose in your answers, but do provide details and examples where it might help the explanation."#.to_string(),
SubPromptType::System,
);

prompt.add_content(format!("Here are segements from a document:"), SubPromptType::User);
for chunk in chunks {
prompt.add_content(format!("{}", chunk), SubPromptType::User);
}
prompt.add_content(
format!("Take a deep breath and then throughtouly summarize the previous document, make it as explanatory as possible."),
format!("Take a deep breath and thoroughly summarize the previous segments, making it as explanatory as possible."),
SubPromptType::User,
);
prompt.add_ebnf(String::from(r#""{" "answer" ":" string "}""#), SubPromptType::System);
Expand Down

0 comments on commit c107d04

Please sign in to comment.