forked from aliyun/ros-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rag-application.yml
563 lines (545 loc) · 16.1 KB
/
rag-application.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
ROSTemplateFormatVersion: '2015-09-01'
Description:
zh-cn: 创建RAG应用,含AnalyticDB实例、ECS实例、VPC环境,自动化部署Streamlit服务与数据库配置。
en: Create RAG applications, including AnalyticDB instances, ECS instances, and VPC environments, and automatically deploy Streamlit services and database configurations.
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>.
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: 本方案会创建一个抢占式实例,并且自动部署 Streamlit 服务。
en: >-
This solution will create a spot instance and automatically deploy a
Streamlit 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: ECS Instance Password
zh-cn: ECS实例密码
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
AdbAccountName:
Type: String
Default: rag_demo
AllowedPattern: '^(?!root$)(?!gp)[a-z][a-z0-9_]{0,14}[a-z0-9]$'
Description:
en: >-
Server login password, Length 8-30, must contain three(Capital letters,
lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special
symbol in
zh-cn: >-
账号名称不能以 gp或pg开头,并且由小写字母、数字、下划线组成,字母开头,字母或数字结尾,字符长度为2-16。
Label:
en: AnalyticDB Account Name
zh-cn: AnalyticDB账号名称
ConstraintDescription:
en: >-
Length 8-30, must contain three(Capital letters, lowercase letters,
numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in)
zh-cn: '长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;''<>,.?/ 中的特殊符号)'
AdbAccountPassword:
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: AnalyticDB Account Password
zh-cn: AnalyticDB账号密码
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'
CommonName:
Type: String
Default: RAG
Resources:
Vpc:
Type: 'ALIYUN::ECS::VPC'
Properties:
CidrBlock: 192.168.0.0/16
VpcName:
Fn::Sub: ${CommonName}-vpc
VSwitch:
Type: 'ALIYUN::ECS::VSwitch'
Properties:
VpcId:
Ref: Vpc
CidrBlock: 192.168.0.0/24
ZoneId:
Ref: ZoneId
VSwitchName:
Fn::Sub: ${CommonName}-vsw
SecurityGroup:
Type: 'ALIYUN::ECS::SecurityGroup'
Properties:
VpcId:
Ref: Vpc
SecurityGroupName:
Fn::Sub: ${CommonName}-sg
SecurityGroupIngress:
- PortRange: 22/22
SourceCidrIp: 0.0.0.0/0
IpProtocol: tcp
- PortRange: 8501/8501
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: ubuntu_22_04_x64_20G_alibase_
InstanceName:
Fn::Sub: ${CommonName}-Streamlit-Server
InstanceType:
Ref: InstanceType
SystemDiskCategory: cloud_essd
MaxAmount: 1
InternetMaxBandwidthOut: 100
SpotStrategy: SpotAsPriceGo
Password:
Ref: InstancePassword
AnalyticDB:
Type: ALIYUN::GPDB::DBInstance
Properties:
EngineVersion: '6.0'
InstanceSpec: 4C16G
ZoneId:
Ref: ZoneId
VSwitchId:
Ref: VSwitch
SegNodeNum: 4
SegStorageType: cloud_essd
SegDiskPerformanceLevel: pl1
StorageSize: 200
VPCId:
Ref: Vpc
SecurityIPList: 192.168.0.0/24
DBInstanceDescription:
Fn::Sub: ${CommonName}-adb
PayType: Postpaid
DBInstanceCategory: Basic
DBInstanceMode: StorageElastic
VectorConfigurationStatus: true
AnalyticDBAccount:
Type: ALIYUN::GPDB::Account
Properties:
AccountDescription:
Fn::Sub: ${CommonName}-account
DBInstanceId:
Ref: AnalyticDB
AccountPassword:
Ref: AdbAccountPassword
AccountName:
Ref: AdbAccountName
User:
Type: ALIYUN::RAM::User
Properties:
UserName:
Fn::Sub: create_by_solution-${ALIYUN::StackId}
PolicyAttachments:
System:
- AliyunECSFullAccess
- AliyunGPDBFullAccess
- AliyunDashscopeFullAccess
AccessKey:
Type: ALIYUN::RAM::AccessKey
Properties:
UserName:
Ref: User
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 postgresql-client python3.10-venv unzip
# 5 - failed to create database
# 6 - failed to unzip adbpg-chat-demo.zip file
# 7 - failed to install requirements.txt
# 8 - failed to run streamlit
# 环境变量配置
export PATH=/usr/local/bin:$PATH
export DASHSCOPE_API_KEY=${DashScopeApiKey}
export ADB_CONNECTION_STRING=${AnalyticDB.ConnectionString}
export PGPASSWORD=${AdbAccountPassword}
export ALI_CLOUD_ACCESS_KEY_ID=${AccessKey.AccessKeyId}
export ALI_CLOUD_ACCESS_KEY_SECRET=${AccessKey.AccessKeySecret}
export ADBPG_INSTANCE_REGION=${ALIYUN::Region}
export ADBPG_INSTANCE_ID=${AnalyticDB}
echo $ADB_CONNECTION_STRING
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 "git clone"
REPO_URL="https://github.com/aliyun/adbpg-chat-demo.git"
ATTEMPT_NUM=1
MAX_RETRIES=10
SLEEP_SECONDS=10
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 create_database() {
log_info "create database"
psql -h ${AnalyticDB.ConnectionString} -U rag -c "CREATE DATABASE rag_demo;"
}
function run_streamlit() {
log_info "install postgresql-client python3.10-venv unzip"
sudo apt update
if ! debug_exec sudo apt install postgresql-client unzip python3.10-venv -y; then
log_fatal 4 "failed to postgresql-client python3.10-venv unzip."
fi
log_info "create database"
DATABASE_RET=`debug_exec create_database`
echo $DATABASE_RET
if [ $DATABASE_RET -ne 0 ] && [ $DATABASE_RET -ne 1 ]; then
log_fatal 5 "failed to create database in AnalyticDB ${AnalyticDB}"
fi
# clone_repo
wget -O adbpg-chat-demo-master.zip 'https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20240424/xzfoql/adbpg-chat-demo-master.zip?spm=a2c4g.2713484.0.0.18844eb5tF3232&file=adbpg-chat-demo-master.zip'
log_info "unzip code"
if ! debug_exec unzip adbpg-chat-demo-master.zip; then
log_fatal 6 "failed to unzip adbpg-chat-demo-mater.zip file."
fi
cd adbpg-chat-demo-master
log_info "pip install requirements.txt"
if ! debug_exec pip install -r requirements.txt; then
log_fatal 7 "failed to install requirements.txt."
fi
log_info "run streamlit"
if ! debug_exec "nohup streamlit run Home.py --server.maxUploadSize 300 > output.log 2>&1 &"; then
log_fatal 8 "failed to run streamlit"
fi
}
log_info "System Information:"
if ! lsb_release -a; then
unsupported_system
fi;
echo ""
run_streamlit
Outputs:
WebUrl:
Description:
zh-cn: Web 访问地址。
en: The Addresses of Web.
Value:
'Fn::Sub':
- 'http://${ServerAddress}:8501'
- 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'
AccountName:
Description:
zh-cn: 管理账户。
en: The account.
Value:
Fn::GetAtt:
- AnalyticDBAccount
- AccountName
Metadata:
'ALIYUN::ROS::Interface':
ParameterGroups:
- Parameters:
- DashScopeApiKey
- AdbAccountName
- AdbAccountPassword
Label:
default:
en: Account Configuration
zh-cn: 账号配置
- Parameters:
- ZoneId
- InstanceType
- InstancePassword
Label:
default:
en: Ecs Configuration
zh-cn: ECS 实例配置
Hidden:
- CommonName
TemplateTags:
- acs:technical-solution:ai:AnalyticDB和通义千问构建RAG应用-tech_solu_111
ALIYUN::ROS::Composer:
4b618060:
Res:
- AccessKey
- AccessKey
Rect:
- 40
- 40
- 733
- 317
- 1
- 0
Label: AccessKey
05953edd:
Res:
- User
- User
Rect:
- 40
- 40
- 733
- 427
- 1
- 0
Label: User
08073e2c:
Rect:
- 642
- 517
- 71
- 96
- 10
- 0
ResT: Composer::ROSParameter::Region
Label: cn-beijing
e1dc5a9f:
Res:
- Vpc
- Vpc
Parent: 08073e2c
Rect:
- 570
- 425
- 107
- 144
- 20
- 0
Label: Vpc
30caa4df:
Res:
- ZoneId
- ZoneId
Parent: e1dc5a9f
Rect:
- 502
- 346
- 143
- 192
- 30
- 0
ResT: Composer::ROSParameter::Zone
Label: ZoneId
9babae9e:
Res:
- VSwitch
- VSwitch
Parent: 30caa4df
Rect:
- 417
- 263
- 195
- 245
- 40
- 0
Label: VSwitch
98e4a8a8:
Res:
- RunCommand
- RunCommand
Parent: 9babae9e
Rect:
- 40
- 40
- 294
- 317
- 41
- 0
Label: RunCommand
84da954f:
Res:
- AnalyticDB
- AnalyticDB
Parent: 9babae9e
Rect:
- 40
- 40
- 485
- 427
- 41
- 0
Label: AnalyticDB
b54f6886:
Res:
- AnalyticDBAccount
- AnalyticDBAccount
Parent: 9babae9e
Rect:
- 40
- 40
- 294
- 427
- 41
- 0
Label: AnalyticDBAccount
2b0f077d:
Parent: 9babae9e
Edge:
- b54f6886
- 84da954f
Line: 0:0:0:gray:0
fd373cae:
Edge:
- 4b618060
- 05953edd
Line: 0:0:0:gray:0
17a0da9a:
Parent: 9babae9e
Edge:
- 98e4a8a8
- 55c471fd
Line: 0:0:0:gray:0
ac531bf8:
Res:
- SecurityGroup
- SecurityGroup
Parent: e1dc5a9f
Rect:
- 124
- 112
- 443
- 292
- 10001
- 0
Label: SecurityGroup
55c471fd:
Res:
- EcsInstance
- EcsInstance
Parent: 9babae9e
Rect:
- 40
- 40
- 485
- 317
- 10002
- 0
Layer:
- ac531bf8
Label: EcsInstance