chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import os
|
||||
import importlib
|
||||
from autoagent.registry import registry
|
||||
|
||||
def import_workflows_recursively(base_dir: str, base_package: str):
|
||||
"""Recursively import all workflows in .py files
|
||||
|
||||
Args:
|
||||
base_dir: the root directory to start searching
|
||||
base_package: the base name of the Python package
|
||||
"""
|
||||
for root, dirs, files in os.walk(base_dir):
|
||||
# get the relative path to the base directory
|
||||
rel_path = os.path.relpath(root, base_dir)
|
||||
|
||||
for file in files:
|
||||
if file.endswith('.py') and not file.startswith('__'):
|
||||
# build the module path
|
||||
if rel_path == '.':
|
||||
# in the root directory
|
||||
module_path = f"{base_package}.{file[:-3]}"
|
||||
else:
|
||||
# in the subdirectory
|
||||
package_path = rel_path.replace(os.path.sep, '.')
|
||||
module_path = f"{base_package}.{package_path}.{file[:-3]}"
|
||||
|
||||
try:
|
||||
importlib.import_module(module_path)
|
||||
except Exception as e:
|
||||
print(f"Warning: Failed to import {module_path}: {e}")
|
||||
|
||||
# get the current directory and import all tools
|
||||
current_dir = os.path.dirname(__file__)
|
||||
import_workflows_recursively(current_dir, 'autoagent.workflows')
|
||||
|
||||
# export all tool creation functions
|
||||
globals().update(registry.workflows)
|
||||
|
||||
__all__ = list(registry.workflows.keys())
|
||||
@@ -0,0 +1,201 @@
|
||||
import asyncio
|
||||
import json
|
||||
import argparse
|
||||
from openai import AsyncOpenAI
|
||||
from openai.types.chat import ChatCompletionMessageToolCall
|
||||
from autoagent.flow import default_drive, EventInput, ReturnBehavior
|
||||
from autoagent.flow.dynamic import goto_events, abort_this
|
||||
import re
|
||||
from autoagent import MetaChain
|
||||
from autoagent.types import Response
|
||||
from autoagent.registry import register_workflow
|
||||
|
||||
def extract_answer(response: str, key: str):
|
||||
pattern = f"<{key}>(.*?)</{key}>"
|
||||
matches = re.findall(pattern, response)
|
||||
return matches[0] if len(matches) > 0 else None
|
||||
|
||||
from autoagent.agents import get_math_solver_agent
|
||||
|
||||
from autoagent.agents import get_vote_aggregator_agent
|
||||
@default_drive.make_event
|
||||
async def on_start(event: EventInput, global_ctx):
|
||||
print("start the workflow:" + 'math_solver_workflow')
|
||||
@default_drive.listen_group([on_start])
|
||||
async def solve_with_gpt4(event: EventInput, global_ctx):
|
||||
inputs = [{'key': 'math_problem', 'description': 'The math problem that needs to be solved.'}]
|
||||
input_dict = dict()
|
||||
for inp in inputs:
|
||||
input_dict[inp["key"]] = global_ctx.get(inp["key"], None)
|
||||
|
||||
messages = global_ctx.get('messages', [])
|
||||
task = 'Solve the math problem using systematic approach and show detailed steps.'
|
||||
outputs = [{'key': 'gpt4_solution', 'description': 'The solution generated by GPT-4 model.', 'condition': None, 'action': {'type': 'RESULT', 'value': None}}]
|
||||
agent = get_math_solver_agent('gpt-4o-2024-08-06')
|
||||
|
||||
|
||||
input_str = []
|
||||
for key, value in input_dict.items():
|
||||
input_str.append(f"The {key.replace('_', ' ')} is {value}")
|
||||
input_str = "\n".join(input_str) + "\n"
|
||||
query = input_str + '.\nThe task is: ' + task + '.\n'
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": query
|
||||
})
|
||||
client = MetaChain()
|
||||
response: Response = await client.run_async(agent = agent, messages = messages, context_variables = global_ctx, debug = True)
|
||||
result = response.messages[-1]["content"]
|
||||
messages.extend(response.messages)
|
||||
global_ctx["messages"] = messages
|
||||
|
||||
for output in outputs:
|
||||
ans = extract_answer(result, output["key"])
|
||||
if ans:
|
||||
if output["action"]["type"] == "RESULT":
|
||||
global_ctx[output["key"]] = ans
|
||||
return ans
|
||||
elif output["action"]["type"] == "ABORT":
|
||||
return abort_this()
|
||||
elif output["action"]["type"] == "GO_TO":
|
||||
return goto_events([output["action"]["value"]])
|
||||
elif len(outputs) == 1:
|
||||
global_ctx[output["key"]] = result
|
||||
return result
|
||||
raise Exception("No valid answer found")
|
||||
@default_drive.listen_group([on_start])
|
||||
async def solve_with_claude(event: EventInput, global_ctx):
|
||||
inputs = [{'key': 'math_problem', 'description': 'The math problem that needs to be solved.'}]
|
||||
input_dict = dict()
|
||||
for inp in inputs:
|
||||
input_dict[inp["key"]] = global_ctx.get(inp["key"], None)
|
||||
|
||||
messages = global_ctx.get('messages', [])
|
||||
task = 'Solve the math problem using systematic approach and show detailed steps.'
|
||||
outputs = [{'key': 'claude_solution', 'description': 'The solution generated by Claude model.', 'condition': None, 'action': {'type': 'RESULT', 'value': None}}]
|
||||
agent = get_math_solver_agent('claude-3-5-sonnet-20241022')
|
||||
|
||||
|
||||
input_str = []
|
||||
for key, value in input_dict.items():
|
||||
input_str.append(f"The {key.replace('_', ' ')} is {value}")
|
||||
input_str = "\n".join(input_str) + "\n"
|
||||
query = input_str + '.\nThe task is: ' + task + '.\n'
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": query
|
||||
})
|
||||
client = MetaChain()
|
||||
response: Response = await client.run_async(agent = agent, messages = messages, context_variables = global_ctx, debug = True)
|
||||
result = response.messages[-1]["content"]
|
||||
messages.extend(response.messages)
|
||||
global_ctx["messages"] = messages
|
||||
|
||||
for output in outputs:
|
||||
ans = extract_answer(result, output["key"])
|
||||
if ans:
|
||||
if output["action"]["type"] == "RESULT":
|
||||
global_ctx[output["key"]] = ans
|
||||
return ans
|
||||
elif output["action"]["type"] == "ABORT":
|
||||
return abort_this()
|
||||
elif output["action"]["type"] == "GO_TO":
|
||||
return goto_events([output["action"]["value"]])
|
||||
elif len(outputs) == 1:
|
||||
global_ctx[output["key"]] = result
|
||||
return result
|
||||
raise Exception("No valid answer found")
|
||||
@default_drive.listen_group([on_start])
|
||||
async def solve_with_deepseek(event: EventInput, global_ctx):
|
||||
inputs = [{'key': 'math_problem', 'description': 'The math problem that needs to be solved.'}]
|
||||
input_dict = dict()
|
||||
for inp in inputs:
|
||||
input_dict[inp["key"]] = global_ctx.get(inp["key"], None)
|
||||
|
||||
messages = global_ctx.get('messages', [])
|
||||
task = 'Solve the math problem using systematic approach and show detailed steps.'
|
||||
outputs = [{'key': 'deepseek_solution', 'description': 'The solution generated by Deepseek model.', 'condition': None, 'action': {'type': 'RESULT', 'value': None}}]
|
||||
agent = get_math_solver_agent('deepseek/deepseek-chat')
|
||||
|
||||
|
||||
input_str = []
|
||||
for key, value in input_dict.items():
|
||||
input_str.append(f"The {key.replace('_', ' ')} is {value}")
|
||||
input_str = "\n".join(input_str) + "\n"
|
||||
query = input_str + '.\nThe task is: ' + task + '.\n'
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": query
|
||||
})
|
||||
client = MetaChain()
|
||||
response: Response = await client.run_async(agent = agent, messages = messages, context_variables = global_ctx, debug = True)
|
||||
result = response.messages[-1]["content"]
|
||||
messages.extend(response.messages)
|
||||
global_ctx["messages"] = messages
|
||||
|
||||
for output in outputs:
|
||||
ans = extract_answer(result, output["key"])
|
||||
if ans:
|
||||
if output["action"]["type"] == "RESULT":
|
||||
global_ctx[output["key"]] = ans
|
||||
return ans
|
||||
elif output["action"]["type"] == "ABORT":
|
||||
return abort_this()
|
||||
elif output["action"]["type"] == "GO_TO":
|
||||
return goto_events([output["action"]["value"]])
|
||||
elif len(outputs) == 1:
|
||||
global_ctx[output["key"]] = result
|
||||
return result
|
||||
raise Exception("No valid answer found")
|
||||
@default_drive.listen_group([solve_with_gpt4, solve_with_claude, solve_with_deepseek])
|
||||
async def aggregate_solutions(event: EventInput, global_ctx):
|
||||
inputs = [{'key': 'gpt4_solution', 'description': 'The solution generated by GPT-4 model.'}, {'key': 'claude_solution', 'description': 'The solution generated by Claude model.'}, {'key': 'deepseek_solution', 'description': 'The solution generated by Deepseek model.'}]
|
||||
input_dict = dict()
|
||||
for inp in inputs:
|
||||
input_dict[inp["key"]] = global_ctx.get(inp["key"], None)
|
||||
|
||||
messages = global_ctx.get('messages', [])
|
||||
task = 'Compare all solutions and determine the final answer through majority voting.'
|
||||
outputs = [{'key': 'final_solution', 'description': 'The final agreed-upon solution after majority voting.', 'condition': None, 'action': {'type': 'RESULT', 'value': None}}]
|
||||
agent = get_vote_aggregator_agent('gpt-4o-2024-08-06')
|
||||
|
||||
|
||||
input_str = []
|
||||
for key, value in input_dict.items():
|
||||
input_str.append(f"The {key.replace('_', ' ')} is {value}")
|
||||
input_str = "\n".join(input_str) + "\n"
|
||||
query = input_str + '.\nThe task is: ' + task + '.\n'
|
||||
messages.append({
|
||||
"role": "user",
|
||||
"content": query
|
||||
})
|
||||
client = MetaChain()
|
||||
response: Response = await client.run_async(agent = agent, messages = messages, context_variables = global_ctx, debug = True)
|
||||
result = response.messages[-1]["content"]
|
||||
messages.extend(response.messages)
|
||||
global_ctx["messages"] = messages
|
||||
|
||||
for output in outputs:
|
||||
ans = extract_answer(result, output["key"])
|
||||
if ans:
|
||||
if output["action"]["type"] == "RESULT":
|
||||
global_ctx[output["key"]] = ans
|
||||
return ans
|
||||
elif output["action"]["type"] == "ABORT":
|
||||
return abort_this()
|
||||
elif output["action"]["type"] == "GO_TO":
|
||||
return goto_events([output["action"]["value"]])
|
||||
elif len(outputs) == 1:
|
||||
global_ctx[output["key"]] = result
|
||||
return result
|
||||
raise Exception("No valid answer found")
|
||||
|
||||
@register_workflow(name = 'majority_voting')
|
||||
async def majority_voting(system_input: str):
|
||||
storage_results = dict(math_problem = system_input)
|
||||
await default_drive.invoke_event(
|
||||
on_start,
|
||||
global_ctx=storage_results,
|
||||
)
|
||||
system_output = storage_results.get('final_solution', None)
|
||||
return system_output
|
||||
Reference in New Issue
Block a user