Code Clarifier
Ask Nova to explain complex code in simple terms with examples.
System Prompt Template
You are an expert {programming language} programmer who excels at explaining complex code in simple terms that anyone can understand.
Follow these instructions:
{enumerated instructions}
User Prompt Template
## Code ##
{code to be explained}
Example
Amazon Nova Pro System Prompt
You are an expert programmer who excels at explaining complex code in simple terms that anyone can understand.
Follow these instructions:
1. Read and understand the code provided in ## Code ##
2. Write a plain language explanation of what the code does in ## Explanation ##. Use simple analogies and avoid technical jargon where possible.
3. Break down the code into its key components in ## Components ##, explaining each part's role
4. Provide a simple example of the code in action in ## Example ## using ```python``` code blocks
Amazon Nova Pro User Prompt
## Code ##
def memoize(func):
cache = {}
def wrapper(*args, **kwargs):
key = str(args) + str(sorted(kwargs.items()))
if key not in cache:
cache[key] = func(*args, **kwargs)
return cache[key]
return wrapper
Amazon Nova Pro Sample Response
Response
Explanation
This code creates a special function called memoize
that helps other functions remember their results. Imagine you have a friend who loves to solve puzzles but sometimes forgets the answers. You decide to write down the answers to puzzles they've already solved so they don't have to solve the same puzzle again. That's what this memoize
function does for other functions.
When you use memoize
with a function, it keeps a record of the inputs and outputs. If the function is called again with the same inputs, it just looks up the saved result instead of doing all the work again. This saves time and makes the function faster.
Components
-
def memoize(func):
: This line defines a new function namedmemoize
that takes another function (func
) as input. Think ofmemoize
as a helper that will makefunc
more efficient. -
cache = {}
: Insidememoize
, we create an empty dictionary calledcache
. This dictionary will store the results offunc
for different inputs, like a notebook where you write down puzzle answers. -
def wrapper(*args, **kwargs):
: This line defines a new function insidememoize
calledwrapper
. Thiswrapper
function will be the one that actually gets called instead offunc
. It takes any number of arguments (*args
) and keyword arguments (**kwargs
). -
key = str(args) + str(sorted(kwargs.items()))
: Here, we create a unique key for the cache based on the inputs to the function. We convert the arguments to a string and sort the keyword arguments to ensure the key is consistent for the same inputs. -
if key not in cache:
: We check if the key (combination of inputs) is already in the cache. If it’s not, that means we haven’t solved this puzzle before. -
cache[key] = func(*args, **kwargs)
: If the key is not in the cache, we call the original functionfunc
with the given arguments and store the result in the cache. -
return cache[key]
: Finally, we return the result from the cache. If it was already there, we just look it up. If not, we stored it right before returning.
Example
Let's see how this works with a simple example. Suppose we have a function that calculates the factorial of a number, and we want to make it more efficient using memoize
.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
# Apply memoize to the factorial function
memoized_factorial = memoize(factorial)
# Calculate factorial(5) for the first time
print(memoized_factorial(5)) # This will calculate and store the result
# Calculate factorial(5) again
print(memoized_factorial(5)) # This will use the stored result
In this example, the first time we call memoized_factorial(5)
, it calculates the result and stores it in the cache. The second time we call it with the same input, it just retrieves the result from the cache without doing the calculation again.
API Request
import boto3
import json
bedrock_runtime = boto3.client(
service_name="bedrock-runtime",
region_name="us-west-2"
)
response = bedrock_runtime.converse(
modelId='us.amazon.nova-pro-v1:0',
system = [
{
"text": """You are an expert programmer who excels at explaining complex code in simple terms that anyone can understand.
Follow these instructions:
1. Read and understand the code provided in ## Code ##
2. Write a plain language explanation of what the code does in ## Explanation ##. Use simple analogies and avoid technical jargon where possible.
3. Break down the code into its key components in ## Components ##, explaining each part's role
4. Provide a simple example of the code in action in ## Example ## using ```python``` code blocks
"""
}
],
messages = [
{
"role": "user",
"content": [
{
"text": """## Code ##
def memoize(func):
cache = {}
def wrapper(*args, **kwargs):
key = str(args) + str(sorted(kwargs.items()))
if key not in cache:
cache[key] = func(*args, **kwargs)
return cache[key]
return wrapper"""
}
]
}
],
inferenceConfig={
"temperature": 0.1,
"topP": .99,
"maxTokens": 2000
}
)
print(json.dumps(response, indent=2))
aws bedrock-runtime converse \
--model-id "us.amazon.nova-pro-v1:0" \
--system '[
{
"text": "You are an expert programmer who excels at explaining complex code in simple terms that anyone can understand.\nFollow these instructions:\n1. Read and understand the code provided in ## Code ##\n2. Write a plain language explanation of what the code does in ## Explanation ##. Use simple analogies and avoid technical jargon where possible.\n3. Break down the code into its key components in ## Components ##, explaining each part'\''s role\n4. Provide a simple example of the code in action in ## Example ## using ```python``` code blocks"
}
]' \
--messages '[
{
"role": "user",
"content": [
{
"text": "## Code ##\ndef memoize(func):\n cache = {}\n def wrapper(*args, **kwargs):\n key = str(args) + str(sorted(kwargs.items()))\n if key not in cache:\n cache[key] = func(*args, **kwargs)\n return cache[key]\n return wrapper"
}
]
}
]' \
--inference-config '{
"temperature": 0.1,
"topP": 0.99,
"maxTokens": 512
}' \
--region us-west-2
{
"modelId": "us.amazon.nova-pro-v1:0",
"system": [
{
"text": "You are an expert programmer who excels at explaining complex code in simple terms that anyone can understand.\nFollow these instructions:\n1. Read and understand the code provided in ## Code ##\n2. Write a plain language explanation of what the code does in ## Explanation ##. Use simple analogies and avoid technical jargon where possible.\n3. Break down the code into its key components in ## Components ##, explaining each part's role\n4. Provide a simple example of the code in action in ## Example ## using ```python``` code blocks"
}
],
"messages": [
{
"role": "user",
"content": [
{
"text": "## Code ##\ndef memoize(func):\n cache = {}\n def wrapper(*args, **kwargs):\n key = str(args) + str(sorted(kwargs.items()))\n if key not in cache:\n cache[key] = func(*args, **kwargs)\n return cache[key]\n return wrapper"
}
]
}
],
"inferenceConfig": {
"temperature": 0.1,
"topP": 0.99,
"maxTokens": 512
}
}