Skip to content

Commit

Permalink
feat: dynamic fetching of openai models
Browse files Browse the repository at this point in the history
  • Loading branch information
BigJk committed Sep 21, 2024
1 parent 8dddb37 commit 62043f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
5 changes: 3 additions & 2 deletions frontend/src/js/ui/views/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default (): m.Component => {
m.redraw();
})
.catch((err) => {
error('Could not fetch AI models... retrying...');
error('Could not fetch AI models. retrying... (' + err + ')');
setTimeout(fetchAiModels, 3000);
});
};
Expand Down Expand Up @@ -293,7 +293,8 @@ export default (): m.Component => {
},
aiContextWindow: {
label: 'Context Window',
description: 'The context window for the AI service',
description:
'The context window for the AI service. This window is used to provide the AI with examples in case of the entry generator. Higher values should provide better results.',
},
aiMaxTokens: {
label: 'Max Tokens',
Expand Down
27 changes: 23 additions & 4 deletions rpc/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,7 @@ func RegisterAI(route *echo.Group, db database.Database) {
})

bind.MustBind(route, "/aiModels", func(provider string) ([]string, error) {
// TODO: dynamically fetch models
switch provider {
case "OpenAI":
return []string{"gpt-3.5-turbo", "gpt-3.5-turbo-1106", "gpt-3.5-turbo-16k", "gpt-4-1106-preview", "gpt-4", "gpt-4-32k"}, nil
case "Custom (e.g. Local)":
return []string{"Custom"}, nil
}
Expand All @@ -235,7 +232,25 @@ func RegisterAI(route *echo.Group, db database.Database) {
return nil, err
}

resp, err := http.Get(endpoint + "/v1/models")
req, err := http.NewRequest("GET", endpoint+"/v1/models", nil)
if err != nil {
return nil, err
}

if provider == "OpenAI" {
settings, err := db.GetSettings()
if err != nil {
return nil, err
}

if settings.AIApiKey == "" {
return nil, errors.New("OpenAI API key not set")
}

req.Header.Set("Authorization", "Bearer "+settings.AIApiKey)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -245,6 +260,10 @@ func RegisterAI(route *echo.Group, db database.Database) {
return nil, err
}

if strings.Contains(string(res), "invalid_api_key") {
return nil, errors.New("invalid API key")
}

var models ModelsList
err = json.Unmarshal(res, &models)
if err != nil {
Expand Down

0 comments on commit 62043f9

Please sign in to comment.