This repository was archived by the owner on Sep 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathlocal.go
More file actions
213 lines (179 loc) · 4.7 KB
/
local.go
File metadata and controls
213 lines (179 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package models
import (
"encoding/json"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"unicode"
"github.com/opencode-ai/opencode/internal/logging"
"github.com/spf13/viper"
)
const (
ProviderLocal ModelProvider = "local"
localModelsPath = "v1/models"
lmStudioBetaModelsPath = "api/v0/models"
)
func init() {
if endpoint := os.Getenv("LOCAL_ENDPOINT"); endpoint != "" {
localEndpoint, err := url.Parse(endpoint)
if err != nil {
logging.Debug("Failed to parse local endpoint",
"error", err,
"endpoint", endpoint,
)
return
}
load := func(url *url.URL, path string) []localModel {
url.Path = path
return listLocalModels(url.String())
}
models := load(localEndpoint, lmStudioBetaModelsPath)
if len(models) == 0 {
models = load(localEndpoint, localModelsPath)
}
if len(models) == 0 {
logging.Debug("No local models found",
"endpoint", endpoint,
)
return
}
loadLocalModels(models)
viper.SetDefault("providers.local.apiKey", "dummy")
ProviderPopularity[ProviderLocal] = 0
}
}
type localModelList struct {
Data []localModel `json:"data"`
}
type localModel struct {
ID string `json:"id"`
Object string `json:"object"`
Type string `json:"type"`
Publisher string `json:"publisher"`
Arch string `json:"arch"`
CompatibilityType string `json:"compatibility_type"`
Quantization string `json:"quantization"`
State string `json:"state"`
MaxContextLength int64 `json:"max_context_length"`
LoadedContextLength int64 `json:"loaded_context_length"`
}
func listLocalModels(modelsEndpoint string) []localModel {
res, err := http.Get(modelsEndpoint)
if err != nil {
logging.Debug("Failed to list local models",
"error", err,
"endpoint", modelsEndpoint,
)
}
defer func() {
if closeErr := res.Body.Close(); closeErr != nil {
logging.Debug("Failed to close response body", "error", closeErr)
}
}()
if res.StatusCode != http.StatusOK {
logging.Debug("Failed to list local models",
"status", res.StatusCode,
"endpoint", modelsEndpoint,
)
}
var modelList localModelList
if err = json.NewDecoder(res.Body).Decode(&modelList); err != nil {
logging.Debug("Failed to list local models",
"error", err,
"endpoint", modelsEndpoint,
)
}
var supportedModels []localModel
for _, model := range modelList.Data {
if strings.HasSuffix(modelsEndpoint, lmStudioBetaModelsPath) {
if model.Object != "model" || model.Type != "llm" {
logging.Debug("Skipping unsupported LMStudio model",
"endpoint", modelsEndpoint,
"id", model.ID,
"object", model.Object,
"type", model.Type,
)
continue
}
}
supportedModels = append(supportedModels, model)
}
return supportedModels
}
func loadLocalModels(models []localModel) {
for i, m := range models {
model := convertLocalModel(m)
SupportedModels[model.ID] = model
if i == 0 || m.State == "loaded" {
viper.SetDefault("agents.coder.model", model.ID)
viper.SetDefault("agents.summarizer.model", model.ID)
viper.SetDefault("agents.task.model", model.ID)
viper.SetDefault("agents.title.model", model.ID)
}
}
}
func convertLocalModel(model localModel) Model {
contextWindow := model.LoadedContextLength
if contextWindow == 0 {
contextWindow = 4096
}
return Model{
ID: ModelID("local." + model.ID),
Name: friendlyModelName(model.ID),
Provider: ProviderLocal,
APIModel: model.ID,
ContextWindow: contextWindow,
DefaultMaxTokens: contextWindow,
CanReason: true,
SupportsAttachments: true,
}
}
var modelInfoRegex = regexp.MustCompile(`(?i)^([a-z0-9]+)(?:[-_]?([rv]?\d[\.\d]*))?(?:[-_]?([a-z]+))?.*`)
func friendlyModelName(modelID string) string {
mainID := modelID
tag := ""
if slash := strings.LastIndex(mainID, "/"); slash != -1 {
mainID = mainID[slash+1:]
}
if at := strings.Index(modelID, "@"); at != -1 {
mainID = modelID[:at]
tag = modelID[at+1:]
}
match := modelInfoRegex.FindStringSubmatch(mainID)
if match == nil {
return modelID
}
capitalize := func(s string) string {
if s == "" {
return ""
}
runes := []rune(s)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
family := capitalize(match[1])
version := ""
label := ""
if len(match) > 2 && match[2] != "" {
version = strings.ToUpper(match[2])
}
if len(match) > 3 && match[3] != "" {
label = capitalize(match[3])
}
var parts []string
if family != "" {
parts = append(parts, family)
}
if version != "" {
parts = append(parts, version)
}
if label != "" {
parts = append(parts, label)
}
if tag != "" {
parts = append(parts, tag)
}
return strings.Join(parts, " ")
}