This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathv1_processing.py
More file actions
534 lines (451 loc) · 19.9 KB
/
v1_processing.py
File metadata and controls
534 lines (451 loc) · 19.9 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import asyncio
import json
from collections import defaultdict
from typing import AsyncGenerator, Dict, List, Optional, Tuple
import regex as re
import structlog
from codegate.api import v1_models
from codegate.api.v1_models import (
AlertConversation,
ChatMessage,
Conversation,
PartialQuestionAnswer,
PartialQuestions,
QuestionAnswer,
QuestionType,
TokenUsageAggregate,
TokenUsageByModel,
)
from codegate.db.connection import alert_queue
from codegate.db.models import Alert, GetPromptWithOutputsRow, TokenUsage
logger = structlog.get_logger("codegate")
SYSTEM_PROMPTS = [
"Given the following... please reply with a short summary that is 4-12 words in length, "
"you should summarize what the user is asking for OR what the user is trying to accomplish. "
"You should only respond with the summary, no additional text or explanation, "
"you don't need ending punctuation.",
]
async def generate_sse_events() -> AsyncGenerator[str, None]:
"""
SSE generator from queue
"""
while True:
message = await alert_queue.get()
yield f"data: {message}\n\n"
async def _is_system_prompt(message: str) -> bool:
"""
Check if the message is a system prompt.
"""
for prompt in SYSTEM_PROMPTS:
if prompt in message or message in prompt:
return True
return False
async def parse_request(request_str: str) -> Tuple[Optional[List[str]], str]: # noqa: C901
"""
Parse the request string from the pipeline and return the message and the model.
"""
try:
request = json.loads(request_str)
except Exception as e:
logger.warning(f"Error parsing request: {request_str}. {e}")
return None, ""
model = request.get("model", "")
messages = []
for message in request.get("messages", []):
role = message.get("role")
if not role == "user":
continue
content = message.get("content")
message_str = ""
if isinstance(content, str):
message_str = content
elif isinstance(content, list):
for content_part in content:
if isinstance(content_part, dict) and content_part.get("type") == "text":
message_str = content_part.get("text")
if message_str and not await _is_system_prompt(message_str):
messages.append(message_str)
# We couldn't get anything from the messages, try the prompt
if not messages:
message_prompt = request.get("prompt", "")
if message_prompt and not await _is_system_prompt(message_prompt):
messages.append(message_prompt)
# If still we don't have anything, return None string
if not messages:
return None, model
# Respond with the messages and the model
return messages, model
async def parse_output(output_str: str) -> Optional[str]: # noqa: C901
"""
Parse the output string from the pipeline and return the message.
"""
try:
if output_str is None:
return None
output = json.loads(output_str)
except Exception as e:
logger.warning(f"Error parsing output: {output_str}. {e}")
return None
def _parse_single_output(single_output: dict) -> str:
single_output_message = ""
for choice in single_output.get("choices", []):
if not isinstance(choice, dict):
continue
content_dict = choice.get("delta", {}) or choice.get("message", {})
single_output_message += content_dict.get("content", "")
return single_output_message
full_output_message = ""
if isinstance(output, list):
for output_chunk in output:
output_message = ""
if isinstance(output_chunk, dict):
output_message = _parse_single_output(output_chunk)
elif isinstance(output_chunk, str):
try:
output_decoded = json.loads(output_chunk)
output_message = _parse_single_output(output_decoded)
except Exception:
logger.error(f"Error reading chunk: {output_chunk}")
else:
logger.warning(
f"Could not handle output: {output_chunk}", out_type=type(output_chunk)
)
full_output_message += output_message
elif isinstance(output, dict):
full_output_message = _parse_single_output(output)
return full_output_message
async def _get_partial_question_answer(
row: GetPromptWithOutputsRow,
) -> Optional[PartialQuestionAnswer]:
"""
Parse a row from the get_prompt_with_outputs query and return a PartialConversation
The row contains the raw request and output strings from the pipeline.
"""
async with asyncio.TaskGroup() as tg:
request_task = tg.create_task(parse_request(row.request))
output_task = tg.create_task(parse_output(row.output))
request_user_msgs, model = request_task.result()
output_msg_str = output_task.result()
# If we couldn't parse the request, return None
if not request_user_msgs:
return None
request_message = PartialQuestions(
messages=request_user_msgs,
timestamp=row.timestamp,
message_id=row.id,
provider=row.provider,
type=row.type,
)
if output_msg_str:
output_message = ChatMessage(
message=output_msg_str,
timestamp=row.output_timestamp,
message_id=row.output_id,
)
else:
output_message = None
token_usage = TokenUsage.from_db(
input_cost=row.input_cost,
input_tokens=row.input_tokens,
output_tokens=row.output_tokens,
output_cost=row.output_cost,
)
# Use the model to update the token cost
provider = row.provider
# TODO: This should come from the database. For now, we are manually changing copilot to openai
# Change copilot provider to openai
if provider == "copilot":
provider = "openai"
model_token_usage = TokenUsageByModel(
model=model, token_usage=token_usage, provider_type=provider
)
return PartialQuestionAnswer(
partial_questions=request_message,
answer=output_message,
model_token_usage=model_token_usage,
)
def parse_question_answer(input_text: str) -> str:
# Remove the <environment_details>...</environment_details> pattern if present
env_details_pattern = r"\n<environment_details>.*?</environment_details>"
input_text = re.sub(env_details_pattern, "", input_text, flags=re.DOTALL).strip()
# Check for the <task>...</task> pattern first
task_pattern = r"^<task>(.*?)</task>"
task_match = re.search(task_pattern, input_text, re.DOTALL)
if task_match:
return task_match.group(1).strip()
# If no <task>...</task>, check for "Context: xxx \n\nQuery: xxx"
context_query_pattern = r"^Context:.*?\n\n\s*Query:\s*(.*)$"
context_query_match = re.search(context_query_pattern, input_text, re.DOTALL)
if context_query_match:
return context_query_match.group(1).strip()
# If no pattern matches, return the original input text
return input_text
def _clean_secrets_from_message(message: str) -> str:
pattern = re.compile(r"REDACTED<(\$?[^>]+)>")
return pattern.sub("REDACTED_SECRET", message)
def _group_partial_messages( # noqa: C901
pq_list: List[PartialQuestions],
) -> List[List[PartialQuestions]]:
"""
A PartialQuestion is an object that contains several user messages provided from a
chat conversation. Example:
- PartialQuestion(messages=["Hello"], timestamp=2022-01-01T00:00:00Z)
- PartialQuestion(messages=["Hello", "How are you?"], timestamp=2022-01-01T00:00:01Z)
In the above example both PartialQuestions are part of the same conversation and should be
matched together.
Group PartialQuestions objects such that:
- If one PartialQuestion (pq) is a subset of another pq's messages, group them together.
- If multiple subsets exist for the same superset, choose only the one
closest in timestamp to the superset.
- Leave any unpaired pq by itself.
- Finally, sort the resulting groups by the earliest timestamp in each group.
"""
# 0) Clean secrets from messages
for pq in pq_list:
pq.messages = [_clean_secrets_from_message(msg) for msg in pq.messages]
# 1) Sort by length of messages descending (largest/most-complete first),
# then by timestamp ascending for stable processing.
pq_list_sorted = sorted(pq_list, key=lambda x: (-len(x.messages), x.timestamp))
used = set()
groups = []
# 2) Iterate in order of "largest messages first"
for sup in pq_list_sorted:
if sup.message_id in used:
continue # Already grouped
# Find all potential subsets of 'sup' that are not yet used
# (If sup's messages == sub's messages, that also counts, because sub ⊆ sup)
possible_subsets: List[PartialQuestions] = []
for sub in pq_list_sorted:
if sub.message_id == sup.message_id or sub.message_id in used:
continue
if (
set(sub.messages).issubset(set(sup.messages))
and sub.provider == sup.provider
and set(sub.messages) != set(sup.messages)
):
possible_subsets.append(sub)
# 3) If there are no subsets, check for time-based grouping
if not possible_subsets:
new_group = [sup]
used.add(sup.message_id)
for other in pq_list_sorted:
if other.message_id in used or other.message_id == sup.message_id:
continue
if abs((other.timestamp - sup.timestamp).total_seconds()) <= 5 and set(
other.messages
) & set(
sup.messages
): # At least one message in common
new_group.append(other)
used.add(other.message_id)
groups.append(new_group)
else:
# 4) Group subsets by messages to discard duplicates e.g.: 2 subsets with single 'hello'
subs_group_by_messages = defaultdict(list)
for q in possible_subsets:
subs_group_by_messages[tuple(q.messages)].append(q)
new_group = [sup]
used.add(sup.message_id)
for subs_same_message in subs_group_by_messages.values():
# If more than one pick the one subset closest in time to sup
closest_subset = min(
subs_same_message, key=lambda s: abs(s.timestamp - sup.timestamp)
)
new_group.append(closest_subset)
used.add(closest_subset.message_id)
groups.append(new_group)
# 5) Sort the groups by the earliest timestamp within each group
groups.sort(key=lambda g: min(pq.timestamp for pq in g))
return groups
def _get_question_answer_from_partial(
partial_question_answer: PartialQuestionAnswer,
) -> QuestionAnswer:
"""
Get a QuestionAnswer object from a PartialQuestionAnswer object. PartialQuestionAnswer
contains a list of messages as question. QuestionAnswer contains a single message as question.
"""
# Get the last user message as the question
message_str = partial_question_answer.partial_questions.messages[-1]
if (
partial_question_answer.partial_questions.provider == "copilot"
and partial_question_answer.partial_questions.type == "chat"
):
message_str = "\n".join(partial_question_answer.partial_questions.messages)
# sanitize answer from reserved words
if partial_question_answer.answer and partial_question_answer.answer.message:
partial_question_answer.answer.message = re.sub(
r"(question_about_specific_files|question_about_specific_code|unknown)\s*",
"",
partial_question_answer.answer.message,
).strip()
question = ChatMessage(
message=message_str,
timestamp=partial_question_answer.partial_questions.timestamp,
message_id=partial_question_answer.partial_questions.message_id,
)
return QuestionAnswer(question=question, answer=partial_question_answer.answer)
async def match_conversations(
partial_question_answers: List[Optional[PartialQuestionAnswer]],
) -> Tuple[List[Conversation], Dict[str, Conversation]]:
"""
Match partial conversations to form a complete conversation.
"""
grouped_partial_questions = _group_partial_messages(
[partial_qs_a.partial_questions for partial_qs_a in partial_question_answers]
)
# Create the conversation objects
conversations = []
map_q_id_to_conversation = {}
for group in grouped_partial_questions:
questions_answers: List[QuestionAnswer] = []
token_usage_agg = TokenUsageAggregate(tokens_by_model={}, token_usage=TokenUsage())
first_partial_qa = None
for partial_question in sorted(group, key=lambda x: x.timestamp):
# Partial questions don't contain the answer, so we need to find the corresponding
# valid partial question answer
selected_partial_qa = None
for partial_qa in partial_question_answers:
if partial_question.message_id == partial_qa.partial_questions.message_id:
selected_partial_qa = partial_qa
break
# check if we have a question and answer, otherwise do not add it
# if the question is a FIM question, we should add it even if there is no answer
# not add Chat questions without answers
if selected_partial_qa and (
selected_partial_qa.answer is not None
or selected_partial_qa.partial_questions.type == QuestionType.fim
):
# if we don't have a first question, set it. We will use it
# to set the conversation timestamp and provider
first_partial_qa = first_partial_qa or selected_partial_qa
qa = _get_question_answer_from_partial(selected_partial_qa)
qa.question.message = parse_question_answer(qa.question.message)
questions_answers.append(qa)
token_usage_agg.add_model_token_usage(selected_partial_qa.model_token_usage)
# if we have a conversation with at least one question and answer
if len(questions_answers) > 0 and first_partial_qa is not None:
if token_usage_agg.token_usage.input_tokens == 0:
token_usage_agg = None
conversation = Conversation(
question_answers=questions_answers,
provider=first_partial_qa.partial_questions.provider,
type=first_partial_qa.partial_questions.type,
chat_id=first_partial_qa.partial_questions.message_id,
conversation_timestamp=first_partial_qa.partial_questions.timestamp,
token_usage_agg=token_usage_agg,
)
for qa in questions_answers:
map_q_id_to_conversation[qa.question.message_id] = conversation
conversations.append(conversation)
return conversations, map_q_id_to_conversation
async def _process_prompt_output_to_partial_qa(
prompts_outputs: List[GetPromptWithOutputsRow],
) -> List[PartialQuestionAnswer]:
"""
Process the prompts and outputs to PartialQuestionAnswer objects.
"""
# Parse the prompts and outputs in parallel
async with asyncio.TaskGroup() as tg:
tasks = [tg.create_task(_get_partial_question_answer(row)) for row in prompts_outputs]
return [task.result() for task in tasks if task.result() is not None]
async def parse_messages_in_conversations(
prompts_outputs: List[GetPromptWithOutputsRow],
) -> Tuple[List[Conversation], Dict[str, Conversation]]:
"""
Get all the messages from the database and return them as a list of conversations.
"""
partial_question_answers = await _process_prompt_output_to_partial_qa(prompts_outputs)
conversations, map_q_id_to_conversation = await match_conversations(partial_question_answers)
return conversations, map_q_id_to_conversation
async def parse_row_alert_conversation(
row: Alert, map_q_id_to_conversation: Dict[str, Conversation]
) -> Optional[AlertConversation]:
"""
Parse a row from the get_alerts_with_prompt_and_output query and return a Conversation
The row contains the raw request and output strings from the pipeline.
"""
conversation = map_q_id_to_conversation.get(row.prompt_id)
if conversation is None:
return None
code_snippet = json.loads(row.code_snippet) if row.code_snippet else None
trigger_string = None
if row.trigger_string:
try:
trigger_string = json.loads(row.trigger_string)
except Exception:
trigger_string = row.trigger_string
return AlertConversation(
conversation=conversation,
alert_id=row.id,
code_snippet=code_snippet,
trigger_string=trigger_string,
trigger_type=row.trigger_type,
trigger_category=row.trigger_category,
timestamp=row.timestamp,
)
async def parse_get_alert_conversation(
alerts: List[Alert],
prompts_outputs: List[GetPromptWithOutputsRow],
) -> List[AlertConversation]:
"""
Parse a list of rows from the get_alerts_with_prompt_and_output query and return a list of
AlertConversation
The rows contain the raw request and output strings from the pipeline.
"""
_, map_q_id_to_conversation = await parse_messages_in_conversations(prompts_outputs)
dedup_alerts = await remove_duplicate_alerts(alerts)
async with asyncio.TaskGroup() as tg:
tasks = [
tg.create_task(parse_row_alert_conversation(row, map_q_id_to_conversation))
for row in dedup_alerts
]
return [task.result() for task in tasks if task.result() is not None]
async def parse_workspace_token_usage(
prompts_outputs: List[GetPromptWithOutputsRow],
) -> TokenUsageAggregate:
"""
Parse the token usage from the workspace.
"""
partial_question_answers = await _process_prompt_output_to_partial_qa(prompts_outputs)
token_usage_agg = TokenUsageAggregate(tokens_by_model={}, token_usage=TokenUsage())
for p_qa in partial_question_answers:
token_usage_agg.add_model_token_usage(p_qa.model_token_usage)
return token_usage_agg
async def remove_duplicate_alerts(alerts: List[v1_models.Alert]) -> List[v1_models.Alert]:
unique_alerts = []
seen = defaultdict(list)
for alert in sorted(
alerts, key=lambda x: x.timestamp, reverse=True
): # Sort alerts by timestamp descending
# Handle trigger_string based on its type
trigger_string_content = ""
if isinstance(alert.trigger_string, dict):
# If it's a dict, use relevant fields for deduplication
trigger_string_content = json.dumps(
{
"name": alert.trigger_string.get("name"),
"type": alert.trigger_string.get("type"),
"status": alert.trigger_string.get("status"),
}
)
elif isinstance(alert.trigger_string, str):
# If it's a string, use the part before "Context" if it exists
trigger_string_content = alert.trigger_string.split("Context")[0]
else:
# For any other case, convert to string
trigger_string_content = str(alert.trigger_string)
key = (
alert.code_snippet,
alert.trigger_type,
alert.trigger_category,
trigger_string_content,
)
# If key exists and new alert is more recent, replace it
if key in seen:
existing_alert = seen[key]
if abs((alert.timestamp - existing_alert.timestamp).total_seconds()) < 5:
seen[key] = alert # Replace with newer alert
continue
seen[key] = alert
unique_alerts.append(alert)
return unique_alerts