Skip to content

feat: Add gptscript --list-models [PROVIDER] #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
defer gptScript.Close()

if r.ListModels {
models, err := gptScript.ListModels(cmd.Context())
models, err := gptScript.ListModels(cmd.Context(), args...)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/gptscript/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ func (g *GPTScript) GetModel() engine.Model {
return g.Registry
}

func (g *GPTScript) ListModels(ctx context.Context) ([]string, error) {
return g.Registry.ListModels(ctx)
func (g *GPTScript) ListModels(ctx context.Context, providers ...string) ([]string, error) {
return g.Registry.ListModels(ctx, providers...)
}
6 changes: 3 additions & 3 deletions pkg/llm/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

type Client interface {
Call(ctx context.Context, messageRequest types.CompletionRequest, status chan<- types.CompletionStatus) (*types.CompletionMessage, error)
ListModels(ctx context.Context) (result []string, _ error)
ListModels(ctx context.Context, providers ...string) (result []string, _ error)
Supports(ctx context.Context, modelName string) (bool, error)
}

Expand All @@ -28,9 +28,9 @@ func (r *Registry) AddClient(client Client) error {
return nil
}

func (r *Registry) ListModels(ctx context.Context) (result []string, _ error) {
func (r *Registry) ListModels(ctx context.Context, providers ...string) (result []string, _ error) {
for _, v := range r.clients {
models, err := v.ListModels(ctx)
models, err := v.ListModels(ctx, providers...)
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ func (c *Client) Supports(ctx context.Context, modelName string) (bool, error) {
return slices.Contains(models, modelName), nil
}

func (c *Client) ListModels(ctx context.Context) (result []string, _ error) {
func (c *Client) ListModels(ctx context.Context, providers ...string) (result []string, _ error) {
// Only serve if providers is empty or "" is in the list
if len(providers) != 0 && !slices.Contains(providers, "") {
return nil, nil
}

models, err := c.c.ListModels(ctx)
if err != nil {
return nil, err
Expand Down
23 changes: 16 additions & 7 deletions pkg/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/gptscript-ai/gptscript/pkg/openai"
"github.com/gptscript-ai/gptscript/pkg/runner"
"github.com/gptscript-ai/gptscript/pkg/types"
"golang.org/x/exp/maps"
)

type Client struct {
Expand Down Expand Up @@ -49,13 +48,23 @@ func (c *Client) Call(ctx context.Context, messageRequest types.CompletionReques
return client.Call(ctx, messageRequest, status)
}

func (c *Client) ListModels(_ context.Context) (result []string, _ error) {
c.clientsLock.Lock()
defer c.clientsLock.Unlock()
func (c *Client) ListModels(ctx context.Context, providers ...string) (result []string, _ error) {
for _, provider := range providers {
client, err := c.load(ctx, provider)
if err != nil {
return nil, err
}
models, err := client.ListModels(ctx, "")
if err != nil {
return nil, err
}
for _, model := range models {
result = append(result, model+" from "+provider)
}
}

keys := maps.Keys(c.models)
sort.Strings(keys)
return keys, nil
sort.Strings(result)
return
}

func (c *Client) Supports(ctx context.Context, modelName string) (bool, error) {
Expand Down