Skip to content

Commit

Permalink
Further Refinement of Smart Agents
Browse files Browse the repository at this point in the history
  • Loading branch information
Cormanz committed May 16, 2023
1 parent 5e355bb commit 4f2117b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
49 changes: 34 additions & 15 deletions src/auto/agents/employee/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ use crate::{Message, AgentInfo, CommandContext, auto::{try_parse_json, agents::e

use super::explain_results;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Step {
pub step: String,
pub tools: Vec<String>
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PlanInfo {
#[serde(rename = "broad overarching stages")]
pub stages: Vec<String>
pub steps: Vec<Step>
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -19,8 +24,10 @@ pub struct FirstInstructInfo {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ThoughtInstructInfo {
pub reflection: String,
pub stage: String,
pub criticism: String,
#[serde(rename = "what I can learn from my criticism")]
pub learned: String,
pub step: String,
#[serde(rename = "explanation of why or why not my task is complete")]
pub explanation: String,
pub instruction: Option<String>
Expand All @@ -43,22 +50,31 @@ The Agent is not smart, so you must talk very simply to it.
You have access to an Agent. Your agent can use external resources to help complete its task.
You will work with your Agent, and your Agent will use its resources to complete each task you give it.
Task:
{task}"
Tools:
google_search {{ "query": "..." }}
wolfram {{ "query": "..." }}
browse_url {{ "url": "..." }}
Only use browse_url on websites that you have found from other searches.
file_append {{ "path": "...", "content": "..." }}
List broad, overarching stages to accomplish your task.
Stages should not be specific or complex.
Task:
{task}
There should be three or four stages.
Break this task down into a list of steps. In each step, list the tool(s) you will use.
Respond in this exact JSON format:
{{
"broad overarching stages": [ "Stage" ]
"steps": [
{{
"step": "step",
"tools": [ "tools" ]
}}
]
}}
"#)));

let plan_info = try_parse_json::<PlanInfo>(&agent.llm, 2, Some(300))?;
let plan_info = try_parse_json::<PlanInfo>(&agent.llm, 2, Some(600))?;
log_yaml(&plan_info.data)?;

agent.llm.message_history.push(Message::Assistant(plan_info.raw));
Expand Down Expand Up @@ -93,12 +109,14 @@ Respond in this exact JSON format:

let agent = &mut context.agents.planner;

println!("Back to the planner!");

agent.llm.message_history.push(Message::User(format!(r#"
Your results from your Agent are:
{results}
Now, self-reflect on what results you got, how it helps you with your task, and what to do next.
Then, deduce what stage of your four-staged plan you're on.
Now, criticize your current workflow and plan. Then, learn from it.
Then, deduce what step you're on.
Then, decide if you are done.
If you are not done, give another instruction.
Expand All @@ -111,8 +129,9 @@ Respond in this exact JSON format:
```json
{{
{{
"reflection": "Constructive self-reflection",
"stage": "Precise stage name",
"criticism": "Constructive self-criticism",
"what I can learn from my criticism": "What my criticism taught me",
"step": "Precise step name (Tools to use)",
"explanation of why or why not my task is complete": "Explanation",
"instruction": "Instruction"
}}
Expand Down
48 changes: 35 additions & 13 deletions src/auto/agents/employee/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ browse_url {{ "url": "..." }}
Only use browse_url on websites that you have found from other searches.
file_append {{ "path": "...", "content": "..." }}
One Special Tool:
done {{}}
Use this once your task is complete.
Task:
{task}
Your goal is to complete your task by running actions.
Adhere to those exact instructions as closely as possible.
Use the exact tools mentioned.
Finish as soon as possible, do not deviate from your task.
You will decide whether or not you have completed your task through a detailed analysis of at least one sentence.
If you have not completed it, create an overarching, broad plan to finish it.
If you have not, begin having "thoughts".
Your "thoughts", "reasoning", and "criticism" are ideas of HOW you will complete your task.
Expand All @@ -37,20 +47,20 @@ Only focus on your task. Do not try to do more then what you are asked.
```json
{{
"what have I done so far": "progress",
"what have I done so far": "N/A" / "progress",
"explanation of why or why not my task is complete": "explanation",
"if my task is not complete, how do I finish it soon": null / "...",
"if my task is not complete, what is my plan to finish it": "N/A" / "...",
"thoughts about completing my task": "thought",
"reasoning": "reasoning",
"criticism": "constructive self-criticism",
"action": {{
"tool": "...",
"tool": "normal tool name, or 'done' if done",
"args": {{ ... }}
}}
}}
```
"action" may only be `null` if the task is complete.
Use the "done" tool if your task is complete.
Respond in the above JSON format exactly.
Ensure every field is filled in.
Expand All @@ -64,7 +74,7 @@ pub struct Thoughts {
pub progress: String,
#[serde(rename = "explanation of why or why not my task is complete")]
pub explanation: String,
#[serde(rename = "if my task is not complete, how do I finish it soon")]
#[serde(rename = "if my task is not complete, what is my plan to finish it")]
pub soon: Option<String>,
#[serde(rename = "thoughts about completing my task")]
pub thoughts: Option<String>,
Expand Down Expand Up @@ -117,9 +127,15 @@ pub fn run_react_action(

match thoughts.action {
Some(action) => {
Ok(ActionResults::Results(
use_tool(context, &|context| &mut context.agents.fast, action)?
))
Ok(if action.tool == "done" {
ActionResults::TaskComplete(
explain_results(context, &get_agent)?
)
} else {
ActionResults::Results(
use_tool(context, &|context| &mut context.agents.fast, action)?
)
})
}
None => {
Ok(ActionResults::TaskComplete(
Expand Down Expand Up @@ -159,14 +175,19 @@ r#"
You have been given a new task:
{task}
Complete this task just as before.
Adhere to those exact instructions as closely as possible.
Use the exact tools mentioned.
Finish as soon as possible, do not deviate from your task.
Ensure that you use the knowledge from your previous results to help you.
Respond in this precise JSON format:
```json
{{
"what have I done so far": "progress",
"explanation of why or why not my task is complete": "explanation",
"if my task is not complete, how do I finish it soon": null / "...",
"if my task is not complete, what is my plan to finish it": null / "...",
"thoughts about completing my task": "thought",
"reasoning": "reasoning",
"criticism": "constructive self-criticism",
Expand All @@ -187,7 +208,7 @@ Respond in this precise JSON format:
&agent.llm.get_messages()
)?;

if remaining_tokens < 1000 {
if remaining_tokens < 1500 {
ask_for_findings(agent)?;

let observations = get_observations(agent, task, 20, Weights {
Expand All @@ -203,7 +224,7 @@ Long Term Observations:
Take advantage of your long-term observations as much as possible."));

agent.llm.crop_to_tokens_remaining(2200)?;
agent.llm.crop_to_tokens_remaining(2500)?;
}

drop(agent);
Expand All @@ -220,6 +241,7 @@ r#"Your tool use gave the following result:
{results}
Please decide on your next action to complete your initial task of '{task}'.
Ensure you explain whether or not your task is complete.
Only focus on your task, do not get tunnel vision."#
)));
},
Expand Down
6 changes: 3 additions & 3 deletions src/auto/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{sync::{Mutex, Arc}, error::Error};
use std::{sync::{Mutex, Arc}, error::Error, collections::HashMap};

use serde::{Deserialize, Serialize};
use tokio::runtime::Runtime;
Expand All @@ -8,7 +8,7 @@ use crate::{ScriptValue, ProgramInfo, Command, CommandContext, Expression, GPTRu
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Action {
pub tool: String,
pub args: ScriptValue
pub args: Option<ScriptValue>
}

pub async fn run_command(
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn run_action_sync(context: &mut CommandContext, action: Action) -> Result<S
action.tool.clone(),
command.box_clone(),
context,
action.args
action.args.unwrap_or(HashMap::new().into())
).await
})?;

Expand Down

0 comments on commit 4f2117b

Please sign in to comment.