Skip to content

Commit 615c885

Browse files
authored
Merge pull request #33 from devsapp/dev_ollama
Dev ollama
2 parents 6cc794d + cb3a7b2 commit 615c885

5 files changed

Lines changed: 110 additions & 13 deletions

File tree

publish.yaml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Type: Application
1111
Name: start-modelscope
1212
Provider:
1313
- 阿里云
14-
Version: 0.2.14
14+
Version: dev
1515
Description: ModelScope应用
1616
HomePage: https://github.com/devsapp/start-modelscope
1717
Tags:
@@ -33,6 +33,7 @@ Parameters:
3333
additionalProperties: false
3434
required: # 必填项
3535
- region
36+
- imageTag
3637
- serviceName
3738
- roleArn
3839
- modelId
@@ -41,6 +42,10 @@ Parameters:
4142
- gpuMemorySize
4243
- memorySize
4344
- modelCache
45+
- ggufFile
46+
- modelfile
47+
- modelFamily
48+
- servedModelName
4449
properties:
4550
region:
4651
title: 地域
@@ -51,6 +56,11 @@ Parameters:
5156
# - cn-beijing
5257
- cn-hangzhou
5358
- cn-shanghai
59+
imageTag:
60+
title: modelscope镜像tag
61+
type: string
62+
default: fc-deploy-common-v17.3.3
63+
description: registry.${vars.region}.aliyuncs.com/modelscope-repo/modelscope镜像的tag版本。24年7月以后,与modelscope镜像统一
5464
serviceName:
5565
title: 服务名
5666
type: string
@@ -121,3 +131,23 @@ Parameters:
121131
- "www.modelscope.cn"
122132
- "modelsce-mirror-modelsce-mirror-txpzbgwcck.cn-hangzhou-vpc.fcapp.run"
123133
- "modelsce-mirror-modelsce-mirror-txpzbgwcck.cn-shanghai-vpc.fcapp.run"
134+
ggufFile:
135+
title: gguf文件
136+
type: string
137+
default: ""
138+
description: gguf格式的模型文件,使用ollama启动时为必须参数
139+
modelfile:
140+
title: ollama模型template文件内容
141+
type: string
142+
default: ""
143+
description: ollama创建模型时所需的模型template文件的内容, 不配置时将以modelFamily取该类模型的默认配置
144+
modelFamily:
145+
title: 模型family
146+
type: string
147+
default: ""
148+
description: 模型的类型,如qwen2, 用以获取ollama创建模型时所需的模型默认template文件
149+
servedModelName:
150+
title: 模型服务名
151+
type: string
152+
pattern: "^[a-zA-Z_][a-zA-Z0-9-_]{0,127}$"
153+
description: 用户可配置的部署后模型名称,只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-128 之间

src/model_deploy/index.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
from modelscope.hub.api import HubApi
3+
from modelscope.hub.snapshot_download import snapshot_download
4+
5+
def handler(event, context):
6+
model_id = os.getenv('MODEL_ID', '')
7+
revision = os.getenv('MODEL_VERSION', '')
8+
cache_dir = os.getenv('MODELSCOPE_CACHE', '')
9+
sdk_token = os.getenv('MODELSCOPE_TOKEN', '')
10+
image_tag = os.getenv('IMAGE_TAG', '')
11+
gguf_file = os.getenv('GGUF_FILE', '')
12+
modelfile = os.getenv('MODELFILE', '')
13+
family = os.getenv('MODEL_FAMILY', '')
14+
served_model_name = os.getenv('SERVED_MODEL_NAME', '')
15+
16+
cmd = f'cd {cache_dir}/ollama-linux && sudo chmod 777 ./ollama-modelscope-install.sh && ./ollama-modelscope-install.sh'
17+
os.system(cmd)
18+
cmd = 'ollama serve &'
19+
os.system(cmd)
20+
21+
if modelfile and len(modelfile):
22+
os.system(f'cat {modelfile} > {cache_dir}/ModelFile')
23+
elif family and len(family):
24+
os.system(f'wget https://modelscope.oss-cn-beijing.aliyuncs.com/llm_template/ollama/{family}.modelfile')
25+
command_gen_modelfile = f'cat {family}.modelfile | sed "s/' + '{gguf_file}' + f'/{gguf_file}/" > ./ModelFile'
26+
os.system(command_gen_modelfile)
27+
else:
28+
raise ValueError(f'modelfile 和 model_family至少需要配置一个。用于ollama模型初始化。')
29+
30+
# run ollama
31+
cmd = f'ollama create {served_model_name} --file ./ModelFile'
32+
os.system(cmd)
33+
cmd = f'ollama run {served_model_name}'
34+
os.system(cmd)

