mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-11-06 09:04:27 +00:00
Refactor command building and argument handling
This commit is contained in:
@@ -1,16 +1,34 @@
|
||||
package llamacpp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"llamactl/pkg/backends"
|
||||
"llamactl/pkg/config"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// multiValuedFlags defines flags that should be repeated for each value rather than comma-separated
|
||||
// Used for both parsing (with underscores) and building (with dashes)
|
||||
var multiValuedFlags = map[string]bool{
|
||||
// Parsing keys (with underscores)
|
||||
"override_tensor": true,
|
||||
"override_kv": true,
|
||||
"lora": true,
|
||||
"lora_scaled": true,
|
||||
"control_vector": true,
|
||||
"control_vector_scaled": true,
|
||||
"dry_sequence_breaker": true,
|
||||
"logit_bias": true,
|
||||
// Building keys (with dashes)
|
||||
"override-tensor": true,
|
||||
"override-kv": true,
|
||||
"lora-scaled": true,
|
||||
"control-vector": true,
|
||||
"control-vector-scaled": true,
|
||||
"dry-sequence-breaker": true,
|
||||
"logit-bias": true,
|
||||
}
|
||||
|
||||
type LlamaServerOptions struct {
|
||||
// Common params
|
||||
VerbosePrompt bool `json:"verbose_prompt,omitempty"`
|
||||
@@ -320,67 +338,13 @@ func (o *LlamaServerOptions) UnmarshalJSON(data []byte) error {
|
||||
// BuildCommandArgs converts InstanceOptions to command line arguments
|
||||
func (o *LlamaServerOptions) BuildCommandArgs() []string {
|
||||
// Llama uses multiple flags for arrays by default (not comma-separated)
|
||||
multipleFlags := map[string]bool{
|
||||
"override-tensor": true,
|
||||
"override-kv": true,
|
||||
"lora": true,
|
||||
"lora-scaled": true,
|
||||
"control-vector": true,
|
||||
"control-vector-scaled": true,
|
||||
"dry-sequence-breaker": true,
|
||||
"logit-bias": true,
|
||||
}
|
||||
return backends.BuildCommandArgs(o, multipleFlags)
|
||||
// Use package-level multiValuedFlags variable
|
||||
return backends.BuildCommandArgs(o, multiValuedFlags)
|
||||
}
|
||||
|
||||
// BuildCommandArgsWithDocker converts InstanceOptions to command line arguments,
|
||||
// handling Docker transformations if needed
|
||||
func (o *LlamaServerOptions) BuildCommandArgsWithDocker(dockerImage string) []string {
|
||||
args := o.BuildCommandArgs()
|
||||
|
||||
// No special Docker transformations needed for llama-cpp
|
||||
return args
|
||||
}
|
||||
|
||||
// BuildCommand creates the complete command for execution, handling Docker vs native execution
|
||||
func (o *LlamaServerOptions) BuildCommand(ctx context.Context, backendConfig *config.BackendSettings) (*exec.Cmd, error) {
|
||||
// Build instance-specific arguments using backend functions
|
||||
var instanceArgs []string
|
||||
if backendConfig.Docker != nil && backendConfig.Docker.Enabled {
|
||||
// Use Docker-aware argument building
|
||||
instanceArgs = o.BuildCommandArgsWithDocker(backendConfig.Docker.Image)
|
||||
} else {
|
||||
// Use regular argument building for native execution
|
||||
instanceArgs = o.BuildCommandArgs()
|
||||
}
|
||||
|
||||
// Combine backend args with instance args
|
||||
finalArgs := append(backendConfig.Args, instanceArgs...)
|
||||
|
||||
// Choose Docker vs Native execution
|
||||
if backendConfig.Docker != nil && backendConfig.Docker.Enabled {
|
||||
return buildDockerCommand(ctx, backendConfig, finalArgs)
|
||||
} else {
|
||||
return exec.CommandContext(ctx, backendConfig.Command, finalArgs...), nil
|
||||
}
|
||||
}
|
||||
|
||||
// buildDockerCommand builds a Docker command with the specified configuration and arguments
|
||||
func buildDockerCommand(ctx context.Context, backendConfig *config.BackendSettings, args []string) (*exec.Cmd, error) {
|
||||
// Start with configured Docker arguments (should include "run", "--rm", etc.)
|
||||
dockerArgs := make([]string, len(backendConfig.Docker.Args))
|
||||
copy(dockerArgs, backendConfig.Docker.Args)
|
||||
|
||||
// Add environment variables
|
||||
for key, value := range backendConfig.Docker.Environment {
|
||||
dockerArgs = append(dockerArgs, "-e", fmt.Sprintf("%s=%s", key, value))
|
||||
}
|
||||
|
||||
// Add image and container arguments
|
||||
dockerArgs = append(dockerArgs, backendConfig.Docker.Image)
|
||||
dockerArgs = append(dockerArgs, args...)
|
||||
|
||||
return exec.CommandContext(ctx, "docker", dockerArgs...), nil
|
||||
func (o *LlamaServerOptions) BuildDockerArgs() []string {
|
||||
// For llama, Docker args are the same as normal args
|
||||
return o.BuildCommandArgs()
|
||||
}
|
||||
|
||||
// ParseLlamaCommand parses a llama-server command string into LlamaServerOptions
|
||||
@@ -392,16 +356,7 @@ func buildDockerCommand(ctx context.Context, backendConfig *config.BackendSettin
|
||||
func ParseLlamaCommand(command string) (*LlamaServerOptions, error) {
|
||||
executableNames := []string{"llama-server"}
|
||||
var subcommandNames []string // Llama has no subcommands
|
||||
multiValuedFlags := map[string]bool{
|
||||
"override_tensor": true,
|
||||
"override_kv": true,
|
||||
"lora": true,
|
||||
"lora_scaled": true,
|
||||
"control_vector": true,
|
||||
"control_vector_scaled": true,
|
||||
"dry_sequence_breaker": true,
|
||||
"logit_bias": true,
|
||||
}
|
||||
// Use package-level multiValuedFlags variable
|
||||
|
||||
var llamaOptions LlamaServerOptions
|
||||
if err := backends.ParseCommand(command, executableNames, subcommandNames, multiValuedFlags, &llamaOptions); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user