This repository implements the NLP API by using OpenAI API (with its open accessible GPT-3 models), AWS Lambda, AWS API Gateway, and Python. The API could be improved by switching to use OpenAI Codex model once the Codex model access is approved. Currently, support:
- Generate code:
- from docstring document
- Rewrite code:
- from iterative to recursive
(TODO: provide the API document via swagger)
https://xrhv8qvx7h.execute-api.us-east-1.amazonaws.com/dev/generate
{
"type":"docstring",
"input":" \"\"\"Takes in two numbers, returns their product.\"\"\"\n"
}
curl -X POST https://xrhv8qvx7h.execute-api.us-east-1.amazonaws.com/dev/generate -H 'Content-Type: application/json' -d '{
"type":"docstring",
"input":" \"\"\"Takes in two numbers, returns their product.\"\"\"\n"
}'
def multiply(a, b):
"""Takes in two numbers, returns their product."""
return a*b
https://xrhv8qvx7h.execute-api.us-east-1.amazonaws.com/dev/rewrite
{
"type":"iterative2recursive",
"input":" def Reverse_iter(s):\n rev = ''\n for k in s:\n rev = k + rev\n return rev\n\n Reverse_iter('Welcome!')"
}
curl -X POST https://xrhv8qvx7h.execute-api.us-east-1.amazonaws.com/dev/rewrite -H 'Content-Type: application/json' -d '{
"type":"iterative2recursive",
"input":" def Reverse_iter(s):\n rev = ''\n for k in s:\n rev = k + rev\n return rev\n\n Reverse_iter('Welcome!')"
}'
def Reverse_recur(s):
# base case
if len(s) == 1:
return s
# general case
else:
return s[-1] + Reverse_recur(s[:-1])
Reverse_recur(Welcome!)
- Install OpenAI API
- Setup AWS Lambda with Python packages by using a virtual environment
- Install AWS CLI
- Setup REST API via AWS API Gateway
- Get OPENAI_API_KEY and set up the environment variable OPENAI_API_KEY in AWS Lambda
- Test REST API
- Deploy the AWS Gateway REST API with path: Part 1, Part 2 (make sure to "Use Lambda Proxy integration")
- In Lambda Function, Add Layer
AWSDataWrangler-Python39
- When packaging Python zip, first delete numpy folder in .venv/lib/python3.9/site-packages
Play the following code rewrite prompt engineering text in OpenAI Playground for iterative to recursive example
converts a text instruction in Natural Language to Python Code with a suitable docstring in numpy style:
[Docstring]
'''
Returns the sum of two decimal numbers in binary digits.
Parameters:
a (int): A decimal integer
b (int): Another decimal integer
Returns:
binary_sum (str): Binary string of the sum of a and b
'''
[Generated Code with Docstring]
def add_binary(a, b):
'''
Returns the sum of two decimal numbers in binary digits.
Parameters:
a (int): A decimal integer
b (int): Another decimal integer
Returns:
binary_sum (str): Binary string of the sum of a and b
'''
binary_sum = bin(a+b)[2:]
return binary_sum
converts a text instruction in Natural Language to Python Code with a suitable docstring in numpy style:
[Docstring]
"""Takes in two numbers, returns their product."""
[Generated Code with Docstring]
Use the following Python code to implement the code generation:
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create(
model="text-davinci-002",
prompt="converts a text instruction in Natural Language to Python Code with a suitable docstring in numpy style:\n[Docstring]\n '''\n Returns the sum of two decimal numbers in binary digits.\n\n Parameters:\n a (int): A decimal integer\n b (int): Another decimal integer\n\n Returns:\n binary_sum (str): Binary string of the sum of a and b\n '''\n[Generated Code with Docstring]\ndef add_binary(a, b):\n '''\n Returns the sum of two decimal numbers in binary digits.\n\n Parameters:\n a (int): A decimal integer\n b (int): Another decimal integer\n\n Returns:\n binary_sum (str): Binary string of the sum of a and b\n '''\n binary_sum = bin(a+b)[2:]\n return binary_sum\n\nconverts a text instruction in Natural Language to Python Code with a suitable docstring in numpy style:\n[Docstring]\n \"\"\"Takes in two numbers, returns their product.\"\"\"\n[Generated Code with Docstring]",
temperature=0,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
Play the following code rewrite prompt engineering text in OpenAI Playground for iterative to recursive example
creating a recursive approach from an iterative approach in python:
[iterative]
n = 10
result = 1
i = 1
while i <= n:
result *= i
i += 1
print(result)
[recursive]
def Factorial(n):
# declare a base case (a limiting criteria)
if n == 1:
return 1
# continue with general case
else:
return n * Factorial(n-1)
print(Factorial(10))
creating a recursive approach from an iterative approach in python:
[iterative]
def Reverse_iter(s):
rev = ''
for k in s:
rev = k + rev
return rev
Reverse_iter('Welcome!')
[recursive]
Use the following Python code to implement the code rewrite
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
response = openai.Completion.create(
model="text-davinci-002",
prompt="creating a recursive approach from an iterative approach in python:\n[iterative]\n n = 10\n result = 1\n i = 1\n while i <= n:\n result *= i\n i += 1\n print(result) \n\n[recursive]\n def Factorial(n):\n # declare a base case (a limiting criteria)\n if n == 1:\n return 1\n # continue with general case\n else:\n return n * Factorial(n-1)\n \n print(Factorial(10))\n\ncreating a recursive approach from an iterative approach in python:\n[iterative]\n def Reverse_iter(s):\n rev = ''\n for k in s:\n rev = k + rev\n return rev\n\n Reverse_iter('Welcome!')\n\n[recursive]\n",
temperature=0,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)