bug: fix minor bugs
Browse files- app.py +18 -23
- docs/adding_new_parser.md +7 -28
- llmdataparser/prompts.py +3 -1
- llmdataparser/tw_legal_parser.py +1 -1
app.py
CHANGED
@@ -35,20 +35,22 @@ def get_available_tasks(parser: DatasetParser[Any]) -> list[str]:
|
|
35 |
|
36 |
|
37 |
def format_entry_attributes(entry: ParseEntry) -> str:
|
38 |
-
"""Format all attributes of a ParseEntry except
|
39 |
from dataclasses import fields
|
40 |
|
41 |
# Get all field names from the dataclass
|
42 |
field_names = [field.name for field in fields(entry)]
|
43 |
-
# Filter out
|
44 |
-
filtered_fields = [
|
|
|
|
|
45 |
# Build the formatted string
|
46 |
return "\n".join(f"{name}: {getattr(entry, name)}" for name in filtered_fields)
|
47 |
|
48 |
|
49 |
def load_and_parse(
|
50 |
parser_name: str, task_name: str | None, split_name: str | None
|
51 |
-
) -> tuple[int, str, str, str,
|
52 |
"""Load and parse the dataset, return the first entry and available splits."""
|
53 |
try:
|
54 |
parser = get_parser_instance(parser_name)
|
@@ -79,15 +81,14 @@ def load_and_parse(
|
|
79 |
|
80 |
info = parser.__repr__()
|
81 |
if not parsed_data:
|
82 |
-
return 0, "
|
83 |
|
84 |
# Get the first entry
|
85 |
first_entry = parsed_data[0]
|
86 |
|
87 |
return (
|
88 |
0, # Return first index instead of list of indices
|
89 |
-
first_entry.
|
90 |
-
first_entry.raw_question,
|
91 |
first_entry.answer,
|
92 |
format_entry_attributes(first_entry),
|
93 |
split_dropdown,
|
@@ -96,22 +97,22 @@ def load_and_parse(
|
|
96 |
except Exception as e:
|
97 |
# Make the error message more user-friendly and detailed
|
98 |
error_msg = f"Failed to load dataset: {str(e)}\nParser: {parser_name}\nTask: {task_name}\nSplit: {split_name}"
|
99 |
-
return 0, error_msg, "", "",
|
100 |
|
101 |
|
102 |
def update_entry(
|
103 |
parsed_data_index: int | None, parser_name: str
|
104 |
-
) -> tuple[str, str, str
|
105 |
"""Update the displayed entry based on the selected index."""
|
106 |
try:
|
107 |
if not parser_name:
|
108 |
-
return "Please select a parser first", "", ""
|
109 |
|
110 |
parser = get_parser_instance(parser_name)
|
111 |
parsed_data = parser.get_parsed_data
|
112 |
|
113 |
if not parsed_data:
|
114 |
-
return "No data available", "", ""
|
115 |
|
116 |
if parsed_data_index is None:
|
117 |
# Random selection using secrets instead of random
|
@@ -123,13 +124,12 @@ def update_entry(
|
|
123 |
entry = parsed_data[index]
|
124 |
|
125 |
return (
|
126 |
-
entry.
|
127 |
-
entry.raw_question,
|
128 |
entry.answer,
|
129 |
format_entry_attributes(entry),
|
130 |
)
|
131 |
except Exception as e:
|
132 |
-
return f"Error: {str(e)}", "", ""
|
133 |
|
134 |
|
135 |
def update_parser_options(parser_name: str) -> tuple[gr.Dropdown, gr.Dropdown, str]:
|
@@ -350,11 +350,8 @@ def create_interface() -> gr.Blocks:
|
|
350 |
|
351 |
with gr.Column(scale=2):
|
352 |
# Output displays
|
353 |
-
|
354 |
-
label="
|
355 |
-
)
|
356 |
-
raw_question_output = gr.Textbox(
|
357 |
-
label="Raw Question", lines=5, show_copy_button=True
|
358 |
)
|
359 |
answer_output = gr.Textbox(
|
360 |
label="Answer", lines=5, show_copy_button=True
|
@@ -411,8 +408,7 @@ def create_interface() -> gr.Blocks:
|
|
411 |
inputs=[parser_dropdown, task_dropdown, split_dropdown],
|
412 |
outputs=[
|
413 |
entry_index,
|
414 |
-
|
415 |
-
raw_question_output,
|
416 |
answer_output,
|
417 |
attributes_output,
|
418 |
split_dropdown,
|
@@ -430,8 +426,7 @@ def create_interface() -> gr.Blocks:
|
|
430 |
fn=update_entry,
|
431 |
inputs=[entry_index, parser_state],
|
432 |
outputs=[
|
433 |
-
|
434 |
-
raw_question_output,
|
435 |
answer_output,
|
436 |
attributes_output,
|
437 |
],
|
|
|
35 |
|
36 |
|
37 |
def format_entry_attributes(entry: ParseEntry) -> str:
|
38 |
+
"""Format all attributes of a ParseEntry except question and answer."""
|
39 |
from dataclasses import fields
|
40 |
|
41 |
# Get all field names from the dataclass
|
42 |
field_names = [field.name for field in fields(entry)]
|
43 |
+
# Filter out question and answer
|
44 |
+
filtered_fields = [
|
45 |
+
name for name in field_names if name not in ["question", "answer"]
|
46 |
+
]
|
47 |
# Build the formatted string
|
48 |
return "\n".join(f"{name}: {getattr(entry, name)}" for name in filtered_fields)
|
49 |
|
50 |
|
51 |
def load_and_parse(
|
52 |
parser_name: str, task_name: str | None, split_name: str | None
|
53 |
+
) -> tuple[int, str, str, str, gr.Dropdown, str]:
|
54 |
"""Load and parse the dataset, return the first entry and available splits."""
|
55 |
try:
|
56 |
parser = get_parser_instance(parser_name)
|
|
|
81 |
|
82 |
info = parser.__repr__()
|
83 |
if not parsed_data:
|
84 |
+
return 0, "", "", "", split_dropdown, info
|
85 |
|
86 |
# Get the first entry
|
87 |
first_entry = parsed_data[0]
|
88 |
|
89 |
return (
|
90 |
0, # Return first index instead of list of indices
|
91 |
+
first_entry.question,
|
|
|
92 |
first_entry.answer,
|
93 |
format_entry_attributes(first_entry),
|
94 |
split_dropdown,
|
|
|
97 |
except Exception as e:
|
98 |
# Make the error message more user-friendly and detailed
|
99 |
error_msg = f"Failed to load dataset: {str(e)}\nParser: {parser_name}\nTask: {task_name}\nSplit: {split_name}"
|
100 |
+
return 0, error_msg, "", "", [], ""
|
101 |
|
102 |
|
103 |
def update_entry(
|
104 |
parsed_data_index: int | None, parser_name: str
|
105 |
+
) -> tuple[str, str, str]:
|
106 |
"""Update the displayed entry based on the selected index."""
|
107 |
try:
|
108 |
if not parser_name:
|
109 |
+
return "Please select a parser first", "", ""
|
110 |
|
111 |
parser = get_parser_instance(parser_name)
|
112 |
parsed_data = parser.get_parsed_data
|
113 |
|
114 |
if not parsed_data:
|
115 |
+
return "No data available", "", ""
|
116 |
|
117 |
if parsed_data_index is None:
|
118 |
# Random selection using secrets instead of random
|
|
|
124 |
entry = parsed_data[index]
|
125 |
|
126 |
return (
|
127 |
+
entry.question,
|
|
|
128 |
entry.answer,
|
129 |
format_entry_attributes(entry),
|
130 |
)
|
131 |
except Exception as e:
|
132 |
+
return f"Error: {str(e)}", "", ""
|
133 |
|
134 |
|
135 |
def update_parser_options(parser_name: str) -> tuple[gr.Dropdown, gr.Dropdown, str]:
|
|
|
350 |
|
351 |
with gr.Column(scale=2):
|
352 |
# Output displays
|
353 |
+
question_output = gr.Textbox(
|
354 |
+
label="Question", lines=5, show_copy_button=True
|
|
|
|
|
|
|
355 |
)
|
356 |
answer_output = gr.Textbox(
|
357 |
label="Answer", lines=5, show_copy_button=True
|
|
|
408 |
inputs=[parser_dropdown, task_dropdown, split_dropdown],
|
409 |
outputs=[
|
410 |
entry_index,
|
411 |
+
question_output,
|
|
|
412 |
answer_output,
|
413 |
attributes_output,
|
414 |
split_dropdown,
|
|
|
426 |
fn=update_entry,
|
427 |
inputs=[entry_index, parser_state],
|
428 |
outputs=[
|
429 |
+
question_output,
|
|
|
430 |
answer_output,
|
431 |
attributes_output,
|
432 |
],
|
docs/adding_new_parser.md
CHANGED
@@ -23,10 +23,10 @@ class YourDatasetParseEntry(HuggingFaceParseEntry):
|
|
23 |
custom_field: str
|
24 |
|
25 |
@classmethod
|
26 |
-
def create(cls,
|
27 |
raw_answer: str, task_name: str, custom_field: str) -> "YourDatasetParseEntry":
|
28 |
return cls(
|
29 |
-
|
30 |
answer=answer,
|
31 |
raw_question=raw_question,
|
32 |
raw_answer=raw_answer,
|
@@ -41,27 +41,9 @@ class YourDatasetParser(HuggingFaceDatasetParser[YourDatasetParseEntry]):
|
|
41 |
_data_source = "huggingface/your-dataset"
|
42 |
_default_task = "default"
|
43 |
_task_names = ["task1", "task2", "task3"]
|
44 |
-
_default_system_prompt = YOUR_SYSTEM_PROMPT
|
45 |
```
|
46 |
|
47 |
-
### 2.
|
48 |
-
|
49 |
-
Add your system prompt to `llmdataparser/prompts.py`:
|
50 |
-
|
51 |
-
```python
|
52 |
-
YOUR_SYSTEM_PROMPT: Final[str] = textwrap.dedent(
|
53 |
-
"""\
|
54 |
-
You are an expert in [your domain]. Your task is to [describe the task].
|
55 |
-
|
56 |
-
Instructions:
|
57 |
-
1. [First instruction]
|
58 |
-
2. [Second instruction]
|
59 |
-
...
|
60 |
-
"""
|
61 |
-
)
|
62 |
-
```
|
63 |
-
|
64 |
-
### 3. Implement Required Methods
|
65 |
|
66 |
Your parser needs to implement these key methods:
|
67 |
|
@@ -78,11 +60,10 @@ def process_entry(
|
|
78 |
raw_answer = row["answer"]
|
79 |
task = task_name or self._get_current_task(row)
|
80 |
|
81 |
-
|
82 |
-
prompt = f"{self._system_prompt}\nQuestion: {raw_question}\nAnswer:"
|
83 |
|
84 |
return YourDatasetParseEntry.create(
|
85 |
-
|
86 |
answer=raw_answer,
|
87 |
raw_question=raw_question,
|
88 |
raw_answer=raw_answer,
|
@@ -115,7 +96,7 @@ def get_evaluation_metrics(self) -> list[EvaluationMetric]:
|
|
115 |
]
|
116 |
```
|
117 |
|
118 |
-
###
|
119 |
|
120 |
Add example usage at the bottom of your parser file:
|
121 |
|
@@ -137,7 +118,7 @@ if __name__ == "__main__":
|
|
137 |
print(f"Answer: {example.answer}")
|
138 |
```
|
139 |
|
140 |
-
###
|
141 |
|
142 |
Create a test file `tests/test_your_dataset_parser.py`:
|
143 |
|
@@ -171,7 +152,6 @@ def test_process_entry():
|
|
171 |
1. **Documentation**: Add clear docstrings and comments explaining your parser's functionality.
|
172 |
1. **Error Handling**: Include appropriate error checking and validation.
|
173 |
1. **Testing**: Write comprehensive tests covering different scenarios.
|
174 |
-
1. **System Prompt**: Design your system prompt carefully to guide the model effectively.
|
175 |
|
176 |
## Examples
|
177 |
|
@@ -185,7 +165,6 @@ Look at existing parsers for reference:
|
|
185 |
|
186 |
1. **Parse Entry Class**: Create a custom parse entry class if you need additional fields.
|
187 |
1. **Task Names**: Define all available tasks in `_task_names`.
|
188 |
-
1. **System Prompt**: Write clear instructions in the system prompt.
|
189 |
1. **Process Entry**: Handle data extraction and formatting in `process_entry`.
|
190 |
1. **Dataset Description**: Provide comprehensive dataset information.
|
191 |
1. **Evaluation Metrics**: Define appropriate metrics for your dataset.
|
|
|
23 |
custom_field: str
|
24 |
|
25 |
@classmethod
|
26 |
+
def create(cls, question: str, answer: str, raw_question: str,
|
27 |
raw_answer: str, task_name: str, custom_field: str) -> "YourDatasetParseEntry":
|
28 |
return cls(
|
29 |
+
question=question,
|
30 |
answer=answer,
|
31 |
raw_question=raw_question,
|
32 |
raw_answer=raw_answer,
|
|
|
41 |
_data_source = "huggingface/your-dataset"
|
42 |
_default_task = "default"
|
43 |
_task_names = ["task1", "task2", "task3"]
|
|
|
44 |
```
|
45 |
|
46 |
+
### 2. Implement Required Methods
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
Your parser needs to implement these key methods:
|
49 |
|
|
|
60 |
raw_answer = row["answer"]
|
61 |
task = task_name or self._get_current_task(row)
|
62 |
|
63 |
+
question = f"Question: {raw_question}\nAnswer:"
|
|
|
64 |
|
65 |
return YourDatasetParseEntry.create(
|
66 |
+
question=question,
|
67 |
answer=raw_answer,
|
68 |
raw_question=raw_question,
|
69 |
raw_answer=raw_answer,
|
|
|
96 |
]
|
97 |
```
|
98 |
|
99 |
+
### 3. Add Example Usage
|
100 |
|
101 |
Add example usage at the bottom of your parser file:
|
102 |
|
|
|
118 |
print(f"Answer: {example.answer}")
|
119 |
```
|
120 |
|
121 |
+
### 4. Create Tests
|
122 |
|
123 |
Create a test file `tests/test_your_dataset_parser.py`:
|
124 |
|
|
|
152 |
1. **Documentation**: Add clear docstrings and comments explaining your parser's functionality.
|
153 |
1. **Error Handling**: Include appropriate error checking and validation.
|
154 |
1. **Testing**: Write comprehensive tests covering different scenarios.
|
|
|
155 |
|
156 |
## Examples
|
157 |
|
|
|
165 |
|
166 |
1. **Parse Entry Class**: Create a custom parse entry class if you need additional fields.
|
167 |
1. **Task Names**: Define all available tasks in `_task_names`.
|
|
|
168 |
1. **Process Entry**: Handle data extraction and formatting in `process_entry`.
|
169 |
1. **Dataset Description**: Provide comprehensive dataset information.
|
170 |
1. **Evaluation Metrics**: Define appropriate metrics for your dataset.
|
llmdataparser/prompts.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import textwrap
|
2 |
from typing import Final
|
3 |
|
|
|
|
|
4 |
MMLU_SYSTEM_PROMPT: Final[str] = textwrap.dedent(
|
5 |
"""\
|
6 |
You are an expert answering multiple-choice questions. Select the single most accurate answer (A, B, C, or D) based on factual knowledge. Respond with the letter only.
|
@@ -44,7 +46,7 @@ IFEVAL_SYSTEM_PROMPT: Final[str] = textwrap.dedent(
|
|
44 |
|
45 |
BBH_SYSTEM_PROMPT: Final[str] = textwrap.dedent(
|
46 |
"""\
|
47 |
-
Solve this reasoning problem
|
48 |
"""
|
49 |
)
|
50 |
|
|
|
1 |
import textwrap
|
2 |
from typing import Final
|
3 |
|
4 |
+
# Only for reference
|
5 |
+
|
6 |
MMLU_SYSTEM_PROMPT: Final[str] = textwrap.dedent(
|
7 |
"""\
|
8 |
You are an expert answering multiple-choice questions. Select the single most accurate answer (A, B, C, or D) based on factual knowledge. Respond with the letter only.
|
|
|
46 |
|
47 |
BBH_SYSTEM_PROMPT: Final[str] = textwrap.dedent(
|
48 |
"""\
|
49 |
+
Solve this reasoning problem step by step.
|
50 |
"""
|
51 |
)
|
52 |
|
llmdataparser/tw_legal_parser.py
CHANGED
@@ -88,7 +88,7 @@ class TWLegalDatasetParser(HuggingFaceDatasetParser[TWLegalParseEntry]):
|
|
88 |
),
|
89 |
citation="""
|
90 |
url={https://huggingface.co/datasets/lianghsun/tw-legal-benchmark-v1}
|
91 |
-
|
92 |
)
|
93 |
|
94 |
def get_evaluation_metrics(self) -> list[EvaluationMetric]:
|
|
|
88 |
),
|
89 |
citation="""
|
90 |
url={https://huggingface.co/datasets/lianghsun/tw-legal-benchmark-v1}
|
91 |
+
""",
|
92 |
)
|
93 |
|
94 |
def get_evaluation_metrics(self) -> list[EvaluationMetric]:
|