x54-729 commited on
Commit
cfe36b6
1 Parent(s): 2b91a35

Add stream_chat example

Browse files
Files changed (1) hide show
  1. README.md +34 -0
README.md CHANGED
@@ -86,6 +86,23 @@ print(output)
86
  # Hello! Today is sunny, and it sounds like a great day to go out an enjoy the weather. What would you like to do?
87
  ```
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  **Limitations:** Although we have made efforts to ensure the safety of the model during the training process and to encourage the model to generate text that complies with ethical and legal requirements, the model may still produce unexpected outputs due to its size and probabilistic generation paradigm. For example, the generated responses may contain biases, discrimination, or other harmful content. Please do not propagate such content. We are not responsible for any consequences resulting from the dissemination of harmful information.
90
 
91
 
@@ -153,6 +170,23 @@ print(output)
153
  # 你好!是的,今天的天气非常晴朗,非常适合户外活动。
154
  ```
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  **局限性:** 尽管在训练过程中我们非常注重模型的安全性,尽力促使模型输出符合伦理和法律要求的文本,但受限于模型大小以及概率生成范式,模型可能会产生各种不符合预期的输出,例如回复内容包含偏见、歧视等有害内容,请勿传播这些内容。由于传播不良信息导致的任何后果,本项目不承担责任。
157
 
158
  ## 开源许可证
 
86
  # Hello! Today is sunny, and it sounds like a great day to go out an enjoy the weather. What would you like to do?
87
  ```
88
 
89
+ The responses can be streamed using `stream_chat`:
90
+
91
+ ```python
92
+ import torch
93
+ from transformers import AutoModelForCausalLM, AutoTokenizer
94
+
95
+ model_path = "internlm/internlm-chat-20b"
96
+ model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.bfloat16, trust_remote_code=True)
97
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
98
+
99
+ model = model.eval()
100
+ length = 0
101
+ for response, history in model.stream_chat(tokenizer, "Hello", history=[]):
102
+ print(response[length:], flush=True, end="")
103
+ length = len(response)
104
+ ```
105
+
106
  **Limitations:** Although we have made efforts to ensure the safety of the model during the training process and to encourage the model to generate text that complies with ethical and legal requirements, the model may still produce unexpected outputs due to its size and probabilistic generation paradigm. For example, the generated responses may contain biases, discrimination, or other harmful content. Please do not propagate such content. We are not responsible for any consequences resulting from the dissemination of harmful information.
107
 
108
 
 
170
  # 你好!是的,今天的天气非常晴朗,非常适合户外活动。
171
  ```
172
 
173
+ 如果想进行流式生成,则可以使用 stream_chat 接口:
174
+
175
+ ```python
176
+ import torch
177
+ from transformers import AutoModelForCausalLM, AutoTokenizer
178
+
179
+ model_path = "internlm/internlm-chat-20b"
180
+ model = AutoModelForCausalLM.from_pretrained(model_path, torch_dype=torch.bfloat16, trust_remote_code=True)
181
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
182
+
183
+ model = model.eval()
184
+ length = 0
185
+ for response, history in model.stream_chat(tokenizer, "你好", history=[]):
186
+ print(response[length:], flush=True, end="")
187
+ length = len(response)
188
+ ```
189
+
190
  **局限性:** 尽管在训练过程中我们非常注重模型的安全性,尽力促使模型输出符合伦理和法律要求的文本,但受限于模型大小以及概率生成范式,模型可能会产生各种不符合预期的输出,例如回复内容包含偏见、歧视等有害内容,请勿传播这些内容。由于传播不良信息导致的任何后果,本项目不承担责任。
191
 
192
  ## 开源许可证