|
from autogpt.llm_utils import create_chat_completion |
|
|
|
next_key = 0 |
|
agents = {} |
|
|
|
|
|
|
|
|
|
|
|
def create_agent(task, prompt, model): |
|
"""Create a new agent and return its key""" |
|
global next_key |
|
global agents |
|
|
|
messages = [ |
|
{"role": "user", "content": prompt}, |
|
] |
|
|
|
|
|
agent_reply = create_chat_completion( |
|
model=model, |
|
messages=messages, |
|
) |
|
|
|
|
|
messages.append({"role": "assistant", "content": agent_reply}) |
|
|
|
key = next_key |
|
|
|
|
|
next_key += 1 |
|
|
|
agents[key] = (task, messages, model) |
|
|
|
return key, agent_reply |
|
|
|
|
|
def message_agent(key, message): |
|
"""Send a message to an agent and return its response""" |
|
global agents |
|
|
|
task, messages, model = agents[int(key)] |
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
agent_reply = create_chat_completion( |
|
model=model, |
|
messages=messages, |
|
) |
|
|
|
|
|
messages.append({"role": "assistant", "content": agent_reply}) |
|
|
|
return agent_reply |
|
|
|
|
|
def list_agents(): |
|
"""Return a list of all agents""" |
|
global agents |
|
|
|
|
|
return [(key, task) for key, (task, _, _) in agents.items()] |
|
|
|
|
|
def delete_agent(key): |
|
"""Delete an agent and return True if successful, False otherwise""" |
|
global agents |
|
|
|
try: |
|
del agents[int(key)] |
|
return True |
|
except KeyError: |
|
return False |
|
|