0
0
Prompt Engineering / GenAIml~10 mins

Caching strategies for LLMs in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to store a generated response in the cache dictionary.

Prompt Engineering / GenAI
cache[[1]] = generated_response
Drag options to blanks, or click blank then click option'
Aprompt
Bresponse
Ccache_key
Dinput_text
Attempts:
3 left
💡 Hint
Common Mistakes
Using the response as the key instead of the prompt.
Using a variable not defined as the key.
2fill in blank
medium

Complete the code to check if the prompt is already cached before generating a new response.

Prompt Engineering / GenAI
if [1] in cache:
    return cache[prompt]
Drag options to blanks, or click blank then click option'
Aresponse
Bprompt
Ccache_key
Dgenerated_response
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for the response instead of the prompt.
Using an undefined variable in the condition.
3fill in blank
hard

Fix the error in the code to retrieve a cached response safely.

Prompt Engineering / GenAI
cached_response = cache.get([1], None)
Drag options to blanks, or click blank then click option'
Aprompt
Bresponse
Ccache_key
Dgenerated_response
Attempts:
3 left
💡 Hint
Common Mistakes
Using the response as the key in .get().
Using an undefined variable as the key.
4fill in blank
hard

Fill both blanks to create a cache dictionary comprehension that stores prompt-response pairs only if the response length is greater than 10.

Prompt Engineering / GenAI
filtered_cache = {prompt: response for prompt, response in cache.items() if len(response) [1] [2]
Drag options to blanks, or click blank then click option'
A>
B10
C<
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using a smaller number like 5 instead of 10.
5fill in blank
hard

Fill all three blanks to implement a function that returns a cached response if available, otherwise generates, caches, and returns a new response.

Prompt Engineering / GenAI
def get_response(prompt):
    if prompt [1] cache:
        return cache[prompt]
    response = [2](prompt)
    cache[[3]] = response
    return response
Drag options to blanks, or click blank then click option'
Ain
Bgenerate_response
Cprompt
Dnot in
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' instead of 'in' in the if condition.
Using wrong variable names for keys or functions.