FizzBuzz with Autogen

0𝕏koji
6 min readNov 1, 2023

--

Tried to write FizzBuzz code in python with Autogen.

What is FizzBuzz? (text was generated by Bing chat)

FizzBuzz is a simple word game that is often used as a coding challenge in interviews or online platforms. The rules are as follows:

For each number from 1 to a given limit, print the number or a word according to these conditions:

If the number is divisible by 3, print “Fizz” instead of the number.

If the number is divisible by 5, print “Buzz” instead of the number.

If the number is divisible by both 3 and 5, print “FizzBuzz” instead of the number.

Otherwise, just print the number.

Kindly Bing chat generated a sample code with python.

def fizzbuzz(limit):
for i in range(1, limit + 1):
if i % 3 == 0 and i % 5 == 0:
print(“FizzBuzz”)
elif i % 3 == 0:
print(“Fizz”)
elif i % 5 == 0:
print(“Buzz”)
else:
print(i)

fizzbuzz(15)

What is Autogen?

AutoGen is a framework that enables the development of LLM applications using multiple agents that can converse with each other to solve tasks. AutoGen agents are customizable, conversable, and seamlessly allow human participation. They can operate in various modes that employ combinations of LLMs, human inputs, and tools.

In this post, I will introduce the code that generate fizzbuzz python code and run it.
Requirements Python version >= 3.8

  1. install autogen
pip install pyautogen

2. create a OAI_CONFIG_LIST
OAI_CONFIG_LIST is config file that you can put information to use OpenAI API.

[
{
"model": "gpt-3.5-turbo",
"api_key": "***********"
}
]

3. write a python script

First load the config list and set llm_config.
Then create an assistant and user_proxy.
Request 2 tasks, one is write a fizzbuzz code and save it as fizz_buzz.py. The other task is executing that python code with input which is 100.

import autogen

config_list_gpt35 = autogen.config_list_from_json(
"OAI_CONFIG_LIST",
file_location=".",
filter_dict={
"model": {'gpt-3.5-turbo-16k',}
},
)

llm_config = {
"request_timeout": 600,
"seed": 42,
"config_list": config_list_gpt35,
"temperature": 0
}


assistant = autogen.AssistantAgent(
name="assistant",
llm_config=llm_config
# system_message=""
)

user_proxy = autogen.UserProxyAgent(
name="user_proxy",
human_input_mode="TERMINATE",
max_consecutive_auto_reply=3,
is_termination_msg=lambda x: x.get(
"content", "").rstrip().endswith("TERMINATE"),
code_execution_config={"work_dir": "coding"},
llm_config=llm_config,
system_message="""Reply TERMINATE if the task has been solved an full satisfaction. Otherwise, reply CONTINUE, or the reason why the task is not solved yet."""
)

task = """
Could you write a python script and save the code as fizz_buzz.py in coding folder.
The problem that I want you to solve is below.

Given an integer n, return a string array answer (1-indexed) where:

answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
answer[i] == "Fizz" if i is divisible by 3.
answer[i] == "Buzz" if i is divisible by 5.
answer[i] == i (as a string) if none of the above conditions are true.
"""

user_proxy.initiate_chat(
assistant,
message=task
)

task2 = """
Run the above task's python code by python coding/fizz_buzz.py and prompt 100 as input.
"""

user_proxy.initiate_chat(
assistant,
message=task2
)

Result

The code can generate a fizzbuzz python code and run it, but didn’t save the code as fizz_buzz.py.
I tried a couple of time with a different task messages. Most cases could generate a python code and save it as fizz_buzz.py but that didn’t run the python code. 😂

user_proxy (to assistant):


Could you write a python script and save the code as fizz_buzz.py in coding folder.
The problem that I want you to solve is below.

Given an integer n, return a string array answer (1-indexed) where:

answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
answer[i] == "Fizz" if i is divisible by 3.
answer[i] == "Buzz" if i is divisible by 5.
answer[i] == i (as a string) if none of the above conditions are true.


--------------------------------------------------------------------------------
assistant (to user_proxy):

# filename: fizz_buzz.py

def fizzBuzz(n):
answer = []
for i in range(1, n+1):
if i % 3 == 0 and i % 5 == 0:
answer.append("FizzBuzz")
elif i % 3 == 0:
answer.append("Fizz")
elif i % 5 == 0:
answer.append("Buzz")
else:
answer.append(str(i))
return answer

n = int(input("Enter an integer: "))
result = fizzBuzz(n)
print(result)

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
user_proxy (to assistant):

The code you provided looks correct and should solve the FizzBuzz problem. It takes an integer input from the user, calls the `fizzBuzz` function with the input, and prints the resulting list.

To test the code, you can run it in a Python environment and enter an integer when prompted. The code will generate the FizzBuzz sequence up to the given number and print the result.

If you encounter any issues or have further questions, please let me know.

--------------------------------------------------------------------------------
assistant (to user_proxy):

TERMINATE

--------------------------------------------------------------------------------
Please give feedback to assistant. Press enter or type 'exit' to stop the conversation:

>>>>>>>> NO HUMAN INPUT RECEIVED.
user_proxy (to assistant):


Run the above task's python code by python coding/fizz_buzz.py and prompt 100 as input.


--------------------------------------------------------------------------------
assistant (to user_proxy):

I'm sorry, but I don't have access to the code you mentioned. Could you please provide the code for the FizzBuzz task?

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
user_proxy (to assistant):

Certainly! Here's a Python code snippet for the FizzBuzz task:

```python
def fizz_buzz(n):
for i in range(1, n+1):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

# Test the function
fizz_buzz(100)
```

You can run this code in any Python environment or save it to a file with a `.py` extension and run it using the command `python fizz_buzz.py` in your terminal.

--------------------------------------------------------------------------------
assistant (to user_proxy):

Thank you for providing the code. You can save the code in a file named `fizz_buzz.py` and run it using the command `python fizz_buzz.py` in your terminal. Make sure you have Python installed on your system.

Here's the code again for reference:

```python
def fizz_buzz(n):
for i in range(1, n+1):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)

# Test the function
fizz_buzz(100)
```

Let me know if you need any further assistance!

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...

>>>>>>>> EXECUTING CODE BLOCK 0 (inferred language is python)...
execute_code was called without specifying a value for use_docker. Since the python docker package is not available, code will be run natively. Note: this fallback behavior is subject to change
user_proxy (to assistant):

exitcode: 0 (execution succeeded)
Code output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz


--------------------------------------------------------------------------------
assistant (to user_proxy):

Great! The code executed successfully and produced the expected output for the FizzBuzz task. Each number from 1 to 100 is printed, with "Fizz" printed for numbers divisible by 3, "Buzz" printed for numbers divisible by 5, and "FizzBuzz" printed for numbers divisible by both 3 and 5.

Is there anything else I can help you with?

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
user_proxy (to assistant):

No, thank you! The task has been solved successfully. I appreciate your assistance.

--------------------------------------------------------------------------------
assistant (to user_proxy):

You're welcome! I'm glad I could help you solve the task successfully. If you have any more questions or need assistance in the future, feel free to ask. Have a great day!

--------------------------------------------------------------------------------
Please give feedback to assistant. Press enter to skip and use auto-reply, or type 'exit' to stop the conversation: exit

In this post, I just tried a very simple case, but Autogen can do more complicated tasks. Also we can use Docker with Autogen.

https://microsoft.github.io/autogen/docs/FAQ#enable-python-3-docker-image

--

--

0𝕏koji

software engineer works for a Biotechnology Research startup in Brooklyn. #CreativeCoding #Art #IoT #MachineLearning #python #typescript #javascript #reactjs