Jneid Jneid

@jjneid94

Published on Jan 19, 2025

Building Agents with AutoGen

Jneid Jneid

@jjneid94

Published on Jan 19, 2025

Been testing AutoGen framework for building AI agents. Here’s my take so far.

Basic Components

1. Tool Definition: Using `FunctionTool` to wrap Python functions

2. Agent Creation: Using `AssistantAgent` to create agents with specific roles

3. Team Organization: Using `RoundRobinGroupChat` for agent coordination

Implementation 1: Specialized Stock Analysis Agents

In my first implementation, I created custom tools and agents specifically for stock analysis:

Custom Tools

def google_search(query: str, num_results: int = 2) -> list:
    # Google search implementation
    pass
def analyze_stock(ticker: str) -> dict:
    # Stock analysis implementation
    pass
def save_report(content: str, filename: str) -> str:
    # Report saving implementation
    pass
# Create tools
google_search_tool = FunctionTool(google_search)
stock_analysis_tool = FunctionTool(analyze_stock)
save_tool = FunctionTool(save_report)

Specialized Agents

search_agent = AssistantAgent(
    name="Google_Search_Agent",
    tools=[google_search_tool],
    description="Search for company information"
)
stock_analysis_agent = AssistantAgent(
    name="Stock_Analysis_Agent",
    tools=[stock_analysis_tool],
    description="Analyze stock data"
)
report_agent = AssistantAgent(
    name="Report_Agent",
    tools=[save_tool],
    description="Generate reports"
)
team = RoundRobinGroupChat(
    [search_agent, stock_analysis_agent, report_agent]
)

This worked well but required creating specific tools and agents for stock analysis, full code of this implementation can be found here.

Note: you can add more capabilities like pulling the SEC filings in particular by adding another agent…

Implementation 2: The Universal Approach

Then I got inspired by Magentic-One and realized - you only need three core components to build such agent-based system:

1. Code Generator Agent

code_generator = AssistantAgent(
    name="Code_Generator",
    model_client=OpenAIChatCompletionClient(model="gpt-4"),
    description="Write analysis code",
    system_message="""Generate clean, executable Python code for given tasks.
    Use standard libraries and proper error handling."""
)

2. Code Executor Agent

executor = LocalCommandLineCodeExecutor(work_dir="results")
code_tool = PythonCodeExecutionTool(executor=executor)
code_executor = AssistantAgent(
    name="Code_Executor",
    tools=[code_tool],
    description="Execute code and handle results"
)

3. Report Agent/File Manager

report_agent = AssistantAgent(
    name="Report_Agent",
    tools=[save_tool],
    description="Generate reports"
)

Why This Works

With these three components:

1. Code Generator writes any necessary code (replacing specialized tools)

2. Code Executor runs the code (handling any type of analysis)

3. Report Agent saves and organizes results

The same stock analysis can be performed by having the Code Generator create the analysis code, the Executor run it, and the Report Agent save the results.

Spent time debugging dependencies and prompting with different techniques and tags but eventually it worked!!! Here's the full implementation https://github.com/JJneid/stock_coder_autogen

Now, trying to figure out how to deploy this to production!!!

The Power of Universal

This approach can handle any task that the first implementation could:

1. Stock Analysis? Code Generator writes the analysis code, Executor runs it

2. Market Research? Code Generator writes web scraping code, Executor runs it

3. Technical Analysis? Code Generator writes the indicators code, Executor runs it

Example Task

task = """Analyze American Airlines (AAL) stock:
1. Download 5 years of data
2. Calculate technical indicators
3. Create price predictions
4. Generate visualizations
5. Save results

team = RoundRobinGroupChat(
    [code_generator, code_executor, report_agent]
)
await Console(team.run_stream(task=task))

Conclusion

While building specialized agents works, it's often unnecessary. You can create systems using just:

  • A Code Generator to write task-specific code

  • A Code Executor to run that code

  • A File Manager to handle outputs

This “minimalist” approach is:

  • More flexible

  • Easier to maintain

  • Adaptable to any task

  • Still powerful enough for complex analyses

Try out our dashboard

Try out our dashboard

Deploy any model In Your Private Cloud or SlashML Cloud

READ OTHER POSTS

©2024 – Made with ❤️ & ☕️ in Montreal

©2024 – Made with ❤️ & ☕️ in Montreal

©2024 – Made with ❤️ & ☕️ in Montreal