Program-of-Thought: Complete Guide and Examples

Master program-of-thought — code execution as reasoning substrate — best for quantitative tasks

返回教程列表
进阶12 分钟

Program-of-Thought: Complete Guide and Examples

Master program-of-thought — code execution as reasoning substrate — best for quantitative tasks

Program-of-Thought: Complete Guide What is Program-of-Thought? Program-of-Thought is a prompting technique that involves code execution as reasoning substrate. It is particularly effective for quantitative tasks. When to Use Program-of-Thought Us

prompt-engineeringprogram-of-thoughtllm-techniquesbest-practices

Program-of-Thought: Complete Guide

What is Program-of-Thought?

Program-of-Thought is a prompting technique that involves code execution as reasoning substrate. It is particularly effective for quantitative tasks.

When to Use Program-of-Thought

Use this technique when:

  • You need quantitative tasks
  • Standard prompting gives inconsistent results
  • The task requires code execution as reasoning substrate
  • You want more reliable, structured outputs
  • How It Works

    The core idea behind Program-of-Thought:

  • Setup: Prepare your prompt with the program-of-thought structure
  • Execution: Send to LLM with appropriate parameters
  • Parsing: Extract the structured response
  • Validation: Verify output quality and format
  • Basic Example

    python
    from openai import OpenAI

    client = OpenAI()

    def program_of_thought_prompt(task: str, context: str = "") -> str: """Apply Program-of-Thought technique.""" # Program-of-Thought prompt structure system = """You are an expert AI assistant. Apply systematic reasoning to every task. Be precise, accurate, and well-structured.""" # Core prompt for Program-of-Thought prompt = f"""Task: {task} {"Context: " + context if context else ""}

    Please code execution as reasoning substrate to complete this task accurately.""" response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": system}, {"role": "user", "content": prompt} ], temperature=0.3, max_tokens=1500 ) return response.choices[0].message.content

    Example usage

    result = program_of_thought_prompt( task="Analyze the pros and cons of microservices architecture", context="For a startup with 5 developers and 1000 users" ) print(result)

    Advanced Implementation

    python
    from pydantic import BaseModel
    from typing import Optional

    class PromptResult(BaseModel): output: str technique: str = "Program-of-Thought" confidence: Optional[float] = None reasoning: Optional[str] = None

    class ProgramofThoughtPrompter: """Production-ready Program-of-Thought implementation.""" def __init__(self, model: str = "gpt-4o"): self.client = OpenAI() self.model = model self.technique = "Program-of-Thought" def run(self, task: str, **kwargs) -> PromptResult: """Execute Program-of-Thought prompting.""" response = self.client.chat.completions.create( model=self.model, messages=self._build_messages(task, **kwargs), temperature=kwargs.get("temperature", 0.3), max_tokens=kwargs.get("max_tokens", 2000) ) content = response.choices[0].message.content return PromptResult( output=content, technique=self.technique ) def _build_messages(self, task: str, **kwargs) -> list[dict]: """Build Program-of-Thought specific messages.""" system = f"""You are an expert using {self.technique} to solve tasks. Apply {desc} systematically. Format: provide clear, structured responses.""" return [ {"role": "system", "content": system}, {"role": "user", "content": self._build_prompt(task, **kwargs)} ] def _build_prompt(self, task: str, **kwargs) -> str: """Build the specific prompt for Program-of-Thought.""" return f"""Using {self.technique}, complete the following task:

    Task: {task}

    Apply {desc} to arrive at a high-quality answer."""

    Usage

    prompter = ProgramofThoughtPrompter() result = prompter.run("Write a Python function to parse JSON safely") print(result.output)

    Real-World Use Cases

    Use Case 1: Quantitative tasks

    python
    

    Specialized for quantitative tasks

    prompter = ProgramofThoughtPrompter(model="gpt-4o")

    Example: quantitative tasks task

    result = prompter.run( f"Solve this quantitative tasks problem: [your specific problem here]" ) print(f"Solution: {result.output}")

    Use Case 2: Content Generation

    python
    

    Apply to content creation

    result = prompter.run( "Write a technical blog post introduction about AI agents", temperature=0.7, # Higher for creative tasks max_tokens=500 ) print(result.output)

    Comparison with Other Techniques

    TechniqueBest ForTokensReliability

    StandardSimple tasksLowMedium Program-of-Thoughtquantitative tasksMediumHigh Chain-of-ThoughtMath/LogicHighHigh Few-ShotFormat tasksHighVery High

    Common Mistakes

  • Over-prompting: Adding too many instructions reduces focus
  • Under-specifying: Vague tasks give vague answers
  • Wrong temperature: High temp for logic, low for factual
  • Missing examples: Some patterns need examples to activate
  • Measuring Effectiveness

    python
    import json
    from statistics import mean

    def evaluate_prompt_quality( prompter: ProgramofThoughtPrompter, test_cases: list[dict], n_runs: int = 3 ) -> dict: """Evaluate prompt quality with multiple runs.""" scores = [] for test in test_cases: run_scores = [] for _ in range(n_runs): result = prompter.run(test["task"]) # Score based on expected output score = 1.0 if test.get("expected") in result.output else 0.5 run_scores.append(score) scores.append(mean(run_scores)) return { "technique": "Program-of-Thought", "avg_score": mean(scores), "test_cases": len(test_cases) }

    Resources

  • Wei et al. (2022) Chain-of-Thought Prompting paper
  • Yao et al. (2023) Tree of Thoughts paper
  • OpenAI Prompt Engineering guide
  • Anthropic Claude prompting documentation
  • 相关工具

    openaianthropicpython