1
1
import instructor
2
2
from pydantic import BaseModel , Field
3
3
from typing import Optional , Type , Generator , AsyncGenerator , get_args
4
- from atomic_agents .lib .components .agent_memory import AgentMemory
4
+ from atomic_agents .lib .components .chat_history import ChatHistory
5
5
from atomic_agents .lib .components .system_prompt_generator import (
6
6
SystemPromptContextProviderBase ,
7
7
SystemPromptGenerator ,
@@ -60,7 +60,7 @@ class BaseAgentOutputSchema(BaseIOSchema):
60
60
class BaseAgentConfig (BaseModel ):
61
61
client : instructor .client .Instructor = Field (..., description = "Client for interacting with the language model." )
62
62
model : str = Field (default = "gpt-4o-mini" , description = "The model to use for generating responses." )
63
- memory : Optional [AgentMemory ] = Field (default = None , description = "Memory component for storing chat history." )
63
+ history : Optional [ChatHistory ] = Field (default = None , description = "History component for storing chat history." )
64
64
system_prompt_generator : Optional [SystemPromptGenerator ] = Field (
65
65
default = None , description = "Component for generating system prompts."
66
66
)
@@ -75,7 +75,7 @@ class BaseAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:
75
75
"""
76
76
Base class for chat agents.
77
77
78
- This class provides the core functionality for handling chat interactions, including managing memory ,
78
+ This class provides the core functionality for handling chat interactions, including managing history ,
79
79
generating system prompts, and obtaining responses from a language model.
80
80
81
81
Type Parameters:
@@ -85,10 +85,10 @@ class BaseAgent[InputSchema: BaseIOSchema, OutputSchema: BaseIOSchema]:
85
85
Attributes:
86
86
client: Client for interacting with the language model.
87
87
model (str): The model to use for generating responses.
88
- memory (AgentMemory ): Memory component for storing chat history.
88
+ history (ChatHistory ): History component for storing chat history.
89
89
system_prompt_generator (SystemPromptGenerator): Component for generating system prompts.
90
90
system_role (Optional[str]): The role of the system in the conversation. None means no system prompt.
91
- initial_memory (AgentMemory ): Initial state of the memory .
91
+ initial_history (ChatHistory ): Initial state of the history .
92
92
current_user_input (Optional[InputSchema]): The current user input being processed.
93
93
model_api_parameters (dict): Additional parameters passed to the API provider.
94
94
- Use this for parameters like 'temperature', 'max_tokens', etc.
@@ -103,18 +103,18 @@ def __init__(self, config: BaseAgentConfig):
103
103
"""
104
104
self .client = config .client
105
105
self .model = config .model
106
- self .memory = config .memory or AgentMemory ()
106
+ self .history = config .history or ChatHistory ()
107
107
self .system_prompt_generator = config .system_prompt_generator or SystemPromptGenerator ()
108
108
self .system_role = config .system_role
109
- self .initial_memory = self .memory .copy ()
109
+ self .initial_history = self .history .copy ()
110
110
self .current_user_input = None
111
111
self .model_api_parameters = config .model_api_parameters or {}
112
112
113
- def reset_memory (self ):
113
+ def reset_history (self ):
114
114
"""
115
- Resets the memory to its initial state.
115
+ Resets the history to its initial state.
116
116
"""
117
- self .memory = self .initial_memory .copy ()
117
+ self .history = self .initial_history .copy ()
118
118
119
119
@property
120
120
def input_schema (self ) -> Type [BaseIOSchema ]:
@@ -145,14 +145,14 @@ def _prepare_messages(self):
145
145
}
146
146
]
147
147
148
- self .messages += self .memory .get_history ()
148
+ self .messages += self .history .get_history ()
149
149
150
150
def run (self , user_input : Optional [InputSchema ] = None ) -> OutputSchema :
151
151
"""
152
152
Runs the chat agent with the given user input synchronously.
153
153
154
154
Args:
155
- user_input (Optional[InputSchema]): The input from the user. If not provided, skips adding to memory .
155
+ user_input (Optional[InputSchema]): The input from the user. If not provided, skips adding to history .
156
156
157
157
Returns:
158
158
OutputSchema: The response from the chat agent.
@@ -161,9 +161,9 @@ def run(self, user_input: Optional[InputSchema] = None) -> OutputSchema:
161
161
self .client , instructor .client .AsyncInstructor
162
162
), "The run method is not supported for async clients. Use run_async instead."
163
163
if user_input :
164
- self .memory .initialize_turn ()
164
+ self .history .initialize_turn ()
165
165
self .current_user_input = user_input
166
- self .memory .add_message ("user" , user_input )
166
+ self .history .add_message ("user" , user_input )
167
167
168
168
self ._prepare_messages ()
169
169
response = self .client .chat .completions .create (
@@ -172,7 +172,7 @@ def run(self, user_input: Optional[InputSchema] = None) -> OutputSchema:
172
172
response_model = self .output_schema ,
173
173
** self .model_api_parameters ,
174
174
)
175
- self .memory .add_message ("assistant" , response )
175
+ self .history .add_message ("assistant" , response )
176
176
177
177
return response
178
178
@@ -181,7 +181,7 @@ def run_stream(self, user_input: Optional[InputSchema] = None) -> Generator[Outp
181
181
Runs the chat agent with the given user input, supporting streaming output.
182
182
183
183
Args:
184
- user_input (Optional[InputSchema]): The input from the user. If not provided, skips adding to memory .
184
+ user_input (Optional[InputSchema]): The input from the user. If not provided, skips adding to history .
185
185
186
186
Yields:
187
187
OutputSchema: Partial responses from the chat agent.
@@ -193,9 +193,9 @@ def run_stream(self, user_input: Optional[InputSchema] = None) -> Generator[Outp
193
193
self .client , instructor .client .AsyncInstructor
194
194
), "The run_stream method is not supported for async clients. Use run_async instead."
195
195
if user_input :
196
- self .memory .initialize_turn ()
196
+ self .history .initialize_turn ()
197
197
self .current_user_input = user_input
198
- self .memory .add_message ("user" , user_input )
198
+ self .history .add_message ("user" , user_input )
199
199
200
200
self ._prepare_messages ()
201
201
@@ -211,7 +211,7 @@ def run_stream(self, user_input: Optional[InputSchema] = None) -> Generator[Outp
211
211
yield partial_response
212
212
213
213
full_response_content = self .output_schema (** partial_response .model_dump ())
214
- self .memory .add_message ("assistant" , full_response_content )
214
+ self .history .add_message ("assistant" , full_response_content )
215
215
216
216
return full_response_content
217
217
@@ -220,7 +220,7 @@ async def run_async(self, user_input: Optional[InputSchema] = None) -> OutputSch
220
220
Runs the chat agent asynchronously with the given user input.
221
221
222
222
Args:
223
- user_input (Optional[InputSchema]): The input from the user. If not provided, skips adding to memory .
223
+ user_input (Optional[InputSchema]): The input from the user. If not provided, skips adding to history .
224
224
225
225
Returns:
226
226
OutputSchema: The response from the chat agent.
@@ -231,34 +231,34 @@ async def run_async(self, user_input: Optional[InputSchema] = None) -> OutputSch
231
231
"""
232
232
assert isinstance (self .client , instructor .client .AsyncInstructor ), "The run_async method is for async clients."
233
233
if user_input :
234
- self .memory .initialize_turn ()
234
+ self .history .initialize_turn ()
235
235
self .current_user_input = user_input
236
- self .memory .add_message ("user" , user_input )
236
+ self .history .add_message ("user" , user_input )
237
237
238
238
self ._prepare_messages ()
239
239
240
240
response = await self .client .chat .completions .create (
241
241
model = self .model , messages = self .messages , response_model = self .output_schema , ** self .model_api_parameters
242
242
)
243
243
244
- self .memory .add_message ("assistant" , response )
244
+ self .history .add_message ("assistant" , response )
245
245
return response
246
246
247
247
async def run_async_stream (self , user_input : Optional [InputSchema ] = None ) -> AsyncGenerator [OutputSchema , None ]:
248
248
"""
249
249
Runs the chat agent asynchronously with the given user input, supporting streaming output.
250
250
251
251
Args:
252
- user_input (Optional[InputSchema]): The input from the user. If not provided, skips adding to memory .
252
+ user_input (Optional[InputSchema]): The input from the user. If not provided, skips adding to history .
253
253
254
254
Yields:
255
255
OutputSchema: Partial responses from the chat agent.
256
256
"""
257
257
assert isinstance (self .client , instructor .client .AsyncInstructor ), "The run_async method is for async clients."
258
258
if user_input :
259
- self .memory .initialize_turn ()
259
+ self .history .initialize_turn ()
260
260
self .current_user_input = user_input
261
- self .memory .add_message ("user" , user_input )
261
+ self .history .add_message ("user" , user_input )
262
262
263
263
self ._prepare_messages ()
264
264
@@ -277,7 +277,7 @@ async def run_async_stream(self, user_input: Optional[InputSchema] = None) -> As
277
277
278
278
if last_response :
279
279
full_response_content = self .output_schema (** last_response .model_dump ())
280
- self .memory .add_message ("assistant" , full_response_content )
280
+ self .history .add_message ("assistant" , full_response_content )
281
281
282
282
def get_context_provider (self , provider_name : str ) -> Type [SystemPromptContextProviderBase ]:
283
283
"""
@@ -365,7 +365,7 @@ def _create_config_table(agent: BaseAgent) -> Table:
365
365
info_table .add_column ("Value" , style = "yellow" )
366
366
367
367
info_table .add_row ("Model" , agent .model )
368
- info_table .add_row ("Memory " , str (type (agent .memory ).__name__ ))
368
+ info_table .add_row ("History " , str (type (agent .history ).__name__ ))
369
369
info_table .add_row ("System Prompt Generator" , str (type (agent .system_prompt_generator ).__name__ ))
370
370
371
371
return info_table
0 commit comments