Spaces:
Sleeping
Sleeping
Added sample_generator.ipynb.
Browse files- demo/sample_generator.ipynb +196 -0
demo/sample_generator.ipynb
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import os\n",
|
10 |
+
"\n",
|
11 |
+
"# Load configuration from YAML file\n",
|
12 |
+
"config = {\n",
|
13 |
+
" \"model_name\": \"llama3-70b-8192\",\n",
|
14 |
+
"}\n"
|
15 |
+
]
|
16 |
+
},
|
17 |
+
{
|
18 |
+
"cell_type": "code",
|
19 |
+
"execution_count": 2,
|
20 |
+
"metadata": {},
|
21 |
+
"outputs": [
|
22 |
+
{
|
23 |
+
"name": "stderr",
|
24 |
+
"output_type": "stream",
|
25 |
+
"text": [
|
26 |
+
"/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",
|
27 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
28 |
+
]
|
29 |
+
},
|
30 |
+
{
|
31 |
+
"name": "stdout",
|
32 |
+
"output_type": "stream",
|
33 |
+
"text": [
|
34 |
+
"Running on local URL: http://127.0.0.1:7861\n",
|
35 |
+
"\n",
|
36 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
37 |
+
]
|
38 |
+
},
|
39 |
+
{
|
40 |
+
"data": {
|
41 |
+
"text/html": [
|
42 |
+
"<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
43 |
+
],
|
44 |
+
"text/plain": [
|
45 |
+
"<IPython.core.display.HTML object>"
|
46 |
+
]
|
47 |
+
},
|
48 |
+
"metadata": {},
|
49 |
+
"output_type": "display_data"
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"name": "stderr",
|
53 |
+
"output_type": "stream",
|
54 |
+
"text": [
|
55 |
+
"/home/yale/work/meta-prompt/.venv/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:141: LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 0.3.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.\n",
|
56 |
+
" warn_deprecated(\n"
|
57 |
+
]
|
58 |
+
}
|
59 |
+
],
|
60 |
+
"source": [
|
61 |
+
"import json\n",
|
62 |
+
"import yaml\n",
|
63 |
+
"from langchain.prompts import ChatPromptTemplate\n",
|
64 |
+
"from langchain.chat_models import ChatOpenAI\n",
|
65 |
+
"from langchain.schema.output_parser import StrOutputParser\n",
|
66 |
+
"\n",
|
67 |
+
"class TaskDescriptionGenerator:\n",
|
68 |
+
" def __init__(self, model_temperature=0.7):\n",
|
69 |
+
" self.model = ChatOpenAI(model=config[\"model_name\"], temperature=model_temperature)\n",
|
70 |
+
" self.output_parser = StrOutputParser()\n",
|
71 |
+
" \n",
|
72 |
+
" self.description_prompt = ChatPromptTemplate.from_template(\n",
|
73 |
+
" \"\"\"Given the following JSON example for a task type:\n",
|
74 |
+
" {raw_example}\n",
|
75 |
+
"\n",
|
76 |
+
" Provide a concise description of the task type.\n",
|
77 |
+
"\n",
|
78 |
+
" Format your response as follows:\n",
|
79 |
+
" Task Description: [Your description here]\n",
|
80 |
+
" \"\"\"\n",
|
81 |
+
" )\n",
|
82 |
+
"\n",
|
83 |
+
" self.examples_prompt = ChatPromptTemplate.from_template(\n",
|
84 |
+
" \"\"\"Given the following task type description and JSON example:\n",
|
85 |
+
" Task Description: {description}\n",
|
86 |
+
" \n",
|
87 |
+
" Example:\n",
|
88 |
+
" {raw_example}\n",
|
89 |
+
"\n",
|
90 |
+
" Generate 3 more input/output examples for this task type in the same JSON format.\n",
|
91 |
+
"\n",
|
92 |
+
" Format your response as a valid JSON object with a single key 'examples' containing a JSON array of 3 objects, each with 'input' and 'output' fields.\n",
|
93 |
+
" \"\"\"\n",
|
94 |
+
" )\n",
|
95 |
+
"\n",
|
96 |
+
" self.description_chain = self.description_prompt | self.model | self.output_parser\n",
|
97 |
+
" # bind json_object to the model\n",
|
98 |
+
" json_model = self.model.bind(response_format={\"type\": \"json_object\"})\n",
|
99 |
+
" self.examples_chain = self.examples_prompt | json_model | self.output_parser\n",
|
100 |
+
"\n",
|
101 |
+
" def generate_description(self, raw_example_str):\n",
|
102 |
+
" result = self.description_chain.invoke({\n",
|
103 |
+
" \"raw_example\": raw_example_str\n",
|
104 |
+
" })\n",
|
105 |
+
" return result.split(\"Task Description: \")[1].strip()\n",
|
106 |
+
"\n",
|
107 |
+
" def generate_examples(self, description, raw_example_str):\n",
|
108 |
+
" result = self.examples_chain.invoke({\n",
|
109 |
+
" \"description\": description,\n",
|
110 |
+
" \"raw_example\": raw_example_str\n",
|
111 |
+
" })\n",
|
112 |
+
"\n",
|
113 |
+
" try:\n",
|
114 |
+
" result = result.strip()\n",
|
115 |
+
" if result.startswith('```') and result.endswith('```'):\n",
|
116 |
+
" result = result.strip('```').strip()\n",
|
117 |
+
" if result.startswith('json'):\n",
|
118 |
+
" result = result.strip('json').strip()\n",
|
119 |
+
" return json.loads(result)\n",
|
120 |
+
" except json.JSONDecodeError as e:\n",
|
121 |
+
" raise ValueError(f\"The generated examples are not in valid JSON format. Error: {str(e)} Result: {result}\")\n",
|
122 |
+
"\n",
|
123 |
+
" def process(self, input_str):\n",
|
124 |
+
" try:\n",
|
125 |
+
" # Try to parse as JSON\n",
|
126 |
+
" try:\n",
|
127 |
+
" data = json.loads(input_str)\n",
|
128 |
+
" except json.JSONDecodeError:\n",
|
129 |
+
" # If JSON parsing fails, try to parse as YAML\n",
|
130 |
+
" try:\n",
|
131 |
+
" data = yaml.safe_load(input_str)\n",
|
132 |
+
" except yaml.YAMLError as e:\n",
|
133 |
+
" raise ValueError(\"Invalid input string. Error: {}\".format(str(e)))\n",
|
134 |
+
" \n",
|
135 |
+
" if not isinstance(data, dict) or 'input' not in data or 'output' not in data:\n",
|
136 |
+
" raise ValueError(\"Invalid input format. Expected an object with 'input' and 'output' fields.\")\n",
|
137 |
+
" \n",
|
138 |
+
" description = self.generate_description(json.dumps(data))\n",
|
139 |
+
" new_examples = self.generate_examples(description, json.dumps(data))\n",
|
140 |
+
" \n",
|
141 |
+
" output = {\n",
|
142 |
+
" \"task_description\": description,\n",
|
143 |
+
" \"additional_examples\": new_examples\n",
|
144 |
+
" }\n",
|
145 |
+
" \n",
|
146 |
+
" return json.dumps(output, indent=2)\n",
|
147 |
+
" \n",
|
148 |
+
" except Exception as e:\n",
|
149 |
+
" raise RuntimeError(f\"An error occurred during processing: {str(e)}\")\n",
|
150 |
+
" \n",
|
151 |
+
"\n",
|
152 |
+
"import gradio as gr\n",
|
153 |
+
"\n",
|
154 |
+
"def process_json(input_json):\n",
|
155 |
+
" try:\n",
|
156 |
+
" generator = TaskDescriptionGenerator()\n",
|
157 |
+
" result = generator.process(input_json)\n",
|
158 |
+
" return \"Process completed successfully. Result:\\n\" + result\n",
|
159 |
+
" except Exception as e:\n",
|
160 |
+
" return f\"An error occurred: {str(e)}\"\n",
|
161 |
+
"\n",
|
162 |
+
"demo = gr.Interface(\n",
|
163 |
+
" fn=process_json,\n",
|
164 |
+
" inputs=gr.Textbox(label=\"Input JSON\"),\n",
|
165 |
+
" outputs=gr.Textbox(label=\"Output\"),\n",
|
166 |
+
" title=\"Task Description Generator\",\n",
|
167 |
+
" description=\"Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples.\"\n",
|
168 |
+
")\n",
|
169 |
+
"\n",
|
170 |
+
"if __name__ == \"__main__\":\n",
|
171 |
+
" demo.launch()"
|
172 |
+
]
|
173 |
+
}
|
174 |
+
],
|
175 |
+
"metadata": {
|
176 |
+
"kernelspec": {
|
177 |
+
"display_name": ".venv",
|
178 |
+
"language": "python",
|
179 |
+
"name": "python3"
|
180 |
+
},
|
181 |
+
"language_info": {
|
182 |
+
"codemirror_mode": {
|
183 |
+
"name": "ipython",
|
184 |
+
"version": 3
|
185 |
+
},
|
186 |
+
"file_extension": ".py",
|
187 |
+
"mimetype": "text/x-python",
|
188 |
+
"name": "python",
|
189 |
+
"nbconvert_exporter": "python",
|
190 |
+
"pygments_lexer": "ipython3",
|
191 |
+
"version": "3.10.12"
|
192 |
+
}
|
193 |
+
},
|
194 |
+
"nbformat": 4,
|
195 |
+
"nbformat_minor": 2
|
196 |
+
}
|