Skip to content

Commit a6cf67c

Browse files
authored
refactor: use slices.Contains to simplify code (#10702)
Signed-off-by: weifanglab <weifanglab@outlook.com>
1 parent 85f5267 commit a6cf67c

2 files changed

Lines changed: 5 additions & 17 deletions

File tree

core/cli/chat/session.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"io"
8+
"slices"
89
"strings"
910
)
1011

@@ -58,7 +59,7 @@ func (s *chatSession) Clear() {
5859
}
5960

6061
func (s *chatSession) SwitchModel(model string) error {
61-
if !modelExists(s.models, model) {
62+
if !slices.Contains(s.models, model) {
6263
return fmt.Errorf("model %q is not available. Use /models to see installed models", model)
6364
}
6465
s.model = model
@@ -103,18 +104,9 @@ Then start a chat session:
103104
b.WriteString("multiple models are available; choose one with --model:\n")
104105
b.WriteString(formatChatModelList(models, ""))
105106
return "", errors.New(b.String())
106-
case !modelExists(models, requested):
107+
case !slices.Contains(models, requested):
107108
return "", fmt.Errorf("model %q is not available. Use `local-ai models list` and `local-ai models install <model>`, or pass an installed model with --model", requested)
108109
default:
109110
return requested, nil
110111
}
111112
}
112-
113-
func modelExists(models []string, name string) bool {
114-
for _, model := range models {
115-
if model == name {
116-
return true
117-
}
118-
}
119-
return false
120-
}

pkg/xsysinfo/clinfo.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"os/exec"
9+
"slices"
910
"strings"
1011
"sync"
1112
"time"
@@ -49,12 +50,7 @@ type clinfoTypeProp struct {
4950
}
5051

5152
func (t clinfoTypeProp) isGPU() bool {
52-
for _, s := range t.Type {
53-
if s == clDeviceTypeGPU {
54-
return true
55-
}
56-
}
57-
return false
53+
return slices.Contains(t.Type, clDeviceTypeGPU)
5854
}
5955

6056
// clinfoOnce caches the result for the process lifetime. GPU hardware

0 commit comments

Comments
 (0)