Skip to content

Instantly share code, notes, and snippets.

@davidar
Created January 8, 2023 02:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidar/29c2f4699f8fa33c4318f52432be030c to your computer and use it in GitHub Desktop.
Save davidar/29c2f4699f8fa33c4318f52432be030c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "c21cb564-78c5-466e-96b2-b9a9935a9b91",
"metadata": {},
"outputs": [],
"source": [
"import inspect\n",
"import json\n",
"import os\n",
"import openai"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cf244c88-25e3-44ec-ae20-1db769fcfc9e",
"metadata": {},
"outputs": [],
"source": [
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1e0b04d4-92fb-4f77-a08c-7df75f6075d4",
"metadata": {},
"outputs": [],
"source": [
"def docprompt(fn):\n",
" def res(*args):\n",
" src = inspect.getsource(fn)\n",
" params = \", \".join(repr(a) for a in args)\n",
" prompt = f\"{src}\\n\\n{fn.__name__}({params})\\n\\n# Return value formatted as JSON:\\n\"\n",
" #print(\"Running prompt:\")\n",
" #print(prompt)\n",
" response = openai.Completion.create(\n",
" model=\"text-davinci-003\",\n",
" prompt=prompt,\n",
" temperature=0.0,\n",
" max_tokens=500,\n",
" top_p=1.0,\n",
" frequency_penalty=0.0,\n",
" presence_penalty=0.0\n",
" )\n",
" #print(response)\n",
" return json.loads(response[\"choices\"][0][\"text\"])\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f7cf0f4b-bcb4-44cd-8ded-31eb8efd2a41",
"metadata": {},
"outputs": [],
"source": [
"@docprompt\n",
"def extractInformation(passage):\n",
" \"\"\"\n",
" Returns an object of information about the literary passage\n",
" :param str passage: the input passage of text\n",
" :return: metadata about the passage\n",
" :rtype: { \"tone\": \"mysterious\" or \"logical\" or \"surreal\", \"tense\": \"present\" or \"past\" }\n",
" \"\"\"\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "ebb4593a-ff6d-45f3-96dc-255490821ada",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'tone': 'logical', 'tense': 'present'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"extractInformation(\"The quick brown fox jumps over the lazy dog\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2c820cdc-74d4-485f-8f81-2919329fa34a",
"metadata": {},
"outputs": [],
"source": [
"@docprompt\n",
"def summarise(text):\n",
" \"\"\"\n",
" Summarises the input text for a second-grade student.\n",
" :rtype: { \"output\": str, \"changes\": str }\n",
" \"\"\"\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fdeabe09-8498-4e61-9634-eddde4916e33",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'output': 'Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets combined. It is very bright and can be seen in the night sky. It is named after the Roman god Jupiter.',\n",
" 'changes': 'Simplified the text for a second-grade student.'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"summarise(\"\"\"\n",
"Jupiter is the fifth planet from the Sun and the largest in the Solar System. \n",
"It is a gas giant with a mass one-thousandth that of the Sun, but \n",
"two-and-a-half times that of all the other planets in the Solar System \n",
"combined. Jupiter is one of the brightest objects visible to the naked eye in \n",
"the night sky, and has been known to ancient civilizations since before \n",
"recorded history. It is named after the Roman god Jupiter.[19] When viewed \n",
"from Earth, Jupiter can be bright enough for its reflected light to cast \n",
"visible shadows,[20] and is on average the third-brightest natural object in \n",
"the night sky after the Moon and Venus.\n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "73d0bec5-c695-4282-b72e-ff6adca279a0",
"metadata": {},
"outputs": [],
"source": [
"@docprompt\n",
"def emojify(text):\n",
" \"\"\"\n",
" Convert the text to a sequence of emoji.\n",
" :rtype: { \"emojis\": list of str }\n",
" \"\"\"\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "c556e0d9-35d1-4289-8f12-1b34ae72a7e1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'emojis': ['🚶', '🌳', '🍎']}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"emojify(\"A man sat under a tree and ate an apple.\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a03b2b25-2809-4660-a00f-8c8c6960b095",
"metadata": {},
"outputs": [],
"source": [
"@docprompt\n",
"def fruits(text):\n",
" \"\"\"\n",
" Extracts a list of fruits from the given text.\n",
" :rtype: list of { \"fruit\": str, \"colour\": str, \"flavour\": str }\n",
" \"\"\"\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "6a2f8b25-7d13-4cb7-9ca9-be436e1b35f2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'fruit': 'neoskizzles', 'colour': 'purple', 'flavour': 'candy'},\n",
" {'fruit': 'loheckles',\n",
" 'colour': 'grayish blue',\n",
" 'flavour': 'tart, a little bit like a lemon'},\n",
" {'fruit': 'pounits',\n",
" 'colour': 'bright green',\n",
" 'flavour': 'more savory than sweet'},\n",
" {'fruit': 'loopnovas', 'colour': 'neon pink', 'flavour': 'cotton candy'},\n",
" {'fruit': 'glowls',\n",
" 'colour': 'pale orange tinge',\n",
" 'flavour': 'very sour and bitter, acidic and caustic'}]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fruits(\"\"\"\n",
"There are many fruits that were found on the recently discovered planet Goocrux. \n",
"There are neoskizzles that grow there, which are purple and taste like candy. \n",
"There are also loheckles, which are a grayish blue fruit and are very tart, a \n",
"little bit like a lemon. Pounits are a bright green color and are more savory \n",
"than sweet. There are also plenty of loopnovas which are a neon pink flavor and \n",
"taste like cotton candy. Finally, there are fruits called glowls, which have a \n",
"very sour and bitter taste which is acidic and caustic, and a pale orange tinge \n",
"to them.\n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "ca69af51-c40b-49c3-a852-85ef0394bf0c",
"metadata": {},
"outputs": [],
"source": [
"@docprompt\n",
"def changeTense(text, tense):\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "6cb00af0-371b-4a82-83f9-383da5e77093",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'text': 'The fox jumps over the dog and runs away'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"changeTense(\"The fox jumped over the dog and ran away\", \"present\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "2e686755-b8a9-43a7-a808-28a1d28a1e9e",
"metadata": {},
"outputs": [],
"source": [
"@docprompt\n",
"def wikipedia(title):\n",
" \"\"\"\n",
" Give the URL and text of the Wikipedia article for the given page name.\n",
" :rtype: { \"page_name\": str, \"page_url\": str, \"page_text\": str }\n",
" \"\"\"\n",
" ..."
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "9980dd10-5f1e-4732-a420-d86386eba7a9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'page_name': 'Taken 4: The Musical',\n",
" 'page_url': 'https://en.wikipedia.org/wiki/Taken_4:_The_Musical',\n",
" 'page_text': 'Taken 4: The Musical is a musical comedy film directed by Steven Spielberg and written by J.J. Abrams. It is the fourth installment in the Taken franchise and stars Liam Neeson, Maggie Grace, and Famke Janssen. The film follows retired CIA agent Bryan Mills as he is forced to go on the run with his daughter Kim when a group of criminals attempt to kidnap her. The film was released on June 12, 2020 and received generally positive reviews from critics.'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"wikipedia(\"Taken 4: The Musical\")"
]
}
],
"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.8.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@jamesaphoenix
Copy link

Awesome pattern!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment