Created
November 19, 2023 12:34
-
-
Save Curtis-64/7f5fbfbc3a44ff6382c5830e251b6767 to your computer and use it in GitHub Desktop.
Execute_Code_Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "0e95dba1", | |
"metadata": { | |
"code_folding": [ | |
2, | |
12 | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"import openai\n", | |
"\n", | |
"def get_model_response(user_message):\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\": \"Generate the code that satisfies the requirements and return it in Python. Return only the code itself.\"}\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" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "e1289c84", | |
"metadata": { | |
"code_folding": [ | |
1 | |
] | |
}, | |
"outputs": [], | |
"source": [ | |
"import re\n", | |
"def clean_and_run_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", | |
"\n", | |
" # Execute the cleaned code\n", | |
" exec(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).content\n", | |
" print(content)\n", | |
" clean_and_run_code(content)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "b3a3ecc4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"```python\n", | |
"def calculate_division():\n", | |
" result = 100 / 5\n", | |
" return result\n", | |
"\n", | |
"print(calculate_division())\n", | |
"```\n", | |
"20.0\n" | |
] | |
} | |
], | |
"source": [ | |
"%execc 100/5" | |
] | |
}, | |
{ | |
"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": 6, | |
"id": "4b348926", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"\n", | |
"Here is a Python code snippet that sorts an array of strings in reverse order based on the given instruction:\n", | |
"\n", | |
"```python\n", | |
"states = [\"alabama\", \"north carolina\", \"new york\", \"california\", \"florida\"]\n", | |
"sorted_states = sorted(states, reverse=True)\n", | |
"print(sorted_states)\n", | |
"```\n", | |
"['north carolina', 'new york', 'florida', 'california', 'alabama']\n" | |
] | |
} | |
], | |
"source": [ | |
"%execc Sort reverse order: alabama, north carolina, new york, california, florida" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "c0f2e8d8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"\n", | |
"```python\n", | |
"def print_word_reversed_with_dashes_for_spaces(text):\n", | |
" words = text.split(\" \")\n", | |
" reversed_words = [word[::-1] for word in words]\n", | |
" result = \"-\".join(reversed_words)\n", | |
" return result\n", | |
"\n", | |
"print(print_word_reversed_with_dashes_for_spaces('hello world'))\n", | |
"```\n", | |
"olleh-dlrow\n" | |
] | |
} | |
], | |
"source": [ | |
"%execc printwordreversedwithdashesforspaces('hello world')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "c0e023a3", | |
"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