forked from aliyun/ros-templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exclusive-qa-service.yml
584 lines (525 loc) · 20 KB
/
exclusive-qa-service.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
ROSTemplateFormatVersion: '2015-09-01'
Description:
en: DashVector and Tongyi QianWen build exclusive Q&A service.
zh-cn: 向量检索与通义千问搭建专属问答服务。
Parameters:
DashScopeApiKey:
Type: String
AssociationProperty: 'ALIYUN::DashScope::ApiKey'
Label:
en: DashScope API-KEY
zh-cn: DashScope API-KEY
Description:
zh-cn: >-
开通灵积模型服务,并获得 API-KEY。请参考:
<a href="https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key"
target="_blank">开通DashScope并创建API-KEY</a>。
en: >-
Activate DashScope and obtain the API-KEY. Please refer to:
<a href="https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key"
target="_blank">Activate DashScope and create API-KEY</a>.
DashVectorApiKey:
Type: String
AssociationProperty: 'ALIYUN::DashVector::ApiKey'
Label:
en: DashVector API-KEY
zh-cn: DashVector API-KEY
Description:
zh-cn: >-
开通DashVector向量检索服务,并获得 API-KEY。请参考:
<a href="https://help.aliyun.com/document_detail/2510230.html"
target="_blank">开通DashVector并创建API-KEY</a>。
en: >-
Activate DashVector and obtain the API-KEY. Please refer to:
<a href="https://help.aliyun.com/document_detail/2510230.html"
target="_blank">Activate DashVector and create API-KEY</a>.
ClusterName:
Type: String
Label:
en: Name of DashVector Cluster
zh-cn: DashVector集群名称
AllowedPattern: '[-a-z0-9_]{3,32}'
AssociationProperty: AutoCompleteInput
AssociationPropertyMetadata:
Length: 5
Prefix: exclusive-qa-server-
CharacterClasses:
- Class: lowercase
min: 1
ConstraintDescription:
zh-cn: 由大小写字母、数字、下划线(_)、中划线(-)组成,长度[3, 32]。
en: It consists of uppercase and lowercase letters, numbers, underline (_), and dash (-). The length is [3, 32]
Description:
zh-cn: 本方案会创建一个免费版的集群,如果您账号下已经创建好了免费版的集群,请输入免费版集群的名称。
en: >-
This solution will create a free version cluster. If you have already created
a free version cluster under your account, please enter the name of the free version cluster.
ZoneId:
Type: String
AssociationProperty: 'ALIYUN::ECS::Instance::ZoneId'
AssociationPropertyMetadata:
AutoSelectFirst: true
Label:
en: Availability Zone
zh-cn: 可用区
InstanceType:
Type: String
AssociationProperty: 'ALIYUN::ECS::Instance::InstanceType'
AssociationPropertyMetadata:
SpotStrategy: SpotAsPriceGo
InstanceChargeType: PostPaid
SystemDiskCategory: cloud_essd
ZoneId: ${ZoneId}
Label:
en: Instance Type
zh-cn: 实例规格
Description:
zh-cn: 本方案会创建一个抢占式实例,并且自动部署专属问答服务。
en: >-
This solution will create a spot instance and automatically deploy a
exclusive question and answer service.
Default: ecs.c7.large
InstancePassword:
NoEcho: true
Type: String
Description:
en: >-
Server login password, Length 8-30, must contain three(Capital letters,
lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
symbol in)
zh-cn: >-
服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/
中的特殊符号)
Label:
en: Instance Password
zh-cn: 实例密码
ConstraintDescription:
en: >-
Length 8-30, must contain three(Capital letters, lowercase letters,
numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
AssociationProperty: 'ALIYUN::ECS::Instance::Password'
Default: null
Resources:
Vpc:
Type: 'ALIYUN::ECS::VPC'
Properties:
CidrBlock: 192.168.0.0/16
VSwitch:
Type: 'ALIYUN::ECS::VSwitch'
Properties:
VpcId:
Ref: Vpc
CidrBlock: 192.168.0.0/24
ZoneId:
Ref: ZoneId
SecurityGroup:
Type: 'ALIYUN::ECS::SecurityGroup'
Properties:
VpcId:
Ref: Vpc
SecurityGroupIngress:
- PortRange: 22/22
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
- PortRange: 5000/5000
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
EcsInstance:
Type: 'ALIYUN::ECS::InstanceGroup'
Properties:
VpcId:
Ref: Vpc
ZoneId:
Ref: ZoneId
VSwitchId:
Ref: VSwitch
SecurityGroupId:
Ref: SecurityGroup
ImageId: aliyun_3_9_x64_20G_alibase_
InstanceName: dashVector
InstanceType:
Ref: InstanceType
SystemDiskCategory: cloud_essd
MaxAmount: 1
InternetMaxBandwidthOut: 100
SpotStrategy: SpotAsPriceGo
Password:
Ref: InstancePassword
DashVectorCluster:
Type: 'ALIYUN::DashVector::Cluster'
Properties:
IgnoreExisting: true
ReplicaCount: 1
ClusterName:
Ref: ClusterName
ClusterType: Serverless
RunCommand:
Type: 'ALIYUN::ECS::RunCommand'
Properties:
InstanceIds:
Fn::GetAtt:
- EcsInstance
- InstanceIds
Type: RunShellScript
Sync: true
Timeout: 3600
CommandContent:
Fn::Sub: |-
#!/bin/bash
# script exit code:
# 0 - success
# 1 - unsupported system
# 2 - network not available
# 3 - failed to git clone
# 4 - failed to init python environment
# 5 - failed to embedding data
# 6 - failed to init git
# 7 - failed to run flask app
# 环境变量配置
export PATH=/usr/local/bin:$PATH
export DASHSCOPE_API_KEY=${DashScopeApiKey}
export DASHVECTOR_API_KEY=${DashVectorApiKey}
export DASHVECTOR_CLUSTER_ENDPOINT=${DashVectorCluster.Endpoint}
# GitHub 仓库 URL
REPO_URL="https://github.com/shijiebei2009/CEC-Corpus.git"
# GitRepoName
REPO_NAME="CEC-Corpus"
# 最大尝试次数
MAX_RETRIES=10
# 当前尝试次数
ATTEMPT_NUM=1
# 等待时间 (秒) 在重试之前
SLEEP_SECONDS=10
# CollectionName
COLLECTION_NAME="news_embeddings"
function unsupported_system() {
log_fatal 1 "Unsupported System: $1"
}
function log_info() {
printf "%s [INFO] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
}
function log_error() {
printf "%s [ERROR] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$1"
}
function log_fatal() {
printf "\n========================================================================\n"
printf "%s [FATAL] %s\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$2"
printf "\n========================================================================\n"
exit $1
}
function debug_exec(){
local cmd="$@"
log_info "$cmd"
eval "$cmd"
ret=$?
echo ""
log_info "$cmd, exit code: $ret"
return $ret
}
function check_network_available() {
log_info "ping github.com ..."
if ! debug_exec ping -c 4 github.com; then
log_fatal 2 "Could not connect to https://github.com"
fi
}
function clone_repo() {
log_info "init git"
if ! debug_exec yum install -y git && git init; then
log_fatal 6 "failed to init git"
fi
rm -rf $REPO_NAME
until debug_exec git clone $REPO_URL; do
if [ "$ATTEMPT_NUM" -ge "$MAX_RETRIES" ]; then
log_fatal 3 "Reached maximum number of retries, aborting."
fi
log_error "Attempt $ATTEMPT_NUM failed! Trying again in $SLEEP_SECONDS seconds..."
sleep "$SLEEP_SECONDS"
ATTEMPT_NUM=$((ATTEMPT_NUM+1))
done
}
function init_env() {
log_info "init python environment"
yum install -y python3.8
ln -sf /usr/bin/python3.8 /usr/bin/python
python -m ensurepip
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
}
function embedding() {
log_info "embedding data"
if ! debug_exec python embedding.py; then
log_fatal 5 "failed to embedding data."
fi
}
log_info "System Information:"
if ! lsb_release -a; then
unsupported_system
fi;
echo ""
check_network_available
cat << 'EOF' > requirements.txt
flask==3.0.2
PySocks==1.7.1
dashscope==1.17.0
dashvector==1.0.14
EOF
if ! debug_exec init_env; then
log_fatal 4 "failed to init python environment"
fi
cat << EOF > embedding.py
# -*- coding: utf-8 -*-
import os
import dashscope
from dashscope import TextEmbedding
from dashvector import Client, Doc
def prepare_data(path, batch_size=25):
batch_docs = []
for file in os.listdir(path):
with open(path + '/' + file, 'r', encoding='utf-8') as f:
batch_docs.append(f.read())
if len(batch_docs) == batch_size:
yield batch_docs
batch_docs = []
if batch_docs:
yield batch_docs
def generate_embeddings(news):
rsp = TextEmbedding.call(
model=TextEmbedding.Models.text_embedding_v1,
input=news
)
embeddings = [record['embedding'] for record in rsp.output['embeddings']]
return embeddings if isinstance(news, list) else embeddings[0]
if __name__ == '__main__':
dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
collection_name = '$COLLECTION_NAME'
# 初始化 dashvector client
client = Client(
api_key=os.environ.get('DASHVECTOR_API_KEY'),
endpoint=os.environ.get('DASHVECTOR_CLUSTER_ENDPOINT')
)
try:
del_rsp = client.delete(collection_name)
assert del_rsp, del_rsp
except AssertionError as e:
if e.args[0].code != -2021:
raise
# 创建集合:指定集合名称和向量维度, text_embedding_v1 模型产生的向量统一为 1536 维
rsp = client.create(collection_name, 1536)
assert rsp, rsp
# 加载语料
id = 0
collection = client.get(collection_name)
for news in list(prepare_data('CEC-Corpus/raw corpus/allSourceText')):
ids = [id + i for i, _ in enumerate(news)]
id += len(news)
vectors = generate_embeddings(news)
# 写入 dashvector 构建索引
rsp = collection.upsert(
[
Doc(id=str(id), vector=vector, fields={"raw": doc})
for id, vector, doc in zip(ids, vectors, news)
]
)
assert rsp, rsp
EOF
cat << EOF > run.py
# -*- coding: utf-8 -*-
import os
import uuid
from flask import Flask, request, render_template_string, redirect, url_for, session
import dashscope
from dashscope import TextEmbedding, Generation
from dashvector import Client
app = Flask(__name__)
app.secret_key = uuid.uuid4().hex
# HTML 模板
HTML_TEMPLATE = '''
<!doctype html>
<html lang="en">
<head>
<title>DashVector</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background: #f7f7f7;
}
.container {
margin: auto;
padding: 20px;
max-width: 600px;
background: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h1 {
color: #333;
}
input[type="text"], textarea {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ddd;
border-radius: 5px;
box-sizing: border-box; /* 加上这个属性,防止 padding 喧嚣计算宽度 */
}
input[type="submit"] {
background: #5cb85c;
color: white;
padding: 10px 20px;
margin: 10px 0;
border: none;
border-radius: 5px;
cursor: pointer;
}
input[type="submit"]:hover {
background: #4cae4c;
}
.loader {
border: 5px solid #f3f3f3; /* Light grey */
border-top: 5px solid #3498db; /* Blue */
border-radius: 50%;
width: 50px;
height: 50px;
animation: spin 2s linear infinite;
margin: auto;
display: none;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="container">
<h1>基于专属知识的问答服务</h1>
<form method="post" id="textForm">
<label for="inputText">请输入问题:</label>
<input type="text" id="inputText" name="inputText" placeholder="Enter..." value="{{ input }}">
<input type="submit" value="Submit">
<!-- 加载动画 -->
<div class="loader" id="loader"></div>
</form>
{% if output %}
<h2>回答:</h2>
<textarea id="outputText" name="outputText" rows="4" disabled>{{ output }}</textarea>
{% endif %}
</div>
<script>
// 使用 JavaScript 在提交表单时显示加载动画
document.getElementById('textForm').onsubmit = function() {
document.getElementById('loader').style.display = 'block';
};
</script>
</body>
</html>
'''
@app.route('/', methods=['GET', 'POST'])
def home():
output_text = None
input_text = "海南安定追尾事故,发生在哪里?原因是什么?人员伤亡情况如何"
if 'output' in session:
output_text = session.pop('output', None)
input_text = session.get('input', None)
if request.method == 'POST':
input_text = request.form.get('inputText')
output_text = get_answers(input_text)
session['output'] = output_text
session['input'] = input_text
return redirect(url_for('home'))
return render_template_string(HTML_TEMPLATE, output=output_text, input=input_text)
def get_answers(input_text):
dashscope.api_key = os.environ.get('DASHSCOPE_API_KEY')
# question = '海南安定追尾事故,发生在哪里?原因是什么?人员伤亡情况如何?'
context = search_relevant_news(input_text)
return answer_question(input_text, context)
def search_relevant_news(question):
# 初始化 dashvector client
client = Client(
api_key=os.environ.get('DASHVECTOR_API_KEY'),
endpoint=os.environ.get('DASHVECTOR_CLUSTER_ENDPOINT')
)
# 获取刚刚存入的集合
collection = client.get('$COLLECTION_NAME')
assert collection, collection
# 向量检索:指定 topk = 1
rsp = collection.query(generate_embeddings(question), output_fields=['raw'], topk=1)
assert rsp, rsp
return rsp.output[0].fields['raw']
def generate_embeddings(news):
rsp = TextEmbedding.call(
model=TextEmbedding.Models.text_embedding_v1,
input=news
)
embeddings = [record['embedding'] for record in rsp.output['embeddings']]
return embeddings if isinstance(news, list) else embeddings[0]
def answer_question(question, context):
prompt = f'''请基于\`\`\`内的内容回答问题。"
\`\`\`
{context}
\`\`\`
我的问题是:{question}。
'''
rsp = Generation.call(model='qwen-turbo', prompt=prompt)
return rsp.output.text
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
EOF
clone_repo
embedding
if ! debug_exec "nohup python run.py > output.log 2>&1 &"; then
log_fatal "failed to run flask app"
fi
Outputs:
WebUrl:
Description:
zh-cn: Web 访问地址。
en: The Addresses of Web.
Value:
'Fn::Sub':
- 'http://${ServerAddress}:5000'
- ServerAddress:
'Fn::Select':
- 0
- 'Fn::GetAtt':
- EcsInstance
- PublicIps
EcsLoginAddress:
Description:
zh-cn: ECS登录地址。
en: Ecs login address.
Value:
'Fn::Sub':
- >-
https://ecs-workbench.aliyun.com/?from=EcsConsole&instanceType=ecs®ionId=${Region}&instanceId=${InstanceId}
- InstanceId:
'Fn::Select':
- 0
- 'Fn::GetAtt':
- EcsInstance
- InstanceIds
Region:
Ref: 'ALIYUN::Region'
Metadata:
'ALIYUN::ROS::Interface':
ParameterGroups:
- Parameters:
- DashScopeApiKey
- DashVectorApiKey
- ClusterName
Label:
default:
en: ApiKey
zh-cn: ApiKey配置
- Parameters:
- ZoneId
- InstanceType
- InstancePassword
Label:
default:
en: Ecs Instance
zh-cn: ECS实例配置
TemplateTags:
- acs:technical-solution:ai:向量检索与通义千问搭建专属问答服务-tech_solu_109