src/model_download/index.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@ def handler(event, context):
77
revision = os.getenv('MODEL_VERSION', '')
88
cache_dir = os.getenv('MODELSCOPE_CACHE', '')
99
sdk_token = os.getenv('MODELSCOPE_TOKEN', '')
10+
image_tag = os.getenv('IMAGE_TAG', '')
11+
gguf_file = os.getenv('GGUF_FILE', '')
12+
1013
# login first.
1114
HubApi().login(sdk_token)
12-
if len(revision) > 0:
13-
snapshot_download (model_id =model_id,
14-
revision =revision,
15-
cache_dir = cache_dir)
16-
else:
17-
snapshot_download (model_id =model_id,
15+
if image_tag == 'fc-deploy-common-v17.3.3':
16+
if len(revision) > 0:
17+
snapshot_download (model_id =model_id,
18+
revision =revision,
1819
cache_dir = cache_dir)
19-
print("download model scuccess!")
20+
else:
21+
snapshot_download (model_id =model_id,
22+
cache_dir = cache_dir)
23+
print("download model scuccess!")
24+
else:
25+
command_download_ollama = f'modelscope download --model=modelscope/ollama-linux --local_dir {cache_dir}/ollama-linux'
26+
os.system(command_download_ollama)
27+
28+
command_download_model = f'modelscope download --model={model_id} --local_dir {cache_dir} ${gguf_file}'
29+
os.system(command_download_model)
30+
31+
print("download model scuccess!")

src/model_meta_info/index.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
from modelscope.hub.api import HubApi
3+
from modelscope.hub.snapshot_download import snapshot_download
4+
5+
def handler(event, context):
6+
served_model_name = os.getenv('SERVED_MODEL_NAME', '')
7+
8+
return {'served_model_name': served_model_name}

src/s.yaml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ access: {{ access }}
77

88
vars: # 全局变量
99
region: {{ region }}
10-
version: fc-deploy-common-v17.3.3
10+
version: {{ imageTag }}
1111
service:
1212
name: {{ serviceName }}
1313
description: "modelscope deployment"
@@ -43,6 +43,10 @@ services:
4343
MODELSCOPE_CACHE: /mnt/auto
4444
MODELSCOPE_TOKEN: {{ accessToken }}
4545
MODELSCOPE_DOMAIN: {{ modelCache }}
46+
IMAGE_TAG: {{ imageTag }}
47+
GGUF_FILE: {{ ggufFile }}
48+
MODELFLE: {{ modelfile }}
49+
MODEL_FAMILY: {{ modelFamily }}
4650

4751
model_meta_func:
4852
component: 'fc'
@@ -52,7 +56,8 @@ services:
5256
function:
5357
name: model_meta_func
5458
description: Meta Api
55-
handler: not-used
59+
codeUri: ./model_meta_info
60+
handler: "{{ if imageTag == 'fc-deploy-common-v17.3.3' }}not-used{{ else }}index.handler{{ /if }}"
5661
timeout: 1800
5762
caPort: 9000
5863
instanceType: g1
@@ -64,7 +69,8 @@ services:
6469
runtime: custom-container
6570
customContainerConfig:
6671
image: registry.${vars.region}.aliyuncs.com/modelscope-repo/modelscope:${vars.version}
67-
72+
environmentVariables:
73+
SERVED_MODEL_NAME: {{ servedModelName }}
6874
triggers:
6975
- name: httpTrigger
7076
type: http
@@ -81,9 +87,10 @@ services:
8187
function:
8288
name: model_app_func
8389
description: Deploy ModelScope applications of model {{ modelId }}
84-
handler: not-used
90+
codeUri: ./model_deploy
91+
handler: "{{ if imageTag == 'fc-deploy-common-v17.3.3' }}not-used{{ else }}index.handler{{ /if }}"
8592
timeout: 1800
86-
caPort: 9000
93+
caPort: "{{ if imageTag == 'fc-deploy-common-v17.3.3' }}9000{{ else }}11434{{ /if }}"
8794
instanceType: {{ gpuInstanceType }}
8895
gpuMemorySize: {{ gpuMemorySize }}
8996
memorySize: {{ memorySize }}
@@ -101,6 +108,12 @@ services:
101108
MODELSCOPE_CACHE: /mnt/auto
102109
MODELSCOPE_TOKEN: {{ accessToken }}
103110
TASK: {{ task }}
111+
IMAGE_TAG: {{ imageTag }}
112+
GGUF_FILE: {{ ggufFile }}
113+
MODELFLE: {{ modelfile }}
114+
MODEL_FAMILY: {{ modelFamily }}
115+
MODELSCOPE_DOMAIN: {{ modelCache }}
116+
SERVED_MODEL_NAME: {{ servedModelName }}
104117

105118
triggers:
106119
- name: httpTrigger

0 commit comments

Comments
 (0)