Prompt Engineering Tutorial and Prompt PracticePrompt Library
Composite Functions
Function composition prompt example
CHAPTER PROMPT DECISION
01Prompt problem
Function composition prompt example
02Reviewable output
A reusable prompt, example set, evaluation record or safety rule with its task boundary preserved.
03Definition of done
Run at least one representative case, inspect the result and record what still requires human review.
TL;DR
- This is a
function compositiontest: giveng(x)=f^{-1}(x)and several mapping points, reverse-engineerf's mappings, then computef(f(f(6))). - Good for testing: whether the model handles inverse mapping correctly, and whether it makes errors during multiple compositions.
- Production tip: require the model to output a mapping table and show intermediate values after each composition step.
Background
This prompt tests an LLM's mathematical capabilities by prompting it to evaluate a function composition.
How to Apply
Follow the "build the mapping first, then compute step by step" approach:
- From
g(x)=f^{-1}(x)we get:f(g(x)) = x(i.e., reverseg's pairs to getf's pairs) - List
f's mapping in a table (e.g.,f(5)=0,f(7)=4, etc.) - Compute sequentially:
f(6) → f(f(6)) → f(f(f(6)))
How to Iterate
- Force output of a
mapping table(structured) before giving the final answer - Add
self-check: cross-verifyfandg(check whetherg(f(x)) = xholds at the given points) - Adversarial testing: add more points, introduce distractor points, or make
fnon-injective to see if the model catches contradictions
Self-check Rubric
- Did it correctly understand the
inverserelationship and constructf's mapping? - Is the composition computed step by step with correct intermediate values?
- Did it check whether the mapping is consistent (no conflicts)?
Practice
Exercise: change the target to f(f(2)), f(f(f(9))), etc., and require the model to output:
- mapping table
- intermediate value at each step
- brief consistency check
Prompt
Suppose g(x) = f^{-1}(x), g(0) = 5, g(4) = 7, g(3) = 2, g(7) = 9, g(9) = 6.
What is f(f(f(6)))?
Code / API
OpenAI (Python)
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[
{
"role": "user",
"content": "Suppose g(x) = f^{-1}(x), g(0) = 5, g(4) = 7, g(3) = 2, g(7) = 9, g(9) = 6. What is f(f(f(6)))?",
}
],
temperature=1,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
Fireworks (Python)
import fireworks.client
fireworks.client.api_key = "<FIREWORKS_API_KEY>"
completion = fireworks.client.ChatCompletion.create(
model="accounts/fireworks/models/mixtral-8x7b-instruct",
messages=[
{
"role": "user",
"content": "Suppose g(x) = f^{-1}(x), g(0) = 5, g(4) = 7, g(3) = 2, g(7) = 9, g(9) = 6. What is f(f(f(6)))?",
}
],
stop=["<|im_start|>", "<|im_end|>", "<|endoftext|>"],
stream=True,
n=1,
top_p=1,
top_k=40,
presence_penalty=0,
frequency_penalty=0,
prompt_truncate_len=1024,
context_length_exceeded_behavior="truncate",
temperature=0.9,
max_tokens=4000,
)