-
Notifications
You must be signed in to change notification settings - Fork 834
Expand file tree
/
Copy pathmessages_kernel.py
More file actions
471 lines (361 loc) · 13.5 KB
/
Copy pathmessages_kernel.py
File metadata and controls
471 lines (361 loc) · 13.5 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import uuid
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Dict, List, Literal, Optional
from semantic_kernel.kernel_pydantic import Field, KernelBaseModel
# Classes specifically for handling runtime interrupts
class GetHumanInputMessage(KernelBaseModel):
"""Message requesting input from a human."""
content: str
class GroupChatMessage(KernelBaseModel):
"""Message in a group chat."""
body: Any
source: str
session_id: str
target: str = ""
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
def __str__(self):
content = self.body.content if hasattr(self.body, "content") else str(self.body)
return f"GroupChatMessage(source={self.source}, content={content})"
class DataType(str, Enum):
"""Enumeration of possible data types for documents in the database."""
session = "session"
plan = "plan"
step = "step"
message = "message"
class AgentType(str, Enum):
"""Enumeration of agent types."""
HUMAN = "Human_Agent"
HR = "Hr_Agent"
MARKETING = "Marketing_Agent"
PROCUREMENT = "Procurement_Agent"
PRODUCT = "Product_Agent"
GENERIC = "Generic_Agent"
TECH_SUPPORT = "Tech_Support_Agent"
GROUP_CHAT_MANAGER = "Group_Chat_Manager"
PLANNER = "Planner_Agent"
# Add other agents as needed
class StepStatus(str, Enum):
"""Enumeration of possible statuses for a step."""
planned = "planned"
awaiting_feedback = "awaiting_feedback"
approved = "approved"
rejected = "rejected"
action_requested = "action_requested"
completed = "completed"
failed = "failed"
class PlanStatus(str, Enum):
"""Enumeration of possible statuses for a plan."""
in_progress = "in_progress"
completed = "completed"
failed = "failed"
class HumanFeedbackStatus(str, Enum):
"""Enumeration of human feedback statuses."""
requested = "requested"
accepted = "accepted"
rejected = "rejected"
class MessageRole(str, Enum):
"""Message roles compatible with Semantic Kernel."""
system = "system"
user = "user"
assistant = "assistant"
function = "function"
class BaseDataModel(KernelBaseModel):
"""Base data model with common fields."""
id: str = Field(default_factory=lambda: str(uuid.uuid4()))
timestamp: Optional[datetime] = Field(default_factory=lambda: datetime.now(timezone.utc))
# Basic message class for Semantic Kernel compatibility
class ChatMessage(KernelBaseModel):
"""Base class for chat messages in Semantic Kernel format."""
role: MessageRole
content: str
metadata: Dict[str, Any] = Field(default_factory=dict)
def to_semantic_kernel_dict(self) -> Dict[str, Any]:
"""Convert to format expected by Semantic Kernel."""
return {
"role": self.role.value,
"content": self.content,
"metadata": self.metadata,
}
class StoredMessage(BaseDataModel):
"""Message stored in the database with additional metadata."""
data_type: Literal["message"] = Field("message", Literal=True)
session_id: str
user_id: str
role: MessageRole
content: str
plan_id: Optional[str] = None
step_id: Optional[str] = None
source: Optional[str] = None
metadata: Dict[str, Any] = Field(default_factory=dict)
def to_chat_message(self) -> ChatMessage:
"""Convert to ChatMessage format."""
return ChatMessage(
role=self.role,
content=self.content,
metadata={
"source": self.source,
"plan_id": self.plan_id,
"step_id": self.step_id,
"session_id": self.session_id,
"user_id": self.user_id,
"message_id": self.id,
**self.metadata,
},
)
class AgentMessage(BaseDataModel):
"""Base class for messages sent between agents."""
data_type: Literal["agent_message"] = Field("agent_message", Literal=True)
session_id: str
user_id: str
plan_id: str
content: str
source: str
step_id: Optional[str] = None
class Session(BaseDataModel):
"""Represents a user session."""
data_type: Literal["session"] = Field("session", Literal=True)
user_id: str
current_status: str
message_to_user: Optional[str] = None
class Plan(BaseDataModel):
"""Represents a plan containing multiple steps."""
data_type: Literal["plan"] = Field("plan", Literal=True)
session_id: str
user_id: str
initial_goal: str
overall_status: PlanStatus = PlanStatus.in_progress
source: str = AgentType.PLANNER.value
summary: Optional[str] = None
human_clarification_request: Optional[str] = None
human_clarification_response: Optional[str] = None
class Step(BaseDataModel):
"""Represents an individual step (task) within a plan."""
data_type: Literal["step"] = Field("step", Literal=True)
plan_id: str
session_id: str # Partition key
user_id: str
action: str
agent: AgentType
status: StepStatus = StepStatus.planned
agent_reply: Optional[str] = None
human_feedback: Optional[str] = None
human_approval_status: Optional[HumanFeedbackStatus] = HumanFeedbackStatus.requested
updated_action: Optional[str] = None
class ThreadIdAgent(BaseDataModel):
"""Represents an individual thread_id."""
data_type: Literal["thread"] = Field("thread", Literal=True)
session_id: str # Partition key
user_id: str
thread_id: str
class AzureIdAgent(BaseDataModel):
"""Represents an individual thread_id."""
data_type: Literal["agent"] = Field("agent", Literal=True)
session_id: str # Partition key
user_id: str
action: str
agent: AgentType
agent_id: str
class PlanWithSteps(Plan):
"""Plan model that includes the associated steps."""
steps: List[Step] = Field(default_factory=list)
total_steps: int = 0
planned: int = 0
awaiting_feedback: int = 0
approved: int = 0
rejected: int = 0
action_requested: int = 0
completed: int = 0
failed: int = 0
def update_step_counts(self):
"""Update the counts of steps by their status."""
status_counts = {
StepStatus.planned: 0,
StepStatus.awaiting_feedback: 0,
StepStatus.approved: 0,
StepStatus.rejected: 0,
StepStatus.action_requested: 0,
StepStatus.completed: 0,
StepStatus.failed: 0,
}
for step in self.steps:
status_counts[step.status] += 1
self.total_steps = len(self.steps)
self.planned = status_counts[StepStatus.planned]
self.awaiting_feedback = status_counts[StepStatus.awaiting_feedback]
self.approved = status_counts[StepStatus.approved]
self.rejected = status_counts[StepStatus.rejected]
self.action_requested = status_counts[StepStatus.action_requested]
self.completed = status_counts[StepStatus.completed]
self.failed = status_counts[StepStatus.failed]
# Mark the plan as complete if the sum of completed and failed steps equals the total number of steps
if self.completed + self.failed == self.total_steps:
self.overall_status = PlanStatus.completed
# Message classes for communication between agents
class InputTask(KernelBaseModel):
"""Message representing the initial input task from the user."""
session_id: str
description: str # Initial goal
class UserLanguage(KernelBaseModel):
language: str
class ApprovalRequest(KernelBaseModel):
"""Message sent to HumanAgent to request approval for a step."""
step_id: str
plan_id: str
session_id: str
user_id: str
action: str
agent: AgentType
class HumanFeedback(KernelBaseModel):
"""Message containing human feedback on a step."""
step_id: Optional[str] = None
plan_id: str
session_id: str
approved: bool
human_feedback: Optional[str] = None
updated_action: Optional[str] = None
class HumanClarification(KernelBaseModel):
"""Message containing human clarification on a plan."""
plan_id: str
session_id: str
human_clarification: str
class ActionRequest(KernelBaseModel):
"""Message sent to an agent to perform an action."""
step_id: str
plan_id: str
session_id: str
action: str
agent: AgentType
class ActionResponse(KernelBaseModel):
"""Message containing the response from an agent after performing an action."""
step_id: str
plan_id: str
session_id: str
result: str
status: StepStatus # Should be 'completed' or 'failed'
class PlanStateUpdate(KernelBaseModel):
"""Optional message for updating the plan state."""
plan_id: str
session_id: str
overall_status: PlanStatus
# Semantic Kernel chat message handler
class SKChatHistory:
"""Helper class to work with Semantic Kernel chat history."""
def __init__(self, memory_store):
"""Initialize with a memory store."""
self.memory_store = memory_store
async def add_system_message(
self, session_id: str, user_id: str, content: str, **kwargs
):
"""Add a system message to the chat history."""
message = StoredMessage(
session_id=session_id,
user_id=user_id,
role=MessageRole.system,
content=content,
**kwargs,
)
await self._store_message(message)
return message
async def add_user_message(
self, session_id: str, user_id: str, content: str, **kwargs
):
"""Add a user message to the chat history."""
message = StoredMessage(
session_id=session_id,
user_id=user_id,
role=MessageRole.user,
content=content,
**kwargs,
)
await self._store_message(message)
return message
async def add_assistant_message(
self, session_id: str, user_id: str, content: str, **kwargs
):
"""Add an assistant message to the chat history."""
message = StoredMessage(
session_id=session_id,
user_id=user_id,
role=MessageRole.assistant,
content=content,
**kwargs,
)
await self._store_message(message)
return message
async def add_function_message(
self, session_id: str, user_id: str, content: str, **kwargs
):
"""Add a function result message to the chat history."""
message = StoredMessage(
session_id=session_id,
user_id=user_id,
role=MessageRole.function,
content=content,
**kwargs,
)
await self._store_message(message)
return message
async def _store_message(self, message: StoredMessage):
"""Store a message in the memory store."""
# Convert to dictionary for storage
message_dict = message.model_dump()
# Use memory store to save the message
# This assumes your memory store has an upsert_async method that takes a collection name and data
await self.memory_store.upsert_async(
f"message_{message.session_id}", message_dict
)
async def get_chat_history(
self, session_id: str, limit: int = 100
) -> List[ChatMessage]:
"""Retrieve chat history for a session."""
# Query messages from the memory store
# This assumes your memory store has a method to query items
messages = await self.memory_store.query_items(
f"message_{session_id}", limit=limit
)
# Convert to ChatMessage objects
chat_messages = []
for msg_dict in messages:
msg = StoredMessage.model_validate(msg_dict)
chat_messages.append(msg.to_chat_message())
return chat_messages
async def clear_history(self, session_id: str):
"""Clear chat history for a session."""
# This assumes your memory store has a method to delete a collection
await self.memory_store.delete_collection_async(f"message_{session_id}")
# Define the expected structure of the LLM response
class PlannerResponseStep(KernelBaseModel):
action: str
agent: AgentType
class PlannerResponsePlan(KernelBaseModel):
initial_goal: str
steps: List[PlannerResponseStep]
summary_plan_and_steps: str
human_clarification_request: Optional[str] = None
# Helper class for Semantic Kernel function calling
class SKFunctionRegistry:
"""Helper class to register and execute functions in Semantic Kernel."""
def __init__(self, kernel):
"""Initialize with a Semantic Kernel instance."""
self.kernel = kernel
self.functions = {}
def register_function(self, name: str, function_obj, description: str = None):
"""Register a function with the kernel."""
self.functions[name] = {
"function": function_obj,
"description": description or "",
}
# Register with the kernel's function registry
# The exact implementation depends on Semantic Kernel's API
# This is a placeholder - adjust according to the actual SK API
if hasattr(self.kernel, "register_function"):
self.kernel.register_function(name, function_obj, description)
async def execute_function(self, name: str, **kwargs):
"""Execute a registered function."""
if name not in self.functions:
raise ValueError(f"Function {name} not registered")
function_obj = self.functions[name]["function"]
# Execute the function
# This might vary based on SK's execution model
return await function_obj(**kwargs)