mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-12-18 05:19:19 +08:00
feat: order env for tensorrt_llm (#10916)
This commit is contained in:
parent
e17bbdd6a7
commit
3773412139
11 changed files with 68 additions and 32 deletions
|
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/1Panel-dev/1Panel/agent/global"
|
||||
"github.com/1Panel-dev/1Panel/agent/utils/compose"
|
||||
"github.com/1Panel-dev/1Panel/agent/utils/docker"
|
||||
"github.com/1Panel-dev/1Panel/agent/utils/env"
|
||||
"github.com/1Panel-dev/1Panel/agent/utils/files"
|
||||
"github.com/subosito/gotenv"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
|
@ -171,7 +172,7 @@ func handleLLMParams(llm *model.TensorRTLLM, create request.TensorRTLLMCreate) e
|
|||
|
||||
var volumes []interface{}
|
||||
var defaultVolumes = map[string]string{
|
||||
"${MODEL_PATH}": "/models",
|
||||
"${MODEL_PATH}": "${MODEL_PATH}",
|
||||
}
|
||||
for k, v := range defaultVolumes {
|
||||
volumes = append(volumes, fmt.Sprintf("%s:%s", k, v))
|
||||
|
|
@ -191,23 +192,24 @@ func handleLLMParams(llm *model.TensorRTLLM, create request.TensorRTLLMCreate) e
|
|||
}
|
||||
|
||||
func handleLLMEnv(llm *model.TensorRTLLM, create request.TensorRTLLMCreate) gotenv.Env {
|
||||
env := make(gotenv.Env)
|
||||
env["CONTAINER_NAME"] = create.ContainerName
|
||||
env["MODEL_PATH"] = create.ModelDir
|
||||
env["VERSION"] = create.Version
|
||||
env["IMAGE"] = create.Image
|
||||
env["COMMAND"] = create.Command
|
||||
envMap := make(gotenv.Env)
|
||||
envMap["CONTAINER_NAME"] = create.ContainerName
|
||||
envMap["MODEL_PATH"] = create.ModelDir
|
||||
envMap["VERSION"] = create.Version
|
||||
envMap["IMAGE"] = create.Image
|
||||
envMap["COMMAND"] = create.Command
|
||||
for i, port := range create.ExposedPorts {
|
||||
containerPortStr := fmt.Sprintf("CONTAINER_PORT_%d", i)
|
||||
hostPortStr := fmt.Sprintf("HOST_PORT_%d", i)
|
||||
hostIPStr := fmt.Sprintf("HOST_IP_%d", i)
|
||||
env[containerPortStr] = strconv.Itoa(port.ContainerPort)
|
||||
env[hostPortStr] = strconv.Itoa(port.HostPort)
|
||||
env[hostIPStr] = port.HostIP
|
||||
envMap[containerPortStr] = strconv.Itoa(port.ContainerPort)
|
||||
envMap[hostPortStr] = strconv.Itoa(port.HostPort)
|
||||
envMap[hostIPStr] = port.HostIP
|
||||
}
|
||||
envStr, _ := gotenv.Marshal(env)
|
||||
orders := []string{"MODEL_PATH", "COMMAND"}
|
||||
envStr, _ := env.MarshalWithOrder(envMap, orders)
|
||||
llm.Env = envStr
|
||||
return env
|
||||
return envMap
|
||||
}
|
||||
|
||||
func (t TensorRTLLMService) Create(create request.TensorRTLLMCreate) error {
|
||||
|
|
@ -250,10 +252,10 @@ func (t TensorRTLLMService) Create(create request.TensorRTLLMCreate) error {
|
|||
if err := handleLLMParams(tensorrtLLM, create); err != nil {
|
||||
return err
|
||||
}
|
||||
env := handleLLMEnv(tensorrtLLM, create)
|
||||
envMap := handleLLMEnv(tensorrtLLM, create)
|
||||
llmDir := path.Join(global.Dir.TensorRTLLMDir, create.Name)
|
||||
envPath := path.Join(llmDir, ".env")
|
||||
if err := gotenv.Write(env, envPath); err != nil {
|
||||
if err := env.Write(envMap, envPath); err != nil {
|
||||
return err
|
||||
}
|
||||
dockerComposePath := path.Join(llmDir, "docker-compose.yml")
|
||||
|
|
@ -287,12 +289,12 @@ func (t TensorRTLLMService) Update(req request.TensorRTLLMUpdate) error {
|
|||
return err
|
||||
}
|
||||
|
||||
env := handleLLMEnv(tensorrtLLM, req.TensorRTLLMCreate)
|
||||
envStr, _ := gotenv.Marshal(env)
|
||||
envMap := handleLLMEnv(tensorrtLLM, req.TensorRTLLMCreate)
|
||||
envStr, _ := gotenv.Marshal(envMap)
|
||||
tensorrtLLM.Env = envStr
|
||||
llmDir := path.Join(global.Dir.TensorRTLLMDir, tensorrtLLM.Name)
|
||||
envPath := path.Join(llmDir, ".env")
|
||||
if err := gotenv.Write(env, envPath); err != nil {
|
||||
if err := env.Write(envMap, envPath); err != nil {
|
||||
return err
|
||||
}
|
||||
dockerComposePath := path.Join(llmDir, "docker-compose.yml")
|
||||
|
|
|
|||
43
agent/utils/env/env.go
vendored
43
agent/utils/env/env.go
vendored
|
|
@ -10,7 +10,7 @@ import (
|
|||
)
|
||||
|
||||
func Write(envMap map[string]string, filename string) error {
|
||||
content, err := marshal(envMap)
|
||||
content, err := Marshal(envMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -26,7 +26,7 @@ func Write(envMap map[string]string, filename string) error {
|
|||
return file.Sync()
|
||||
}
|
||||
|
||||
func marshal(envMap map[string]string) (string, error) {
|
||||
func Marshal(envMap map[string]string) (string, error) {
|
||||
lines := make([]string, 0, len(envMap))
|
||||
for k, v := range envMap {
|
||||
if d, err := strconv.Atoi(v); err == nil && !isStartWithZero(v) {
|
||||
|
|
@ -37,10 +37,47 @@ func marshal(envMap map[string]string) (string, error) {
|
|||
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, v))
|
||||
}
|
||||
}
|
||||
sort.Strings(lines)
|
||||
return strings.Join(lines, "\n"), nil
|
||||
}
|
||||
|
||||
func MarshalWithOrder(envMap map[string]string, orders []string) (string, error) {
|
||||
lines := make([]string, 0, len(envMap))
|
||||
for _, k := range orders {
|
||||
if v, ok := envMap[k]; ok {
|
||||
lines = append(lines, formatEnvLine(k, v))
|
||||
}
|
||||
}
|
||||
|
||||
extraKeys := make([]string, 0)
|
||||
for k := range envMap {
|
||||
found := false
|
||||
for _, okk := range orders {
|
||||
if k == okk {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
extraKeys = append(extraKeys, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(extraKeys)
|
||||
for _, k := range extraKeys {
|
||||
lines = append(lines, formatEnvLine(k, envMap[k]))
|
||||
}
|
||||
return strings.Join(lines, "\n"), nil
|
||||
}
|
||||
|
||||
func formatEnvLine(k, v string) string {
|
||||
if d, err := strconv.Atoi(v); err == nil && !isStartWithZero(v) {
|
||||
return fmt.Sprintf(`%s=%d`, k, d)
|
||||
} else if hasEvenDoubleQuotes(v) {
|
||||
return fmt.Sprintf(`%s='%s'`, k, v)
|
||||
} else {
|
||||
return fmt.Sprintf(`%s="%s"`, k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func GetEnvValueByKey(envPath, key string) (string, error) {
|
||||
envMap, err := godotenv.Read(envPath)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -744,7 +744,7 @@ const message = {
|
|||
llm: 'TensorRT LLM',
|
||||
modelDir: 'Model Directory',
|
||||
commandHelper:
|
||||
'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',
|
||||
'If external access is needed, set the port in the command to be the same as the application port',
|
||||
imageAlert:
|
||||
'Due to the large image size, it is recommended to manually download the image to the server before installation',
|
||||
modelSpeedup: 'Enable model acceleration',
|
||||
|
|
|
|||
|
|
@ -744,7 +744,7 @@ const message = {
|
|||
llm: 'TensorRT LLM',
|
||||
modelDir: 'Directorio del Modelo',
|
||||
commandHelper:
|
||||
'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',
|
||||
'Si se necesita acceso externo, establezca el puerto en el comando para que sea el mismo que el puerto de la aplicación',
|
||||
imageAlert:
|
||||
'Debido al gran tamaño de la imagen, se recomienda descargar manualmente la imagen al servidor antes de la instalación',
|
||||
modelSpeedup: 'Habilitar aceleración de modelo',
|
||||
|
|
|
|||
|
|
@ -731,7 +731,7 @@ const message = {
|
|||
llm: 'TensorRT LLM',
|
||||
modelDir: 'モデルディレクトリ',
|
||||
commandHelper:
|
||||
'起動コマンドの /models の後にはモデル名を補完する必要があります;外部アクセスが必要な場合は、コマンド内のポートをアプリケーションポートと同じに設定してください',
|
||||
'外部アクセスが必要な場合は、コマンド内のポートをアプリケーションポートと同じに設定してください',
|
||||
imageAlert:
|
||||
'イメージサイズが大きいため、インストール前にサーバーにイメージを手動でダウンロードすることをお勧めします',
|
||||
modelSpeedup: 'モデル加速を有効化',
|
||||
|
|
|
|||
|
|
@ -726,8 +726,7 @@ const message = {
|
|||
tensorRT: {
|
||||
llm: 'TensorRT LLM',
|
||||
modelDir: '모델 디렉토리',
|
||||
commandHelper:
|
||||
'시작 명령의 /models 뒤에는 모델 이름을 완성해야 합니다; 외부 액세스가 필요한 경우 명령의 포트를 애플리케이션 포트와 동일하게 설정하세요',
|
||||
commandHelper: '외부 액세스가 필요한 경우 명령에서 포트를 애플리케이션 포트와 동일하게 설정하십시오',
|
||||
imageAlert: '이미지 크기가 크므로 설치 전에 서버에 이미지를 수동으로 다운로드하는 것이 좋습니다',
|
||||
modelSpeedup: '모델 가속 활성화',
|
||||
modelType: '모델 유형',
|
||||
|
|
|
|||
|
|
@ -743,8 +743,7 @@ const message = {
|
|||
tensorRT: {
|
||||
llm: 'TensorRT LLM',
|
||||
modelDir: 'Direktori Model',
|
||||
commandHelper:
|
||||
'Selepas /models dalam arahan permulaan, nama model perlu dilengkapkan; jika akses luar diperlukan, tetapkan port dalam arahan sama dengan port aplikasi',
|
||||
commandHelper: 'Jika akses luar diperlukan, tetapkan port dalam arahan sama dengan port aplikasi',
|
||||
imageAlert:
|
||||
'Disebabkan saiz imej yang besar, disyorkan untuk memuat turun imej secara manual ke pelayan sebelum pemasangan',
|
||||
modelSpeedup: 'Dayakan pecutan model',
|
||||
|
|
|
|||
|
|
@ -740,7 +740,7 @@ const message = {
|
|||
llm: 'TensorRT LLM',
|
||||
modelDir: 'Diretório do Modelo',
|
||||
commandHelper:
|
||||
'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',
|
||||
'Se for necessário acesso externo, defina a porta no comando para ser a mesma que a porta do aplicativo',
|
||||
imageAlert:
|
||||
'Devido ao grande tamanho da imagem, recomenda-se baixar manualmente a imagem para o servidor antes da instalação',
|
||||
modelSpeedup: 'Ativar aceleração de modelo',
|
||||
|
|
|
|||
|
|
@ -737,8 +737,7 @@ const message = {
|
|||
tensorRT: {
|
||||
llm: 'TensorRT LLM',
|
||||
modelDir: 'Каталог модели',
|
||||
commandHelper:
|
||||
'После /models в команде запуска необходимо указать имя модели; если требуется внешний доступ, установите порт в команде таким же, как порт приложения',
|
||||
commandHelper: 'Если требуется внешний доступ, установите порт в команде таким же, как порт приложения',
|
||||
imageAlert:
|
||||
'Из-за большого размера образа рекомендуется вручную загрузить образ на сервер перед установкой',
|
||||
modelSpeedup: 'Включить ускорение модели',
|
||||
|
|
|
|||
|
|
@ -752,7 +752,7 @@ const message = {
|
|||
llm: 'TensorRT LLM',
|
||||
modelDir: 'Model Dizini',
|
||||
commandHelper:
|
||||
'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',
|
||||
'Harici erişim gerekiyorsa, komuttaki bağlantı noktasını uygulama bağlantı noktasıyla aynı olacak şekilde ayarlayın',
|
||||
imageAlert:
|
||||
'Görüntü boyutu büyük olduğundan, kurulumdan önce görüntüyü sunucuya manuel olarak indirmeniz önerilir',
|
||||
modelSpeedup: 'Model hızlandırmayı etkinleştir',
|
||||
|
|
|
|||
|
|
@ -714,7 +714,7 @@ const message = {
|
|||
tensorRT: {
|
||||
llm: 'TensorRT LLM',
|
||||
modelDir: '模型目錄',
|
||||
commandHelper: '啟動指令中的 /models 後需補全模型名稱;若需外部訪問,請將指令中的埠設定為與應用埠相同',
|
||||
commandHelper: '若需外部訪問,請將命令中的端口設置為與應用端口相同',
|
||||
imageAlert: '由於鏡像較大,建議先手動將鏡像下載到伺服器後再進行安裝',
|
||||
modelSpeedup: '啟用模型加速',
|
||||
modelType: '模型類型',
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue