fakezeta commited on
Commit
09e2bb3
·
verified ·
1 Parent(s): 8707a11

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +194 -3
README.md CHANGED
@@ -1,3 +1,194 @@
1
- ---
2
- license: llama3
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: llama3
3
+ language:
4
+ - en
5
+ pipeline_tag: text-generation
6
+ tags:
7
+ - nvidia
8
+ - chatqa-1.5
9
+ - chatqa
10
+ - llama-3
11
+ - pytorch
12
+ ---
13
+
14
+ # OpenVINO IR model with int8 quantization
15
+
16
+ Model definition for LocalAI:
17
+ ```
18
+ name: ChatQA
19
+ backend: transformers
20
+ parameters:
21
+ model: fakezeta/ChatQA-1.5-8B-ov-int8
22
+ context_size: 8192
23
+ type: OVModelForCausalLM
24
+ template:
25
+ use_tokenizer_template: true
26
+ stopwords:
27
+ - "<|eot_id|>"
28
+ - "<|end_of_text|>"
29
+ ```
30
+
31
+ ## Model Details
32
+ We introduce ChatQA-1.5, which excels at conversational question answering (QA) and retrieval-augumented generation (RAG). ChatQA-1.5 is built using the training recipe from [ChatQA (1.0)](https://arxiv.org/abs/2401.10225), and it is built on top of Llama-3 foundation model. Additionally, we incorporate more conversational QA data to enhance its tabular and arithmatic calculation capability. ChatQA-1.5 has two variants: ChatQA-1.5-8B and ChatQA-1.5-70B. Both models were originally trained using [Megatron-LM](https://github.com/NVIDIA/Megatron-LM), we converted the checkpoints to Hugging Face format.
33
+
34
+ ## Other Resources
35
+ [ChatQA-1.5-70B](https://huggingface.co/nvidia/ChatQA-1.5-70B) &ensp; [Evaluation Data](https://huggingface.co/datasets/nvidia/ConvRAG-Bench) &ensp; [Training Data](https://huggingface.co/datasets/nvidia/ChatQA-Training-Data) &ensp; [Retriever](https://huggingface.co/nvidia/dragon-multiturn-query-encoder)
36
+
37
+ ## Benchmark Results
38
+ Results in ConvRAG Bench are as follows:
39
+
40
+ | | ChatQA-1.0-7B | Command-R-Plus | Llama-3-instruct-70b | GPT-4-0613 | ChatQA-1.0-70B | ChatQA-1.5-8B | ChatQA-1.5-70B |
41
+ | -- |:--:|:--:|:--:|:--:|:--:|:--:|:--:|
42
+ | Doc2Dial | 37.88 | 33.51 | 37.88 | 34.16 | 38.9 | 39.33 | 41.26 |
43
+ | QuAC | 29.69 | 34.16 | 36.96 | 40.29 | 41.82 | 39.73 | 38.82 |
44
+ | QReCC | 46.97 | 49.77 | 51.34 | 52.01 | 48.05 | 49.03 | 51.40 |
45
+ | CoQA | 76.61 | 69.71 | 76.98 | 77.42 | 78.57 | 76.46 | 78.44 |
46
+ | DoQA | 41.57 | 40.67 | 41.24 | 43.39 | 51.94 | 49.6 | 50.67 |
47
+ | ConvFinQA | 51.61 | 71.21 | 76.6 | 81.28 | 73.69 | 78.46 | 81.88 |
48
+ | SQA | 61.87 | 74.07 | 69.61 | 79.21 | 69.14 | 73.28 | 83.82 |
49
+ | TopioCQA | 45.45 | 53.77 | 49.72 | 45.09 | 50.98 | 49.96 | 55.63 |
50
+ | HybriDial* | 54.51 | 46.7 | 48.59 | 49.81 | 56.44 | 65.76 | 68.27 |
51
+ | INSCIT | 30.96 | 35.76 | 36.23 | 36.34 | 31.9 | 30.1 | 32.31 |
52
+ | Average (all) | 47.71 | 50.93 | 52.52 | 53.90 | 54.14 | 55.17 | 58.25 |
53
+ | Average (exclude HybriDial) | 46.96 | 51.40 | 52.95 | 54.35 | 53.89 | 53.99 | 57.14 |
54
+
55
+ Note that ChatQA-1.5 used some samples from the HybriDial training dataset. To ensure fair comparison, we also compare average scores excluding HybriDial. The data and evaluation scripts for ConvRAG can be found [here](https://huggingface.co/datasets/nvidia/ConvRAG-Bench).
56
+
57
+
58
+ ## Prompt Format
59
+ <pre>
60
+ System: {System}
61
+
62
+ {Context}
63
+
64
+ User: {Question}
65
+
66
+ Assistant: {Response}
67
+
68
+ User: {Question}
69
+
70
+ Assistant:
71
+ </pre>
72
+
73
+
74
+ ## How to use
75
+
76
+ ### take the whole document as context
77
+ This can be applied to the scenario where the whole document can be fitted into the model, so that there is no need to run retrieval over the document.
78
+ ```python
79
+ from transformers import AutoTokenizer, AutoModelForCausalLM
80
+ import torch
81
+
82
+ model_id = "nvidia/ChatQA-1.5-8B"
83
+
84
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
85
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
86
+
87
+ messages = [
88
+ {"role": "user", "content": "what is the percentage change of the net income from Q4 FY23 to Q4 FY24?"}
89
+ ]
90
+
91
+ document = """NVIDIA (NASDAQ: NVDA) today reported revenue for the fourth quarter ended January 28, 2024, of $22.1 billion, up 22% from the previous quarter and up 265% from a year ago.\nFor the quarter, GAAP earnings per diluted share was $4.93, up 33% from the previous quarter and up 765% from a year ago. Non-GAAP earnings per diluted share was $5.16, up 28% from the previous quarter and up 486% from a year ago.\nQ4 Fiscal 2024 Summary\nGAAP\n| $ in millions, except earnings per share | Q4 FY24 | Q3 FY24 | Q4 FY23 | Q/Q | Y/Y |\n| Revenue | $22,103 | $18,120 | $6,051 | Up 22% | Up 265% |\n| Gross margin | 76.0% | 74.0% | 63.3% | Up 2.0 pts | Up 12.7 pts |\n| Operating expenses | $3,176 | $2,983 | $2,576 | Up 6% | Up 23% |\n| Operating income | $13,615 | $10,417 | $1,257 | Up 31% | Up 983% |\n| Net income | $12,285 | $9,243 | $1,414 | Up 33% | Up 769% |\n| Diluted earnings per share | $4.93 | $3.71 | $0.57 | Up 33% | Up 765% |"""
92
+
93
+ def get_formatted_input(messages, context):
94
+ system = "System: This is a chat between a user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions based on the context. The assistant should also indicate when the answer cannot be found in the context."
95
+ instruction = "Please give a full and complete answer for the question."
96
+
97
+ for item in messages:
98
+ if item['role'] == "user":
99
+ ## only apply this instruction for the first user turn
100
+ item['content'] = instruction + " " + item['content']
101
+ break
102
+
103
+ conversation = '\n\n'.join(["User: " + item["content"] if item["role"] == "user" else "Assistant: " + item["content"] for item in messages]) + "\n\nAssistant:"
104
+ formatted_input = system + "\n\n" + context + "\n\n" + conversation
105
+
106
+ return formatted_input
107
+
108
+ formatted_input = get_formatted_input(messages, document)
109
+ tokenized_prompt = tokenizer(tokenizer.bos_token + formatted_input, return_tensors="pt").to(model.device)
110
+
111
+ terminators = [
112
+ tokenizer.eos_token_id,
113
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
114
+ ]
115
+
116
+ outputs = model.generate(input_ids=tokenized_prompt.input_ids, attention_mask=tokenized_prompt.attention_mask, max_new_tokens=128, eos_token_id=terminators)
117
+
118
+ response = outputs[0][tokenized_prompt.input_ids.shape[-1]:]
119
+ print(tokenizer.decode(response, skip_special_tokens=True))
120
+ ```
121
+
122
+ ### run retrieval to get top-n chunks as context
123
+ This can be applied to the scenario when the document is very long, so that it is necessary to run retrieval. Here, we use our [Dragon-multiturn](https://huggingface.co/nvidia/dragon-multiturn-query-encoder) retriever which can handle conversatinoal query. In addition, we provide a few [documents](https://huggingface.co/nvidia/ChatQA-1.5-8B/tree/main/docs) for users to play with.
124
+
125
+ ```python
126
+ from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel
127
+ import torch
128
+ import json
129
+
130
+ ## load ChatQA-1.5 tokenizer and model
131
+ model_id = "nvidia/ChatQA-1.5-8B"
132
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
133
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
134
+
135
+ ## load retriever tokenizer and model
136
+ retriever_tokenizer = AutoTokenizer.from_pretrained('nvidia/dragon-multiturn-query-encoder')
137
+ query_encoder = AutoModel.from_pretrained('nvidia/dragon-multiturn-query-encoder')
138
+ context_encoder = AutoModel.from_pretrained('nvidia/dragon-multiturn-context-encoder')
139
+
140
+ ## prepare documents, we take landrover car manual document that we provide as an example
141
+ chunk_list = json.load(open("docs.json"))['landrover']
142
+
143
+ messages = [
144
+ {"role": "user", "content": "how to connect the bluetooth in the car?"}
145
+ ]
146
+
147
+ ### running retrieval
148
+ ## convert query into a format as follows:
149
+ ## user: {user}\nagent: {agent}\nuser: {user}
150
+ formatted_query_for_retriever = '\n'.join([turn['role'] + ": " + turn['content'] for turn in messages]).strip()
151
+
152
+ query_input = retriever_tokenizer(formatted_query_for_retriever, return_tensors='pt')
153
+ ctx_input = retriever_tokenizer(chunk_list, padding=True, truncation=True, max_length=512, return_tensors='pt')
154
+ query_emb = query_encoder(**query_input).last_hidden_state[:, 0, :]
155
+ ctx_emb = context_encoder(**ctx_input).last_hidden_state[:, 0, :]
156
+
157
+ ## Compute similarity scores using dot product and rank the similarity
158
+ similarities = query_emb.matmul(ctx_emb.transpose(0, 1)) # (1, num_ctx)
159
+ ranked_results = torch.argsort(similarities, dim=-1, descending=True) # (1, num_ctx)
160
+
161
+ ## get top-n chunks (n=5)
162
+ retrieved_chunks = [chunk_list[idx] for idx in ranked_results.tolist()[0][:5]]
163
+ context = "\n\n".join(retrieved_chunks)
164
+
165
+ ### running text generation
166
+ formatted_input = get_formatted_input(messages, context)
167
+ tokenized_prompt = tokenizer(tokenizer.bos_token + formatted_input, return_tensors="pt").to(model.device)
168
+
169
+ terminators = [
170
+ tokenizer.eos_token_id,
171
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
172
+ ]
173
+ outputs = model.generate(input_ids=tokenized_prompt.input_ids, attention_mask=tokenized_prompt.attention_mask, max_new_tokens=128, eos_token_id=terminators)
174
+
175
+ response = outputs[0][tokenized_prompt.input_ids.shape[-1]:]
176
+ print(tokenizer.decode(response, skip_special_tokens=True))
177
+ ```
178
+
179
+ ## Correspondence to
180
+ Zihan Liu ([email protected]), Wei Ping ([email protected])
181
+
182
+ ## Citation
183
+ <pre>
184
+ @article{liu2024chatqa,
185
+ title={ChatQA: Building GPT-4 Level Conversational QA Models},
186
+ author={Liu, Zihan and Ping, Wei and Roy, Rajarshi and Xu, Peng and Lee, Chankyu and Shoeybi, Mohammad and Catanzaro, Bryan},
187
+ journal={arXiv preprint arXiv:2401.10225},
188
+ year={2024}}
189
+ </pre>
190
+
191
+
192
+ ## License
193
+ The use of this model is governed by the [META LLAMA 3 COMMUNITY LICENSE AGREEMENT](https://llama.meta.com/llama3/license/)
194
+