P
Prompt Master
Prompt Engineering 教程与提示词实战

从任务定义、示例和工作流到评测与安全边界

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 composition test: given g(x)=f^{-1}(x) and several mapping points, reverse-engineer f's mappings, then compute f(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:

  1. From g(x)=f^{-1}(x) we get: f(g(x)) = x (i.e., reverse g's pairs to get f's pairs)
  2. List f's mapping in a table (e.g., f(5)=0, f(7)=4, etc.)
  3. Compute sequentially: f(6) → f(f(6)) → f(f(f(6)))

How to Iterate

  1. Force output of a mapping table (structured) before giving the final answer
  2. Add self-check: cross-verify f and g (check whether g(f(x)) = x holds at the given points)
  3. Adversarial testing: add more points, introduce distractor points, or make f non-injective to see if the model catches contradictions

Self-check Rubric

  • Did it correctly understand the inverse relationship and construct f'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,
)

Reference

📚 Related resources