File size: 11,024 Bytes
89ef0d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df523ee
89ef0d1
 
 
 
 
 
df523ee
 
89ef0d1
df523ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89ef0d1
 
 
df523ee
 
89ef0d1
df523ee
 
 
 
 
 
 
 
 
 
 
 
 
 
89ef0d1
 
 
 
df523ee
89ef0d1
 
df523ee
89ef0d1
 
 
 
 
 
 
 
df523ee
 
 
 
 
 
 
 
 
89ef0d1
df523ee
89ef0d1
 
 
 
 
 
 
 
 
 
 
 
df523ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89ef0d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df523ee
 
 
 
 
89ef0d1
 
df523ee
 
89ef0d1
 
df523ee
89ef0d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# Load configuration from YAML file\n",
    "config = {\n",
    "    \"model_name\": \"llama3-70b-8192\",\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/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",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7861\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<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>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/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",
      "  warn_deprecated(\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "import yaml\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.schema.output_parser import StrOutputParser\n",
    "\n",
    "class TaskDescriptionGenerator:\n",
    "    def __init__(self, model_temperature=0.7):\n",
    "        self.model = ChatOpenAI(model=config[\"model_name\"], temperature=model_temperature)\n",
    "        self.output_parser = StrOutputParser()\n",
    "        \n",
    "        self.description_prompt = ChatPromptTemplate.from_template(\n",
    "            \"\"\"Given the following JSON example for a task type:\n",
    "            {raw_example}\n",
    "\n",
    "            Provide a concise description of the task type, including the format and style of the output.\n",
    "\n",
    "            Format your response as follows:\n",
    "            Task Description: [Your description here]\n",
    "            \"\"\"\n",
    "        )\n",
    "\n",
    "        self.briefs_prompt = ChatPromptTemplate.from_template(\n",
    "            \"\"\"Given the following task type description:\n",
    "            Task Description: {description}\n",
    "\n",
    "            Generate descriptions for 3 new examples with detailed attributes\n",
    "            based on this task type.\n",
    "\n",
    "            Format your response as a valid YAML object with a single key\n",
    "            'brief_descriptions' containing a YAML array of 3 objects, each\n",
    "            with a 'description' field.\n",
    "            \"\"\"\n",
    "        )\n",
    "\n",
    "        self.examples_from_briefs_prompt = ChatPromptTemplate.from_template(\n",
    "            \"\"\"Given the following task type description, brief description for new examples, \n",
    "            and JSON example:\n",
    "\n",
    "            Task Description: {description}\n",
    "            Brief Description: {brief_description}\n",
    "\n",
    "            Example:\n",
    "            {raw_example}\n",
    "\n",
    "            Generate 3 more input/output examples for this task type, based on the brief\n",
    "            description, in the same JSON format.\n",
    "\n",
    "            Format your response as a valid JSON object with a single key 'examples' \n",
    "            containing a JSON array of 3 objects, each with 'input' and 'output' fields.\n",
    "            \"\"\"\n",
    "        )\n",
    "\n",
    "        self.examples_prompt = ChatPromptTemplate.from_template(\n",
    "            \"\"\"Given the following task type description, and input/output example:\n",
    "            Task Description: {description}\n",
    "            Example: {example}\n",
    "\n",
    "            Generate 3 new input/output examples for this task type.\n",
    "\n",
    "            Format your response as a valid JSON object with a single key 'examples' \n",
    "            containing a JSON array of 3 objects, each with 'input' and 'output' fields.\n",
    "            \"\"\"\n",
    "        )\n",
    "\n",
    "        self.description_chain = self.description_prompt | self.model | self.output_parser\n",
    "        self.briefs_chain = self.briefs_prompt | self.model | self.output_parser\n",
    "        # bind json_object to the model\n",
    "        json_model = self.model.bind(response_format={\"type\": \"json_object\"})\n",
    "        self.examples_from_briefs_chain = self.examples_from_briefs_prompt | json_model | self.output_parser\n",
    "        self.examples_chain = self.examples_prompt | json_model | self.output_parser\n",
    "\n",
    "    def generate_description(self, raw_example_str):\n",
    "        result = self.description_chain.invoke({\n",
    "            \"raw_example\": raw_example_str\n",
    "        })\n",
    "        return result.split(\"Task Description: \")[1].strip()\n",
    "\n",
    "    def generate_briefs(self, description):\n",
    "        result = self.briefs_chain.invoke({\n",
    "            \"description\": description\n",
    "        })\n",
    "        # return result.split(\"Brief Description: \")[1].strip()\n",
    "        return result\n",
    "\n",
    "    def generate_examples_from_briefs(self, description, brief_description, raw_example_str):\n",
    "        result = self.examples_from_briefs_chain.invoke({\n",
    "            \"description\": description,\n",
    "            \"brief_description\": brief_description,\n",
    "            \"raw_example\": raw_example_str\n",
    "        })\n",
    "\n",
    "        try:\n",
    "            result = result.strip()\n",
    "            if result.startswith('```') and result.endswith('```'):\n",
    "                result = result.strip('```').strip()\n",
    "                if result.startswith('json'):\n",
    "                    result = result.strip('json').strip()\n",
    "            return json.loads(result)\n",
    "        except json.JSONDecodeError as e:\n",
    "            raise ValueError(f\"The generated examples are not in valid JSON format. Error: {str(e)} Result: {result}\")\n",
    "        \n",
    "    def generate_examples(self, description, example):\n",
    "        result = self.examples_chain.invoke({\n",
    "            \"description\": description,\n",
    "            \"example\": example\n",
    "        })\n",
    "        \n",
    "        try:\n",
    "            result = result.strip()\n",
    "            if result.startswith('```') and result.endswith('```'):\n",
    "                result = result.strip('```').strip()\n",
    "                if result.startswith('json'):\n",
    "                    result = result.strip('json').strip()\n",
    "            return json.loads(result)\n",
    "        except json.JSONDecodeError as e:\n",
    "            raise ValueError(f\"The generated examples are not in valid JSON format. Error: {str(e)} Result: {result}\")\n",
    "\n",
    "    def process(self, input_str):\n",
    "        try:\n",
    "            # Try to parse as JSON\n",
    "            try:\n",
    "                data = json.loads(input_str)\n",
    "            except json.JSONDecodeError:\n",
    "                # If JSON parsing fails, try to parse as YAML\n",
    "                try:\n",
    "                    data = yaml.safe_load(input_str)\n",
    "                except yaml.YAMLError as e:\n",
    "                    raise ValueError(\"Invalid input string. Error: {}\".format(str(e)))\n",
    "            \n",
    "            if not isinstance(data, dict) or 'input' not in data or 'output' not in data:\n",
    "                raise ValueError(\"Invalid input format. Expected an object with 'input' and 'output' fields.\")\n",
    "            \n",
    "            description = self.generate_description(json.dumps(data, ensure_ascii=False))\n",
    "            brief_description = self.generate_briefs(description)\n",
    "            new_examples_from_briefs = self.generate_examples_from_briefs(description, brief_description, json.dumps(data, ensure_ascii=False))\n",
    "            new_examples = self.generate_examples(description, json.dumps(data, ensure_ascii=False))\n",
    "    \n",
    "            output = {\n",
    "                \"task_description\": description,\n",
    "                \"brief_description\": brief_description,\n",
    "                \"additional_examples\": list(new_examples_from_briefs.values()) + list(new_examples.values())\n",
    "            }\n",
    "            \n",
    "            return json.dumps(output, indent=2, ensure_ascii=False)\n",
    "        \n",
    "        except Exception as e:\n",
    "            raise RuntimeError(f\"An error occurred during processing: {str(e)}\")\n",
    "    \n",
    "\n",
    "import gradio as gr\n",
    "\n",
    "def process_json(input_json):\n",
    "    try:\n",
    "        generator = TaskDescriptionGenerator()\n",
    "        result = generator.process(input_json)\n",
    "        return \"Process completed successfully. Result:\\n\" + result\n",
    "    except Exception as e:\n",
    "        return f\"An error occurred: {str(e)}\"\n",
    "\n",
    "demo = gr.Interface(\n",
    "    fn=process_json,\n",
    "    inputs=gr.Textbox(label=\"Input JSON\"),\n",
    "    outputs=gr.Textbox(label=\"Output\"),\n",
    "    title=\"Task Description Generator\",\n",
    "    description=\"Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples.\"\n",
    ")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    demo.launch()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}