# 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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zjunlp.gitbook.io/easyedit/easyeditor/models/ike.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
