Skip to content

Instantly share code, notes, and snippets.

@nkrumm
Created August 24, 2023 22:24
Show Gist options
  • Save nkrumm/2b154ea2041511233079222373c83057 to your computer and use it in GitHub Desktop.
Save nkrumm/2b154ea2041511233079222373c83057 to your computer and use it in GitHub Desktop.
AI decorator Idea
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 93,
"id": "638a8710-56c5-4923-ae48-a73e95e58b1b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import openai\n",
"import inspect\n",
"# Set up OpenAI API\n",
"#openai.api_key = \n",
"\n",
"def generate_code(docstring):\n",
" prompt = f'Generate Python code:\\n\\n{docstring}\\n\\nCode:'\n",
"\n",
" response = openai.Completion.create(\n",
" engine='text-davinci-003',\n",
" prompt=prompt,\n",
" max_tokens=1000,\n",
" n=1,\n",
" stop=None,\n",
" temperature=0.7\n",
" )\n",
"\n",
" return response.choices[0].text.strip()\n",
"\n",
"def decorator(show_code=False):\n",
" def decorator_func(func):\n",
" def wrapper(*args, **kwargs):\n",
" docstring = func.__doc__\n",
" \n",
" # Only generate code once per decorator invocation\n",
" if not wrapper.has_run:\n",
" code = generate_code(docstring)\n",
" \n",
" # If the decorator is called with show_code, print code\n",
" if show_code:\n",
" print(\"---- GPT-3 Generated Code ----\")\n",
" print(code)\n",
" print(\"---- END GPT-3 Generated Code ----\\n\\n\")\n",
" \n",
" # Replace the decorated function's code with GPT-3 code\n",
" func.__code__ = compile(code, func.__code__.co_filename, 'exec')\n",
" wrapper.has_run = True\n",
" \n",
" return func(*args, **kwargs)\n",
" \n",
" wrapper.has_run = False\n",
" return wrapper\n",
" \n",
" return decorator_func\n"
]
},
{
"cell_type": "code",
"execution_count": 110,
"id": "52e6e6ee-cf0a-4b3d-b3fc-11ccffb23582",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"@decorator(show_code=True)\n",
"def my_function():\n",
" \"\"\"\n",
" This is a hello world printer that prints a random number of times, at least three times, separated by a tilde (~).\n",
" \n",
" No arguments.\n",
" \n",
" Returns:\n",
" - \"hello world\" a random number of times.\n",
" \"\"\"\n"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "2103630a-e593-4a92-b1c1-d9e921590208",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---- GPT-3 Generated Code ----\n",
"import random\n",
"\n",
"times = random.randint(3, 20)\n",
"\n",
"for _ in range(times):\n",
" print(\"hello world~\", end=\"\")\n",
"print()\n",
"---- END GPT-3 Generated Code ----\n",
"\n",
"\n",
"hello world~hello world~hello world~hello world~hello world~hello world~hello world~hello world~hello world~\n"
]
}
],
"source": [
"my_function()"
]
},
{
"cell_type": "code",
"execution_count": 107,
"id": "52362b47-0b83-4c8a-9a28-8848a8939549",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello world, hello world, hello world, hello world, hello world, hello world, hello world, hello world\n"
]
}
],
"source": [
"my_function()"
]
},
{
"cell_type": "code",
"execution_count": 119,
"id": "23bab46f-1f3f-4c2f-97fe-5da989530ba4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"@decorator(show_code=True)\n",
"def quote_writer():\n",
" \"\"\"\n",
" This function generates a random seinfeld quote of the day\n",
" \n",
" Returns a JSON blob with:\n",
" - quote: the quote\n",
" - character: the seinfled character\n",
" \"\"\""
]
},
{
"cell_type": "code",
"execution_count": 120,
"id": "26230786-01d8-41c0-b0e4-3760529685c7",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---- GPT-3 Generated Code ----\n",
"import random\n",
"\n",
"def seinfeld_quote_of_the_day():\n",
" quotes = {\n",
" \"quote1\": \"These pretzels are making me thirsty.\",\n",
" \"character1\": \"Jerry Seinfeld\",\n",
" \"quote2\": \"No soup for you!\",\n",
" \"character2\": \"The Soup Nazi\",\n",
" \"quote3\": \"Serenity now!\",\n",
" \"character3\": \"Frank Costanza\",\n",
" \"quote4\": \"I'm out there, I'm living in the real world.\",\n",
" \"character4\": \"George Costanza\",\n",
" \"quote5\": \"You know, you can't just go around doing things people don't approve of.\",\n",
" \"character5\": \"Kramer\"\n",
" }\n",
"\n",
" # Pick a random quote\n",
" quote_idx = random.randint(1, 5)\n",
" quote_key = \"quote\" + str(quote_idx)\n",
" character_key = \"character\" + str(quote_idx)\n",
"\n",
" # Return quote and character as JSON\n",
" return {\n",
" \"quote\": quotes[quote_key],\n",
" \"character\": quotes[character_key]\n",
" }\n",
"\n",
"print(seinfeld_quote_of_the_day())\n",
"---- END GPT-3 Generated Code ----\n",
"\n",
"\n",
"{'quote': 'These pretzels are making me thirsty.', 'character': 'Jerry Seinfeld'}\n"
]
}
],
"source": [
"quote_writer()"
]
},
{
"cell_type": "code",
"execution_count": 118,
"id": "e84d91ae-e09b-4f00-9dda-b3e31d1be8bd",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"quote_writer()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d5949c7-6c6a-4063-9fec-7b292e075339",
"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.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment