-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresponse_middleware.py
More file actions
234 lines (204 loc) · 7.93 KB
/
response_middleware.py
File metadata and controls
234 lines (204 loc) · 7.93 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
from __future__ import annotations
import logging
from typing import Any
from uuid import uuid4
from src.core.common.exceptions import LoopDetectionError
from src.core.interfaces.loop_detector_interface import ILoopDetector
from src.core.interfaces.response_processor_interface import (
IResponseMiddleware,
ProcessedResponse,
)
logger = logging.getLogger(__name__)
class ResponseLoggingMiddleware(IResponseMiddleware):
"""Middleware to log response details (part of response processing pipeline)."""
async def process(
self,
response: Any,
session_id: str,
context: dict[str, Any],
is_streaming: bool = False,
stop_event: Any = None,
) -> Any:
"""Process a response, logging information as needed."""
if logger.isEnabledFor(logging.DEBUG):
response_type = (
context.get("response_type", "unknown") if context else "unknown"
)
if isinstance(response, dict):
raw_content = response.get("content")
usage_info = response.get("usage", {}) or {}
else:
raw_content = getattr(response, "content", None)
usage_info = getattr(response, "usage", {}) or {}
try:
content_length = len(raw_content) if raw_content else 0
except TypeError:
content_length = 0
logger.debug(
"Response processed for session %s (%s): content_len=%s, usage=%s",
session_id,
response_type,
content_length,
usage_info,
)
return response
class ContentFilterMiddleware(IResponseMiddleware):
"""Middleware to filter response content."""
async def process(
self,
response: Any,
session_id: str,
context: dict[str, Any],
is_streaming: bool = False,
stop_event: Any = None,
) -> Any:
"""Process a response, filtering content as needed."""
prefix = "I'll help you with that. "
if isinstance(response, dict):
content = response.get("content")
if not isinstance(content, str) or not content:
return response
if not content.startswith(prefix):
return response
filtered_content = content.replace(prefix, "", 1)
updated_response = response.copy()
updated_response["content"] = filtered_content
return updated_response
content = getattr(response, "content", None)
if not isinstance(content, str) or not content:
return response
if not content.startswith(prefix):
return response
filtered_content = content.replace(prefix, "", 1)
try:
response.content = filtered_content
return response
except AttributeError:
usage = getattr(response, "usage", None)
metadata = getattr(response, "metadata", None)
return ProcessedResponse(
content=filtered_content,
usage=usage,
metadata=metadata,
)
class LoopDetectionMiddleware(IResponseMiddleware):
"""Middleware to detect response loops."""
def __init__(self, loop_detector: ILoopDetector, priority: int = 0) -> None:
self._loop_detector = loop_detector
self._accumulated_content: dict[str, str] = {}
self._priority = priority
self._anonymous_session_aliases: dict[int, str] = {}
@property
def priority(self) -> int:
return self._priority
def _resolve_session_key(
self,
session_id: str,
context: dict[str, Any] | None,
response: Any,
stop_event: Any,
) -> tuple[str, bool]:
candidate_fields = (
"session_id",
"stream_id",
"id",
"request_id",
"conversation_id",
"thread_id",
"message_id",
)
if session_id:
normalized = str(session_id).strip()
if normalized:
return normalized, False
sources: list[dict[str, Any]] = []
if isinstance(context, dict):
sources.append(context)
metadata = getattr(response, "metadata", None)
if isinstance(metadata, dict):
sources.append(metadata)
for source in sources:
for field in candidate_fields:
try:
value = source.get(field) # type: ignore[call-arg]
except AttributeError:
continue
if value is None:
continue
candidate = str(value).strip()
if candidate:
return candidate, False
if stop_event is not None:
alias = self._anonymous_session_aliases.get(id(stop_event))
if alias is None:
alias = uuid4().hex
self._anonymous_session_aliases[id(stop_event)] = alias
return alias, False
return uuid4().hex, True
def _cleanup_session_state(
self,
resolved_session_id: str,
ephemeral_key: bool,
stop_event: Any,
) -> None:
if ephemeral_key:
self._accumulated_content.pop(resolved_session_id, None)
return
if stop_event is None:
return
alias_id = id(stop_event)
alias_value = self._anonymous_session_aliases.get(alias_id)
try:
is_done = bool(stop_event.is_set()) # type: ignore[attr-defined]
except AttributeError:
is_done = False
if is_done:
if alias_value is not None:
self._accumulated_content.pop(alias_value, None)
self._anonymous_session_aliases.pop(alias_id, None)
async def process(
self,
response: Any,
session_id: str,
context: dict[str, Any],
is_streaming: bool = False,
stop_event: Any = None,
) -> Any:
"""Process a response, checking for loops."""
resolved_session_id, ephemeral_key = self._resolve_session_key(
session_id, context, response, stop_event
)
try:
if not response.content:
return response
previous = self._accumulated_content.get(resolved_session_id, "")
self._accumulated_content[resolved_session_id] = previous + response.content
content = self._accumulated_content[resolved_session_id]
if len(content) > 100:
loop_result = await self._loop_detector.check_for_loops(content)
if loop_result.has_loop:
error_message = (
"Loop detected: The response contains repetitive content. "
f"Detected {loop_result.repetitions} repetitions."
)
logger.warning(
f"Loop detected in session {resolved_session_id}: {loop_result.repetitions} repetitions"
)
raise LoopDetectionError(
message=error_message,
details={
"repetitions": loop_result.repetitions,
"pattern": loop_result.pattern,
"session_id": resolved_session_id,
},
)
return response
finally:
self._cleanup_session_state(resolved_session_id, ephemeral_key, stop_event)
def reset_session(self, session_id: str) -> None:
"""Reset the accumulated content for a session."""
if session_id in self._accumulated_content:
del self._accumulated_content[session_id]
for alias_id, alias_value in list(self._anonymous_session_aliases.items()):
if alias_value == session_id:
self._anonymous_session_aliases.pop(alias_id, None)