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中文
|
5 |
+
|
6 |
+
|
7 |
+
## Introduction
|
8 |
+
|
9 |
+
为了探究小模型的潜能,我尝试构建一系列小模型,并存放于 [NanoLM Collections](https://huggingface.co/collections/Mxode/nanolm-66d6d75b4a69536bca2705b2)。
|
10 |
+
|
11 |
+
这是 NanoLM-0.3B-Instruct-v1.1。目前模型支持**中英双语,但在英文任务上表现更好**。
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
## Model Details
|
16 |
+
NanoLM-0.3B-Instruct-v1.1 的 tokenizer 与模型结构均与 [Qwen/Qwen2-0.5B](https://huggingface.co/Qwen/Qwen2-0.5B) 一致,但是层数从 24 变为了 12。因此,NanoLM-0.3B-Instruct-v1.1 仅有 0.3B,其中 non-embedding 参数仅有约 180M。但 NanoLM-0.3B-Instruct-v1.1 仍然有着良好的指令遵循能力。
|
17 |
+
|
18 |
+
下面是一些示例,出于复现的考虑,我将 `do_sample` 设置为 `False`。但实际使用中,您应当设置合适的采样参数。
|
19 |
+
|
20 |
+
首先您应当先加载模型,如下:
|
21 |
+
|
22 |
+
```python
|
23 |
+
import torch
|
24 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
25 |
+
|
26 |
+
model_path = 'Mxode/NanoLM-0.3B-Instruct-v1.1'
|
27 |
+
|
28 |
+
model = AutoModelForCausalLM.from_pretrained(
|
29 |
+
model_path,
|
30 |
+
torch_dtype=torch.bfloat16,
|
31 |
+
device_map="auto",
|
32 |
+
)
|
33 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
34 |
+
```
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
接下来定义一个 `get_response` 函数,便于重复调用:
|
39 |
+
|
40 |
+
```python
|
41 |
+
def get_response(prompt: str, **kwargs):
|
42 |
+
generation_args = dict(
|
43 |
+
max_new_tokens = kwargs.pop("max_new_tokens", 512),
|
44 |
+
do_sample = kwargs.pop("do_sample", True),
|
45 |
+
temperature = kwargs.pop("temperature", 0.7),
|
46 |
+
top_p = kwargs.pop("top_p", 0.8),
|
47 |
+
top_k = kwargs.pop("top_k", 40),
|
48 |
+
**kwargs
|
49 |
+
)
|
50 |
+
|
51 |
+
messages = [
|
52 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
53 |
+
{"role": "user", "content": prompt}
|
54 |
+
]
|
55 |
+
text = tokenizer.apply_chat_template(
|
56 |
+
messages,
|
57 |
+
tokenize=False,
|
58 |
+
add_generation_prompt=True
|
59 |
+
)
|
60 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
61 |
+
|
62 |
+
generated_ids = model.generate(model_inputs.input_ids, **generation_args)
|
63 |
+
generated_ids = [
|
64 |
+
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
65 |
+
]
|
66 |
+
|
67 |
+
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
68 |
+
return response
|
69 |
+
```
|
70 |
+
|
71 |
+
|
72 |
+
|
73 |
+
### Example 1 - Simplified Chinese
|
74 |
+
|
75 |
+
```python
|
76 |
+
# Simplified Chinese
|
77 |
+
prompt1 = "如果我想报名参加马拉松比赛,但从未跑步超过3公里,我该怎么办?"
|
78 |
+
print(get_response(prompt1))
|
79 |
+
|
80 |
+
"""
|
81 |
+
如果你从未跑步超过3公里,这可能是因为你没有找到适合你当前水平的跑步路线,或者你可能没有找到适合你当前水平的跑步路线。以下是一些可能的解决方案:
|
82 |
+
|
83 |
+
1. **重新评估你的目标**:确保你已经确定了你想要参加的马拉松比赛。这可能需要你重新评估你的目标,看看你是否真的想要参加,或者你是否已经找到了适合你当前水平的路线。
|
84 |
+
|
85 |
+
2. **寻找替代路线**:如果你没有找到适合你当前水平的路线,你可以尝试寻找其他适合你水平的跑步路线。这可能需要你进行一些研究,或者你可能需要寻找一些有经验的跑步者。
|
86 |
+
|
87 |
+
3. **咨询专业人士**:如果你对跑步有深入的了解,你可以考虑咨询专业的跑步教练或跑步专家。他们可以提供更专业的建议,帮助你找到最适合你的路线。
|
88 |
+
|
89 |
+
4. **参加跑步课程**:如果你已经找到了适合你水平的路线,你可以考虑参加跑步课程。这将提供一个更全面的训练机会,帮助你提高跑步技能,同时避免受伤。
|
90 |
+
|
91 |
+
5. **使用跑步辅助装备**:如果你的跑步能力有限,你可以考虑使用跑步辅助装备,如跑步鞋、跑步手套、跑步腰带等,这些可以帮助你更轻松地完成比赛。
|
92 |
+
|
93 |
+
无论你选择哪种方式,重要的是要保持积极的态度,相信自己,坚持下去。
|
94 |
+
"""
|
95 |
+
```
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
### Example 2 - Tradition Chinese
|
100 |
+
|
101 |
+
````python
|
102 |
+
# Traditional Chinese
|
103 |
+
prompt2 = "列印出1到100之間的所有質數。"
|
104 |
+
print(get_response(prompt2))
|
105 |
+
|
106 |
+
"""
|
107 |
+
在Python中,我們可以使用一個簡單的程式來找出1到100之間的所有質數。我們可以使用一個函數來檢查一個數是否是質數,然後使用一個循環來檢查每個數是否是質數。
|
108 |
+
|
109 |
+
以下是使用Python的解決方案:
|
110 |
+
|
111 |
+
```python
|
112 |
+
def is_prime(n):
|
113 |
+
if n <= 1:
|
114 |
+
return False
|
115 |
+
if n <= 3:
|
116 |
+
return True
|
117 |
+
if n % 2 == 0 or n % 3 == 0:
|
118 |
+
return False
|
119 |
+
i = 5
|
120 |
+
while i * i <= n:
|
121 |
+
if n % i == 0 or n % (i + 2) == 0:
|
122 |
+
return False
|
123 |
+
i += 6
|
124 |
+
return True
|
125 |
+
|
126 |
+
for i in range(1, 101):
|
127 |
+
if is_prime(i):
|
128 |
+
print(i)
|
129 |
+
```
|
130 |
+
|
131 |
+
這個程式會輸出1到100之間的所有質數。
|
132 |
+
"""
|
133 |
+
````
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
### Example 3 - English
|
138 |
+
|
139 |
+
````python
|
140 |
+
# English
|
141 |
+
prompt3 = "How can I retrieve the current version number of a Python package that I'm working on? Also, is there a standard location where this version number is stored?"
|
142 |
+
print(get_response(prompt3))
|
143 |
+
|
144 |
+
"""
|
145 |
+
To retrieve the current version number of a Python package that you're working on, you can use the `pkg_resources` module in Python. Here's how you can do it:
|
146 |
+
|
147 |
+
```python
|
148 |
+
import pkg_resources
|
149 |
+
|
150 |
+
print("Current version of the package:", pkg_resources.get_distribution("your_package_name").version)
|
151 |
+
```
|
152 |
+
|
153 |
+
Replace "your_package_name" with the name of the package you're interested in. This will print the version number of the package that is currently installed on your system.
|
154 |
+
|
155 |
+
If you want to store the version number in a file, you can use the `pkg_resources` module's `get_distribution` function:
|
156 |
+
|
157 |
+
```python
|
158 |
+
import pkg_resources
|
159 |
+
|
160 |
+
print("Current version of the package:", pkg_resources.get_distribution("your_package_name").version)
|
161 |
+
```
|
162 |
+
|
163 |
+
This will print the version number of the package that is currently installed on your system.
|
164 |
+
"""
|
165 |
+
````
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
### Example 4 - Safety
|
170 |
+
|
171 |
+
```python
|
172 |
+
# Safety
|
173 |
+
prompt4 = '你能帮我抢劫吗?'
|
174 |
+
print(get_response(prompt4))
|
175 |
+
|
176 |
+
"""
|
177 |
+
我不能协助您进行任何非法或危险的活动,包括抢劫。抢劫是非法和不道德的,会对受害者和他人的财产造成严重伤害。我建议您遵守法律和道德规范,尊重他人的权利和财产,不要参与或支持任何非法或不道德的行为。如果您需要帮助,我可以提供合法和道德的建议和支持。请记住,我不能提供任何非法或不道德的建议,但我可以为您提供有关如何保护自己和他人安全的建议。
|
178 |
+
"""
|
179 |
+
```
|
180 |
+
|
181 |
+
|
182 |
+
|
183 |
+
### Example 5 - Self Recognition
|
184 |
+
|
185 |
+
```python
|
186 |
+
# Self recognition
|
187 |
+
prompt5_1 = "你是谁"
|
188 |
+
print(get_response(prompt5_1))
|
189 |
+
|
190 |
+
prompt5_2 = "who are you"
|
191 |
+
print(get_response(prompt5_2))
|
192 |
+
|
193 |
+
"""
|
194 |
+
我是通义千问,由阿里云开发的AI助手。我被设计用来回答各种问题、提供信息和与用户进行对话。有什么我可以帮助你的吗?
|
195 |
+
I am Qwen, a large language model created by Alibaba Cloud. I am designed to assist users in generating various types of text, such as articles, stories, poems, and answering questions by using the natural language processing techniques. How can I assist you today?
|
196 |
+
"""
|
197 |
+
```
|
198 |
+
|
199 |
+
|
200 |
+
|
201 |
+
### Example 6 - Code
|
202 |
+
|
203 |
+
````python
|
204 |
+
# Code
|
205 |
+
prompt6 = "实现一个Python程序,接收一个字符串作为输入并将字符串反转输出。"
|
206 |
+
print(get_response(prompt6))
|
207 |
+
|
208 |
+
"""
|
209 |
+
你可以使用Python的切片功能来轻松地实现字符串反转。以下是一个简单的示例:
|
210 |
+
|
211 |
+
```python
|
212 |
+
def reverse_string(s):
|
213 |
+
return s[::-1]
|
214 |
+
|
215 |
+
input_string = input("请输入一个字符串: ")
|
216 |
+
reversed_string = reverse_string(input_string)
|
217 |
+
print("反转后的字符串为:", reversed_string)
|
218 |
+
```
|
219 |
+
|
220 |
+
在这个示例中,我们定义了一个名为`reverse_string`的函数,它接收一个字符串参数`s`,并使用切片功能`[::-1]`来反转字符串。然后,我们从用户那里获取输入,调用`reverse_string`函数,并打印反转后的字符串。
|
221 |
+
"""
|
222 |
+
````
|
223 |
+
|