Skip to content

Instantly share code, notes, and snippets.

@Curtis-64
Last active November 20, 2023 03:58
Show Gist options
  • Save Curtis-64/8dfcbdbe3e10bceb15b77342c5e7c674 to your computer and use it in GitHub Desktop.
Save Curtis-64/8dfcbdbe3e10bceb15b77342c5e7c674 to your computer and use it in GitHub Desktop.
Pipeline Example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 96,
"id": "0e95dba1",
"metadata": {
"code_folding": [
2,
12
]
},
"outputs": [],
"source": [
"# This notebook demonstrates pipelining \n",
"\n",
"import openai\n",
"\n",
"generateCodePrompt = \"Generate the code that satisfies the requirements and return it in Python. Return only the code itself.\"\n",
"improveCodePrompt = \"Improve the code by making it more universal, generous diagnostics, exception handling, and return it in Python. Return only the code itself.\"\n",
"documentCodePrompt = \"Improve the code by documenting as an expert QA and return it in Python. Return only the code itself.\"\n",
"\n",
"\n",
"def get_model_response(user_message, system_prompt):\n",
" # Set the base URL and API key\n",
" openai.api_base = \"http://localhost:1234/v1\" # Local server URL\n",
" openai.api_key = \"\" # No API key needed for local server\n",
"\n",
" # Prepare the messages for the model\n",
" system_message = {\"role\": \"system\", \"content\": system_prompt }\n",
" user_message = {\"role\": \"user\", \"content\": user_message}\n",
"\n",
" # Make the API call\n",
" completion = openai.ChatCompletion.create(\n",
" model=\"local-model\", # Currently unused field\n",
" messages=[system_message, user_message]\n",
" )\n",
"\n",
" # Return the response\n",
" return completion.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": 97,
"id": "e1289c84",
"metadata": {
"code_folding": []
},
"outputs": [],
"source": [
"import re\n",
"def clean_code(code_string):\n",
" # Ensure code_string is a string\n",
" if not isinstance(code_string, str):\n",
" raise ValueError(\"Code must be a string\")\n",
"\n",
" # Use regular expression to extract code after ```python\n",
" # The closing backticks are made optional to handle missing end ticks\n",
" pattern = r'```python(.*?)(```|$)'\n",
" match = re.search(pattern, code_string, re.DOTALL)\n",
" if match:\n",
" # Extract the code part and strip leading/trailing whitespace\n",
" cleaned_code = match.group(1).strip()\n",
" else:\n",
" raise ValueError(\"No valid Python code block found\")\n",
" return cleaned_code"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0801b2cb",
"metadata": {
"code_folding": []
},
"outputs": [],
"source": [
"from IPython.core.magic import register_line_magic\n",
"@register_line_magic\n",
"def execc(command):\n",
" content = get_model_response(command, generateCodeMessage).content\n",
" print(content)\n",
" clean_and_run_code(content)"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "28336343",
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import HTML\n",
"\n",
"def format_code_as_html(code_str):\n",
" \"\"\"\n",
" Formats a given string of Python code as HTML so it is displayed\n",
" with code formatting in a Jupyter notebook output cell.\n",
"\n",
" Args:\n",
" code_str (str): The Python code as a string.\n",
"\n",
" Returns:\n",
" IPython.display.HTML: The HTML representation of the code for IPython Display.\n",
" \"\"\"\n",
" formatted_html = HTML(f\"<pre><code>{code_str}</code></pre>\")\n",
" return formatted_html"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "17891110",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"```python\n",
"print(\"hello world\")\n",
"```\n",
"hello world\n"
]
}
],
"source": [
"%execc lowercase HELLO WORLD"
]
},
{
"cell_type": "code",
"execution_count": 108,
"id": "9f0d4af5",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<pre><code>\n",
"\n",
"Below is the improved version of your code with detailed documentation and error handling for any input that's not suitable for division operation:\n",
"\n",
"```python\n",
"def safe_division(numerator, denominator):\n",
" \"\"\"\n",
" This function performs a division operation while handling ZeroDivisionError gracefully. \n",
" \n",
" :param numerator: The number that should be divided (the dividend).\n",
" :type numerator: int or float\n",
" :param denominator: The number to divide by (the divisor).\n",
" :type denominator: int or float\n",
" :return: The result of the division operation. If the denominator is 0, it returns a string saying \"Cannot divide by zero\".\n",
" :rtype: int or float or str\n",
" \"\"\"\n",
" try:\n",
" return numerator / denominator\n",
" except ZeroDivisionError:\n",
" return \"Cannot divide by zero\"\n",
" \n",
"# Testing the function \n",
"result = safe_division(5, 0)\n",
"print(result) # Output: Cannot divide by zero\n",
"```</code></pre>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"documentedCode = get_model_response(code,documentCodePrompt)\n",
"format_code_as_html(documentedCode)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "d50aa060",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cannot divide by zero\n"
]
}
],
"source": [
"cleaned = clean_code(documentedCode)\n",
"exec(cleaned)"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "5a2da937",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Here is your requested Python code:\n",
"```python\n",
"def Div(a, b):\n",
" try:\n",
" result = a / b\n",
" if type(result) == int:\n",
" return float(result)\n",
" else:\n",
" return result\n",
" except ZeroDivisionError:\n",
" print(\"Error: Division by zero is not allowed!\")\n",
"\n",
"print(Div(5, 34))\n",
"```\n",
"-------------------------------\n",
"\n",
"Your original code doesn't have any exception handling or diagnostic messages. I've improved the function to return a float value regardless of the division result (integer or float) and added error handling for the case when the divisor is zero.\n",
"\n",
"Here's the updated version of your Python code:\n",
"\n",
"```python\n",
"def Div(a, b):\n",
" try:\n",
" result = a / b\n",
" if type(result) == int:\n",
" return float(result)\n",
" else:\n",
" return result\n",
" except ZeroDivisionError:\n",
" print(\"Error: Division by zero is not allowed!\")\n",
"\n",
"print(Div(5, 34))\n",
"```\n",
"-------------------------------\n",
"def Div(a, b):\n",
" \"\"\"\n",
" This function takes two arguments a and b and performs division operation on them.\n",
"\n",
" Args:\n",
" a (int or float): The numerator for division operation.\n",
" b (int or float): The denominator for division operation.\n",
"\n",
" Returns:\n",
" float: If the result is an integer, it returns the result as a float to ensure decimal points are preserved. \n",
" Else, return the result as it is.\n",
"\n",
" Raises:\n",
" ZeroDivisionError: This error is raised when the denominator (b) is zero.\n",
" Division by zero is not allowed in mathematics.\n",
"\n",
" \"\"\"\n",
" try:\n",
" # Perform division operation on a and b\n",
" result = a / b\n",
" \n",
" # Check if the type of result is int, convert it to float and return\n",
" if type(result) == int:\n",
" return float(result)\n",
"\n",
" # If not an int, return the result as it is\n",
" else:\n",
" return result\n",
" \n",
" except ZeroDivisionError:\n",
" print(\"Error: Division by zero is not allowed!\")\n",
"\n",
"# Testing the Div function with example input values 5 and 34\n",
"print(Div(5, 34))\n",
"-------------------------------\n"
]
}
],
"source": [
"code = \"Div(5,34) 'only float\"\n",
"returnedCode = get_model_response(code,generateCodePrompt)\n",
"print(returnedCode)\n",
"print(\"-------------------------------\")\n",
"returnedCode = get_model_response(returnedCode,improveCodePrompt)\n",
"print(returnedCode)\n",
"print(\"-------------------------------\")\n",
"cleanedCode = clean_code(get_model_response(returnedCode,documentCodePrompt))\n",
"print(cleanedCode)\n",
"print(\"-------------------------------\")"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "e4ba0529",
"metadata": {
"code_folding": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.14705882352941177\n"
]
}
],
"source": [
"#cleanedCode += \"\\nDiv(5,3)\"\n",
"exec(cleanedCode)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9765b98",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment