Skip to content

feat: add ability to list models from other providers #802

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
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
41 changes: 12 additions & 29 deletions pkg/sdkserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,13 @@ func (s *server) version(w http.ResponseWriter, r *http.Request) {
// listTools will return the output of `gptscript --list-tools`
func (s *server) listTools(w http.ResponseWriter, r *http.Request) {
logger := gcontext.GetLogger(r.Context())
var prg types.Program
if r.ContentLength != 0 {
reqObject := new(toolOrFileRequest)
err := json.NewDecoder(r.Body).Decode(reqObject)
if err != nil {
writeError(logger, w, http.StatusBadRequest, fmt.Errorf("failed to decode request body: %w", err))
return
}

if reqObject.Content != "" {
prg, err = loader.ProgramFromSource(r.Context(), reqObject.Content, reqObject.SubTool, loader.Options{Cache: s.client.Cache})
} else if reqObject.File != "" {
prg, err = loader.Program(r.Context(), reqObject.File, reqObject.SubTool, loader.Options{Cache: s.client.Cache})
} else {
prg, err = loader.ProgramFromSource(r.Context(), reqObject.ToolDefs.String(), reqObject.SubTool, loader.Options{Cache: s.client.Cache})
}
if err != nil {
writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to load program: %w", err))
return
}
}

tools := s.client.ListTools(r.Context(), prg)
tools := s.client.ListTools(r.Context(), types.Program{})
sort.Slice(tools, func(i, j int) bool {
return tools[i].Name < tools[j].Name
})

lines := make([]string, 0, len(tools))
for _, tool := range tools {
if tool.Name == "" {
tool.Name = prg.Name
}

// Don't print instructions
tool.Instructions = ""

Expand All @@ -118,22 +92,31 @@ func (s *server) listTools(w http.ResponseWriter, r *http.Request) {
// listModels will return the output of `gptscript --list-models`
func (s *server) listModels(w http.ResponseWriter, r *http.Request) {
logger := gcontext.GetLogger(r.Context())
client := s.client

var providers []string
if r.ContentLength != 0 {
reqObject := new(modelsRequest)
if err := json.NewDecoder(r.Body).Decode(reqObject); err != nil {
err := json.NewDecoder(r.Body).Decode(reqObject)
if err != nil {
writeError(logger, w, http.StatusBadRequest, fmt.Errorf("failed to decode request body: %w", err))
return
}

providers = reqObject.Providers

client, err = gptscript.New(r.Context(), s.gptscriptOpts, gptscript.Options{Env: reqObject.Env, Runner: runner.Options{CredentialOverrides: reqObject.CredentialOverrides}})
if err != nil {
writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to create client: %w", err))
return
}
}

if s.gptscriptOpts.DefaultModelProvider != "" {
providers = append(providers, s.gptscriptOpts.DefaultModelProvider)
}

out, err := s.client.ListModels(r.Context(), providers...)
out, err := client.ListModels(r.Context(), providers...)
if err != nil {
writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to list models: %w", err))
return
Expand Down
4 changes: 3 additions & 1 deletion pkg/sdkserver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ type parseRequest struct {
}

type modelsRequest struct {
Providers []string `json:"providers"`
Providers []string `json:"providers"`
Env []string `json:"env"`
CredentialOverrides []string `json:"credentialOverrides"`
}

type runInfo struct {
Expand Down