yuccaaa commited on
Commit
47dda14
·
verified ·
1 Parent(s): 95db7d3

Upload ms-swift/examples/notebook/qwen2_5-self-cognition/infer.ipynb with huggingface_hub

Browse files
ms-swift/examples/notebook/qwen2_5-self-cognition/infer.ipynb ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "## Inference\n",
8
+ "We have trained a well-trained checkpoint through the `self-cognition-sft.ipynb` tutorial, and here we use `PtEngine` to do the inference on it."
9
+ ]
10
+ },
11
+ {
12
+ "cell_type": "code",
13
+ "execution_count": 6,
14
+ "metadata": {},
15
+ "outputs": [],
16
+ "source": [
17
+ "# import some libraries\n",
18
+ "import os\n",
19
+ "os.environ['CUDA_VISIBLE_DEVICES'] = '0'\n",
20
+ "\n",
21
+ "from swift.llm import InferEngine, InferRequest, PtEngine, RequestConfig, get_template"
22
+ ]
23
+ },
24
+ {
25
+ "cell_type": "code",
26
+ "execution_count": 7,
27
+ "metadata": {},
28
+ "outputs": [],
29
+ "source": [
30
+ "# Hyperparameters for inference\n",
31
+ "last_model_checkpoint = 'output/checkpoint-xxx'\n",
32
+ "\n",
33
+ "# model\n",
34
+ "model_id_or_path = 'Qwen/Qwen2.5-3B-Instruct' # model_id or model_path\n",
35
+ "system = 'You are a helpful assistant.'\n",
36
+ "infer_backend = 'pt'\n",
37
+ "\n",
38
+ "# generation_config\n",
39
+ "max_new_tokens = 512\n",
40
+ "temperature = 0\n",
41
+ "stream = True"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": null,
47
+ "metadata": {},
48
+ "outputs": [],
49
+ "source": [
50
+ "# Get model and template, and load LoRA weights.\n",
51
+ "engine = PtEngine(model_id_or_path, adapters=[last_model_checkpoint])\n",
52
+ "template = get_template(engine.model_meta.template, engine.tokenizer, default_system=system)\n",
53
+ "# You can modify the `default_template` directly here, or pass it in during `engine.infer`.\n",
54
+ "engine.default_template = template"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "code",
59
+ "execution_count": 11,
60
+ "metadata": {},
61
+ "outputs": [
62
+ {
63
+ "name": "stdout",
64
+ "output_type": "stream",
65
+ "text": [
66
+ "query: who are you?\n",
67
+ "response: I am an artificial intelligence language model named Xiao Huang, developed by ModelScope. I can answer various questions and engage in conversation with humans. If you have any questions or need help, feel free to ask me at any time.\n",
68
+ "--------------------------------------------------\n",
69
+ "query: What should I do if I can't sleep at night?\n",
70
+ "response: If you're having trouble sleeping, there are several things you can try:\n",
71
+ "\n",
72
+ "1. Establish a regular sleep schedule: Try to go to bed and wake up at the same time every day, even on weekends.\n",
73
+ "\n",
74
+ "2. Create a relaxing bedtime routine: Engage in calming activities before bed, such as reading a book or taking a warm bath.\n",
75
+ "\n",
76
+ "3. Make your bedroom conducive to sleep: Keep your bedroom cool, dark, and quiet. Invest in comfortable bedding and pillows.\n",
77
+ "\n",
78
+ "4. Avoid stimulating activities before bed: Avoid using electronic devices, watching TV, or engaging in mentally stimulating activities before bed.\n",
79
+ "\n",
80
+ "5. Exercise regularly: Regular physical activity can help improve your sleep quality, but avoid exercising too close to bedtime.\n",
81
+ "\n",
82
+ "6. Manage stress: Practice relaxation techniques, such as deep breathing, meditation, or yoga, to help manage stress and promote better sleep.\n",
83
+ "\n",
84
+ "7. Limit caffeine and alcohol intake: Both caffeine and alcohol can disrupt sleep patterns, so it's best to limit their consumption, especially in the evening.\n",
85
+ "\n",
86
+ "8. Seek professional help: If you continue to have difficulty sleeping despite trying these strategies, consider seeking help from a healthcare provider or a sleep specialist.\n",
87
+ "--------------------------------------------------\n",
88
+ "query: 你是谁训练的?\n",
89
+ "response: 我是由魔搭团队训练和开发的。\n",
90
+ "--------------------------------------------------\n"
91
+ ]
92
+ }
93
+ ],
94
+ "source": [
95
+ "query_list = [\n",
96
+ " 'who are you?',\n",
97
+ " \"What should I do if I can't sleep at night?\",\n",
98
+ " '你是谁训练的?',\n",
99
+ "]\n",
100
+ "\n",
101
+ "def infer_stream(engine: InferEngine, infer_request: InferRequest):\n",
102
+ " request_config = RequestConfig(max_tokens=max_new_tokens, temperature=temperature, stream=True)\n",
103
+ " gen_list = engine.infer([infer_request], request_config)\n",
104
+ " query = infer_request.messages[0]['content']\n",
105
+ " print(f'query: {query}\\nresponse: ', end='')\n",
106
+ " for resp in gen_list[0]:\n",
107
+ " if resp is None:\n",
108
+ " continue\n",
109
+ " print(resp.choices[0].delta.content, end='', flush=True)\n",
110
+ " print()\n",
111
+ "\n",
112
+ "def infer(engine: InferEngine, infer_request: InferRequest):\n",
113
+ " request_config = RequestConfig(max_tokens=max_new_tokens, temperature=temperature)\n",
114
+ " resp_list = engine.infer([infer_request], request_config)\n",
115
+ " query = infer_request.messages[0]['content']\n",
116
+ " response = resp_list[0].choices[0].message.content\n",
117
+ " print(f'query: {query}')\n",
118
+ " print(f'response: {response}')\n",
119
+ "\n",
120
+ "infer_func = infer_stream if stream else infer\n",
121
+ "for query in query_list:\n",
122
+ " infer_func(engine, InferRequest(messages=[{'role': 'user', 'content': query}]))\n",
123
+ " print('-' * 50)"
124
+ ]
125
+ }
126
+ ],
127
+ "metadata": {
128
+ "kernelspec": {
129
+ "display_name": "test_py310",
130
+ "language": "python",
131
+ "name": "python3"
132
+ },
133
+ "language_info": {
134
+ "codemirror_mode": {
135
+ "name": "ipython",
136
+ "version": 3
137
+ },
138
+ "file_extension": ".py",
139
+ "mimetype": "text/x-python",
140
+ "name": "python",
141
+ "nbconvert_exporter": "python",
142
+ "pygments_lexer": "ipython3",
143
+ "version": "3.10.15"
144
+ }
145
+ },
146
+ "nbformat": 4,
147
+ "nbformat_minor": 2
148
+ }