Prompt Engineering Guide
πŸ˜ƒ Basics
πŸ’Ό Applications
πŸ§™β€β™‚οΈ Intermediate
🧠 Advanced
Special Topics
🌱 New Techniques
πŸ€– Agents
βš–οΈ Reliability
πŸ–ΌοΈ Image Prompting
πŸ”“ Prompt Hacking
πŸ”¨ Tooling
πŸ’ͺ Prompt Tuning
πŸ—‚οΈ RAG
🎲 Miscellaneous
Models
πŸ”§ Models
Resources
πŸ“™ Vocabulary Resource
πŸ“š Bibliography
πŸ“¦ Prompted Products
πŸ›Έ Additional Resources
πŸ”₯ Hot Topics
✨ Credits
🌱 New Techniques🟦 Code Prompting

Code Prompting

🟦 This article is rated medium
Reading Time: 3 minutes
Last updated on October 3, 2024

Valeriia Kuka

Takeaways
  • Code Prompting Overview: Code prompting enhances reasoning in large language models (LLMs) by transforming natural language tasks into structured code representations with conditional logic, which improves accuracy in conditional reasoning.
  • Performance Gains: This approach significantly boosts performance on conditional reasoning tasks across various models and datasets, proving to be more efficient with fewer training examples and improving variable tracking.
Overview of Code Prompting

What is Code Prompting?

Code prompting is a novel technique that enhances reasoning abilities in text+code Large Language Models (LLMs) by transforming natural language (NL) tasks into code representations. Instead of executing the code, the model uses it as a structured input format to reason and generate answers. This approach aims to improve conditional reasoning, where conclusions depend on specific conditions or logical steps like determining eligibility for a visa or loan based on given rules.

How Code Prompting Works

  1. Problem Conversion: Natural language problems are transformed into a code-like structure, with conditional logic and variables, while preserving the original text as comments.
  2. Prompting: The LLM receives the generated code, interprets the structure, and produces answers in natural language.

Example

A question like "Can a widow claim benefits?" is transformed into a code representation, with variables for key terms (e.g., "widow") and conditional logic for eligibility. The LLM uses this structured format to track variables and conditions effectively, improving its reasoning accuracy.

How to Use Code Prompting

Code prompting can be applied to any reasoning task requiring logical steps or condition-based conclusions. Below is a template for how the code transformation might look:

Astronaut

Template for Code Prompting


You are a helpful assistant. Your task is to process a pseudo-code that describes a question and a document. You need to reason using that document and the comments to return the answers. Answers must be a short span of the document. You have to extract the span from the code comments. Do not write anything else. I will give you some examples first.

Q1: [Question 1] A1: [Solution using preudo-code]

Q2: [Question 2] A2: [Solution using preudo-code]

Q3: [Your question] A3:

Answers must be a short span of the document. You have to extract the span from the code comments. Do not write anything else.

Let’s think step by step:

Tip

You can find original prompts and their implementations on GitGub.

Results of Code Prompting

The performance of code prompting was evaluated on three datasets that require conditional reasoning:

  • ConditionalQA (CondQA): Scenario-based reasoning
  • BoardgameQA (BGQA): Rule-based logic for board games
  • ShARC: Conversational question-answering
ModelCondQA (F1)ShARC (F1)BGQA-3 (F1)Avg. Gain (F1)
GPT-3.5+22.52%+8.42%+18.52%+8.53%
Mixtral+7.75%+4.22%+14.57%+4.23%
Mistral+16.78%+2.74%+5.93%+2.74%

Code prompts significantly outperformed text-based prompts across all models and datasets, showing the highest gains on reasoning-intensive tasks like BGQA.

  • Sample efficiency: Code prompts are more efficient, performing better with fewer training examples. For example, code prompts with only one demonstration outperformed text prompts with three demonstrations.
  • Improved variable tracking: Code formatting helps LLMs track variables and conditions more effectively, leading to fewer errors in reasoning, particularly for multi-step logical tasks.

Conclusion

Code prompting is an effective strategy for eliciting conditional reasoning abilities in text+code LLMs, enhancing their performance on logical tasks by providing structured inputs. This method not only improves accuracy but also reduces the need for extensive demonstrations, making it a valuable tool for improving reasoning in AI applications.

Valeriia Kuka

Valeriia Kuka, Head of Content at Learn Prompting, is passionate about making AI and ML accessible. Valeriia previously grew a 60K+ follower AI-focused social media account, earning reposts from Stanford NLP, Amazon Research, Hugging Face, and AI researchers. She has also worked with AI/ML newsletters and global communities with 100K+ members and authored clear and concise explainers and historical articles.