Spaces:
Running
Running
Applied fallback chains.
Browse files- app/streamlit_sample_generator.py +22 -8
- meta_prompt/sample_generator.py +85 -110
app/streamlit_sample_generator.py
CHANGED
@@ -262,6 +262,18 @@ def apply_suggestions():
|
|
262 |
except Exception as e:
|
263 |
st.warning(f"Failed to update description: {str(e)}")
|
264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
# Streamlit UI
|
266 |
st.title("LLM Task Example Generator")
|
267 |
st.markdown("Enter input-output pairs in the table below to generate a task description, analysis, and additional examples.")
|
@@ -310,6 +322,16 @@ with st.expander("Description and Analysis"):
|
|
310 |
description_output = st.text_area(
|
311 |
"Description", value=st.session_state.description_output_text, height=100)
|
312 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
# Add multiselect for suggestions
|
314 |
selected_suggestions = st.multiselect(
|
315 |
"Suggestions", options=st.session_state.suggestions, key="selected_suggestions")
|
@@ -317,14 +339,6 @@ with st.expander("Description and Analysis"):
|
|
317 |
# Add button to apply suggestions
|
318 |
apply_suggestions_button = st.button("Apply Suggestions", on_click=apply_suggestions)
|
319 |
|
320 |
-
col3, col4 = st.columns(2)
|
321 |
-
with col3:
|
322 |
-
generate_examples_directly_button = st.button(
|
323 |
-
"Generate Examples Directly", on_click=update_examples_directly_dataframe)
|
324 |
-
with col4:
|
325 |
-
analyze_input_button = st.button(
|
326 |
-
"Analyze Input", on_click=update_input_analysis_output_text)
|
327 |
-
|
328 |
examples_directly_output = st.dataframe(st.session_state.examples_directly_dataframe, use_container_width=True,
|
329 |
selection_mode="multi-row", key="selected_example_directly_id",
|
330 |
on_select=example_directly_selected)
|
|
|
262 |
except Exception as e:
|
263 |
st.warning(f"Failed to update description: {str(e)}")
|
264 |
|
265 |
+
def generate_suggestions():
|
266 |
+
try:
|
267 |
+
description = st.session_state.description_output_text
|
268 |
+
input_json = package_input_data()
|
269 |
+
|
270 |
+
model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
|
271 |
+
generator = TaskDescriptionGenerator(model)
|
272 |
+
result = generator.generate_suggestions(input_json, description)
|
273 |
+
st.session_state.suggestions = result["suggestions"]
|
274 |
+
except Exception as e:
|
275 |
+
st.warning(f"Failed to generate suggestions: {str(e)}")
|
276 |
+
|
277 |
# Streamlit UI
|
278 |
st.title("LLM Task Example Generator")
|
279 |
st.markdown("Enter input-output pairs in the table below to generate a task description, analysis, and additional examples.")
|
|
|
322 |
description_output = st.text_area(
|
323 |
"Description", value=st.session_state.description_output_text, height=100)
|
324 |
|
325 |
+
col3, col4, col5 = st.columns(3)
|
326 |
+
with col3:
|
327 |
+
generate_suggestions_button = st.button("Generate Suggestions", on_click=generate_suggestions)
|
328 |
+
with col4:
|
329 |
+
generate_examples_directly_button = st.button(
|
330 |
+
"Generate Examples Directly", on_click=update_examples_directly_dataframe)
|
331 |
+
with col5:
|
332 |
+
analyze_input_button = st.button(
|
333 |
+
"Analyze Input", on_click=update_input_analysis_output_text)
|
334 |
+
|
335 |
# Add multiselect for suggestions
|
336 |
selected_suggestions = st.multiselect(
|
337 |
"Suggestions", options=st.session_state.suggestions, key="selected_suggestions")
|
|
|
339 |
# Add button to apply suggestions
|
340 |
apply_suggestions_button = st.button("Apply Suggestions", on_click=apply_suggestions)
|
341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
examples_directly_output = st.dataframe(st.session_state.examples_directly_dataframe, use_container_width=True,
|
343 |
selection_mode="multi-row", key="selected_example_directly_id",
|
344 |
on_select=example_directly_selected)
|
meta_prompt/sample_generator.py
CHANGED
@@ -63,55 +63,28 @@ Task Description: [Your updated description here]
|
|
63 |
|
64 |
|
65 |
SPECIFICATION_SUGGESTIONS_PROMPT = [
|
66 |
-
("system", """
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
- Start each suggestion with a verb, such as "Limit the scope of supported tasks to..." Make sure it is an actionable, self-contained, and complete suggestion.
|
89 |
-
- Ensure suggestions are compatible with the provided input/output examples.
|
90 |
-
|
91 |
-
5. Output Example:
|
92 |
-
|
93 |
-
```json
|
94 |
-
{{
|
95 |
-
"suggestions": [
|
96 |
-
{{
|
97 |
-
"suggestion": "..."
|
98 |
-
}},
|
99 |
-
{{
|
100 |
-
"suggestion": "..."
|
101 |
-
}},
|
102 |
-
...
|
103 |
-
]
|
104 |
-
}}
|
105 |
-
```
|
106 |
-
|
107 |
-
6. Completeness Check:
|
108 |
-
- Ensure all important aspects of the task description are covered.
|
109 |
-
- Check for any missing key information or dimensions.
|
110 |
-
|
111 |
-
7. Quantity Requirement:
|
112 |
-
- Provide at least 5 specification suggestions across different dimensions.
|
113 |
-
|
114 |
-
Please begin the task directly. After completion, verify that your JSON output meets all requirements and directly addresses the task needs.
|
115 |
|
116 |
***Task Description:***
|
117 |
|
@@ -125,56 +98,28 @@ Please begin the task directly. After completion, verify that your JSON output m
|
|
125 |
]
|
126 |
|
127 |
GENERALIZATION_SUGGESTIONS_PROMPT = [
|
128 |
-
("system", """
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
```json
|
152 |
-
{{
|
153 |
-
"suggestions": [
|
154 |
-
{{
|
155 |
-
"suggestion": "..."
|
156 |
-
}},
|
157 |
-
{{
|
158 |
-
"suggestion": "..."
|
159 |
-
}},
|
160 |
-
...
|
161 |
-
]
|
162 |
-
}}
|
163 |
-
```
|
164 |
-
|
165 |
-
6. Quality Requirements:
|
166 |
-
- Ensure each generalization suggestion is meaningful and feasible.
|
167 |
-
- Provide diverse generalization dimensions to cover different aspects of possible extensions.
|
168 |
-
- Maintain conciseness and clarity in suggestions.
|
169 |
-
|
170 |
-
7. Quantity Requirement:
|
171 |
-
- Provide at least 5 generalization suggestions across different dimensions.
|
172 |
-
|
173 |
-
8. Notes:
|
174 |
-
- Avoid proposing generalizations completely unrelated to the original task.
|
175 |
-
- Ensure the JSON format is correct and can be parsed.
|
176 |
-
|
177 |
-
After completing the task, please check if your output meets all requirements, especially the correctness of the JSON format and the quality of generalization suggestions.
|
178 |
|
179 |
***Task Description:***
|
180 |
|
@@ -184,7 +129,6 @@ After completing the task, please check if your output meets all requirements, e
|
|
184 |
|
185 |
{raw_example}
|
186 |
|
187 |
-
|
188 |
""")
|
189 |
]
|
190 |
|
@@ -262,11 +206,29 @@ containing a JSON array of {generating_batch_size} objects, each with 'input' an
|
|
262 |
]
|
263 |
|
264 |
EXAMPLES_DIRECTLY_PROMPT = [
|
265 |
-
("system", """Given the task type description, and input/output example(s), generate {generating_batch_size}
|
266 |
-
new input/output examples for this task type.
|
267 |
|
268 |
-
Format your response as a valid JSON object with a single key 'examples'
|
269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
|
271 |
***Task Description:***
|
272 |
|
@@ -292,18 +254,19 @@ class TaskDescriptionGenerator:
|
|
292 |
self.examples_directly_prompt = ChatPromptTemplate.from_messages(EXAMPLES_DIRECTLY_PROMPT)
|
293 |
|
294 |
json_model = model.bind(response_format={"type": "json_object"})
|
|
|
295 |
|
296 |
output_parser = StrOutputParser()
|
297 |
json_parse = JsonOutputParser()
|
298 |
|
299 |
self.description_chain = self.description_prompt | model | output_parser
|
300 |
self.description_updating_chain = self.description_updating_prompt | model | output_parser
|
301 |
-
self.specification_suggestions_chain = self.specification_suggestions_prompt | json_model | json_parse
|
302 |
-
self.generalization_suggestions_chain = self.generalization_suggestions_prompt | json_model | json_parse
|
303 |
self.input_analysis_chain = self.input_analysis_prompt | model | output_parser
|
304 |
self.briefs_chain = self.briefs_prompt | model | output_parser
|
305 |
-
self.examples_from_briefs_chain = self.examples_from_briefs_prompt | json_model | json_parse
|
306 |
-
self.examples_directly_chain = self.examples_directly_prompt | json_model | json_parse
|
307 |
|
308 |
# New sub-chain for loading and validating input
|
309 |
self.input_loader = RunnableLambda(self.load_and_validate_input)
|
@@ -381,7 +344,7 @@ class TaskDescriptionGenerator:
|
|
381 |
"suggestions": {
|
382 |
"specification": self.specification_suggestions_chain,
|
383 |
"generalization": self.generalization_suggestions_chain
|
384 |
-
} | RunnableLambda(lambda x: [item['suggestion'] for sublist in [v['suggestions'] for v in x.values()] for item in sublist])
|
385 |
}
|
386 |
)
|
387 |
return chain.invoke({
|
@@ -403,7 +366,7 @@ class TaskDescriptionGenerator:
|
|
403 |
"suggestions": {
|
404 |
"specification": self.specification_suggestions_chain,
|
405 |
"generalization": self.generalization_suggestions_chain
|
406 |
-
} | RunnableLambda(lambda x: [item['suggestion'] for sublist in [v['suggestions'] for v in x.values()] for item in sublist])
|
407 |
}
|
408 |
)
|
409 |
return chain.invoke({
|
@@ -412,6 +375,18 @@ class TaskDescriptionGenerator:
|
|
412 |
"suggestions": suggestions_str
|
413 |
})
|
414 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
def analyze_input(self, description):
|
416 |
return self.input_analysis_chain.invoke(description)
|
417 |
|
|
|
63 |
|
64 |
|
65 |
SPECIFICATION_SUGGESTIONS_PROMPT = [
|
66 |
+
("system", """Generate suggestions to narrow the task scope for a given task type and example:
|
67 |
+
|
68 |
+
1. Analyze the task description and input/output examples.
|
69 |
+
2. Identify 3~5 relevant dimensions (e.g., purpose, input/output format, language, steps, criteria, constraints).
|
70 |
+
3. Create 3~5 actionable suggestions to narrow the task scope based on the above dimensions. Make sure the suggestions are compatible with the provided example.
|
71 |
+
4. Start each suggestion with a verb.
|
72 |
+
5. Output in JSON format:
|
73 |
+
|
74 |
+
```json
|
75 |
+
{{
|
76 |
+
"dimensions": [
|
77 |
+
{{ "dimension": "..." }},
|
78 |
+
{{ "dimension": "..." }}
|
79 |
+
],
|
80 |
+
"suggestions": [
|
81 |
+
{{ "suggestion": "..." }},
|
82 |
+
{{ "suggestion": "..." }}
|
83 |
+
]
|
84 |
+
}}
|
85 |
+
```
|
86 |
+
|
87 |
+
Ensure suggestions are feasible, diverse, concise, and related to the original task. Verify JSON format is correct.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
***Task Description:***
|
90 |
|
|
|
98 |
]
|
99 |
|
100 |
GENERALIZATION_SUGGESTIONS_PROMPT = [
|
101 |
+
("system", """Generate task generalization suggestions for a given task type and example:
|
102 |
+
|
103 |
+
1. Analyze the task description and input/output examples.
|
104 |
+
2. Identify 3~5 relevant dimensions (e.g., purpose, input/output format, language, steps, criteria, constraints).
|
105 |
+
3. Create 3~5 actionable suggestions to expand the scope of the task based on the above dimensions. Make sure the suggestions are compatible with the provided example.
|
106 |
+
4. Start each suggestion with a verb.
|
107 |
+
5. Output in JSON format:
|
108 |
+
|
109 |
+
```json
|
110 |
+
{{
|
111 |
+
"dimensions": [
|
112 |
+
{{ "dimension": "..." }},
|
113 |
+
{{ "dimension": "..." }}
|
114 |
+
],
|
115 |
+
"suggestions": [
|
116 |
+
{{ "suggestion": "..." }},
|
117 |
+
{{ "suggestion": "..." }}
|
118 |
+
]
|
119 |
+
}}
|
120 |
+
```
|
121 |
+
|
122 |
+
Ensure suggestions are feasible, diverse, concise, and related to the original task. Verify JSON format is correct.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
***Task Description:***
|
125 |
|
|
|
129 |
|
130 |
{raw_example}
|
131 |
|
|
|
132 |
""")
|
133 |
]
|
134 |
|
|
|
206 |
]
|
207 |
|
208 |
EXAMPLES_DIRECTLY_PROMPT = [
|
209 |
+
("system", """Given the task type description, and input/output example(s), generate {generating_batch_size} new input/output examples for this task type.
|
|
|
210 |
|
211 |
+
Format your response as a valid JSON object with a single key 'examples' containing a JSON array of {generating_batch_size} objects, each with 'input' and 'output' fields.
|
212 |
+
|
213 |
+
Format example:
|
214 |
+
|
215 |
+
```json
|
216 |
+
{{
|
217 |
+
"examples": [
|
218 |
+
{{
|
219 |
+
"input": "...",
|
220 |
+
"output": "..."
|
221 |
+
}},
|
222 |
+
{{
|
223 |
+
"input": "...",
|
224 |
+
"output": "..."
|
225 |
+
}},
|
226 |
+
...
|
227 |
+
]
|
228 |
+
}}
|
229 |
+
```
|
230 |
+
|
231 |
+
After completing the task, please check if your output meets all requirements, especially the correctness of the JSON format and the quality of generalization suggestions. Validate the JSON format to ensure it can be parsed correctly.
|
232 |
|
233 |
***Task Description:***
|
234 |
|
|
|
254 |
self.examples_directly_prompt = ChatPromptTemplate.from_messages(EXAMPLES_DIRECTLY_PROMPT)
|
255 |
|
256 |
json_model = model.bind(response_format={"type": "json_object"})
|
257 |
+
# json_model = model
|
258 |
|
259 |
output_parser = StrOutputParser()
|
260 |
json_parse = JsonOutputParser()
|
261 |
|
262 |
self.description_chain = self.description_prompt | model | output_parser
|
263 |
self.description_updating_chain = self.description_updating_prompt | model | output_parser
|
264 |
+
self.specification_suggestions_chain = (self.specification_suggestions_prompt | json_model | json_parse).with_fallbacks([RunnableLambda(lambda x: {"dimensions": [], "suggestions": []})])
|
265 |
+
self.generalization_suggestions_chain = (self.generalization_suggestions_prompt | json_model | json_parse).with_fallbacks([RunnableLambda(lambda x: {"dimensions": [], "suggestions": []})])
|
266 |
self.input_analysis_chain = self.input_analysis_prompt | model | output_parser
|
267 |
self.briefs_chain = self.briefs_prompt | model | output_parser
|
268 |
+
self.examples_from_briefs_chain = (self.examples_from_briefs_prompt | json_model | json_parse).with_fallbacks([RunnableLambda(lambda x: {"examples": []})])
|
269 |
+
self.examples_directly_chain = (self.examples_directly_prompt | json_model | json_parse).with_fallbacks([RunnableLambda(lambda x: {"examples": []})])
|
270 |
|
271 |
# New sub-chain for loading and validating input
|
272 |
self.input_loader = RunnableLambda(self.load_and_validate_input)
|
|
|
344 |
"suggestions": {
|
345 |
"specification": self.specification_suggestions_chain,
|
346 |
"generalization": self.generalization_suggestions_chain
|
347 |
+
} | RunnableLambda(lambda x: [item['suggestion'] for sublist in [v['suggestions'] for v in x.values() if 'suggestions' in v] for item in sublist if 'suggestion' in item])
|
348 |
}
|
349 |
)
|
350 |
return chain.invoke({
|
|
|
366 |
"suggestions": {
|
367 |
"specification": self.specification_suggestions_chain,
|
368 |
"generalization": self.generalization_suggestions_chain
|
369 |
+
} | RunnableLambda(lambda x: [item['suggestion'] for sublist in [v['suggestions'] for v in x.values() if 'suggestions' in v] for item in sublist if 'suggestion' in item])
|
370 |
}
|
371 |
)
|
372 |
return chain.invoke({
|
|
|
375 |
"suggestions": suggestions_str
|
376 |
})
|
377 |
|
378 |
+
def generate_suggestions(self, input_str, description):
|
379 |
+
chain = RunnablePassthrough.assign(
|
380 |
+
suggestions={
|
381 |
+
"specification": self.specification_suggestions_chain,
|
382 |
+
"generalization": self.generalization_suggestions_chain
|
383 |
+
} | RunnableLambda(lambda x: [item['suggestion'] for sublist in [v['suggestions'] for v in x.values() if 'suggestions' in v] for item in sublist if 'suggestion' in item])
|
384 |
+
)
|
385 |
+
return chain.invoke({
|
386 |
+
"description": description,
|
387 |
+
"raw_example": input_str
|
388 |
+
})
|
389 |
+
|
390 |
def analyze_input(self, description):
|
391 |
return self.input_analysis_chain.invoke(description)
|
392 |
|