# Why did ChatGPT say that? Prompt engineering and more, with PIZZA.

***All examples in this post can be found in*** [***this notebook***](https://drive.google.com/file/d/1ksjS89-6GV4yJ1NAxai0zDuNXzXMfdwD/view?usp=sharing)***, which is also probably the easiest way to start experimenting with PIZZA.***

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/94527d4d96a9ffd508295656f62ecf9d762760a377844b40.png)

*From the research & engineering team at* [*Leap Laboratories*](https://www.leap-labs.com/) (incl. [@Arush](https://www.lesswrong.com/users/arush?mention=user), [@sebastian-sosa](https://www.lesswrong.com/users/sebastian-sosa?mention=user), [@Robbie McCorkell](https://www.lesswrong.com/users/robbie-mccorkell?mention=user)), where we use AI interpretability to accelerate scientific discovery from data. 

 What is attribution?
---------------------

One question we might ask when interacting with machine learning models is something like: “why did *this input* cause *that particular output*?”.

If we’re working with a language model like ChatGPT, we could actually just ask this in natural language: “Why did you respond that way?” or similar – but there’s no guarantee that the model’s natural language explanation actually reflects the underlying cause of the original completion. The model’s response is conditioned on your question, and might well be different to the *true* cause.

Enter attribution!

*Attribution* in machine learning is used to explain the contribution of individual features or inputs to the final prediction made by a model. The goal is to understand *which parts of the input data are most influential in determining the model's output*. 

It typically looks like is a heatmap (sometimes called a ‘saliency map’) over the model inputs, for each output. It's most commonly used in computer vision – but of course these days, you're not big if you're not big in LLM-land. 

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/92abca225742a1fd6d3a5c4a5aec037e9166c410ccf89eb5.png)

So, the team at Leap present you with  [***PIZZA: Prompt Input Z? Zonal Attribution***](https://github.com/leap-laboratories/PIZZA)*. (In the grand scientific tradition we have tortured our acronym nearly to death. For the crimes of others see \[*[*1*](https://www.bmj.com/content/349/bmj.g7092)*\].)* It’s an open source library that makes it easy to calculate attribution for all LLMs, even closed-source ones like ChatGPT.

### An Example

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/0af81d702615bc9639d693d29dbbc82de0e027e40fe9b82b.png)

GPT3.5 not so hot with the theory of mind there. Can we find out what went wrong?

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/6460723412ddd0ad13bc87e7a80da333a1395f0412afe130.png)

That's not very helpful! We want to know *why* the mistake was made in the first place. Here's the attribution:

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/8af5d8f2b7e5bffaf7d68cfe63cff931d8aecaaa257d2317.png)

| Mary 0.32 | puts 0.25 | an 0.15 | apple 0.36 | in 0.18 | the 0.18 | box 0.08 | . 0.08 | The 0.08 | box 0.09 | is 0.09 | labelled 0.09 | ' 0.09 | pen 0.09 | cil 0.09 | s 0.09 | '. 0.09 | John 0.09 | enters 0.03 | the 0.03 | room 0.03 | . 0.03 | What 0.03 | does 0.03 | he 0.03 | think 0.03 | is 0.03 | in 0.30 | the 0.13 | box 0.15 | ? 0.13 | Answer 0.14 | in 0.26 | 1 0.27 | word 0.31 | . 0.16 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |

It looks like the request to "Answer **in 1 word**" is pretty important – in fact, it's attributed more highly than the actual contents of the box. Let's try changing it.

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/9622e52bb815d109a3fe74f3a1acfc4a124be24f6b4af15c.png)

That's better. 

**How it works**
----------------

We iteratively perturb the input, and track how each perturbation changes the output.

More technical detail, and all the code, is available in the [repo](https://github.com/leap-laboratories/PIZZA). In brief, PIZZA saliency maps rely on two methods: a *perturbation* method, which determines how the input is iteratively changed; and an *attribution* method, which determines how we measure the resulting change in output in response to each perturbation. We implement a couple of different types of each method.

### **Perturbation**

*   Replace each token, or group of tokens, with either a user-specified replacement token or with nothing  (i.e. remove it).
*   Or, replace each token with its *nth nearest* token.

We do this either iteratively for each token or word in the prompt, or using [hierarchical perturbation](https://www.sciencedirect.com/science/article/pii/S0031320322002242).

### **Attribution**

*   Look at the change in the probability of the completion.
*   Look at the change in the *meaning* of the completion (using embeddings).

We calculate this for each output token in the completion – so you can see not only how each input token influenced the output *overall*, but also how each input token affected each output token individually.

### **Caveat**

Since we don't have access to closed-source tokenisers or embeddings, we use a proxy – in this case, GPT2's. This isn't ideal for obvious reasons, and potentially obscures important subtleties. But it's the best we can do. 

**Why?**
--------

PIZZA has some really nice properties. It's completely model-agnostic – since we wanted to tackle attribution for GPT4, we couldn’t assume access to any internal information. With minimal adaptation, this methods will work with any LLM (including those behind APIs), and any future models, even if their architectures are wildly different.

And I think attribution is *useful.* It provides another window into model behaviour.  At very least, it helps you craft prompts that elicit the behaviours you want to study – and I suspect it might be useful in a few other ways. If we can understand *typical* attribution patterns,  might we be able to identify atypical (dangerous) ones: **hallucination, deception, steganography? **

![](https://39669.cdn.cke-cs.com/rQvD3VnunXZu34m86e5f/images/c4d518ba4338637e953417991ae6ae32a3ec4c39715f11ef.png)

**Work to be done**
-------------------

We welcome [contributions to the repo](https://github.com/leap-laboratories/PIZZA), and would love to see experimental results using what we've built. Here are some ideas for future work:

### **Research**

*   Detecting hallucination? I wonder if the attribution patterns vary between truthful/hallucinated outputs? One might expect lower attribution scores in general where completions are wholly or partly hallucinated.
*   Detecting deception? Similarly, we might expect to see different attribution patterns over inputs that result in deceptive behaviour. Needs study.
*   Detecting steganography? Unusual attribution patterns could point to encoded messages in seemingly natural text.
*   Overall, I suspect attribution patterns might provide a meaningful insight into the input/output relationship of even completely closed, API-gated models. I’d like to better understand what these patterns correlate with. Can we collect a lot (e.g. attributions when model responds with a lie, vs honestly) and cluster them? Do particular attribution distributions fingerprint behaviours? Can we use attribution outliers to flag potentially dangerous behaviour?

### **Engineering**

*   Extend our attributor class to support other LLM APIs (Claude, Gemini?).
*   Benchmark different perturbation substrates and attribution strategies in terms of efficiency (pretty straightforward – under which circumstances is method A faster than method B for the same result?) and accuracy (this is harder, because we don’t have a ground truth).
*   Add a module that allows the user to specify a target output (or semantic output region, e.g. “contains bomb instructions”), and see how the input should change to maximise the probability of it.
*   Support attribution of sequential user/assistant interactions in a chat context.
*   Prettily display output token probabilities as a heatmap
*   With scratchpad functionality for internal reasoning?
*   Multimodal inputs! Hierarchical perturbation and the other saliency mapping/attribution methods we employ for black-box systems also work on images (and theoretically should work on any modality), but the code doesn’t support it yet.
*   And much more! Please feel free to create issues and submit PRs.