1
1
"""Agent manager for managing GPT agents"""
2
2
from __future__ import annotations
3
3
4
- from typing import List
5
-
6
4
from autogpt .config .config import Config
7
5
from autogpt .llm_utils import create_chat_completion
8
6
from autogpt .singleton import Singleton
9
- from autogpt .types .openai import Message
7
+ from autogpt .types .openai import Message , ensure_messages
10
8
11
9
12
10
class AgentManager (metaclass = Singleton ):
@@ -20,7 +18,7 @@ def __init__(self):
20
18
# Create new GPT agent
21
19
# TODO: Centralise use of create_chat_completion() to globally enforce token limit
22
20
23
- def handle_preinstruction (self , messages : List [Message ]) -> List [Message ]:
21
+ def handle_preinstruction (self , messages : list [Message ]) -> list [Message ]:
24
22
"""Handle pre-instruction plugins
25
23
26
24
Args:
@@ -34,7 +32,7 @@ def handle_preinstruction(self, messages: List[Message]) -> List[Message]:
34
32
continue
35
33
if plugin_messages := plugin .pre_instruction (messages ):
36
34
messages .extend (iter (plugin_messages ))
37
- return messages
35
+ return ensure_messages ( messages )
38
36
39
37
def handle_postinstruction (self , agent_reply ) -> str :
40
38
"""Handle post-instruction plugins
@@ -52,7 +50,7 @@ def handle_postinstruction(self, agent_reply) -> str:
52
50
agent_reply = plugin .post_instruction (agent_reply )
53
51
return agent_reply
54
52
55
- def handle_oninstruction (self , messages : List [Message ]) -> List [Message ]:
53
+ def handle_oninstruction (self , messages : list [Message ]) -> list [Message ]:
56
54
"""Handle on-instruction plugins
57
55
58
56
Args:
@@ -70,7 +68,7 @@ def handle_oninstruction(self, messages: List[Message]) -> List[Message]:
70
68
plugins_reply = f"{ plugins_reply } { sep } { plugin_result } "
71
69
72
70
if plugins_reply and plugins_reply != "" :
73
- messages .append ({ "role" : " assistant" , "content" : plugins_reply } )
71
+ messages .append (Message ( " assistant" , plugins_reply ) )
74
72
return messages
75
73
76
74
def create_agent (self , task : str , prompt : str , model : str ) -> tuple [int , str ]:
@@ -84,19 +82,27 @@ def create_agent(self, task: str, prompt: str, model: str) -> tuple[int, str]:
84
82
Returns:
85
83
The key of the new agent
86
84
"""
87
- messages : List [Message ] = [
88
- {"role" : "system" , "content" : "You are an autonomous AI agent. You can ask me to do things. I will try my best to do them." },
89
- {"role" : "user" , "content" : prompt },
85
+ messages : list [Message ] = [
86
+ Message (
87
+ "system" ,
88
+ "You are an autonomous AI agent. You can ask me to do things."
89
+ " I will try my best to do them." ,
90
+ ),
91
+ Message ("user" , prompt ),
90
92
]
91
93
messages = self .handle_preinstruction (messages )
92
- token_limit = self .cfg .fast_token_limit if not self .cfg .use_fastchat else self .cfg .fastchat_token_limit
94
+ token_limit = (
95
+ self .cfg .fast_token_limit
96
+ if not self .cfg .use_fastchat
97
+ else self .cfg .fastchat_token_limit
98
+ )
93
99
agent_reply = create_chat_completion (
94
100
model = model ,
95
101
messages = messages ,
96
102
max_tokens = token_limit ,
97
103
use_fastchat = self .cfg .use_fastchat ,
98
104
)
99
- messages .append ({ "role" : " assistant" , "content" : agent_reply } )
105
+ messages .append (Message ( " assistant" , agent_reply ) )
100
106
messages = self .handle_oninstruction (messages )
101
107
key = self .next_key
102
108
self .next_key += 1
@@ -114,16 +120,21 @@ def message_agent(self, key: str | int, message: str) -> str:
114
120
The agent's response
115
121
"""
116
122
task , messages , model = self .agents [int (key )]
117
- messages .append ({ "role" : " user" , "content" : message } )
123
+ messages .append (Message ( " user" , message ) )
118
124
messages = self .handle_preinstruction (messages )
119
- token_limit = self .cfg .fast_token_limit if not self .cfg .use_fastchat else self .cfg .fastchat_token_limit
125
+
126
+ token_limit = (
127
+ self .cfg .fast_token_limit
128
+ if not self .cfg .use_fastchat
129
+ else self .cfg .fastchat_token_limit
130
+ )
120
131
agent_reply = create_chat_completion (
121
132
model = model ,
122
133
messages = messages ,
123
134
max_tokens = token_limit ,
124
135
use_fastchat = self .cfg .use_fastchat ,
125
136
)
126
- messages .append ({ "role" : " assistant" , "content" : agent_reply } )
137
+ messages .append (Message ( " assistant" , agent_reply ) )
127
138
messages = self .handle_oninstruction (messages )
128
139
return self .handle_postinstruction (agent_reply )
129
140
0 commit comments