Untitled

mail@pastecode.io avatar
unknown
python
a year ago
5.0 kB
3
Indexable
Never
import autogen
import json

# I am using docker in this example.

config_list = [
    {
        "model": "gpt-4-32k-0613",
        "api_key": "hypr-lab-3vZKGr34cV1ZMdlwr2PsT3BlbkFbewbzrekNFneBQNRDf",
        "api_base": "https://api.hyprlab.io/v1"
    }
]

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

# This agent plans the next steps
planner = autogen.AssistantAgent(
    name="Planner",
    system_message='''Planner. Suggest a plan. Revise the plan based on feedback from admin, until admin approval.
The plan may involve the Coder who can write code, an User_proxy that can execute the code and a Sales_Analysist .
Explain the plan first. Be clear which step is performed by the Coder, and which step is performed by the User_proxy.
''',
    llm_config=llm_config,
)
# Human user input to confirm the plan
admin = autogen.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.",
   code_execution_config=False,
)
# coding assistant that writes the code
coder = autogen.AssistantAgent(
    name="Coder",
    llm_config=llm_config,
        system_message="""If you want the User_proxy to save the code in a file before executing it, put # filename: <filename> inside the code block as the first line.
        You write python code to solve tasks. Wrap the code in a code block that specifies the script type. The User_proxy can't modify your code. So do not suggest incomplete code which requires others to modify, if you need information, ask Admin for the information before sending the code to user_proxy. Don't use a code block if it's not intended to be executed by the user_proxy.
        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 user_proxy.
        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.
        """,
)
# code execution
# you will be prompted to provide feedback after the assistant agent sends a "TERMINATE" signal in the end of the message. If you don't provide any feedback (by pressing Enter directly), the conversation will finish. Before the "TERMINATE" signal, the User_proxy agent will try to execute the code suggested by the assistant agent on behalf of the user.
user_proxy = autogen.UserProxyAgent(
    name="User_proxy",
    human_input_mode="TERMINATE",
    max_consecutive_auto_reply=10,
    is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
    # Make sure to set use_docker to True if you want to use docker. if not specified, it will use the default value True.
    code_execution_config={"last_n_messages": 2, "work_dir": "PD", "use_docker":False},
    llm_config=llm_config,
    system_message="""Reply TERMINATE if the task has been solved at full satisfaction.
Otherwise, reply CONTINUE, or the reason why the task is not solved yet.
""",
)
#Sales_Analysist
sales_analysist = autogen.AssistantAgent(
    name="Sales_Analysist",
    system_message='''Sales Analyst. Review the json output and analyse the key details that can be used in automation to identify which customers to target or service first.
Provide reasoning for your decisions. Once completed, and after the Admin has signed off on it, the User_proxy will create the file json file with your analysist.
''',
    llm_config=llm_config,
)

#initialise groupchat/manager
groupchat = autogen.GroupChat(agents=[planner, admin, coder, user_proxy, sales_analysist], messages=[], max_round=30)
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config=llm_config)

# the purpose of the following line is to log the conversation history
autogen.ChatCompletion.start_logging()

#start a loop for a chat
first_message = True
while True:
    if first_message:
        user_input = input("Enter your first message: ")
        user_proxy.initiate_chat( #initiate chat wipes the conversational memory and starts again
            manager,
            message=f"""{user_input}""",
        )
        first_message = False

    else:
        user_input = input("Enter your next message: ")
        user_proxy.send( #'send' continues on from the current conversation
            recipient=manager,
            message=f"""{user_input}""",
        )
    
    # Save the conversation history to conversations.json
    with open("conversations.json", "w") as f:
        json.dump(autogen.ChatCompletion.logged_history, f, indent=4)