Spaces:
Running
Running
Update prompts.
Browse files- demo/sample_generator.ipynb +48 -47
demo/sample_generator.ipynb
CHANGED
@@ -2,7 +2,7 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
@@ -20,30 +20,9 @@
|
|
20 |
},
|
21 |
{
|
22 |
"cell_type": "code",
|
23 |
-
"execution_count":
|
24 |
"metadata": {},
|
25 |
-
"outputs": [
|
26 |
-
{
|
27 |
-
"name": "stderr",
|
28 |
-
"output_type": "stream",
|
29 |
-
"text": [
|
30 |
-
"/home/yale/work/meta-prompt/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
31 |
-
" from .autonotebook import tqdm as notebook_tqdm\n"
|
32 |
-
]
|
33 |
-
},
|
34 |
-
{
|
35 |
-
"ename": "TypeError",
|
36 |
-
"evalue": "Dataframe.__init__() got an unexpected keyword argument 'format'",
|
37 |
-
"output_type": "error",
|
38 |
-
"traceback": [
|
39 |
-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
40 |
-
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
41 |
-
"Cell \u001b[0;32mIn[2], line 139\u001b[0m\n\u001b[1;32m 131\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 132\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m gr\u001b[38;5;241m.\u001b[39mError(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAn error occurred: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mstr\u001b[39m(e)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 134\u001b[0m demo \u001b[38;5;241m=\u001b[39m gr\u001b[38;5;241m.\u001b[39mInterface(\n\u001b[1;32m 135\u001b[0m fn\u001b[38;5;241m=\u001b[39mprocess_json,\n\u001b[1;32m 136\u001b[0m inputs\u001b[38;5;241m=\u001b[39m[gr\u001b[38;5;241m.\u001b[39mTextbox(label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInput JSON\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[1;32m 137\u001b[0m gr\u001b[38;5;241m.\u001b[39mSlider(label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGenerating Batch Size\u001b[39m\u001b[38;5;124m\"\u001b[39m, value\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m, minimum\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m, maximum\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m10\u001b[39m, step\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)],\n\u001b[1;32m 138\u001b[0m outputs\u001b[38;5;241m=\u001b[39m[gr\u001b[38;5;241m.\u001b[39mTextbox(label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDescription\u001b[39m\u001b[38;5;124m\"\u001b[39m),\n\u001b[0;32m--> 139\u001b[0m \u001b[43mgr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mDataFrame\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlabel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mExamples\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\n\u001b[1;32m 140\u001b[0m \u001b[43m \u001b[49m\u001b[43mheaders\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mInput\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mOutput\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 141\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mformat\u001b[39;49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmarkdown\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m],\n\u001b[1;32m 142\u001b[0m title\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mTask Description Generator\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 143\u001b[0m description\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnter a JSON object with \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minput\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m and \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124moutput\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m fields to generate a task description and additional examples.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 144\u001b[0m )\n\u001b[1;32m 146\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 147\u001b[0m demo\u001b[38;5;241m.\u001b[39mlaunch()\n",
|
42 |
-
"File \u001b[0;32m~/work/meta-prompt/.venv/lib/python3.10/site-packages/gradio/component_meta.py:163\u001b[0m, in \u001b[0;36mupdateable.<locals>.wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 163\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
|
43 |
-
"\u001b[0;31mTypeError\u001b[0m: Dataframe.__init__() got an unexpected keyword argument 'format'"
|
44 |
-
]
|
45 |
-
}
|
46 |
-
],
|
47 |
"source": [
|
48 |
"import json\n",
|
49 |
"import yaml\n",
|
@@ -55,14 +34,16 @@
|
|
55 |
"from langchain.output_parsers import YamlOutputParser\n",
|
56 |
"\n",
|
57 |
"# Define prompt strings as constants\n",
|
58 |
-
"DESCRIPTION_PROMPT = \
|
|
|
59 |
"{raw_example}\n",
|
60 |
"\n",
|
61 |
"Provide a concise description of the task type, including the format and style of the output.\n",
|
62 |
"\n",
|
63 |
"Format your response as follows:\n",
|
64 |
"Task Description: [Your description here]\n",
|
65 |
-
"\"\"\"\n",
|
|
|
66 |
"\n",
|
67 |
"BRIEFS_PROMPT = [\n",
|
68 |
" (\"system\", \"\"\"Given the task type description, generate descriptions for {generating_batch_size} new \n",
|
@@ -104,24 +85,31 @@
|
|
104 |
"\"\"\")\n",
|
105 |
"]\n",
|
106 |
"\n",
|
107 |
-
"EXAMPLES_PROMPT = \
|
108 |
-
"
|
109 |
-
"
|
110 |
-
"\n",
|
111 |
-
"Generate {generating_batch_size} new input/output examples for this task type.\n",
|
112 |
"\n",
|
113 |
"Format your response as a valid JSON object with a single key 'examples' \n",
|
114 |
"containing a JSON array of {generating_batch_size} objects, each with 'input' and 'output' fields.\n",
|
115 |
-
"\"\"\"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
"\n",
|
117 |
"class TaskDescriptionGenerator:\n",
|
118 |
-
" def __init__(self,
|
119 |
-
" self.description_prompt = ChatPromptTemplate.
|
120 |
" self.briefs_prompt = ChatPromptTemplate.from_messages(BRIEFS_PROMPT)\n",
|
121 |
" self.examples_from_briefs_prompt = ChatPromptTemplate.from_messages(EXAMPLES_FROM_BRIEFS_PROMPT)\n",
|
122 |
-
" self.examples_prompt = ChatPromptTemplate.
|
123 |
"\n",
|
124 |
-
" model = ChatOpenAI(model=config[\"model_name\"], temperature=model_temperature)\n",
|
125 |
" json_model = model.bind(response_format={\"type\": \"json_object\"})\n",
|
126 |
"\n",
|
127 |
" output_parser = StrOutputParser()\n",
|
@@ -163,30 +151,43 @@
|
|
163 |
" return result\n",
|
164 |
"\n",
|
165 |
" except Exception as e:\n",
|
166 |
-
" raise RuntimeError(f\"An error occurred during processing: {str(e)}\")
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
"import gradio as gr\n",
|
169 |
"\n",
|
170 |
-
"def process_json(input_json, generating_batch_size=3):\n",
|
171 |
" try:\n",
|
172 |
-
"
|
|
|
173 |
" result = generator.process(input_json, generating_batch_size)\n",
|
174 |
" description = result[\"description\"]\n",
|
175 |
" examples = [[example[\"input\"], example[\"output\"]] for example in result[\"additional_examples\"]]\n",
|
176 |
-
" print(examples)\n",
|
177 |
" return description, examples\n",
|
178 |
" except Exception as e:\n",
|
179 |
" raise gr.Error(f\"An error occurred: {str(e)}\")\n",
|
180 |
"\n",
|
181 |
"demo = gr.Interface(\n",
|
182 |
" fn=process_json,\n",
|
183 |
-
" inputs=[
|
184 |
-
"
|
185 |
-
"
|
186 |
-
"
|
187 |
-
"
|
|
|
|
|
|
|
|
|
188 |
" title=\"Task Description Generator\",\n",
|
189 |
-
" description=\"Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples.\"
|
|
|
|
|
190 |
")\n",
|
191 |
"\n",
|
192 |
"if __name__ == \"__main__\":\n",
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
|
|
20 |
},
|
21 |
{
|
22 |
"cell_type": "code",
|
23 |
+
"execution_count": null,
|
24 |
"metadata": {},
|
25 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
"source": [
|
27 |
"import json\n",
|
28 |
"import yaml\n",
|
|
|
34 |
"from langchain.output_parsers import YamlOutputParser\n",
|
35 |
"\n",
|
36 |
"# Define prompt strings as constants\n",
|
37 |
+
"DESCRIPTION_PROMPT = [\n",
|
38 |
+
" (\"system\", \"\"\"Given the following JSON example for a task type:\n",
|
39 |
"{raw_example}\n",
|
40 |
"\n",
|
41 |
"Provide a concise description of the task type, including the format and style of the output.\n",
|
42 |
"\n",
|
43 |
"Format your response as follows:\n",
|
44 |
"Task Description: [Your description here]\n",
|
45 |
+
"\"\"\")\n",
|
46 |
+
"]\n",
|
47 |
"\n",
|
48 |
"BRIEFS_PROMPT = [\n",
|
49 |
" (\"system\", \"\"\"Given the task type description, generate descriptions for {generating_batch_size} new \n",
|
|
|
85 |
"\"\"\")\n",
|
86 |
"]\n",
|
87 |
"\n",
|
88 |
+
"EXAMPLES_PROMPT = [\n",
|
89 |
+
" (\"system\", \"\"\"Given the task type description, and input/output example, generate {generating_batch_size}\n",
|
90 |
+
"new input/output examples for this task type.\n",
|
|
|
|
|
91 |
"\n",
|
92 |
"Format your response as a valid JSON object with a single key 'examples' \n",
|
93 |
"containing a JSON array of {generating_batch_size} objects, each with 'input' and 'output' fields.\n",
|
94 |
+
"\"\"\"),\n",
|
95 |
+
" (\"user\", \"\"\"Task Description:\n",
|
96 |
+
"\n",
|
97 |
+
"{description}\n",
|
98 |
+
"\n",
|
99 |
+
"Example:\n",
|
100 |
+
"\n",
|
101 |
+
"{raw_example}\n",
|
102 |
+
"\n",
|
103 |
+
"\"\"\")\n",
|
104 |
+
"]\n",
|
105 |
"\n",
|
106 |
"class TaskDescriptionGenerator:\n",
|
107 |
+
" def __init__(self, model): \n",
|
108 |
+
" self.description_prompt = ChatPromptTemplate.from_messages(DESCRIPTION_PROMPT)\n",
|
109 |
" self.briefs_prompt = ChatPromptTemplate.from_messages(BRIEFS_PROMPT)\n",
|
110 |
" self.examples_from_briefs_prompt = ChatPromptTemplate.from_messages(EXAMPLES_FROM_BRIEFS_PROMPT)\n",
|
111 |
+
" self.examples_prompt = ChatPromptTemplate.from_messages(EXAMPLES_PROMPT)\n",
|
112 |
"\n",
|
|
|
113 |
" json_model = model.bind(response_format={\"type\": \"json_object\"})\n",
|
114 |
"\n",
|
115 |
" output_parser = StrOutputParser()\n",
|
|
|
151 |
" return result\n",
|
152 |
"\n",
|
153 |
" except Exception as e:\n",
|
154 |
+
" raise RuntimeError(f\"An error occurred during processing: {str(e)}\")"
|
155 |
+
]
|
156 |
+
},
|
157 |
+
{
|
158 |
+
"cell_type": "code",
|
159 |
+
"execution_count": null,
|
160 |
+
"metadata": {},
|
161 |
+
"outputs": [],
|
162 |
+
"source": [
|
163 |
"import gradio as gr\n",
|
164 |
"\n",
|
165 |
+
"def process_json(input_json, model_name, generating_batch_size=3):\n",
|
166 |
" try:\n",
|
167 |
+
" model = ChatOpenAI(model=model_name)\n",
|
168 |
+
" generator = TaskDescriptionGenerator(model)\n",
|
169 |
" result = generator.process(input_json, generating_batch_size)\n",
|
170 |
" description = result[\"description\"]\n",
|
171 |
" examples = [[example[\"input\"], example[\"output\"]] for example in result[\"additional_examples\"]]\n",
|
|
|
172 |
" return description, examples\n",
|
173 |
" except Exception as e:\n",
|
174 |
" raise gr.Error(f\"An error occurred: {str(e)}\")\n",
|
175 |
"\n",
|
176 |
"demo = gr.Interface(\n",
|
177 |
" fn=process_json,\n",
|
178 |
+
" inputs=[\n",
|
179 |
+
" gr.Textbox(label=\"Input JSON\"),\n",
|
180 |
+
" gr.Dropdown(label=\"Model Name\", choices=[\"llama3-70b-8192\", \"llama3-8b-8192\", \"llama-3.1-70b-versatile\", \"llama-3.1-8b-instant\", \"gemma2-9b-it\"], value=\"llama3-70b-8192\"),\n",
|
181 |
+
" gr.Slider(label=\"Generating Batch Size\", value=3, minimum=1, maximum=10, step=1)\n",
|
182 |
+
" ],\n",
|
183 |
+
" outputs=[\n",
|
184 |
+
" gr.Textbox(label=\"Description\"),\n",
|
185 |
+
" gr.DataFrame(label=\"Examples\", headers=[\"Input\", \"Output\"])\n",
|
186 |
+
" ],\n",
|
187 |
" title=\"Task Description Generator\",\n",
|
188 |
+
" description=\"Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples.\",\n",
|
189 |
+
" allow_flagging=\"manual\",\n",
|
190 |
+
" flagging_callback=gr.CSVLogger()\n",
|
191 |
")\n",
|
192 |
"\n",
|
193 |
"if __name__ == \"__main__\":\n",
|