Skip to content

Commit

Permalink
Merge pull request gptscript-ai#487 from thedadams/set-openai-options…
Browse files Browse the repository at this point in the history
…-from-sdk

feat: add ability to pass OpenAI config in the SDK
  • Loading branch information
thedadams authored Jun 12, 2024
2 parents e0aa2ad + f5e2585 commit ac679cd
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (e *Eval) Run(cmd *cobra.Command, args []string) error {
return err
}

runner, err := gptscript.New(&opts)
runner, err := gptscript.New(opts)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) (retErr error) {
return s.Start(ctx)
}

gptScript, err := gptscript.New(&gptOpt)
gptScript, err := gptscript.New(gptOpt)
if err != nil {
return err
}
Expand Down
35 changes: 27 additions & 8 deletions pkg/gptscript/gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,25 @@ type Options struct {
Env []string
}

func complete(opts *Options) (result *Options) {
result = opts
if result == nil {
result = &Options{}
func complete(opts ...Options) (Options, error) {
var (
result Options
err error
)

for _, opt := range opts {
result.Cache = cache.Complete(result.Cache, opt.Cache)
result.Monitor = monitor.Complete(result.Monitor, opt.Monitor)
result.Runner = runner.Complete(result.Runner, opt.Runner)
result.OpenAI, err = openai.Complete(result.OpenAI, opt.OpenAI)
if err != nil {
return Options{}, err
}

result.CredentialContext = types.FirstSet(opt.CredentialContext, result.CredentialContext)
result.Quiet = types.FirstSet(opt.Quiet, result.Quiet)
result.Workspace = types.FirstSet(opt.Workspace, result.Workspace)
result.Env = append(result.Env, opt.Env...)
}
if result.Quiet == nil {
result.Quiet = new(bool)
Expand All @@ -60,13 +75,17 @@ func complete(opts *Options) (result *Options) {
result.Env = os.Environ()
}
if result.CredentialContext == "" {
opts.CredentialContext = "default"
result.CredentialContext = "default"
}
return

return result, nil
}

func New(opts *Options) (*GPTScript, error) {
opts = complete(opts)
func New(o ...Options) (*GPTScript, error) {
opts, err := complete(o...)
if err != nil {
return nil, err
}

registry := llm.NewRegistry()

Expand Down
4 changes: 2 additions & 2 deletions pkg/monitor/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Options struct {
DebugMessages bool `usage:"Enable logging of chat completion calls"`
}

func complete(opts ...Options) (result Options) {
func Complete(opts ...Options) (result Options) {
for _, opt := range opts {
result.DumpState = types.FirstSet(opt.DumpState, result.DumpState)
result.DisplayProgress = types.FirstSet(opt.DisplayProgress, result.DisplayProgress)
Expand Down Expand Up @@ -313,7 +313,7 @@ func (d *display) Stop(output string, err error) {
}

func NewConsole(opts ...Options) *Console {
opt := complete(opts...)
opt := Complete(opts...)
return &Console{
dumpState: opt.DumpState,
displayProgress: opt.DisplayProgress,
Expand Down
4 changes: 2 additions & 2 deletions pkg/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Options struct {
Cache *cache.Client
}

func complete(opts ...Options) (result Options, err error) {
func Complete(opts ...Options) (result Options, err error) {
for _, opt := range opts {
result.BaseURL = types.FirstSet(opt.BaseURL, result.BaseURL)
result.APIKey = types.FirstSet(opt.APIKey, result.APIKey)
Expand Down Expand Up @@ -87,7 +87,7 @@ func complete(opts ...Options) (result Options, err error) {
}

func NewClient(credStore credentials.CredentialStore, opts ...Options) (*Client, error) {
opt, err := complete(opts...)
opt, err := Complete(opts...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func DefaultAuthorizer(engine.Context, string) (AuthorizerResponse, error) {
}, nil
}

func complete(opts ...Options) (result Options) {
func Complete(opts ...Options) (result Options) {
for _, opt := range opts {
result.MonitorFactory = types.FirstSet(opt.MonitorFactory, result.MonitorFactory)
result.RuntimeManager = types.FirstSet(opt.RuntimeManager, result.RuntimeManager)
Expand Down Expand Up @@ -91,7 +91,7 @@ type Runner struct {
}

func New(client engine.Model, credStore credentials.CredentialStore, opts ...Options) (*Runner, error) {
opt := complete(opts...)
opt := Complete(opts...)

runner := &Runner{
c: client,
Expand Down
8 changes: 6 additions & 2 deletions pkg/sdkserver/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
"time"

"github.com/acorn-io/broadcaster"
"github.com/gptscript-ai/gptscript/pkg/cache"
gcontext "github.com/gptscript-ai/gptscript/pkg/context"
"github.com/gptscript-ai/gptscript/pkg/gptscript"
"github.com/gptscript-ai/gptscript/pkg/input"
"github.com/gptscript-ai/gptscript/pkg/loader"
"github.com/gptscript-ai/gptscript/pkg/openai"
"github.com/gptscript-ai/gptscript/pkg/parser"
"github.com/gptscript-ai/gptscript/pkg/runner"
gserver "github.com/gptscript-ai/gptscript/pkg/server"
Expand All @@ -27,6 +29,7 @@ import (
const toolRunTimeout = 15 * time.Minute

type server struct {
gptscriptOpts gptscript.Options
address, token string
client *gptscript.GPTScript
events *broadcaster.Broadcaster[event]
Expand Down Expand Up @@ -189,8 +192,9 @@ func (s *server) execHandler(w http.ResponseWriter, r *http.Request) {
programLoader = loader.Program
}

opts := &gptscript.Options{
Cache: reqObject.Options,
opts := gptscript.Options{
Cache: cache.Options(reqObject.cacheOptions),
OpenAI: openai.Options(reqObject.openAIOptions),
Env: reqObject.Env,
Workspace: reqObject.Workspace,
CredentialContext: reqObject.CredentialContext,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdkserver/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

type loaderFunc func(context.Context, string, string, ...loader.Options) (types.Program, error)

func (s *server) execAndStream(ctx context.Context, programLoader loaderFunc, logger mvl.Logger, w http.ResponseWriter, opts *gptscript.Options, chatState, input, subTool string, toolDef fmt.Stringer) {
g, err := gptscript.New(opts)
func (s *server) execAndStream(ctx context.Context, programLoader loaderFunc, logger mvl.Logger, w http.ResponseWriter, opts gptscript.Options, chatState, input, subTool string, toolDef fmt.Stringer) {
g, err := gptscript.New(s.gptscriptOpts, opts)
if err != nil {
writeError(logger, w, http.StatusInternalServerError, fmt.Errorf("failed to initialize gptscript: %w", err))
return
Expand Down
3 changes: 2 additions & 1 deletion pkg/sdkserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Start(ctx context.Context, opts Options) error {
// prompt server because it is only used for fmt, parse, etc.
opts.Env = append(opts.Env, fmt.Sprintf("%s=%s", types.PromptTokenEnvVar, token))

g, err := gptscript.New(&opts.Options)
g, err := gptscript.New(opts.Options)
if err != nil {
return err
}
Expand All @@ -64,6 +64,7 @@ func Start(ctx context.Context, opts Options) error {
}

s := &server{
gptscriptOpts: opts.Options,
address: listener.Addr().String(),
token: token,
client: g,
Expand Down
9 changes: 8 additions & 1 deletion pkg/sdkserver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gptscript-ai/gptscript/pkg/cache"
"github.com/gptscript-ai/gptscript/pkg/engine"
"github.com/gptscript-ai/gptscript/pkg/openai"
"github.com/gptscript-ai/gptscript/pkg/parser"
"github.com/gptscript-ai/gptscript/pkg/runner"
gserver "github.com/gptscript-ai/gptscript/pkg/server"
Expand Down Expand Up @@ -40,10 +41,16 @@ func (t toolDefs) String() string {
return s.String()
}

type (
cacheOptions cache.Options
openAIOptions openai.Options
)

type toolOrFileRequest struct {
content `json:",inline"`
file `json:",inline"`
cache.Options `json:",inline"`
cacheOptions `json:",inline"`
openAIOptions `json:",inline"`

ToolDefs toolDefs `json:"toolDefs,inline"`
SubTool string `json:"subTool"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func New(opts *Options) (*Server, error) {
opts = complete(opts)
opts.GPTScript.Runner.MonitorFactory = NewSessionFactory(events)

g, err := gptscript.New(&opts.GPTScript)
g, err := gptscript.New(opts.GPTScript)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit ac679cd

Please sign in to comment.