Skip to content

Commit 9c28283

Browse files
feat: order env for tensorrt_llm (#10916)
1 parent ad5df58 commit 9c28283

File tree

11 files changed

+68
-32
lines changed

11 files changed

+68
-32
lines changed

agent/app/service/tensorrt_llm.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/1Panel-dev/1Panel/agent/global"
1414
"github.com/1Panel-dev/1Panel/agent/utils/compose"
1515
"github.com/1Panel-dev/1Panel/agent/utils/docker"
16+
"github.com/1Panel-dev/1Panel/agent/utils/env"
1617
"github.com/1Panel-dev/1Panel/agent/utils/files"
1718
"github.com/subosito/gotenv"
1819
"gopkg.in/yaml.v3"
@@ -171,7 +172,7 @@ func handleLLMParams(llm *model.TensorRTLLM, create request.TensorRTLLMCreate) e
171172

172173
var volumes []interface{}
173174
var defaultVolumes = map[string]string{
174-
"${MODEL_PATH}": "/models",
175+
"${MODEL_PATH}": "${MODEL_PATH}",
175176
}
176177
for k, v := range defaultVolumes {
177178
volumes = append(volumes, fmt.Sprintf("%s:%s", k, v))
@@ -191,23 +192,24 @@ func handleLLMParams(llm *model.TensorRTLLM, create request.TensorRTLLMCreate) e
191192
}
192193

193194
func handleLLMEnv(llm *model.TensorRTLLM, create request.TensorRTLLMCreate) gotenv.Env {
194-
env := make(gotenv.Env)
195-
env["CONTAINER_NAME"] = create.ContainerName
196-
env["MODEL_PATH"] = create.ModelDir
197-
env["VERSION"] = create.Version
198-
env["IMAGE"] = create.Image
199-
env["COMMAND"] = create.Command
195+
envMap := make(gotenv.Env)
196+
envMap["CONTAINER_NAME"] = create.ContainerName
197+
envMap["MODEL_PATH"] = create.ModelDir
198+
envMap["VERSION"] = create.Version
199+
envMap["IMAGE"] = create.Image
200+
envMap["COMMAND"] = create.Command
200201
for i, port := range create.ExposedPorts {
201202
containerPortStr := fmt.Sprintf("CONTAINER_PORT_%d", i)
202203
hostPortStr := fmt.Sprintf("HOST_PORT_%d", i)
203204
hostIPStr := fmt.Sprintf("HOST_IP_%d", i)
204-
env[containerPortStr] = strconv.Itoa(port.ContainerPort)
205-
env[hostPortStr] = strconv.Itoa(port.HostPort)
206-
env[hostIPStr] = port.HostIP
205+
envMap[containerPortStr] = strconv.Itoa(port.ContainerPort)
206+
envMap[hostPortStr] = strconv.Itoa(port.HostPort)
207+
envMap[hostIPStr] = port.HostIP
207208
}
208-
envStr, _ := gotenv.Marshal(env)
209+
orders := []string{"MODEL_PATH", "COMMAND"}
210+
envStr, _ := env.MarshalWithOrder(envMap, orders)
209211
llm.Env = envStr
210-
return env
212+
return envMap
211213
}
212214

213215
func (t TensorRTLLMService) Create(create request.TensorRTLLMCreate) error {
@@ -250,10 +252,10 @@ func (t TensorRTLLMService) Create(create request.TensorRTLLMCreate) error {
250252
if err := handleLLMParams(tensorrtLLM, create); err != nil {
251253
return err
252254
}
253-
env := handleLLMEnv(tensorrtLLM, create)
255+
envMap := handleLLMEnv(tensorrtLLM, create)
254256
llmDir := path.Join(global.Dir.TensorRTLLMDir, create.Name)
255257
envPath := path.Join(llmDir, ".env")
256-
if err := gotenv.Write(env, envPath); err != nil {
258+
if err := env.Write(envMap, envPath); err != nil {
257259
return err
258260
}
259261
dockerComposePath := path.Join(llmDir, "docker-compose.yml")
@@ -287,12 +289,12 @@ func (t TensorRTLLMService) Update(req request.TensorRTLLMUpdate) error {
287289
return err
288290
}
289291

290-
env := handleLLMEnv(tensorrtLLM, req.TensorRTLLMCreate)
291-
envStr, _ := gotenv.Marshal(env)
292+
envMap := handleLLMEnv(tensorrtLLM, req.TensorRTLLMCreate)
293+
envStr, _ := gotenv.Marshal(envMap)
292294
tensorrtLLM.Env = envStr
293295
llmDir := path.Join(global.Dir.TensorRTLLMDir, tensorrtLLM.Name)
294296
envPath := path.Join(llmDir, ".env")
295-
if err := gotenv.Write(env, envPath); err != nil {
297+
if err := env.Write(envMap, envPath); err != nil {
296298
return err
297299
}
298300
dockerComposePath := path.Join(llmDir, "docker-compose.yml")

agent/utils/env/env.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func Write(envMap map[string]string, filename string) error {
13-
content, err := marshal(envMap)
13+
content, err := Marshal(envMap)
1414
if err != nil {
1515
return err
1616
}
@@ -26,7 +26,7 @@ func Write(envMap map[string]string, filename string) error {
2626
return file.Sync()
2727
}
2828

29-
func marshal(envMap map[string]string) (string, error) {
29+
func Marshal(envMap map[string]string) (string, error) {
3030
lines := make([]string, 0, len(envMap))
3131
for k, v := range envMap {
3232
if d, err := strconv.Atoi(v); err == nil && !isStartWithZero(v) {
@@ -37,10 +37,47 @@ func marshal(envMap map[string]string) (string, error) {
3737
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, v))
3838
}
3939
}
40-
sort.Strings(lines)
4140
return strings.Join(lines, "\n"), nil
4241
}
4342

43+
func MarshalWithOrder(envMap map[string]string, orders []string) (string, error) {
44+
lines := make([]string, 0, len(envMap))
45+
for _, k := range orders {
46+
if v, ok := envMap[k]; ok {
47+
lines = append(lines, formatEnvLine(k, v))
48+
}
49+
}
50+
51+
extraKeys := make([]string, 0)
52+
for k := range envMap {
53+
found := false
54+
for _, okk := range orders {
55+
if k == okk {
56+
found = true
57+
break
58+
}
59+
}
60+
if !found {
61+
extraKeys = append(extraKeys, k)
62+
}
63+
}
64+
sort.Strings(extraKeys)
65+
for _, k := range extraKeys {
66+
lines = append(lines, formatEnvLine(k, envMap[k]))
67+
}
68+
return strings.Join(lines, "\n"), nil
69+
}
70+
71+
func formatEnvLine(k, v string) string {
72+
if d, err := strconv.Atoi(v); err == nil && !isStartWithZero(v) {
73+
return fmt.Sprintf(`%s=%d`, k, d)
74+
} else if hasEvenDoubleQuotes(v) {
75+
return fmt.Sprintf(`%s='%s'`, k, v)
76+
} else {
77+
return fmt.Sprintf(`%s="%s"`, k, v)
78+
}
79+
}
80+
4481
func GetEnvValueByKey(envPath, key string) (string, error) {
4582
envMap, err := godotenv.Read(envPath)
4683
if err != nil {

frontend/src/lang/modules/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ const message = {
728728
llm: 'TensorRT LLM',
729729
modelDir: 'Model Directory',
730730
commandHelper:
731-
'After /models in the startup command, the model name needs to be completed; if external access is required, set the port in the command to be the same as the application port',
731+
'If external access is needed, set the port in the command to be the same as the application port',
732732
imageAlert:
733733
'Due to the large image size, it is recommended to manually download the image to the server before installation',
734734
modelSpeedup: 'Enable model acceleration',

frontend/src/lang/modules/es-es.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ const message = {
729729
llm: 'TensorRT LLM',
730730
modelDir: 'Directorio del Modelo',
731731
commandHelper:
732-
'Después de /models en el comando de inicio, se debe completar el nombre del modelo; si se requiere acceso externo, configure el puerto en el comando para que sea el mismo que el puerto de la aplicación',
732+
'Si se necesita acceso externo, establezca el puerto en el comando para que sea el mismo que el puerto de la aplicación',
733733
imageAlert:
734734
'Debido al gran tamaño de la imagen, se recomienda descargar manualmente la imagen al servidor antes de la instalación',
735735
modelSpeedup: 'Habilitar aceleración de modelo',

frontend/src/lang/modules/ja.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ const message = {
716716
llm: 'TensorRT LLM',
717717
modelDir: 'モデルディレクトリ',
718718
commandHelper:
719-
'起動コマンドの /models の後にはモデル名を補完する必要があります;外部アクセスが必要な場合は、コマンド内のポートをアプリケーションポートと同じに設定してください',
719+
'外部アクセスが必要な場合は、コマンド内のポートをアプリケーションポートと同じに設定してください',
720720
imageAlert:
721721
'イメージサイズが大きいため、インストール前にサーバーにイメージを手動でダウンロードすることをお勧めします',
722722
modelSpeedup: 'モデル加速を有効化',

frontend/src/lang/modules/ko.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,7 @@ const message = {
711711
tensorRT: {
712712
llm: 'TensorRT LLM',
713713
modelDir: '모델 디렉토리',
714-
commandHelper:
715-
'시작 명령의 /models 뒤에는 모델 이름을 완성해야 합니다; 외부 액세스가 필요한 경우 명령의 포트를 애플리케이션 포트와 동일하게 설정하세요',
714+
commandHelper: '외부 액세스가 필요한 경우 명령에서 포트를 애플리케이션 포트와 동일하게 설정하십시오',
716715
imageAlert: '이미지 크기가 크므로 설치 전에 서버에 이미지를 수동으로 다운로드하는 것이 좋습니다',
717716
modelSpeedup: '모델 가속 활성화',
718717
modelType: '모델 유형',

frontend/src/lang/modules/ms.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,7 @@ const message = {
728728
tensorRT: {
729729
llm: 'TensorRT LLM',
730730
modelDir: 'Direktori Model',
731-
commandHelper:
732-
'Selepas /models dalam arahan permulaan, nama model perlu dilengkapkan; jika akses luar diperlukan, tetapkan port dalam arahan sama dengan port aplikasi',
731+
commandHelper: 'Jika akses luar diperlukan, tetapkan port dalam arahan sama dengan port aplikasi',
733732
imageAlert:
734733
'Disebabkan saiz imej yang besar, disyorkan untuk memuat turun imej secara manual ke pelayan sebelum pemasangan',
735734
modelSpeedup: 'Dayakan pecutan model',

frontend/src/lang/modules/pt-br.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ const message = {
725725
llm: 'TensorRT LLM',
726726
modelDir: 'Diretório do Modelo',
727727
commandHelper:
728-
'Após /models no comando de inicialização, o nome do modelo precisa ser completado; se for necessário acesso externo, defina a porta no comando para ser a mesma que a porta do aplicativo',
728+
'Se for necessário acesso externo, defina a porta no comando para ser a mesma que a porta do aplicativo',
729729
imageAlert:
730730
'Devido ao grande tamanho da imagem, recomenda-se baixar manualmente a imagem para o servidor antes da instalação',
731731
modelSpeedup: 'Ativar aceleração de modelo',

frontend/src/lang/modules/ru.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,7 @@ const message = {
722722
tensorRT: {
723723
llm: 'TensorRT LLM',
724724
modelDir: 'Каталог модели',
725-
commandHelper:
726-
'После /models в команде запуска необходимо указать имя модели; если требуется внешний доступ, установите порт в команде таким же, как порт приложения',
725+
commandHelper: 'Если требуется внешний доступ, установите порт в команде таким же, как порт приложения',
727726
imageAlert:
728727
'Из-за большого размера образа рекомендуется вручную загрузить образ на сервер перед установкой',
729728
modelSpeedup: 'Включить ускорение модели',

frontend/src/lang/modules/tr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ const message = {
737737
llm: 'TensorRT LLM',
738738
modelDir: 'Model Dizini',
739739
commandHelper:
740-
'Başlatma komutundaki /models sonrasında model adı tamamlanmalıdır; harici erişim gerekiyorsa, komuttaki bağlantı noktasını uygulama bağlantı noktasıyla aynı olacak şekilde ayarlayın',
740+
'Harici erişim gerekiyorsa, komuttaki bağlantı noktasını uygulama bağlantı noktasıyla aynı olacak şekilde ayarlayın',
741741
imageAlert:
742742
'Görüntü boyutu büyük olduğundan, kurulumdan önce görüntüyü sunucuya manuel olarak indirmeniz önerilir',
743743
modelSpeedup: 'Model hızlandırmayı etkinleştir',

0 commit comments

Comments
 (0)