{ "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": [ "
" ], "text/plain": [ "" ] }, "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 }