|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/briandowns/spinner" |
| 11 | + "github.com/runware/runware-cli/internal/api" |
| 12 | + "github.com/runware/runware-cli/internal/config" |
| 13 | + "github.com/runware/runware-cli/internal/output" |
| 14 | + "github.com/spf13/cobra" |
| 15 | +) |
| 16 | + |
| 17 | +var textInferenceCmd = &cobra.Command{ |
| 18 | + Use: "textInference [message]", |
| 19 | + Short: "Generate text using a language model", |
| 20 | + Long: `Send a message to a language model and get a text response. |
| 21 | +
|
| 22 | +Examples: |
| 23 | + runware textInference "What is the capital of France?" |
| 24 | + runware textInference "Explain quantum computing" --model runware:qwen3-thinking@1 --max-tokens 500 |
| 25 | + runware textInference "Write a haiku about coding" --system "You are a poet" --temperature 0.8 |
| 26 | + runware textInference "List 3 facts about Mars" --output-format json`, |
| 27 | + Args: cobra.ExactArgs(1), |
| 28 | + RunE: runTextInference, |
| 29 | +} |
| 30 | + |
| 31 | +func init() { |
| 32 | + f := textInferenceCmd.Flags() |
| 33 | + f.String("model", "", "Model identifier (e.g. runware:qwen3-thinking@1)") |
| 34 | + f.String("system", "", "System prompt") |
| 35 | + f.Int("max-tokens", 0, "Maximum tokens in response (1-128000)") |
| 36 | + f.Float64("temperature", 0, "Sampling temperature (0-2)") |
| 37 | + f.Float64("top-p", 0, "Nucleus sampling parameter (0-1)") |
| 38 | + f.Int("top-k", 0, "Top-k sampling parameter (1-100)") |
| 39 | + f.Int64("seed", 0, "Random seed for reproducibility") |
| 40 | + f.StringSlice("stop", nil, "Stop sequences (max 5)") |
| 41 | + f.Int("count", 1, "Number of results to generate (1-4)") |
| 42 | + f.String("output-format", "", "LLM output format: text or json") |
| 43 | + f.Bool("include-cost", false, "Include cost info in response") |
| 44 | + f.String("preset", "", "Named preset to apply") |
| 45 | + f.Bool("dry-run", false, "Print the API request without executing") |
| 46 | + |
| 47 | + _ = textInferenceCmd.RegisterFlagCompletionFunc("output-format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 48 | + return []string{"text", "json"}, cobra.ShellCompDirectiveNoFileComp |
| 49 | + }) |
| 50 | + |
| 51 | + _ = textInferenceCmd.RegisterFlagCompletionFunc("preset", completePresetNames) |
| 52 | +} |
| 53 | + |
| 54 | +func runTextInference(cmd *cobra.Command, args []string) error { |
| 55 | + key := config.GetAPIKey() |
| 56 | + if key == "" { |
| 57 | + output.Error("No API key configured. Run 'runware auth login' to authenticate.") |
| 58 | + return api.ErrNoAPIKey |
| 59 | + } |
| 60 | + |
| 61 | + cfg := config.Get() |
| 62 | + message := args[0] |
| 63 | + |
| 64 | + model := cfg.Defaults.Model |
| 65 | + |
| 66 | + // Apply preset if specified |
| 67 | + presetName, _ := cmd.Flags().GetString("preset") |
| 68 | + if presetName != "" { |
| 69 | + preset := config.GetPreset(presetName) |
| 70 | + if preset == nil { |
| 71 | + return fmt.Errorf("preset '%s' not found", presetName) |
| 72 | + } |
| 73 | + if preset.Model != "" { |
| 74 | + model = preset.Model |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Override with explicit CLI flags |
| 79 | + if cmd.Flags().Changed("model") { |
| 80 | + model, _ = cmd.Flags().GetString("model") |
| 81 | + } |
| 82 | + |
| 83 | + systemPrompt, _ := cmd.Flags().GetString("system") |
| 84 | + maxTokens, _ := cmd.Flags().GetInt("max-tokens") |
| 85 | + temperature, _ := cmd.Flags().GetFloat64("temperature") |
| 86 | + topP, _ := cmd.Flags().GetFloat64("top-p") |
| 87 | + topK, _ := cmd.Flags().GetInt("top-k") |
| 88 | + seed, _ := cmd.Flags().GetInt64("seed") |
| 89 | + stopSequences, _ := cmd.Flags().GetStringSlice("stop") |
| 90 | + count, _ := cmd.Flags().GetInt("count") |
| 91 | + outputFmt, _ := cmd.Flags().GetString("output-format") |
| 92 | + includeCost, _ := cmd.Flags().GetBool("include-cost") |
| 93 | + dryRun, _ := cmd.Flags().GetBool("dry-run") |
| 94 | + |
| 95 | + // Build request |
| 96 | + req := &api.TextInferenceRequest{ |
| 97 | + TaskType: "textInference", |
| 98 | + TaskUUID: api.NewUUID(), |
| 99 | + Model: model, |
| 100 | + Messages: []api.Message{ |
| 101 | + {Role: "user", Content: message}, |
| 102 | + }, |
| 103 | + IncludeCost: includeCost, |
| 104 | + } |
| 105 | + |
| 106 | + if systemPrompt != "" { |
| 107 | + req.SystemPrompt = systemPrompt |
| 108 | + } |
| 109 | + if maxTokens > 0 { |
| 110 | + req.MaxTokens = maxTokens |
| 111 | + } |
| 112 | + if cmd.Flags().Changed("temperature") { |
| 113 | + req.Temperature = temperature |
| 114 | + } |
| 115 | + if cmd.Flags().Changed("top-p") { |
| 116 | + req.TopP = topP |
| 117 | + } |
| 118 | + if topK > 0 { |
| 119 | + req.TopK = topK |
| 120 | + } |
| 121 | + if seed > 0 { |
| 122 | + req.Seed = seed |
| 123 | + } |
| 124 | + if len(stopSequences) > 0 { |
| 125 | + req.StopSequences = stopSequences |
| 126 | + } |
| 127 | + if count > 1 { |
| 128 | + req.NumberResults = count |
| 129 | + } |
| 130 | + if outputFmt != "" { |
| 131 | + req.OutputFormat = outputFmt |
| 132 | + } |
| 133 | + |
| 134 | + // Dry run |
| 135 | + if dryRun { |
| 136 | + data, _ := json.MarshalIndent([]interface{}{req}, "", " ") |
| 137 | + fmt.Println(string(data)) |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + // Submit |
| 142 | + var s *spinner.Spinner |
| 143 | + if output.IsTTY() { |
| 144 | + s = spinner.New(spinner.CharSets[14], 100*time.Millisecond, spinner.WithWriter(os.Stderr)) |
| 145 | + s.Suffix = " Generating text..." |
| 146 | + s.Start() |
| 147 | + } |
| 148 | + |
| 149 | + client := api.NewClient(key, config.GetBaseURL(), flagVerbose) |
| 150 | + results, err := client.TextInference(context.Background(), req) |
| 151 | + |
| 152 | + if s != nil { |
| 153 | + s.Stop() |
| 154 | + } |
| 155 | + |
| 156 | + if err != nil { |
| 157 | + if api.IsAuthError(err) { |
| 158 | + output.Error("Authentication failed. Run 'runware auth login' to set your API key.") |
| 159 | + return err |
| 160 | + } |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + if len(results) == 0 { |
| 165 | + output.Error("No results returned") |
| 166 | + return fmt.Errorf("empty response from text inference") |
| 167 | + } |
| 168 | + |
| 169 | + // JSON/YAML output |
| 170 | + format := output.ParseFormat(getFormat()) |
| 171 | + if format != output.FormatTable { |
| 172 | + return output.Print(format, results, nil, nil) |
| 173 | + } |
| 174 | + |
| 175 | + // Table: single result without cost — print text directly (pipe-friendly) |
| 176 | + if len(results) == 1 && !includeCost { |
| 177 | + fmt.Println(results[0].Text) |
| 178 | + return nil |
| 179 | + } |
| 180 | + |
| 181 | + // Multiple results or cost requested — use table |
| 182 | + headers := []interface{}{"#", "Text"} |
| 183 | + if includeCost { |
| 184 | + headers = append(headers, "Cost") |
| 185 | + } |
| 186 | + var rows [][]interface{} |
| 187 | + for i, r := range results { |
| 188 | + text := r.Text |
| 189 | + if len(text) > 100 { |
| 190 | + text = text[:100] + "..." |
| 191 | + } |
| 192 | + row := []interface{}{i + 1, text} |
| 193 | + if includeCost { |
| 194 | + row = append(row, fmt.Sprintf("%.6f", r.Cost)) |
| 195 | + } |
| 196 | + rows = append(rows, row) |
| 197 | + } |
| 198 | + return output.Print(format, results, headers, rows) |
| 199 | +} |
0 commit comments