Skip to content

Instantly share code, notes, and snippets.

@macintoxic
Created October 29, 2023 05:52
Show Gist options
  • Save macintoxic/064f478b312e516b24dcffc9f2c3f5ce to your computer and use it in GitHub Desktop.
Save macintoxic/064f478b312e516b24dcffc9f2c3f5ce to your computer and use it in GitHub Desktop.
Testing autogen
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager, ChatCompletion, retrieve_utils
### Default data and functions:
def write_file(file_name, file_content):
with open(file_name, "w") as file1:
file1.writelines(file_content)
function_map_definition = []
function_map_definition.append({
'name' : 'write_file',
'description' : 'write a file to disk',
'parameters' : {
'type' : 'object',
'properties' :{
'file_name' : {
'type':'string',
'description' : 'A valid file name'
},
'file_content' : {
'type':'string',
'description' : 'The content of the file.'
}
}
},
'required' : ['file_name', 'file_content']
})
config_list = [{
"api_type": "open_ai",
"api_key": "NULL",
'model' : 'gpt-3.5-turbo',
"api_base" : "http://localhost:1234/v1",
'functions' : function_map_definition ,
}]
#"api_base" : "https://api.openai.com/v1"
#"api_base" : "http://localhost:1234/v1"
llm_config = {
"request_timeout" : 9600,
"seed": 42,
"config_list" : config_list,
"temperature" : 0.1,
"max_tokens": 4096,
}
ChatCompletion.start_logging()
user_proxy = UserProxyAgent(
name="Admin",
system_message="""A human admin.
Interact with the planner to discuss the plan. Plan execution needs to be approved by this admin.
Reply TERMINATE if the task has been solved at full satisfaction otherwise CONTINUE, or reply why the reason the task is not solved yet.
""",
code_execution_config=False,
human_input_mode="TERMINATE",
llm_config=llm_config,
)
engineer = AssistantAgent(
name="Engineer",
llm_config=llm_config,
system_message='''Engineer. You follow an approved plan. You write python/shell or csharp code to solve tasks.
Wrap the code in a code block that specifies the script type.
The user can't modify your code. So do not suggest incomplete code which requires others to modify.
Don't use a code block if it's not intended to be executed by the executor.
Don't include multiple code blocks in one response.
Do not ask others to copy and paste the result.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes.
If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption,
collect additional info you need, and think of a different approach to try.
''',
max_consecutive_auto_reply=2,
code_execution_config={"work_dir": "coding"},
)
planner = AssistantAgent(
name="Planner",
system_message='''Planner. Suggest a plan. Break down the task in smaller steps. Revise the plan based on feedback from admin and critic, until admin approval.
The plan may involve an engineer who can write code.
Explain the plan first. Be clear which step is performed by an engineer. Do not write code. ask for an engineer to do it.
''',
llm_config=llm_config,
max_consecutive_auto_reply=5,
)
planner.register_function( function_map={"write_file": write_file})
executor = UserProxyAgent(
name="Executor",
system_message="Executor. Execute the code written by the engineers and report the result. When you receive a csharp or sql file, dont execute-it. Just write it down. ",
human_input_mode="NEVER",
max_consecutive_auto_reply=2,
llm_config=llm_config,
code_execution_config={"work_dir": "coding"},
)
sr_python = AssistantAgent(
name='sr_python_engineer',
system_message='''python engineer. You follow an approved plan. You write python/shell code to solve tasks.
Wrap the code in a code block that specifies the script type.
The user can't modify your code. So do not suggest incomplete code which requires others to modify.
Don't use a code block if it's not intended to be executed by the executor.
Don't include multiple code blocks in one response.
Do not ask others to copy and paste the result. Check the execution result returned by the executor.
If the result indicates there is an error, fix the error and output the code again. Suggest the full code instead of partial code or code changes.
If the error can't be fixed or if the task is not solved even after the code is executed successfully, analyze the problem, revisit your assumption,
collect additional info you need, and think of a different approach to try.
''',
llm_config=llm_config,
code_execution_config={"work_dir": "coding"},
)
sr_python.register_function( function_map={"write_file": write_file})
sr_dot_net = AssistantAgent(
name='csharp_engineer',
system_message='''Csharp Engineer. You follow an approved plan. You write csharp code to solve tasks.
Wrap the code in a code block that specifies the script type.
The user can't modify your code. So do not suggest incomplete code which requires others to modify.
Don't use a code block if it's not intended to be executed by the executor.
Don't include multiple code blocks in one response.
Do not ask others to copy and paste the result.
After each file you generate, write it down in the disk using the write_file method.
''',
#is_termination_msg=lambda x : x.get("content","").rstrip().endswith("TERMINATE"),
llm_config=llm_config,
code_execution_config={"work_dir": "coding"},
max_consecutive_auto_reply=2
)
sr_dot_net.register_function( function_map={"write_file": write_file})
sr_sql = AssistantAgent(
name='sql_engineer',
system_message='''Sql Engineer. You follow an approved plan. sql code to solve tasks.
Wrap the code in a code block that specifies the script type.
The user can't modify your code. So do not suggest incomplete code which requires others to modify.
Don't use a code block if it's not intended to be executed by the executor.
Don't include multiple code blocks in one response.
Do not ask others to copy and paste the result.
Unless specified, your code is for postgress database.
After each file you generate, write it down in the disk using the write_file method.
''',
llm_config=llm_config,
)
sr_sql.register_function( function_map={"write_file": write_file})
critic = AssistantAgent(
name="Critic",
system_message="Critic. Double check plan, claims, code from other agents and provide feedback. Check whether the plan includes adding verifiable info such as source URL.",
llm_config=llm_config,
)
groupchat = GroupChat(agents=[user_proxy, planner, sr_dot_net, sr_sql, critic], messages=[], max_round=30,)
manager = GroupChatManager(groupchat=groupchat, llm_config=llm_config)
task = """
Given the following class follow these steps:
Write an csharp controller for the crud methods.
Write an csharp service for the crud methods.
Write an csharp repository for the crud methods using entity framework.
Write unity tests for aiming to 100% code coverage using xunit, moq and bogus.
Write the SQL command to create the table in a postgres database.
The default namespace is BuscaCep.
The class:
```c#
//Cep.cs
public class Cep
{
[Key]
//max lenght 8
public string ZipCode { get ; set; }= null!;
public string TipoLogradouro { get; set; }= null!;
public string Logradouro { get; set; }= null!;
public string Complemento { get; set; }= null!;
public string Local { get; set; }= null!;
public string Bairro { get; set; }= null!;
public string Cidade { get; set; }= null!;
public string CodCidade { get; set; }= null!;
public string Uf { get; set; }= null!;
public string Estado { get; set; }= null!;
public string CodEstado { get; set; }= null!;
}
```
Planner, break down this plan to best execution. You have a Csharp engineer, a sql engineer and a critic that verifies what was done.
Please use the function write_file to write the files on disk and wait for the planner end his planning to continue.
"""
def write_file(file_name, file_content):
with open(file_name, "w") as file1:
file1.writelines(file_content)
try:
#proxy_agent.initiate_chat(assistant, message=task3)
user_proxy.initiate_chat(
manager,
message=task,
clear_history=True,
)
except Exception as ex:
print(50* '*')
print(ex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment