forked from Significant-Gravitas/AutoGPT
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathagent_manager.py
75 lines (52 loc) · 1.74 KB
/
agent_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from autogpt.llm_utils import create_chat_completion
next_key = 0
agents = {} # key, (task, full_message_history, model)
# Create new GPT agent
# TODO: Centralise use of create_chat_completion() to globally enforce token limit
def create_agent(task, prompt, model):
"""创建新代理并返回其密钥"""
global next_key
global agents
messages = [
{"role": "user", "content": prompt},
]
# Start GPT instance
agent_reply = create_chat_completion(
model=model,
messages=messages,
)
# Update full message history
messages.append({"role": "assistant", "content": agent_reply})
key = next_key
# This is done instead of len(agents) to make keys unique even if agents
# are deleted
next_key += 1
agents[key] = (task, messages, model)
return key, agent_reply
def message_agent(key, message):
"""向代理发送消息并返回其响应"""
global agents
task, messages, model = agents[int(key)]
# Add user message to message history before sending to agent
messages.append({"role": "user", "content": message})
# Start GPT instance
agent_reply = create_chat_completion(
model=model,
messages=messages,
)
# Update full message history
messages.append({"role": "assistant", "content": agent_reply})
return agent_reply
def list_agents():
"""返回所有代理的列表"""
global agents
# Return a list of agent keys and their tasks
return [(key, task) for key, (task, _, _) in agents.items()]
def delete_agent(key):
"""删除代理,如果成功则返回True,否则返回False"""
global agents
try:
del agents[int(key)]
return True
except KeyError:
return False