Update README.md
Browse files
README.md
CHANGED
@@ -13,7 +13,7 @@ tags:
|
|
13 |
|
14 |
|
15 |
## Model Details
|
16 |
-
We
|
17 |
|
18 |
|
19 |
## Benchmark Results
|
@@ -54,6 +54,9 @@ Assistant:
|
|
54 |
|
55 |
|
56 |
## How to use
|
|
|
|
|
|
|
57 |
```python
|
58 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
59 |
import torch
|
@@ -67,7 +70,7 @@ messages = [
|
|
67 |
{"role": "user", "content": "what is the percentage change of the net income from Q4 FY23 to Q4 FY24?"}
|
68 |
]
|
69 |
|
70 |
-
|
71 |
|
72 |
def get_formatted_input(messages, context):
|
73 |
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."
|
@@ -79,18 +82,12 @@ def get_formatted_input(messages, context):
|
|
79 |
item['content'] = instruction + " " + item['content']
|
80 |
break
|
81 |
|
82 |
-
conversation = ""
|
83 |
-
for item in messages:
|
84 |
-
if item["role"] == "user":
|
85 |
-
conversation += "User: " + item["content"] + "\n\n"
|
86 |
-
else:
|
87 |
-
conversation += "Assistant: " + item["content"] + "\n\n"
|
88 |
-
conversation += "Assistant:"
|
89 |
-
|
90 |
formatted_input = system + "\n\n" + context + "\n\n" + conversation
|
|
|
91 |
return formatted_input
|
92 |
|
93 |
-
formatted_input = get_formatted_input(messages,
|
94 |
tokenized_prompt = tokenizer(tokenizer.bos_token + formatted_input, return_tensors="pt").to(model.device)
|
95 |
|
96 |
terminators = [
|
@@ -104,6 +101,63 @@ response = outputs[0][tokenized_prompt.input_ids.shape[-1]:]
|
|
104 |
print(tokenizer.decode(response, skip_special_tokens=True))
|
105 |
```
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
## Correspondence to
|
108 |
Zihan Liu ([email protected]), Wei Ping ([email protected])
|
109 |
|
|
|
13 |
|
14 |
|
15 |
## Model Details
|
16 |
+
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.
|
17 |
|
18 |
|
19 |
## Benchmark Results
|
|
|
54 |
|
55 |
|
56 |
## How to use
|
57 |
+
|
58 |
+
### take the whole document as context
|
59 |
+
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.
|
60 |
```python
|
61 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
62 |
import torch
|
|
|
70 |
{"role": "user", "content": "what is the percentage change of the net income from Q4 FY23 to Q4 FY24?"}
|
71 |
]
|
72 |
|
73 |
+
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% |"""
|
74 |
|
75 |
def get_formatted_input(messages, context):
|
76 |
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."
|
|
|
82 |
item['content'] = instruction + " " + item['content']
|
83 |
break
|
84 |
|
85 |
+
conversation = '\n\n'.join(["User: " + item["content"] if item["role"] == "user" else "Assistant: " + item["content"] for item in messages]) + "\n\nAssistant:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
formatted_input = system + "\n\n" + context + "\n\n" + conversation
|
87 |
+
|
88 |
return formatted_input
|
89 |
|
90 |
+
formatted_input = get_formatted_input(messages, document)
|
91 |
tokenized_prompt = tokenizer(tokenizer.bos_token + formatted_input, return_tensors="pt").to(model.device)
|
92 |
|
93 |
terminators = [
|
|
|
101 |
print(tokenizer.decode(response, skip_special_tokens=True))
|
102 |
```
|
103 |
|
104 |
+
### run retrieval to get top-n chunks as context
|
105 |
+
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]() for users to play with.
|
106 |
+
|
107 |
+
```python
|
108 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoModel
|
109 |
+
import torch
|
110 |
+
import json
|
111 |
+
|
112 |
+
## load ChatQA-1.5 tokenizer and model
|
113 |
+
model_id = "nvidia/ChatQA-1.5-70B"
|
114 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
115 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
|
116 |
+
|
117 |
+
## load retriever tokenizer and model
|
118 |
+
retriever_tokenizer = AutoTokenizer.from_pretrained('nvidia/dragon-multiturn-query-encoder')
|
119 |
+
query_encoder = AutoModel.from_pretrained('nvidia/dragon-multiturn-query-encoder')
|
120 |
+
context_encoder = AutoModel.from_pretrained('nvidia/dragon-multiturn-context-encoder')
|
121 |
+
|
122 |
+
## prepare documents, we take landrover car manual document that we provide as an example
|
123 |
+
chunk_list = json.load(open("docs.json"))['landrover']
|
124 |
+
|
125 |
+
messages = [
|
126 |
+
{"role": "user", "content": "how to connect the bluetooth in the car?"}
|
127 |
+
]
|
128 |
+
|
129 |
+
### running retrieval
|
130 |
+
## convert query into a format as follows:
|
131 |
+
## user: {user}\nagent: {agent}\nuser: {user}
|
132 |
+
formatted_query_for_retriever = '\n'.join([turn['role'] + ": " + turn['content'] for turn in messages]).strip()
|
133 |
+
|
134 |
+
query_input = retriever_tokenizer(formatted_query_for_retriever, return_tensors='pt')
|
135 |
+
ctx_input = retriever_tokenizer(chunk_list, padding=True, truncation=True, max_length=512, return_tensors='pt')
|
136 |
+
query_emb = query_encoder(**query_input).last_hidden_state[:, 0, :]
|
137 |
+
ctx_emb = context_encoder(**ctx_input).last_hidden_state[:, 0, :]
|
138 |
+
|
139 |
+
## Compute similarity scores using dot product and rank the similarity
|
140 |
+
similarities = query_emb.matmul(ctx_emb.transpose(0, 1)) # (1, num_ctx)
|
141 |
+
ranked_results = torch.argsort(similarities, dim=-1, descending=True) # (1, num_ctx)
|
142 |
+
|
143 |
+
## get top-n chunks (n=5)
|
144 |
+
retrieved_chunks = [chunk_list[idx] for idx in ranked_results.tolist()[0][:5]]
|
145 |
+
context = "\n\n".join(retrieved_chunks)
|
146 |
+
|
147 |
+
### running text generation
|
148 |
+
formatted_input = get_formatted_input(messages, context)
|
149 |
+
tokenized_prompt = tokenizer(tokenizer.bos_token + formatted_input, return_tensors="pt").to(model.device)
|
150 |
+
|
151 |
+
terminators = [
|
152 |
+
tokenizer.eos_token_id,
|
153 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
154 |
+
]
|
155 |
+
outputs = model.generate(input_ids=tokenized_prompt.input_ids, attention_mask=tokenized_prompt.attention_mask, max_new_tokens=128, eos_token_id=terminators)
|
156 |
+
|
157 |
+
response = outputs[0][tokenized_prompt.input_ids.shape[-1]:]
|
158 |
+
print(tokenizer.decode(response, skip_special_tokens=True))
|
159 |
+
```
|
160 |
+
|
161 |
## Correspondence to
|
162 |
Zihan Liu ([email protected]), Wei Ping ([email protected])
|
163 |
|