Tiny AI robot working in a colorized terminal

Enhancing Your AutoGen Experience: A Guide to Beautifying Terminal Inputs/Outputs

Dragos Campean

--

Introduction:

AutoGen is a rapidly growing open source framework from Microsoft which enables users to orchestrate multi-agent workflows to solve complex tasks.

While users can execute these workflows through the user-friendly AutoGen Studio GUI, many prefer the flexibility and efficiency of working directly within the terminal. However, lengthy workflow outputs can become challenging to read and comprehend, adding cognitive load and diverting focus from the actual processing of the output.

Fortunately, with the introduction of the IOStream protocol in AutoGen version 0.2.21, customising your terminal’s display has become more straightforward and cleaner than before.

In this article, I will guide you through the process of beautifying your AutoGen inputs and outputs to make large white on black text blocks more digestible.

The Evolution of AutoGen’s IO Customization Options:

Before AutoGen v0.2.21, users had two options for customising terminal outputs:

1. Forking the repository and modifying the print() statements within the ConversableAgent class.

2. Overriding the builtin print() method used in ConversableAgent._print_received_message(), (an internal method that should not be modified) with a custom print function.

Both options were cumbersome, however, with the introduction of the IOStream protocol, users can now set a global default for input/output streams, making the process much simpler and more elegant.

Enhancing Terminal Output with Rich:

One powerful library for enhancing terminal output is Rich. By integrating Rich with AutoGen’s IOStream protocol, you can easily colourize and format your workflow outputs, making them more visually appealing and easier to read.

Here’s how to set up Rich with AutoGen:

from autogen.io.base import IOStream
from rich.console import Console
from rich.text import Text
from typing import Any

class RichIOStream(IOStream):
def __init__(self):
self.console = Console()

def print(self, *args: Any, **kwargs) -> None:
# Remove 'flush' argument if present, as it's not supported by rich.console.Console.print
kwargs.pop('flush', None)
processed_args = []
for arg in args:
if isinstance(arg, str):
# Convert args with ANSI codes into rich Text objects
processed_args.append(Text.from_ansi(arg))
else:
# Non-string arguments are added without modification
processed_args.append(arg)

self.console.print(markup=False, *processed_args, **kwargs)

def input(self, prompt: str = "", *, password: bool = False) -> str:
return input(prompt)

def set_custom_IO_overrides():
IOStream.set_global_default(RichIOStream())

By calling set_custom_IO_overrides() in your script that handles the multi-agent orchestration workflow, you can enjoy the benefits of colourized and formatted terminal output.

The result will be something like this for code output:

LLM code output in terminal

Enhancing Input with Prompt Toolkit:

In addition to beautifying output, you can also customise input functionality using libraries like Prompt Toolkit. Prompt Toolkit allows for easy navigation through multi-line input prompts, further improving your user experience.

To integrate Prompt Toolkit with AutoGen, define a custom input function within the `RichIOStream` class:

from pygments.lexers.python import PythonLexer
from prompt_toolkit import prompt as toolkit_prompt
from prompt_toolkit.lexers import PygmentsLexer

def ask_for_prompt_input(
prompt='Please input user prompt.',
):
"""
Prompts the user for a multiline string.
- multiline=True has a side effect where Enter key now inserts a new line
instead of accepting and returning the input. Accept input via (Meta|Esc)+Enter.
- mouse_support=True has a side effect that actions like scroll, select text
from the terminal requires keeping the Fn key pressed.
"""
prompt_suffix = 'Submit prompt via (Meta|Esc)+Enter.'
user_prompt = toolkit_prompt(
HTML(f'<ansigreen>\n{prompt}\n{prompt_suffix}\n\n</ansigreen>'),
multiline=True,
lexer=PygmentsLexer(PythonLexer),
)
print("\n", "-" * 80, flush=True, sep="")
return user_prompt

class RichIOStream(IOStream):
...
def input(self, prompt):
return ask_for_prompt_input(prompt)

The result will look something like this:

The Benefits of Beautifying Your AutoGen Workflow:

Enhancing your AutoGen inputs and outputs offers several key benefits:

  • Improved Readability: Colorizing terminal outputs makes the text more digestible, reducing cognitive load and allowing you to focus on processing and comprehending the workflow outputs.
  • Enhanced User Experience: Libraries like Rich and Prompt Toolkit offer advanced formatting and navigation options, making multi-line inputs and outputs more manageable and visually appealing.
  • Increased Efficiency: By streamlining the process of working with complex multi-agent workflows, you can save time and effort, ultimately boosting productivity.

Troubleshooting Tips:

There’s currently a small bug in AutoGen v0.2.21 related to the IOStream feature which pollutes the terminal with multiple warning messages. Issue was fixed here: https://github.com/microsoft/autogen/pull/2207/files and should be deployed soon.

If you encounter any issues with the customization process, make sure you have the latest versions of the required libraries (Rich and Prompt Toolkit) installed.

You can see a working implementation of the above in this small wrapper over Autogen: https://github.com/dragosMC91/AutoGen-Experiments (see the `RichIOStream` implementation in `utils/prompt_utils.py` and the usage of `set_custom_IO_overrides()` in `agents/custom_agents.py`)

Conclusion:

The evolution of AutoGen and the introduction of the `IOStream` protocol have opened up new possibilities for customising terminal outputs and inputs.

By leveraging external libraries like Rich for colorization and Prompt Toolkit for enhanced input functionality, you can create a more visually appealing and user-friendly terminal experience.

This not only improves readability and comprehension but also makes the process of working with complex multi-agent workflows more enjoyable and efficient.

#AutoGen #Microsoft #Terminal colorization #IOStream protocol #Rich library Python #Prompt Toolkit Python #Multi-agent workflow orchestration #Customize AutoGen terminal outputs

--

--

Dragos Campean

Software tester passionate about coffee and reading ☕📚