jingwang commited on
Commit
0d20ff6
1 Parent(s): 39fff47

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +56 -1
README.md CHANGED
@@ -19,4 +19,59 @@ base_model: unsloth/mistral-7b-v0.3-bnb-4bit
19
 
20
  This mistral model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
21
 
22
- [<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  This mistral model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
21
 
22
+
23
+ # install dependencies in google colab
24
+
25
+ ```shell
26
+ !pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
27
+ !pip install --no-deps xformers "trl<0.9.0" peft accelerate bitsandbytes
28
+ ```
29
+
30
+ # inference
31
+ ```python
32
+
33
+ from unsloth import FastLanguageModel
34
+ from typing import Dict, List, Tuple, Union, Any
35
+ import pandas
36
+ from tqdm import trange, tqdm
37
+ import torch
38
+
39
+ class FormatPrompt_QA_with_citation():
40
+ '''format prompt class'''
41
+ def __init__(self, eos_token:str='</s>') -> None:
42
+ self.inputs = ['context','question'] # required input fields
43
+ self.outputs = ['answer', 'citation'] # for training, and model inference output fields
44
+ self.eos_token = eos_token
45
+
46
+ def __call__(self, instance: Dict[str, Any]) -> str:
47
+ '''
48
+ function call operator
49
+ Args:
50
+ instance: dictionary with keys: 'question', 'answer'
51
+ Returns:
52
+ prompt: formatted prompt
53
+ '''
54
+ return self.formatting_prompt_func(instance)
55
+
56
+ def formatting_prompt_func(self, instance: dict) -> str:
57
+ '''format prompt for domain specific QA
58
+ note this is for fine-tuning pre-trained model,
59
+ if starting with instuct tuned model, use `tokenizer.apply_chat_template(messages)` instead
60
+ '''
61
+
62
+ assert all([ item in instance.keys() for item in self.inputs ]), logging.info(f"instance must have {self.inputs}!")
63
+
64
+ prompt = f"""<s> [INST] Context: {str(instance["context"])}\
65
+ Question: {str(instance["question"])} [/INST]
66
+ Answer: """
67
+
68
+ if ('answer' in instance):
69
+ if ('citation' in instance):
70
+ answer = {"answer":str(instance['answer']), "citation":str(instance['citation'])}
71
+ else:
72
+ answer = {"answer":str(instance['answer']), "citation":""}
73
+ prompt += json.dumps(answer, ensure_ascii=False) + self.eos_token # json format
74
+ else:
75
+ pass
76
+ return prompt
77
+ ```