I thought that it would be great to create a post to share your Codex completion examples that have resulted in “usable” code being generated so that everyone can learn from one another.
When posting, please try to follow a similar request/response format below.
I will start off with a simple example I found while tinkering in the playground and with the API:
Settings:
- “temperature”: 0.70,
- “max_tokens”: 1000,
- “top_p”: 0,
- “frequency_penalty”: 0.00,
- “presence_penalty”: 0.00
Example 1: Python AWS Lambda Function
Request: POST /v1/engines/davinci-codex/completions
Prompt:
"""
1. Create an AWS Lambda function
2. Create a DynamoDB connection to query a DynamoDB table called "movies" using "id"
3. Return the "year" field from the query response if a "movie" is found and a "200" response
4. If a "movie" is not found, return a "204" response
5. If an exception occurs, return a "500" response and a "An exception occurred" body
"""
def lambda_handler(event, context):
Response:
def lambda_handler(event, context):
try:
# Create a DynamoDB resource
dynamodb = boto3.resource('dynamodb')
# Create a table resource
table = dynamodb.Table('movies')
# Get the item from the table
response = table.get_item(
Key={
'id': event['pathParameters']['id']
}
)
# If the item is found, return the year
if 'Item' in response:
return {
'statusCode': 200,
'body': json.dumps(response['Item']['year'])
}
else:
# If the item is not found, return a 204
return {
'statusCode': 204,
'body': ''
}
except:
# If an exception occurs, return a 500
return {
'statusCode': 500,
'body': 'An exception occurred'
}
Example 2: Creating Tests From The Previously Generated Code in Example 1
Request: POST /v1/engines/davinci-codex/completions
Prompt:
"""
1. Create unit tests using Pytest for the following code
2. Mock out calls to DynamoDB and return a stubbed response
3. Test for each status code
"""
def lambda_handler(event, context):
try:
# Create a DynamoDB resource
dynamodb = boto3.resource('dynamodb')
# Create a table resource
table = dynamodb.Table('movies')
# Get the item from the table
response = table.get_item(
Key={
'id': event['pathParameters']['id']
}
)
# If the item is found, return the year
if 'Item' in response:
return {
'statusCode': 200,
'body': json.dumps(response['Item']['year'])
}
# If the item is not found, return a 204
else:
return {
'statusCode': 204,
'body': ''
}
except:
# If an exception occurs, return a 500
return {
'statusCode': 500,
'body': 'An exception occurred'
}
def test_function():
Response:
def test_function():
# Mock out the boto3 call to DynamoDB
with patch('boto3.resource') as res:
# Setup the mock to return the stubbed value
res.return_value.Table.return_value.get_item.return_value = {
'Item': {
'year': '2020'
}
}
# Call the function
event = {
'pathParameters': {
'id': '123'
}
}
context = {}
result = lambda_handler(event, context)
# Assert that the result is as expected
assert result['statusCode'] == 200
assert result['body'] == '2020'
def test_function_not_found():
# Mock out the boto3 call to DynamoDB
with patch('boto3.resource') as res:
# Setup the mock to return the stubbed value
res.return_value.Table.return_value.get_item.return_value = {}
# Call the function
event = {
'pathParameters': {
'id': '123'
}
}
context = {}
result = lambda_handler(event, context)
# Assert that the result is as expected
assert result['statusCode'] == 204
assert result['body'] == ''
def test_function_exception():
# Mock out the boto3 call to DynamoDB
with patch('boto3.resource') as res:
# Setup the mock to return the stubbed value
res.return_value.Table.return_value.get_item.side_effect = Exception('test')
# Call the function
event = {
'pathParameters': {
'id': '123'
}
}
context = {}
result = lambda_handler(event, context)
# Assert that the result is as expected
assert result['statusCode'] == 500
assert result['body'] == 'An exception occurred'
Overall, I would say not bad as an initial pass. I would also say though that the structure of the prompts makes a huge different. I landed on this bulleted format after a LOT of tinkering and seemed to worked well for these examples.