@@ -12,6 +12,7 @@ import (
12
12
"fmt"
13
13
"io"
14
14
"log"
15
+ "math"
15
16
"net"
16
17
"net/http"
17
18
"os"
@@ -324,6 +325,18 @@ func RunHandler(cmd *cobra.Command, args []string) error {
324
325
}
325
326
opts .Format = format
326
327
328
+ keepAlive , err := cmd .Flags ().GetString ("keepalive" )
329
+ if err != nil {
330
+ return err
331
+ }
332
+ if keepAlive != "" {
333
+ d , err := time .ParseDuration (keepAlive )
334
+ if err != nil {
335
+ return err
336
+ }
337
+ opts .KeepAlive = & api.Duration {Duration : d }
338
+ }
339
+
327
340
prompts := args [1 :]
328
341
// prepend stdin to the prompt if provided
329
342
if ! term .IsTerminal (int (os .Stdin .Fd ())) {
@@ -496,6 +509,52 @@ func ListHandler(cmd *cobra.Command, args []string) error {
496
509
return nil
497
510
}
498
511
512
+ func ListRunningHandler (cmd * cobra.Command , args []string ) error {
513
+ client , err := api .ClientFromEnvironment ()
514
+ if err != nil {
515
+ return err
516
+ }
517
+
518
+ models , err := client .ListRunning (cmd .Context ())
519
+ if err != nil {
520
+ return err
521
+ }
522
+
523
+ var data [][]string
524
+
525
+ for _ , m := range models .Models {
526
+ if len (args ) == 0 || strings .HasPrefix (m .Name , args [0 ]) {
527
+ var procStr string
528
+ switch {
529
+ case m .SizeVRAM == 0 :
530
+ procStr = "100% CPU"
531
+ case m .SizeVRAM == m .Size :
532
+ procStr = "100% GPU"
533
+ case m .SizeVRAM > m .Size || m .Size == 0 :
534
+ procStr = "Unknown"
535
+ default :
536
+ sizeCPU := m .Size - m .SizeVRAM
537
+ cpuPercent := math .Round (float64 (sizeCPU ) / float64 (m .Size ) * 100 )
538
+ procStr = fmt .Sprintf ("%d%%/%d%% CPU/GPU" , int (cpuPercent ), int (100 - cpuPercent ))
539
+ }
540
+ data = append (data , []string {m .Name , m .Digest [:12 ], format .HumanBytes (m .Size ), procStr , format .HumanTime (m .ExpiresAt , "Never" )})
541
+ }
542
+ }
543
+
544
+ table := tablewriter .NewWriter (os .Stdout )
545
+ table .SetHeader ([]string {"NAME" , "ID" , "SIZE" , "PROCESSOR" , "UNTIL" })
546
+ table .SetHeaderAlignment (tablewriter .ALIGN_LEFT )
547
+ table .SetAlignment (tablewriter .ALIGN_LEFT )
548
+ table .SetHeaderLine (false )
549
+ table .SetBorder (false )
550
+ table .SetNoWhiteSpace (true )
551
+ table .SetTablePadding ("\t " )
552
+ table .AppendBulk (data )
553
+ table .Render ()
554
+
555
+ return nil
556
+ }
557
+
499
558
func DeleteHandler (cmd * cobra.Command , args []string ) error {
500
559
client , err := api .ClientFromEnvironment ()
501
560
if err != nil {
@@ -672,6 +731,7 @@ type runOptions struct {
672
731
Images []api.ImageData
673
732
Options map [string ]interface {}
674
733
MultiModal bool
734
+ KeepAlive * api.Duration
675
735
}
676
736
677
737
type displayResponseState struct {
@@ -766,6 +826,10 @@ func chat(cmd *cobra.Command, opts runOptions) (*api.Message, error) {
766
826
Options : opts .Options ,
767
827
}
768
828
829
+ if opts .KeepAlive != nil {
830
+ req .KeepAlive = opts .KeepAlive
831
+ }
832
+
769
833
if err := client .Chat (cancelCtx , req , fn ); err != nil {
770
834
if errors .Is (err , context .Canceled ) {
771
835
return nil , nil
@@ -1075,6 +1139,7 @@ func NewCLI() *cobra.Command {
1075
1139
RunE : RunHandler ,
1076
1140
}
1077
1141
1142
+ runCmd .Flags ().String ("keepalive" , "" , "Duration to keep a model loaded (e.g. 5m)" )
1078
1143
runCmd .Flags ().Bool ("verbose" , false , "Show timings for response" )
1079
1144
runCmd .Flags ().Bool ("insecure" , false , "Use an insecure registry" )
1080
1145
runCmd .Flags ().Bool ("nowordwrap" , false , "Don't wrap words to the next line automatically" )
@@ -1123,6 +1188,14 @@ Environment Variables:
1123
1188
PreRunE : checkServerHeartbeat ,
1124
1189
RunE : ListHandler ,
1125
1190
}
1191
+
1192
+ psCmd := & cobra.Command {
1193
+ Use : "ps" ,
1194
+ Short : "List running models" ,
1195
+ PreRunE : checkServerHeartbeat ,
1196
+ RunE : ListRunningHandler ,
1197
+ }
1198
+
1126
1199
copyCmd := & cobra.Command {
1127
1200
Use : "cp SOURCE DESTINATION" ,
1128
1201
Short : "Copy a model" ,
@@ -1146,6 +1219,7 @@ Environment Variables:
1146
1219
pullCmd ,
1147
1220
pushCmd ,
1148
1221
listCmd ,
1222
+ psCmd ,
1149
1223
copyCmd ,
1150
1224
deleteCmd ,
1151
1225
} {
@@ -1160,6 +1234,7 @@ Environment Variables:
1160
1234
pullCmd ,
1161
1235
pushCmd ,
1162
1236
listCmd ,
1237
+ psCmd ,
1163
1238
copyCmd ,
1164
1239
deleteCmd ,
1165
1240
)
0 commit comments