> For the complete documentation index, see [llms.txt](https://zjunlp.gitbook.io/easyedit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zjunlp.gitbook.io/easyedit/easyeditor/models/ike.md).

# IKE

<figure><img src="/files/BK4fOYHliqli1iz5JNty" alt="" width="375"><figcaption><p>Paper: Can We Edit Factual Knowledge by In-Context Learning?</p></figcaption></figure>

#### encode\_ike\_facts

> In order to retrieve the nearest neighbors as in-context demonstrations, samples in the dev set need to be encoded

```python
def encode_ike_facts(
    sentence_model: SentenceTransformer,
    ds: Dataset,
    hparams: IKEHyperParams
):
```

**Paramters**

* sentence\_model(<mark style="color:purple;">SentenceTransformer</mark>): the model to encode demonstrations
  * default: [**all-MiniLM-L6-v2**](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)
* ds(<mark style="color:purple;">Dataset</mark>): the dev set, as a corpus for retrieving demonstrations
* hparams(<mark style="color:purple;">Hyperparams</mark>): hyperparameters for editing method

store dense embeddings in the form of pickle

#### apply\_ike\_to\_model()-> *PreTrainedModel*

> Main function: Given the request, it applies IKE to your model. Utilizing the preceding prompt to modify the behavior of the model

```python
def apply_ike_to_model(
    self,
    model: AutoModelForCausalLM,
    tok: AutoTokenizer,
    requests: List[Dict],
    hparams: IKEHyperParams,
    copy=False,
    return_orig_weights=False,
    keep_original_weight=False,
    **kwargs
):
```

* **Paramters**
  * model(<mark style="color:purple;">PreTrainedModel</mark>): model to be edited
  * tok(<mark style="color:purple;">PreTrainedTokenizer</mark>): tokenizer for inputs
  * requests(<mark style="color:purple;">List\[Dict]</mark>): The edit descriptors and targets.
  * hparams(<mark style="color:purple;">Hyperparams</mark>): hyperparameters for editing method
  * copy(<mark style="color:purple;">bool</mark>): whether to copy original model
  * return\_orig\_weights(<mark style="color:purple;">bool</mark>): whether to return the weights of original model
  * keep\_original\_weight(<mark style="color:purple;">bool</mark>): whether to edit sequentially
    * `False`: edit sequentially(because the original weight is not maintained after each edit)
    * `True`: not edit sequentially
* **Return Type**
  * edited\_model(<mark style="color:purple;">PreTrainedModel</mark>): model weights after editing

#### Example

<pre class="language-python"><code class="lang-python">// ...
hparams = IKEHyperaParams.from_hparams("llama-7b.yaml")
editor = BaseEditor.from_hparams(hparams)
prompts = ['What university did Watts Humphrey attend?',
    'Which family does Ramalinaceae belong to',
    'What role does Denny Herzig play in football?'
<strong>]
</strong>target_new = ['University of Michigan',
    'Lamiinae',
    'winger'
]
metrics, edited_model, _ = editor.edit(
    prompts=prompts,
    target_new=target_new,
    keep_original_weight=True
)
</code></pre>
