Skip to content

Commit

Permalink
added to_json with list formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
andthattoo committed Jul 22, 2024
1 parent c448a15 commit 64533d5
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 9 deletions.
18 changes: 16 additions & 2 deletions src/memory/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,22 @@ impl MemoryReturnType {
}
}

pub fn to_json(&self) -> serde_json::Result<String> {
serde_json::to_string(&self)
pub fn to_json(&self) -> Option<String> {
match self {
MemoryReturnType::EntryVec(Some(entries)) => {
// Extracting the string directly from each Entry.
let values: Vec<String> = entries
.iter()
.map(|entry| entry.to_string().clone())
.collect();
let res = serde_json::to_string(&values);
match res {
Ok(json) => Some(json),
Err(_) => None,
}
}
_ => None,
}
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/program/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ impl Executor {
let return_value = self.handle_input(&rv.input, memory).await;
let mut return_string = return_value.to_string().clone();

if rv.to_json.is_some() && rv.to_json.unwrap() {
let res = return_value.to_json();
if let Some(result) = res {
return result;
}
}

if let Some(post_pr) = rv.post_process.clone() {
for process in post_pr {
return_string = match process.process_type {
Expand Down Expand Up @@ -206,13 +213,6 @@ impl Executor {
};
}
}

if rv.to_json.is_some() && rv.to_json.unwrap() {
let res = return_value.to_json();
if res.is_ok() {
return_string = res.unwrap();
}
}
return_string
}

Expand Down
12 changes: 12 additions & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ async fn test_search_workflow() {
exe.execute(Some(&input), workflow, &mut memory).await;
}

#[tokio::test]
async fn test_question_generation() {
dotenv().ok();
let env = Env::default().filter_or("LOG_LEVEL", "info");
env_logger::Builder::from_env(env).init();
let exe = Executor::new(Model::GPT4oMini);
let workflow = Workflow::new_from_json("./tests/test_workflows/questions.json").unwrap();
let mut memory = ProgramMemory::new();
let input = Entry::try_value_or_str("Best ZK Rollups to date, july 2024?");
exe.execute(Some(&input), workflow, &mut memory).await;
}

#[tokio::test]
async fn test_search_workflow_openai() {
dotenv().ok();
Expand Down
97 changes: 97 additions & 0 deletions tests/test_workflows/questions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"name": "Create questions",
"description": "Generate questions related to given topic",
"config": {
"max_steps": 35,
"max_time": 600,
"tools": ["ALL"]
},
"external_memory":{
"topic":["yield farming strategies"],
"perspective": [
"Analytical and data-driven approach to evaluating crypto companies and market trends",
"Strategic mindset focused on identifying competitive advantages and market positioning",
"Technical perspective to assess founder capabilities and protocol functionalities",
"Forward-looking attitude to validate company visions and understand market narratives",
"Comparative viewpoint for conducting competitor research and market analysis",
"Holistic understanding of the blockchain ecosystem to contextualize individual companies",
"Categorization-oriented approach to classify and position companies within the industry",
"Advisory stance to assist VC firms' Limited Partners (LPs) and General Partners (GPs)",
"Objective and critical perspective for evaluating claims and potential in the crypto space"
]
},
"tasks": [
{
"id": "A",
"name": "Web Search Query",
"description": "Write a web search query to collect useful information for the given question",
"prompt": "You are a researcher AI tasked with generating web search queries to aid in the research process on a given topic. Your goal is to formulate questions that will help explore different aspects of the topic and gather comprehensive knowledge.\n\nThe topic you are researching is:\n<topic>\n{{topic}}\n</topic>\n\nThese are your perspectives as a researcher while asking questions:\n<perspectives>\n{{perspectives}}\n</perspectives>\n\nHere is the history of previously asked questions:\n<previous_questions>\n{{previous_questions}}\n</previous_questions>\n\nTo generate a new search query:\n\n1. Analyze the given topic and the history of previous questions.\n2. Identify aspects of the topic that have not been covered or areas that could benefit from deeper exploration.\n3. Consider different perspectives, time periods, or interdisciplinary connections related to the topic.\n4. Formulate a clear, concise, and specific search query that addresses an unexplored or underexplored aspect of the topic.\n5. Ensure your query is distinct from the previous questions and adds value to the research process.\n\nGuidelines for your search query:\n- Keep it between 5-15 words\n- Use specific terms related to the topic\n- Include relevant keywords that will yield informative results\n- Avoid overly broad or vague questions\n- Do not repeat previous questions or ask for information already covered\n\nPresent your final search query within <search_query> tags.\n\nExample output:\n\n<search_query>\n[Your generated search query here]\n</search_query>\n\nNow, based on the given topic and previous questions, generate a new search query following these instructions.",
"inputs": [
{
"name": "topic",
"value": {
"type": "peek",
"index": 0,
"key": "topic"
},
"required": true
},
{
"name": "perspectives",
"value": {
"type": "get_all",
"key": "perspective"
},
"required": true
},
{
"name": "previous_questions",
"value": {
"type": "get_all",
"key": "history"
},
"required": false
}
],
"operator": "generation",
"outputs": [
{
"type": "push",
"key": "queries",
"value": "__result"
}
]
},
{
"id": "__end",
"name": "end",
"description": "End of the task",
"prompt": "End of the task",
"inputs": [],
"operator": "end",
"outputs": []
}
],
"steps": [
{
"source": "A",
"target": "_end",
"condition": {
"input":{
"type":"size",
"key": "queries"
},
"expression": "GreaterThan",
"expected": "2",
"target_if_not": "A"
}
}
],
"return_value":{
"input":{
"type": "get_all",
"key": "queries"
},
"to_json": true
}
}

0 comments on commit 64533d5

Please sign in to comment.