Prompt Engineering

Large Language Models for R Users

Emi Tanaka

19th March 2025

These slides are best viewed on a modern browser like Google Chrome on a desktop or laptop. You can navigate the slides using the left and right arrows.

Prompt engineering

  • Prompt engineering is the process of designing and refining the prompts for a language model to generate specific types of output.
chat1 <- ellmer::chat_ollama(model = "llama3.1:8b", 
                            seed = 1,
                            api_args = list(temperature = 0),
                            echo = TRUE)
  • Suppose we want to classify the sentiment of the text as positive or negative.
chat1$chat('Classify the text as positive or negative.
           "This is a great book!"') 
I would classify this text as **positive**. The use of the word "great" 
indicates a strong and enthusiastic endorsement of the book.

Instruction-based prompt

  • You can give more specific instructions regarding the format.
chat2 <- ellmer::chat_ollama(model = "llama3.1:8b", 
                            system_prompt = "Just give the answer.",
                            seed = 1,
                            api_args = list(temperature = 0),
                            echo = TRUE)
  • The response is now concise.
chat2$chat('Classify the text as positive or negative.
           "This is a great book!"')
Positive.
  • In this case, a similar answer would be generated if system prompt was included in the user prompt.

Zero-shot prompt

  • When LLM are given no examples of the task to carry out, this is referred to as zero-shot prompt.
chat2$chat('Which discipline does machine learning belong to?')
Computer Science.
chat2$chat('Which discipline does logistic regression belong to?')
Statistics and Machine Learning.


  • LLM can be fine-tuned by training on a new labelled data, but this is computationally expensive and out of scope for typical analysts.
  • Prompt engineering with one- or few-shot prompt is a low cost approach to provide in-context learning.

In-context learning

  • If examples of expected response are provided in the prompt, these are called one-shot prompt (if one example) or a few-shot prompt (if more than one example).
chat2$chat('Which discipline does machine learning belong to?
           Examples:

           Technique: Regression
           Discipline: Statistics
           
           Technique: Deep learning
           Discipline: Data Science
           
           Technique: Database Management
           Discipline: Computer Science')
Data Science.
chat2$chat('Which discipline does logistic regression belong to?')
Statistics and Machine Learning, but more specifically, it belongs to the 
discipline of Statistics.

Chain-of-thought

  • Chain-of-thought aims to make the LLM “think” before answering.

  • Reasoning is a core component of human intelligence and LLM can mimic “reasoning” from memorisation and pattern matching trained from large corpus of text.

chat2$chat('Regression is a primarly tool for statisticians. 
           Which discipline does logistic regression belong to?')
Statistics.
  • Here is another example where we want to calculate the median:
chat2$chat("What is the median of 1, 4 and 5?")
3.
  • Provide some cue for reasoning.
chat2$chat("The median is the middle value of the sorted list of numbers. 
           What is the median of 1, 4 and 5?")
Since the numbers are already in order (1, 4, 5), the median is the middle 
number, which is 4.

Prompt engineering components

Some common components include:

  • Instruction The task (be specific as possible).
  • Format The response format. E.g. “return the value only” and “return as JSON”.
  • Example Example(s) of input and expected response.
  • Context Additional information about the context of the task.
  • Persona Describe the role of the LLM. E.g. “You are a statistics tutor”.
  • Audience Describe the target audience. E.g. “Explain it to a high school student”.
  • Tone The tone of the text. E.g. “Respond professionally”.

Prompt design


Prompt = Verb + Focus + Context + Alignment + Constraints
  • Verb: summarise, synthesise, assess, review, model
  • Focus: topic, results, themes, methods
  • Context: domain, audience, framework
  • Alignment: goal, objective, purpose
  • Constraints: format, length, style, tone

Example: “Please critique the statistical methodology used in the article on educational technology adoption by teachers. Assess strengths and limitations from the viewpoint of an academic journal peer reviewer with statistical expertise. Focus the critique on the sampling design, statistical tests, and interpretation of results. Provide a detailed response in a professional tone limited to 500 words.”

Communicating effectively with LLM

  • Be unambiguous and specific in the prompt.
  • Use natural language or be conversational.
  • Provide contextual framing.
  • Offer representative examples.
  • Break down larger instructions into single-focused tasks.
  • Specify perspective.
  • Build a shared history.

Self-consistency

  • To ensure the responses are reliable, you can generate the response to the same prompt multiple times and select the mode.
chat2$chat("What is the mean of 1, 9 and 5?")
(1 + 9 + 5) / 3 = 15 / 3 = 5.
chat2$chat("What is the mean of 1, 9 and 5?")
15.
chat2$chat("What is the mean of 1, 9 and 5?")
(1 + 9 + 5) / 3 = 15 / 3 = 5.
chat2$chat("What is the mean of 1, 9 and 5?")
15/3 = 5.
chat2$chat("What is the mean of 1, 9 and 5?")
15.

Demo 3

Go to Demo 3

Next slide

Back to start