|
| 1 | +from typing import Iterator, List, Optional |
| 2 | +from attr import define, field |
| 3 | +import json |
| 4 | +import logging |
| 5 | +from datamaestro.data import File |
| 6 | +from datamaestro.record import Record |
| 7 | + |
| 8 | +from datamaestro_text.data.ir.base import ( |
| 9 | + IDItem, |
| 10 | + SimpleTextItem, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +from .base import ( |
| 15 | + AnswerDocumentURL, |
| 16 | + AnswerEntry, |
| 17 | + ConversationTree, |
| 18 | + EntryType, |
| 19 | + SimpleDecontextualizedItem, |
| 20 | + SingleConversationTree, |
| 21 | +) |
| 22 | +from . import ConversationDataset |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +@define(kw_only=True) |
| 27 | +class IkatConversationEntry: |
| 28 | + """A query with past history""" |
| 29 | + |
| 30 | + turn_id: int |
| 31 | + """Turn number in the conversation""" |
| 32 | + |
| 33 | + user_utterance: str |
| 34 | + """The last issued query""" |
| 35 | + |
| 36 | + resolved_utterance: str |
| 37 | + """Manually rewritten query""" |
| 38 | + |
| 39 | + response: str |
| 40 | + """The system response to the query""" |
| 41 | + |
| 42 | + relevant_ptkbs: List[str] |
| 43 | + """The list of relevant personal knowledge bases for the query""" |
| 44 | + |
| 45 | + citations: List[str] |
| 46 | + """The list of citations for the response""" |
| 47 | + |
| 48 | + |
| 49 | +@define(kw_only=True) |
| 50 | +class IkatDatasetEntry: |
| 51 | + """A query with past history""" |
| 52 | + |
| 53 | + number: str |
| 54 | + """Conversation ID""" |
| 55 | + |
| 56 | + title: str |
| 57 | + """Title of the conversation""" |
| 58 | + |
| 59 | + ptkb: str |
| 60 | + """The personal knowledge base associated with the user""" |
| 61 | + |
| 62 | + responses: List[IkatConversationEntry] = field( |
| 63 | + converter=lambda items: [IkatConversationEntry(**item) if isinstance(item, dict) else item for item in items] |
| 64 | + ) |
| 65 | + """The list of responses to the query""" |
| 66 | + |
| 67 | + |
| 68 | +class IkatDataset(ConversationDataset, File): |
| 69 | + |
| 70 | + def entries(self) -> Iterator[IkatDatasetEntry]: |
| 71 | + """Reads all conversation entries from the dataset file.""" |
| 72 | + with self.path.open("rt") as fp: |
| 73 | + raw_data = json.load(fp) |
| 74 | + |
| 75 | + logging.debug("Loaded %d entries from %s", len(raw_data), self.path) |
| 76 | + logging.debug(f"raw data has keys {raw_data[0].keys()}") |
| 77 | + |
| 78 | + processed_data = [] |
| 79 | + for entry in raw_data: |
| 80 | + processed_data.append(IkatDatasetEntry(**{key.lower(): value for key, value in entry.items()})) |
| 81 | + |
| 82 | + logging.debug(f"First parsed data sample: {processed_data[0]}") |
| 83 | + return iter(processed_data) |
| 84 | + |
| 85 | + def __iter__(self) -> Iterator[ConversationTree]: |
| 86 | + for entry in self.entries(): |
| 87 | + history: List[Record] = [] |
| 88 | + |
| 89 | + for turn in entry.responses: |
| 90 | + turn: IkatConversationEntry = turn # Ensure type is correct |
| 91 | + query_id = f"{entry.number}#{turn.turn_id}" |
| 92 | + |
| 93 | + # USER QUERY record |
| 94 | + history.append( |
| 95 | + Record( |
| 96 | + IDItem(query_id), |
| 97 | + SimpleTextItem(turn.user_utterance), |
| 98 | + SimpleDecontextualizedItem(turn.resolved_utterance), |
| 99 | + EntryType.USER_QUERY, |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + # Build citation info (stubbed relevance to match format) |
| 104 | + relevances = {} |
| 105 | + if turn.relevant_ptkbs: |
| 106 | + # Example: just use first as relevant (can be improved) |
| 107 | + relevances[0] = (0, None) # No position info in this structure |
| 108 | + |
| 109 | + # SYSTEM ANSWER record |
| 110 | + history.append( |
| 111 | + Record( |
| 112 | + AnswerEntry(turn.response), |
| 113 | + EntryType.SYSTEM_ANSWER, |
| 114 | + ) |
| 115 | + ) |
| 116 | + |
| 117 | + # Ensure reverse if needed for compatibility (optional) |
| 118 | + history.reverse() |
| 119 | + yield SingleConversationTree(entry.number, history) |
| 120 | + |
0 commit comments