-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathagent.py
More file actions
406 lines (329 loc) · 14.2 KB
/
agent.py
File metadata and controls
406 lines (329 loc) · 14.2 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Copyright (c) Microsoft. All rights reserved.
import logging
import uuid
from abc import ABC, abstractmethod
from collections.abc import AsyncIterable, Awaitable, Callable, Iterable, Sequence
from typing import Any, ClassVar, Generic, TypeVar
from pydantic import Field, model_validator
from semantic_kernel.agents.channels.agent_channel import AgentChannel
from semantic_kernel.contents.chat_message_content import CMC_ITEM_TYPES, ChatMessageContent
from semantic_kernel.contents.streaming_chat_message_content import StreamingChatMessageContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.exceptions.agent_exceptions import AgentExecutionException
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.functions.kernel_plugin import KernelPlugin
from semantic_kernel.kernel import Kernel
from semantic_kernel.kernel_pydantic import KernelBaseModel
from semantic_kernel.prompt_template.kernel_prompt_template import KernelPromptTemplate
from semantic_kernel.prompt_template.prompt_template_base import PromptTemplateBase
from semantic_kernel.prompt_template.prompt_template_config import PromptTemplateConfig
from semantic_kernel.utils.feature_stage_decorator import release_candidate
from semantic_kernel.utils.naming import generate_random_ascii_name
from semantic_kernel.utils.validation import AGENT_NAME_REGEX
logger: logging.Logger = logging.getLogger(__name__)
TMessage = TypeVar("TMessage", bound=ChatMessageContent)
TThreadType = TypeVar("TThreadType", bound="AgentThread")
@release_candidate
class AgentThread(ABC):
"""Base class for agent threads."""
def __init__(self):
"""Initialize the agent thread."""
self._is_deleted: bool = False # type: ignore
self._id: str | None = None # type: ignore
@property
def id(self) -> str | None:
"""Returns the ID of the current thread (if any)."""
if self._is_deleted:
raise RuntimeError("Thread has been deleted; call `create()` to recreate it.")
return self._id
async def create(self) -> str | None:
"""Starts the thread and returns the thread ID."""
# A thread should not be recreated after it has been deleted.
if self._is_deleted:
raise RuntimeError("Cannot create thread because it has already been deleted.")
# If the thread ID is already set, we're done, just return the Id.
if self.id is not None:
return self.id
# Otherwise, create the thread.
self._id = await self._create()
return self.id
async def delete(self) -> None:
"""Ends the current thread."""
# A thread should not be deleted if it has already been deleted.
if self._is_deleted:
return
# If the thread ID is not set, we're done, just return.
if self.id is None:
self._is_deleted = True
return
# Otherwise, delete the thread.
await self._delete()
self._id = None
self._is_deleted = True
async def on_new_message(
self,
new_message: ChatMessageContent,
) -> None:
"""Invoked when a new message has been contributed to the chat by any participant."""
# If the thread is not created yet, create it.
if self.id is None:
await self.create()
await self._on_new_message(new_message)
@abstractmethod
async def _create(self) -> str:
"""Starts the thread and returns the thread ID."""
raise NotImplementedError
@abstractmethod
async def _delete(self) -> None:
"""Ends the current thread."""
raise NotImplementedError
@abstractmethod
async def _on_new_message(
self,
new_message: ChatMessageContent,
) -> None:
"""Invoked when a new message has been contributed to the chat by any participant."""
raise NotImplementedError
@release_candidate
class AgentResponseItem(KernelBaseModel, Generic[TMessage]):
"""Class representing a response item from an agent.
Attributes:
message: The message content of the response item.
thread: The conversation thread associated with the response item.
"""
message: TMessage
thread: AgentThread
@property
def content(self) -> TMessage:
"""Get the content of the response item."""
return self.message
@property
def items(self) -> list[CMC_ITEM_TYPES]:
"""Get the items of the response item."""
return self.message.items
@property
def metadata(self) -> dict[str, Any]:
"""Get the metadata of the response item."""
return self.message.metadata
@property
def name(self) -> str | None:
"""Get the name of the response item."""
return self.message.name
@property
def role(self) -> str | None:
"""Get the role of the response item."""
return self.message.role
def __str__(self):
"""Get the string representation of the response item."""
return str(self.content)
def __getattr__(self, item):
"""Get an attribute of the response item."""
return getattr(self.message, item)
def __hash__(self):
"""Get the hash of the response item."""
return hash((self.message, self.thread))
class Agent(KernelBaseModel, ABC):
"""Base abstraction for all Semantic Kernel agents.
An agent instance may participate in one or more conversations.
A conversation may include one or more agents.
In addition to identity and descriptive meta-data, an Agent
must define its communication protocol, or AgentChannel.
Attributes:
arguments: The arguments for the agent
channel_type: The type of the agent channel
description: The description of the agent
id: The unique identifier of the agent If no id is provided,
a new UUID will be generated.
instructions: The instructions for the agent (optional)
kernel: The kernel instance for the agent
name: The name of the agent
prompt_template: The prompt template for the agent
"""
arguments: KernelArguments | None = None
channel_type: ClassVar[type[AgentChannel] | None] = None
description: str | None = None
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
instructions: str | None = None
kernel: Kernel = Field(default_factory=Kernel)
name: str = Field(default_factory=lambda: f"agent_{generate_random_ascii_name()}", pattern=AGENT_NAME_REGEX)
prompt_template: PromptTemplateBase | None = None
@staticmethod
def _get_plugin_name(plugin: KernelPlugin | object) -> str:
"""Helper method to get the plugin name."""
if isinstance(plugin, KernelPlugin):
return plugin.name
return plugin.__class__.__name__
@model_validator(mode="before")
@classmethod
def _configure_plugins(cls, data: Any) -> Any:
"""Configure any plugins passed in."""
if isinstance(data, dict) and (plugins := data.pop("plugins", None)):
kernel = data.get("kernel", None)
if not kernel:
kernel = Kernel()
for plugin in plugins:
name = Agent._get_plugin_name(plugin)
kernel.add_plugin(plugin, plugin_name=name)
data["kernel"] = kernel
return data
@abstractmethod
def get_response(
self,
*,
messages: str | ChatMessageContent | list[str | ChatMessageContent],
thread: AgentThread | None = None,
**kwargs,
) -> Awaitable[AgentResponseItem[ChatMessageContent]]:
"""Get a response from the agent.
This method returns the final result of the agent's execution
as a single ChatMessageContent object. The caller is blocked until
the final result is available.
Note: For streaming responses, use the invoke_stream method, which returns
intermediate steps and the final result as a stream of StreamingChatMessageContent
objects. Streaming only the final result is not feasible because the timing of
the final result's availability is unknown, and blocking the caller until then
is undesirable in streaming scenarios.
Args:
messages: The message(s) to send to the agent.
thread: The conversation thread associated with the message(s).
kwargs: Additional keyword arguments.
Returns:
An agent response item.
"""
pass
@abstractmethod
def invoke(
self,
*,
messages: str | ChatMessageContent | list[str | ChatMessageContent],
thread: AgentThread | None = None,
**kwargs,
) -> AsyncIterable[AgentResponseItem[ChatMessageContent]]:
"""Invoke the agent.
This invocation method will return the intermediate steps and the final results
of the agent's execution as a stream of ChatMessageContent objects to the caller.
Note: A ChatMessageContent object contains an entire message.
Args:
messages: The message(s) to send to the agent.
thread: The conversation thread associated with the message(s).
kwargs: Additional keyword arguments.
Yields:
An agent response item.
"""
pass
@abstractmethod
def invoke_stream(
self,
*,
messages: str | ChatMessageContent | list[str | ChatMessageContent],
thread: AgentThread | None = None,
**kwargs,
) -> AsyncIterable[AgentResponseItem[StreamingChatMessageContent]]:
"""Invoke the agent as a stream.
This invocation method will return the intermediate steps and final results of the
agent's execution as a stream of StreamingChatMessageContent objects to the caller.
Note: A StreamingChatMessageContent object contains a chunk of a message.
Args:
messages: The message(s) to send to the agent.
thread: The conversation thread associated with the message(s).
kwargs: Additional keyword arguments.
Yields:
An agent response item.
"""
pass
def get_channel_keys(self) -> Iterable[str]:
"""Get the channel keys.
Returns:
A list of channel keys.
"""
if not self.channel_type:
raise NotImplementedError("Unable to get channel keys. Channel type not configured.")
yield self.channel_type.__name__
async def create_channel(self) -> AgentChannel:
"""Create a channel.
Returns:
An instance of AgentChannel.
"""
if not self.channel_type:
raise NotImplementedError("Unable to create channel. Channel type not configured.")
return self.channel_type()
async def format_instructions(self, kernel: Kernel, arguments: KernelArguments | None = None) -> str | None:
"""Format the instructions.
Args:
kernel: The kernel instance.
arguments: The kernel arguments.
Returns:
The formatted instructions.
"""
if self.prompt_template is None:
if self.instructions is None:
return None
self.prompt_template = KernelPromptTemplate(
prompt_template_config=PromptTemplateConfig(template=self.instructions)
)
return await self.prompt_template.render(kernel, arguments)
def _merge_arguments(self, override_args: KernelArguments | None) -> KernelArguments:
"""Merge the arguments with the override arguments.
Args:
override_args: The arguments to override.
Returns:
The merged arguments. If both are None, return None.
"""
if not self.arguments:
if not override_args:
return KernelArguments()
return override_args
if not override_args:
return self.arguments
# Both are not None, so merge with precedence for override_args.
merged_execution_settings = self.arguments.execution_settings or {}
if override_args.execution_settings:
merged_execution_settings.update(override_args.execution_settings)
merged_params = dict(self.arguments)
merged_params.update(override_args)
return KernelArguments(settings=merged_execution_settings, **merged_params)
async def _ensure_thread_exists_with_messages(
self,
messages: str | ChatMessageContent | Sequence[str | ChatMessageContent],
thread: AgentThread | None,
construct_thread: Callable[[], TThreadType],
expected_type: type[TThreadType],
) -> TThreadType:
"""Ensure the thread exists with the provided message(s)."""
if isinstance(messages, (str, ChatMessageContent)):
messages = [messages]
normalized_messages = [
ChatMessageContent(role=AuthorRole.USER, content=msg) if isinstance(msg, str) else msg for msg in messages
]
if thread is None:
thread = construct_thread()
await thread.create()
if not isinstance(thread, expected_type):
raise AgentExecutionException(
f"{self.__class__.__name__} currently only supports agent threads of type {expected_type.__name__}."
)
# Notify the thread that new messages are available.
for msg in normalized_messages:
await self._notify_thread_of_new_message(thread, msg)
return thread
async def _notify_thread_of_new_message(
self,
thread: AgentThread,
new_message: ChatMessageContent,
) -> None:
"""Notify the thread of a new message."""
await thread.on_new_message(new_message)
def __eq__(self, other):
"""Check if two agents are equal."""
if isinstance(other, Agent):
return (
self.id == other.id
and self.name == other.name
and self.description == other.description
and self.instructions == other.instructions
and self.channel_type == other.channel_type
)
return False
def __hash__(self):
"""Get the hash of the agent."""
return hash((self.id, self.name, self.description, self.instructions, self.channel_type))