MonteXiaofeng
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -27,4 +27,84 @@ select best ckpt by the evaluation loss
|
|
27 |
|
28 |
The following is an evaluation on the FinerBen dataset metrci. Since there are too many samples in the dataset, I randomly selected 500 samples from each dataset for evaluation.
|
29 |
|
30 |
-
![image/png](https://cdn-uploads.huggingface.co/production/uploads/642f6c64f945a8a5c9ee5b5d/shSgSkQ7nQqiBAl6IwBy5.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
The following is an evaluation on the FinerBen dataset metrci. Since there are too many samples in the dataset, I randomly selected 500 samples from each dataset for evaluation.
|
29 |
|
30 |
+
![image/png](https://cdn-uploads.huggingface.co/production/uploads/642f6c64f945a8a5c9ee5b5d/shSgSkQ7nQqiBAl6IwBy5.png)
|
31 |
+
|
32 |
+
|
33 |
+
## how to use
|
34 |
+
|
35 |
+
```python
|
36 |
+
|
37 |
+
# !/usr/bin/env python
|
38 |
+
# -*- coding:utf-8 -*-
|
39 |
+
# ==================================================================
|
40 |
+
# [Author] : xiaofeng
|
41 |
+
# [Descriptions] :
|
42 |
+
# ==================================================================
|
43 |
+
|
44 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
45 |
+
import transformers
|
46 |
+
import torch
|
47 |
+
|
48 |
+
|
49 |
+
llama3_jinja = """{% if messages[0]['role'] == 'system' %}
|
50 |
+
{% set offset = 1 %}
|
51 |
+
{% else %}
|
52 |
+
{% set offset = 0 %}
|
53 |
+
{% endif %}
|
54 |
+
|
55 |
+
{{ bos_token }}
|
56 |
+
{% for message in messages %}
|
57 |
+
{% if (message['role'] == 'user') != (loop.index0 % 2 == offset) %}
|
58 |
+
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
|
59 |
+
{% endif %}
|
60 |
+
|
61 |
+
{{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] | trim + '<|eot_id|>' }}
|
62 |
+
{% endfor %}
|
63 |
+
|
64 |
+
{% if add_generation_prompt %}
|
65 |
+
{{ '<|start_header_id|>' + 'assistant' + '<|end_header_id|>\n\n' }}
|
66 |
+
{% endif %}"""
|
67 |
+
|
68 |
+
|
69 |
+
dtype = torch.bfloat16
|
70 |
+
|
71 |
+
model_dir = "MonteXiaofeng/Finance-llama3_1_8B_instruct"
|
72 |
+
model = AutoModelForCausalLM.from_pretrained(
|
73 |
+
model_dir,
|
74 |
+
device_map="cuda",
|
75 |
+
torch_dtype=dtype,
|
76 |
+
)
|
77 |
+
|
78 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
79 |
+
tokenizer.chat_template = llama3_jinja # update template
|
80 |
+
|
81 |
+
message = [
|
82 |
+
{"role": "system", "content": "You are a helpful assistant"},
|
83 |
+
{"role": "user", "content": "天气如何"},
|
84 |
+
]
|
85 |
+
prompt = tokenizer.apply_chat_template(
|
86 |
+
message, tokenize=False, add_generation_prompt=True
|
87 |
+
)
|
88 |
+
print(prompt)
|
89 |
+
inputs = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
|
90 |
+
prompt_length = len(inputs[0])
|
91 |
+
print(f"prompt_length:{prompt_length}")
|
92 |
+
|
93 |
+
generating_args = {
|
94 |
+
"do_sample": True,
|
95 |
+
"temperature": 1.0,
|
96 |
+
"top_p": 0.5,
|
97 |
+
"top_k": 15,
|
98 |
+
"max_new_tokens": 150,
|
99 |
+
}
|
100 |
+
|
101 |
+
|
102 |
+
generate_output = model.generate(input_ids=inputs.to(model.device), **generating_args)
|
103 |
+
|
104 |
+
response_ids = generate_output[:, prompt_length:]
|
105 |
+
response = tokenizer.batch_decode(
|
106 |
+
response_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
|
107 |
+
)
|
108 |
+
print(response)
|
109 |
+
|
110 |
+
```
|