Skip to content

Commit ea3e081

Browse files
added clean_citation method in response_handlers.py
1 parent c140b0b commit ea3e081

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/backend/v3/callbacks/response_handlers.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import asyncio
77
import logging
88
import time
9-
9+
import re
1010
from semantic_kernel.contents import ChatMessageContent, StreamingChatMessageContent
1111
from v3.config.settings import connection_config
1212
from v3.models.messages import (
@@ -17,6 +17,22 @@
1717
WebsocketMessageType,
1818
)
1919

20+
def clean_citations(text: str) -> str:
21+
"""Remove citation markers from agent responses while preserving formatting."""
22+
if not text:
23+
return text
24+
25+
# Remove citation patterns like [9:0|source], [9:1|source], etc.
26+
text = re.sub(r'\[\d+:\d+\|source\]', '', text)
27+
28+
#Remove other common citation pattern
29+
text = re.sub(r'\[\s*source\s*\]', '', text, flags=re.IGNORECASE)
30+
text = re.sub(r'\[\d+\]', '', text)
31+
text = re.sub(r'【[^】]*】', '', text) # Unicode brackets
32+
text = re.sub(r'\(source:[^)]*\)', '', text, flags=re.IGNORECASE)
33+
text = re.sub(r'\[source:[^\]]*\]', '', text, flags=re.IGNORECASE)
34+
35+
return text
2036

2137
def agent_response_callback(message: ChatMessageContent, user_id: str = None) -> None:
2238
"""Observer function to print detailed information about streaming messages."""
@@ -55,7 +71,7 @@ def agent_response_callback(message: ChatMessageContent, user_id: str = None) ->
5571
final_message = AgentMessage(
5672
agent_name=agent_name,
5773
timestamp=time.time() or "",
58-
content=message.content or "",
74+
content=clean_citations(message.content) or "",
5975
)
6076

6177
asyncio.create_task(
@@ -80,7 +96,7 @@ async def streaming_agent_response_callback(
8096
try:
8197
message = AgentMessageStreaming(
8298
agent_name=streaming_message.name or "Unknown Agent",
83-
content=streaming_message.content,
99+
content=clean_citations(streaming_message.content),
84100
is_final=is_final,
85101
)
86102
await connection_config.send_status_update_async(

0 commit comments

Comments
 (0)