Commit
•
f04dfa8
1
Parent(s):
9d1a2d6
refactor: separate pipeline logic
Browse files
app.py
CHANGED
@@ -1,13 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
from src.distilabel_dataset_generator.faq import app as faq_app
|
4 |
-
from src.distilabel_dataset_generator.sft import app as sft_app
|
5 |
-
|
6 |
|
7 |
theme = gr.themes.Monochrome(
|
8 |
spacing_size="md",
|
9 |
-
font=[gr.themes.GoogleFont(
|
10 |
-
|
11 |
)
|
12 |
|
13 |
demo = gr.TabbedInterface(
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from src.distilabel_dataset_generator.apps.faq import app as faq_app
|
4 |
+
from src.distilabel_dataset_generator.apps.sft import app as sft_app
|
|
|
5 |
|
6 |
theme = gr.themes.Monochrome(
|
7 |
spacing_size="md",
|
8 |
+
font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"],
|
|
|
9 |
)
|
10 |
|
11 |
demo = gr.TabbedInterface(
|
src/distilabel_dataset_generator/{faq.py → apps/faq.py}
RENAMED
File without changes
|
src/distilabel_dataset_generator/{sft.py → apps/sft.py}
RENAMED
@@ -4,189 +4,32 @@ import time
|
|
4 |
import gradio as gr
|
5 |
import pandas as pd
|
6 |
from distilabel.distiset import Distiset
|
7 |
-
from distilabel.llms import InferenceEndpointsLLM
|
8 |
-
from distilabel.pipeline import Pipeline
|
9 |
-
from distilabel.steps import KeepColumns
|
10 |
-
from distilabel.steps.tasks import MagpieGenerator, TextGeneration
|
11 |
from huggingface_hub import whoami
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
REASONING_PROMPT = (
|
22 |
-
"You are an AI assistant specialized in logical thinking and problem-solving. Your"
|
23 |
-
" purpose is to help users work through complex ideas, analyze situations, and draw"
|
24 |
-
" conclusions based on given information. Approach each query with structured thinking,"
|
25 |
-
" break down problems into manageable parts, and guide users through the reasoning"
|
26 |
-
" process step-by-step."
|
27 |
-
)
|
28 |
-
|
29 |
-
PLANNING_PROMPT = (
|
30 |
-
"You are an AI assistant focused on helping users create effective plans and strategies."
|
31 |
-
" Your purpose is to assist in organizing thoughts, setting goals, and developing"
|
32 |
-
" actionable steps for various projects or activities. Offer structured approaches,"
|
33 |
-
" consider potential challenges, and provide tips for efficient execution of plans."
|
34 |
-
)
|
35 |
-
|
36 |
-
EDITING_PROMPT = (
|
37 |
-
"You are an AI assistant specialized in editing and improving written content. Your"
|
38 |
-
" purpose is to help users refine their writing by offering suggestions for grammar,"
|
39 |
-
" style, clarity, and overall structure. Provide constructive feedback, explain your"
|
40 |
-
" edits, and offer alternative phrasings when appropriate."
|
41 |
-
)
|
42 |
-
|
43 |
-
CODING_DEBUGGING_PROMPT = (
|
44 |
-
"You are an AI assistant designed to help with programming tasks. Your purpose is to"
|
45 |
-
" assist users in writing, reviewing, and debugging code across various programming"
|
46 |
-
" languages. Provide clear explanations, offer best practices, and help troubleshoot"
|
47 |
-
" issues. When appropriate, suggest optimizations or alternative approaches to coding"
|
48 |
-
" problems."
|
49 |
-
)
|
50 |
-
|
51 |
-
MATH_SYSTEM_PROMPT = (
|
52 |
-
"You are an AI assistant designed to provide helpful, step-by-step guidance on solving"
|
53 |
-
" math problems. The user will ask you a wide range of complex mathematical questions."
|
54 |
-
" Your purpose is to assist users in understanding mathematical concepts, working through"
|
55 |
-
" equations, and arriving at the correct solutions."
|
56 |
-
)
|
57 |
-
|
58 |
-
ROLE_PLAYING_PROMPT = (
|
59 |
-
"You are an AI assistant capable of engaging in various role-playing scenarios. Your"
|
60 |
-
" purpose is to adopt different personas or characters as requested by the user. Maintain"
|
61 |
-
" consistency with the chosen role, respond in character, and help create immersive and"
|
62 |
-
" interactive experiences for the user."
|
63 |
-
)
|
64 |
-
|
65 |
-
DATA_ANALYSIS_PROMPT = (
|
66 |
-
"You are an AI assistant specialized in data analysis and interpretation. Your purpose is"
|
67 |
-
" to help users understand and derive insights from data sets, statistics, and analytical"
|
68 |
-
" tasks. Offer clear explanations of data trends, assist with statistical calculations,"
|
69 |
-
" and provide guidance on data visualization and interpretation techniques."
|
70 |
-
)
|
71 |
-
|
72 |
-
CREATIVE_WRITING_PROMPT = (
|
73 |
-
"You are an AI assistant designed to support creative writing endeavors. Your purpose is"
|
74 |
-
" to help users craft engaging stories, poems, and other creative texts. Offer"
|
75 |
-
" suggestions for plot development, character creation, dialogue writing, and other"
|
76 |
-
" aspects of creative composition. Provide constructive feedback and inspire creativity."
|
77 |
-
)
|
78 |
-
|
79 |
-
ADVICE_SEEKING_PROMPT = (
|
80 |
-
"You are an AI assistant focused on providing thoughtful advice and guidance. Your"
|
81 |
-
" purpose is to help users navigate various personal or professional issues by offering"
|
82 |
-
" balanced perspectives, considering potential outcomes, and suggesting practical"
|
83 |
-
" solutions. Encourage users to think critically about their situations while providing"
|
84 |
-
" supportive and constructive advice."
|
85 |
-
)
|
86 |
-
|
87 |
-
BRAINSTORMING_PROMPT = (
|
88 |
-
"You are an AI assistant specialized in generating ideas and facilitating creative"
|
89 |
-
" thinking. Your purpose is to help users explore possibilities, think outside the box,"
|
90 |
-
" and develop innovative concepts. Encourage free-flowing thoughts, offer diverse"
|
91 |
-
" perspectives, and help users build upon and refine their ideas."
|
92 |
-
)
|
93 |
-
|
94 |
-
PROMPT_CREATION_PROMPT = f"""You are an AI assistant specialized in generating very precise prompts for dataset creation.
|
95 |
-
Your task is to write a prompt following the instruction of the user. Respond with the prompt and nothing else.
|
96 |
-
The prompt you write should follow the same style and structure as the following example prompts:
|
97 |
-
|
98 |
-
{INFORMATION_SEEKING_PROMPT}
|
99 |
-
|
100 |
-
{REASONING_PROMPT}
|
101 |
-
|
102 |
-
{PLANNING_PROMPT}
|
103 |
-
|
104 |
-
{CODING_DEBUGGING_PROMPT}
|
105 |
-
|
106 |
-
{EDITING_PROMPT}
|
107 |
-
|
108 |
-
{ROLE_PLAYING_PROMPT}
|
109 |
-
|
110 |
-
{DATA_ANALYSIS_PROMPT}
|
111 |
-
|
112 |
-
{CREATIVE_WRITING_PROMPT}
|
113 |
-
|
114 |
-
{ADVICE_SEEKING_PROMPT}
|
115 |
-
|
116 |
-
{BRAINSTORMING_PROMPT}
|
117 |
-
|
118 |
-
User dataset description:
|
119 |
-
"""
|
120 |
-
|
121 |
-
MODEL = "meta-llama/Meta-Llama-3.1-70B-Instruct"
|
122 |
-
DEFAULT_SYSTEM_PROMPT_DESCRIPTION = (
|
123 |
-
"A chemistry dataset for an assistant that explains chemical reactions and formulas"
|
124 |
-
)
|
125 |
-
DEFAULT_SYSTEM_PROMPT = "You are an AI assistant specializing in chemistry and chemical reactions. Your purpose is to help users understand and work with chemical formulas, equations, and reactions. Provide clear explanations of reaction mechanisms, assist in balancing chemical equations, and offer guidance on the interpretation of chemical structures. Explain the roles of reactants, products, catalysts, and solvents, and define key chemistry terms when necessary."
|
126 |
-
DEFAULT_DATASET = pd.DataFrame(
|
127 |
-
{
|
128 |
-
"instruction": [
|
129 |
-
"What is the term for the study of the structure and evolution of the Earth's interior. "
|
130 |
-
],
|
131 |
-
"response": [
|
132 |
-
"""The study of the structure and evolution of the Earth's interior is called geophysics, particularly the subfield of geology known as geodynamics, and more specifically the subfield of geology known as geotectonics. However, a more specific term for this study is "geology of the Earth's interior" or "Earth internal structure." However, the most commonly used term for this study is geophysics. """
|
133 |
-
],
|
134 |
-
}
|
135 |
)
|
136 |
|
137 |
|
138 |
-
def _run_pipeline(result_queue, num_turns, num_rows, system_prompt
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
magpie = MagpieGenerator(
|
145 |
-
llm=InferenceEndpointsLLM(
|
146 |
-
model_id=MODEL,
|
147 |
-
tokenizer_id=MODEL,
|
148 |
-
magpie_pre_query_template="llama3",
|
149 |
-
generation_kwargs={
|
150 |
-
"temperature": 0.8, # it's the best value for Llama 3.1 70B Instruct
|
151 |
-
"do_sample": True,
|
152 |
-
"max_new_tokens": 2048,
|
153 |
-
"stop_sequences": [
|
154 |
-
"<|eot_id|>",
|
155 |
-
"<|end_of_text|>",
|
156 |
-
"<|start_header_id|>",
|
157 |
-
"<|end_header_id|>",
|
158 |
-
"assistant",
|
159 |
-
],
|
160 |
-
},
|
161 |
-
api_key=token,
|
162 |
-
),
|
163 |
-
n_turns=num_turns,
|
164 |
-
num_rows=num_rows,
|
165 |
-
system_prompt=system_prompt,
|
166 |
-
output_mappings=output_mappings,
|
167 |
-
)
|
168 |
-
keep_columns = KeepColumns(
|
169 |
-
columns=list(output_mappings.values()) + ["model_name"],
|
170 |
-
)
|
171 |
-
magpie.connect(keep_columns)
|
172 |
distiset: Distiset = pipeline.run(use_cache=False)
|
173 |
result_queue.put(distiset)
|
174 |
|
175 |
|
176 |
def generate_system_prompt(dataset_description, progress=gr.Progress()):
|
177 |
progress(0.1, desc="Initializing text generation")
|
178 |
-
generate_description =
|
179 |
-
llm=InferenceEndpointsLLM(
|
180 |
-
model_id=MODEL,
|
181 |
-
tokenizer_id=MODEL,
|
182 |
-
generation_kwargs={
|
183 |
-
"temperature": 0.8,
|
184 |
-
"max_new_tokens": 2048,
|
185 |
-
"do_sample": True,
|
186 |
-
},
|
187 |
-
),
|
188 |
-
use_system_prompt=True,
|
189 |
-
)
|
190 |
progress(0.4, desc="Loading model")
|
191 |
generate_description.load()
|
192 |
progress(0.7, desc="Generating system prompt")
|
@@ -231,15 +74,13 @@ def generate_dataset(
|
|
231 |
)
|
232 |
|
233 |
if num_turns > 4:
|
234 |
-
raise gr.Info(
|
235 |
-
"You can only generate a dataset with 4 or fewer turns. Setting to 4."
|
236 |
-
)
|
237 |
num_turns = 4
|
|
|
238 |
if num_rows > 5000:
|
239 |
-
|
|
|
240 |
"You can only generate a dataset with 5000 or fewer rows. Setting to 5000."
|
241 |
)
|
242 |
-
num_rows = 5000
|
243 |
|
244 |
if num_rows < 50:
|
245 |
duration = 60
|
@@ -264,7 +105,7 @@ def generate_dataset(
|
|
264 |
p.start()
|
265 |
total_steps = 100
|
266 |
for step in range(total_steps):
|
267 |
-
if not p.is_alive():
|
268 |
break
|
269 |
progress(
|
270 |
(step + 1) / total_steps,
|
@@ -339,9 +180,11 @@ if __name__ == "__main__":
|
|
339 |
"""
|
340 |
return code
|
341 |
|
|
|
342 |
def update_pipeline_code(system_prompt):
|
343 |
return generate_pipeline_code(system_prompt)
|
344 |
|
|
|
345 |
with gr.Blocks(
|
346 |
title="⚗️ Distilabel Dataset Generator",
|
347 |
head="⚗️ Distilabel Dataset Generator",
|
@@ -349,7 +192,7 @@ with gr.Blocks(
|
|
349 |
gr.Markdown("## Iterate on a sample dataset")
|
350 |
dataset_description = gr.TextArea(
|
351 |
label="Provide a description of the dataset",
|
352 |
-
value=
|
353 |
)
|
354 |
with gr.Row():
|
355 |
gr.Column(scale=1)
|
@@ -400,7 +243,6 @@ with gr.Blocks(
|
|
400 |
"Once you're satisfied with the sample, generate a larger dataset and push it to the hub. Get <a href='https://huggingface.co/settings/tokens' target='_blank'>a Hugging Face token</a> with write access to the organization you want to push the dataset to."
|
401 |
)
|
402 |
|
403 |
-
|
404 |
with gr.Column() as push_to_hub_ui:
|
405 |
with gr.Row(variant="panel"):
|
406 |
num_turns = gr.Number(
|
@@ -432,31 +274,30 @@ with gr.Blocks(
|
|
432 |
success_message = gr.Markdown(visible=False)
|
433 |
|
434 |
def show_success_message(repo_id_value):
|
435 |
-
return gr.update(
|
|
|
436 |
<div style="padding: 1em; background-color: #e6f3e6; border-radius: 5px; margin-top: 1em;">
|
437 |
<h3 style="color: #2e7d32; margin: 0;">Dataset Published Successfully!</h3>
|
438 |
<p style="margin-top: 0.5em;">
|
439 |
-
Your dataset is now available at:
|
440 |
<a href="https://huggingface.co/datasets/{repo_id_value}" target="_blank" style="color: #1565c0; text-decoration: none;">
|
441 |
https://huggingface.co/datasets/{repo_id_value}
|
442 |
</a>
|
443 |
</p>
|
444 |
</div>
|
445 |
-
""",
|
|
|
|
|
446 |
|
447 |
btn_generate_full_dataset.click(
|
448 |
fn=generate_dataset,
|
449 |
inputs=[system_prompt, num_turns, num_rows, private, repo_id, hf_token],
|
450 |
outputs=[table],
|
451 |
show_progress=True,
|
452 |
-
).then(
|
453 |
-
fn=show_success_message,
|
454 |
-
inputs=[repo_id],
|
455 |
-
outputs=[success_message]
|
456 |
-
)
|
457 |
|
458 |
gr.Markdown("## Or run this pipeline locally with distilabel")
|
459 |
-
|
460 |
with gr.Accordion("Run this pipeline on Distilabel", open=False):
|
461 |
pipeline_code = gr.Code(language="python", label="Distilabel Pipeline Code")
|
462 |
|
@@ -464,4 +305,4 @@ with gr.Blocks(
|
|
464 |
fn=update_pipeline_code,
|
465 |
inputs=[system_prompt],
|
466 |
outputs=[pipeline_code],
|
467 |
-
)
|
|
|
4 |
import gradio as gr
|
5 |
import pandas as pd
|
6 |
from distilabel.distiset import Distiset
|
|
|
|
|
|
|
|
|
7 |
from huggingface_hub import whoami
|
8 |
|
9 |
+
from src.distilabel_dataset_generator.pipelines.sft import (
|
10 |
+
DEFAULT_DATASET,
|
11 |
+
DEFAULT_DATASET_DESCRIPTION,
|
12 |
+
DEFAULT_SYSTEM_PROMPT,
|
13 |
+
MODEL,
|
14 |
+
PROMPT_CREATION_PROMPT,
|
15 |
+
get_pipeline,
|
16 |
+
get_prompt_generation_step,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
)
|
18 |
|
19 |
|
20 |
+
def _run_pipeline(result_queue, num_turns, num_rows, system_prompt):
|
21 |
+
pipeline = get_pipeline(
|
22 |
+
num_turns,
|
23 |
+
num_rows,
|
24 |
+
system_prompt,
|
25 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
distiset: Distiset = pipeline.run(use_cache=False)
|
27 |
result_queue.put(distiset)
|
28 |
|
29 |
|
30 |
def generate_system_prompt(dataset_description, progress=gr.Progress()):
|
31 |
progress(0.1, desc="Initializing text generation")
|
32 |
+
generate_description = get_prompt_generation_step()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
progress(0.4, desc="Loading model")
|
34 |
generate_description.load()
|
35 |
progress(0.7, desc="Generating system prompt")
|
|
|
74 |
)
|
75 |
|
76 |
if num_turns > 4:
|
|
|
|
|
|
|
77 |
num_turns = 4
|
78 |
+
gr.Info("You can only generate a dataset with 4 or fewer turns. Setting to 4.")
|
79 |
if num_rows > 5000:
|
80 |
+
num_rows = 5000
|
81 |
+
gr.Info(
|
82 |
"You can only generate a dataset with 5000 or fewer rows. Setting to 5000."
|
83 |
)
|
|
|
84 |
|
85 |
if num_rows < 50:
|
86 |
duration = 60
|
|
|
105 |
p.start()
|
106 |
total_steps = 100
|
107 |
for step in range(total_steps):
|
108 |
+
if not p.is_alive() or p._popen.poll() is not None:
|
109 |
break
|
110 |
progress(
|
111 |
(step + 1) / total_steps,
|
|
|
180 |
"""
|
181 |
return code
|
182 |
|
183 |
+
|
184 |
def update_pipeline_code(system_prompt):
|
185 |
return generate_pipeline_code(system_prompt)
|
186 |
|
187 |
+
|
188 |
with gr.Blocks(
|
189 |
title="⚗️ Distilabel Dataset Generator",
|
190 |
head="⚗️ Distilabel Dataset Generator",
|
|
|
192 |
gr.Markdown("## Iterate on a sample dataset")
|
193 |
dataset_description = gr.TextArea(
|
194 |
label="Provide a description of the dataset",
|
195 |
+
value=DEFAULT_DATASET_DESCRIPTION,
|
196 |
)
|
197 |
with gr.Row():
|
198 |
gr.Column(scale=1)
|
|
|
243 |
"Once you're satisfied with the sample, generate a larger dataset and push it to the hub. Get <a href='https://huggingface.co/settings/tokens' target='_blank'>a Hugging Face token</a> with write access to the organization you want to push the dataset to."
|
244 |
)
|
245 |
|
|
|
246 |
with gr.Column() as push_to_hub_ui:
|
247 |
with gr.Row(variant="panel"):
|
248 |
num_turns = gr.Number(
|
|
|
274 |
success_message = gr.Markdown(visible=False)
|
275 |
|
276 |
def show_success_message(repo_id_value):
|
277 |
+
return gr.update(
|
278 |
+
value=f"""
|
279 |
<div style="padding: 1em; background-color: #e6f3e6; border-radius: 5px; margin-top: 1em;">
|
280 |
<h3 style="color: #2e7d32; margin: 0;">Dataset Published Successfully!</h3>
|
281 |
<p style="margin-top: 0.5em;">
|
282 |
+
Your dataset is now available at:
|
283 |
<a href="https://huggingface.co/datasets/{repo_id_value}" target="_blank" style="color: #1565c0; text-decoration: none;">
|
284 |
https://huggingface.co/datasets/{repo_id_value}
|
285 |
</a>
|
286 |
</p>
|
287 |
</div>
|
288 |
+
""",
|
289 |
+
visible=True,
|
290 |
+
)
|
291 |
|
292 |
btn_generate_full_dataset.click(
|
293 |
fn=generate_dataset,
|
294 |
inputs=[system_prompt, num_turns, num_rows, private, repo_id, hf_token],
|
295 |
outputs=[table],
|
296 |
show_progress=True,
|
297 |
+
).then(fn=show_success_message, inputs=[repo_id], outputs=[success_message])
|
|
|
|
|
|
|
|
|
298 |
|
299 |
gr.Markdown("## Or run this pipeline locally with distilabel")
|
300 |
+
|
301 |
with gr.Accordion("Run this pipeline on Distilabel", open=False):
|
302 |
pipeline_code = gr.Code(language="python", label="Distilabel Pipeline Code")
|
303 |
|
|
|
305 |
fn=update_pipeline_code,
|
306 |
inputs=[system_prompt],
|
307 |
outputs=[pipeline_code],
|
308 |
+
)
|
src/distilabel_dataset_generator/pipelines/sft.py
ADDED
@@ -0,0 +1,198 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from distilabel.llms import InferenceEndpointsLLM
|
3 |
+
from distilabel.pipeline import Pipeline
|
4 |
+
from distilabel.steps import KeepColumns
|
5 |
+
from distilabel.steps.tasks import MagpieGenerator, TextGeneration
|
6 |
+
|
7 |
+
INFORMATION_SEEKING_PROMPT = (
|
8 |
+
"You are an AI assistant designed to provide accurate and concise information on a wide"
|
9 |
+
" range of topics. Your purpose is to assist users in finding specific facts,"
|
10 |
+
" explanations, or details about various subjects. Provide clear, factual responses and,"
|
11 |
+
" when appropriate, offer additional context or related information that might be useful"
|
12 |
+
" to the user."
|
13 |
+
)
|
14 |
+
|
15 |
+
REASONING_PROMPT = (
|
16 |
+
"You are an AI assistant specialized in logical thinking and problem-solving. Your"
|
17 |
+
" purpose is to help users work through complex ideas, analyze situations, and draw"
|
18 |
+
" conclusions based on given information. Approach each query with structured thinking,"
|
19 |
+
" break down problems into manageable parts, and guide users through the reasoning"
|
20 |
+
" process step-by-step."
|
21 |
+
)
|
22 |
+
|
23 |
+
PLANNING_PROMPT = (
|
24 |
+
"You are an AI assistant focused on helping users create effective plans and strategies."
|
25 |
+
" Your purpose is to assist in organizing thoughts, setting goals, and developing"
|
26 |
+
" actionable steps for various projects or activities. Offer structured approaches,"
|
27 |
+
" consider potential challenges, and provide tips for efficient execution of plans."
|
28 |
+
)
|
29 |
+
|
30 |
+
EDITING_PROMPT = (
|
31 |
+
"You are an AI assistant specialized in editing and improving written content. Your"
|
32 |
+
" purpose is to help users refine their writing by offering suggestions for grammar,"
|
33 |
+
" style, clarity, and overall structure. Provide constructive feedback, explain your"
|
34 |
+
" edits, and offer alternative phrasings when appropriate."
|
35 |
+
)
|
36 |
+
|
37 |
+
CODING_DEBUGGING_PROMPT = (
|
38 |
+
"You are an AI assistant designed to help with programming tasks. Your purpose is to"
|
39 |
+
" assist users in writing, reviewing, and debugging code across various programming"
|
40 |
+
" languages. Provide clear explanations, offer best practices, and help troubleshoot"
|
41 |
+
" issues. When appropriate, suggest optimizations or alternative approaches to coding"
|
42 |
+
" problems."
|
43 |
+
)
|
44 |
+
|
45 |
+
MATH_SYSTEM_PROMPT = (
|
46 |
+
"You are an AI assistant designed to provide helpful, step-by-step guidance on solving"
|
47 |
+
" math problems. The user will ask you a wide range of complex mathematical questions."
|
48 |
+
" Your purpose is to assist users in understanding mathematical concepts, working through"
|
49 |
+
" equations, and arriving at the correct solutions."
|
50 |
+
)
|
51 |
+
|
52 |
+
ROLE_PLAYING_PROMPT = (
|
53 |
+
"You are an AI assistant capable of engaging in various role-playing scenarios. Your"
|
54 |
+
" purpose is to adopt different personas or characters as requested by the user. Maintain"
|
55 |
+
" consistency with the chosen role, respond in character, and help create immersive and"
|
56 |
+
" interactive experiences for the user."
|
57 |
+
)
|
58 |
+
|
59 |
+
DATA_ANALYSIS_PROMPT = (
|
60 |
+
"You are an AI assistant specialized in data analysis and interpretation. Your purpose is"
|
61 |
+
" to help users understand and derive insights from data sets, statistics, and analytical"
|
62 |
+
" tasks. Offer clear explanations of data trends, assist with statistical calculations,"
|
63 |
+
" and provide guidance on data visualization and interpretation techniques."
|
64 |
+
)
|
65 |
+
|
66 |
+
CREATIVE_WRITING_PROMPT = (
|
67 |
+
"You are an AI assistant designed to support creative writing endeavors. Your purpose is"
|
68 |
+
" to help users craft engaging stories, poems, and other creative texts. Offer"
|
69 |
+
" suggestions for plot development, character creation, dialogue writing, and other"
|
70 |
+
" aspects of creative composition. Provide constructive feedback and inspire creativity."
|
71 |
+
)
|
72 |
+
|
73 |
+
ADVICE_SEEKING_PROMPT = (
|
74 |
+
"You are an AI assistant focused on providing thoughtful advice and guidance. Your"
|
75 |
+
" purpose is to help users navigate various personal or professional issues by offering"
|
76 |
+
" balanced perspectives, considering potential outcomes, and suggesting practical"
|
77 |
+
" solutions. Encourage users to think critically about their situations while providing"
|
78 |
+
" supportive and constructive advice."
|
79 |
+
)
|
80 |
+
|
81 |
+
BRAINSTORMING_PROMPT = (
|
82 |
+
"You are an AI assistant specialized in generating ideas and facilitating creative"
|
83 |
+
" thinking. Your purpose is to help users explore possibilities, think outside the box,"
|
84 |
+
" and develop innovative concepts. Encourage free-flowing thoughts, offer diverse"
|
85 |
+
" perspectives, and help users build upon and refine their ideas."
|
86 |
+
)
|
87 |
+
|
88 |
+
PROMPT_CREATION_PROMPT = f"""You are an AI assistant specialized in generating very precise prompts for dataset creation.
|
89 |
+
Your task is to write a prompt following the instruction of the user. Respond with the prompt and nothing else.
|
90 |
+
The prompt you write should follow the same style and structure as the following example prompts:
|
91 |
+
|
92 |
+
{INFORMATION_SEEKING_PROMPT}
|
93 |
+
|
94 |
+
{REASONING_PROMPT}
|
95 |
+
|
96 |
+
{PLANNING_PROMPT}
|
97 |
+
|
98 |
+
{CODING_DEBUGGING_PROMPT}
|
99 |
+
|
100 |
+
{EDITING_PROMPT}
|
101 |
+
|
102 |
+
{ROLE_PLAYING_PROMPT}
|
103 |
+
|
104 |
+
{DATA_ANALYSIS_PROMPT}
|
105 |
+
|
106 |
+
{CREATIVE_WRITING_PROMPT}
|
107 |
+
|
108 |
+
{ADVICE_SEEKING_PROMPT}
|
109 |
+
|
110 |
+
{BRAINSTORMING_PROMPT}
|
111 |
+
|
112 |
+
User dataset description:
|
113 |
+
"""
|
114 |
+
|
115 |
+
MODEL = "meta-llama/Meta-Llama-3.1-70B-Instruct"
|
116 |
+
DEFAULT_DATASET_DESCRIPTION = (
|
117 |
+
"A chemistry dataset for an assistant that explains chemical reactions and formulas"
|
118 |
+
)
|
119 |
+
DEFAULT_SYSTEM_PROMPT = "You are an AI assistant specializing in chemistry and chemical reactions. Your purpose is to help users understand and work with chemical formulas, equations, and reactions. Provide clear explanations of reaction mechanisms, assist in balancing chemical equations, and offer guidance on the interpretation of chemical structures. Explain the roles of reactants, products, catalysts, and solvents, and define key chemistry terms when necessary."
|
120 |
+
DEFAULT_DATASET = pd.DataFrame(
|
121 |
+
{
|
122 |
+
"instruction": [
|
123 |
+
"What is the term for the study of the structure and evolution of the Earth's interior. "
|
124 |
+
],
|
125 |
+
"response": [
|
126 |
+
"""The study of the structure and evolution of the Earth's interior is called geophysics, particularly the subfield of geology known as geodynamics, and more specifically the subfield of geology known as geotectonics. However, a more specific term for this study is "geology of the Earth's interior" or "Earth internal structure." However, the most commonly used term for this study is geophysics. """
|
127 |
+
],
|
128 |
+
}
|
129 |
+
)
|
130 |
+
|
131 |
+
|
132 |
+
def get_pipeline(num_turns, num_rows, system_prompt):
|
133 |
+
if num_turns == 1:
|
134 |
+
output_mappings = {"instruction": "prompt", "response": "completion"}
|
135 |
+
else:
|
136 |
+
output_mappings = {"conversation": "messages"}
|
137 |
+
with Pipeline(name="sft") as pipeline:
|
138 |
+
magpie = MagpieGenerator(
|
139 |
+
llm=InferenceEndpointsLLM(
|
140 |
+
model_id=MODEL,
|
141 |
+
tokenizer_id=MODEL,
|
142 |
+
magpie_pre_query_template="llama3",
|
143 |
+
generation_kwargs={
|
144 |
+
"temperature": 0.8, # it's the best value for Llama 3.1 70B Instruct
|
145 |
+
"do_sample": True,
|
146 |
+
"max_new_tokens": 2048,
|
147 |
+
"stop_sequences": [
|
148 |
+
"<|eot_id|>",
|
149 |
+
"<|end_of_text|>",
|
150 |
+
"<|start_header_id|>",
|
151 |
+
"<|end_header_id|>",
|
152 |
+
"assistant",
|
153 |
+
],
|
154 |
+
},
|
155 |
+
),
|
156 |
+
n_turns=num_turns,
|
157 |
+
num_rows=num_rows,
|
158 |
+
system_prompt=system_prompt,
|
159 |
+
output_mappings=output_mappings,
|
160 |
+
)
|
161 |
+
keep_columns = KeepColumns(
|
162 |
+
columns=list(output_mappings.values()) + ["model_name"],
|
163 |
+
)
|
164 |
+
magpie.connect(keep_columns)
|
165 |
+
return pipeline
|
166 |
+
|
167 |
+
|
168 |
+
def get_prompt_generation_step():
|
169 |
+
generate_description = TextGeneration(
|
170 |
+
llm=InferenceEndpointsLLM(
|
171 |
+
model_id=MODEL,
|
172 |
+
tokenizer_id=MODEL,
|
173 |
+
generation_kwargs={
|
174 |
+
"temperature": 0.8,
|
175 |
+
"max_new_tokens": 2048,
|
176 |
+
"do_sample": True,
|
177 |
+
},
|
178 |
+
),
|
179 |
+
use_system_prompt=True,
|
180 |
+
)
|
181 |
+
return generate_description
|
182 |
+
|
183 |
+
|
184 |
+
if __name__ == "__main__":
|
185 |
+
prompt_generation_step = get_prompt_generation_step()
|
186 |
+
prompt_generation_step.load()
|
187 |
+
result = next(
|
188 |
+
prompt_generation_step.process(
|
189 |
+
[
|
190 |
+
{
|
191 |
+
"system_prompt": PROMPT_CREATION_PROMPT,
|
192 |
+
"instruction": DEFAULT_DATASET_DESCRIPTION,
|
193 |
+
}
|
194 |
+
]
|
195 |
+
)
|
196 |
+
)[0]["generation"]
|
197 |
+
pipeline = get_pipeline(num_rows=100, num_turns=1, system_prompt=result)
|
198 |
+
pipeline.run()
|