AutoGen

AutoGen#

AutoGen is another AI framework that allows you to create custom agent crews designed to perform several tasks using a LLM. Autogen Agent are also able to run code and test the output to be able to do modifications if needed.

Installation#

pip install autogen-agentchat~=0.2

Basic example#

In this example we ask two agents to code a quicksort function. We have two agents here, one coding the function and the other executing the code.

import httpx

from autogen import ConversableAgent, UserProxyAgent


class HttpClient(httpx.Client):
    def __deepcopy__(self, memo):
        return self


# Enter base_url and api_key
config_list = [
    {
        "model": "Llama-3.1-8B",
        "base_url": "",
        "api_key": "",
        "max_tokens": 256,
        "temperature": 0.5,
        "http_client": HttpClient(verify=False),
    }
]

assistant = ConversableAgent("assistant", llm_config={"config_list": config_list})
user_proxy = UserProxyAgent(
    "user_proxy",
    code_execution_config={
        "work_dir": "coding",
        "use_docker": False,
    },
)

code_writer_agent_system_message = assistant.system_message

print(" system message ".center(100, "="))
print(code_writer_agent_system_message)

print(
    " code for prompt: Write python code to perform a quicksort over a python list ".center(
        100, "="
    )
)
user_proxy.initiate_chat(
    assistant,
    message="Write python code to perform a quicksort over a python list",
    verify=False,
)

print("=" * 100)

This would generate an output similar to:

Here is an example of a Python implementation of the quicksort algorithm:
```
def quicksort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[0]
    less = [x for x in arr[1:] if x <= pivot]
    greater = [x for x in arr[1:] if x > pivot]
    return quicksort(less) + [pivot] + quicksort(greater)
```
This implementation uses the "Lomuto" partition scheme, which is a variation of the standard "Hoare" partition scheme that is slightly faster and more efficient.

Here's an explanation of how the code works:

1. If the length of the input array is 0 or 1, return the original array (since it is already sorted).
2. Choose the first element of the array as the pivot.
3. Create two lists: `less` and `greater`. `less` contains all elements in the array that are less than or equal to the pivot, and `greater` contains all elements that are greater than the pivot.
4. Recursively call the `quicksort` function on the `less` and `greater` lists.
5. Concatenate the results of the recursive calls