Skip to content

chore: make GPTScript a struct rather than an interface #45

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
Jul 18, 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ import (
)

func listTools(ctx context.Context) (string, error) {
g, err := gptscript.NewGPTScript()
g, err := gptscript.NewGPTScript(gptscript.GlobalOptions{})
if err != nil {
return "", err
}
defer g.Close()

return g.ListTools(ctx)
}
```
Expand All @@ -92,6 +93,7 @@ func listModels(ctx context.Context) ([]string, error) {
return nil, err
}
defer g.Close()

return g.ListModels(ctx)
}
```
Expand Down
44 changes: 15 additions & 29 deletions gptscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,11 @@ var (

const relativeToBinaryPath = "<me>"

type GPTScript interface {
Run(context.Context, string, Options) (*Run, error)
Evaluate(context.Context, Options, ...ToolDef) (*Run, error)
Parse(context.Context, string) ([]Node, error)
ParseTool(context.Context, string) ([]Node, error)
Version(context.Context) (string, error)
Fmt(context.Context, []Node) (string, error)
ListTools(context.Context) (string, error)
ListModels(context.Context) ([]string, error)
Confirm(context.Context, AuthResponse) error
PromptResponse(context.Context, PromptResponse) error
Close()
}

type gptscript struct {
type GPTScript struct {
url string
}

func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
func NewGPTScript(opts GlobalOptions) (*GPTScript, error) {
lock.Lock()
defer lock.Unlock()
gptscriptCount++
Expand Down Expand Up @@ -98,7 +84,7 @@ func NewGPTScript(opts GlobalOptions) (GPTScript, error) {
return nil, fmt.Errorf("failed to wait for gptscript to be ready: %w", err)
}
}
return &gptscript{url: "http://" + serverURL}, nil
return &GPTScript{url: "http://" + serverURL}, nil
}

func waitForServerReady(ctx context.Context, serverURL string) error {
Expand All @@ -122,7 +108,7 @@ func waitForServerReady(ctx context.Context, serverURL string) error {
}
}

func (g *gptscript) Close() {
func (g *GPTScript) Close() {
lock.Lock()
defer lock.Unlock()
gptscriptCount--
Expand All @@ -133,7 +119,7 @@ func (g *gptscript) Close() {
}
}

func (g *gptscript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef) (*Run, error) {
func (g *GPTScript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef) (*Run, error) {
return (&Run{
url: g.url,
requestPath: "evaluate",
Expand All @@ -143,7 +129,7 @@ func (g *gptscript) Evaluate(ctx context.Context, opts Options, tools ...ToolDef
}).NextChat(ctx, opts.Input)
}

func (g *gptscript) Run(ctx context.Context, toolPath string, opts Options) (*Run, error) {
func (g *GPTScript) Run(ctx context.Context, toolPath string, opts Options) (*Run, error) {
return (&Run{
url: g.url,
requestPath: "run",
Expand All @@ -154,7 +140,7 @@ func (g *gptscript) Run(ctx context.Context, toolPath string, opts Options) (*Ru
}

// Parse will parse the given file into an array of Nodes.
func (g *gptscript) Parse(ctx context.Context, fileName string) ([]Node, error) {
func (g *GPTScript) Parse(ctx context.Context, fileName string) ([]Node, error) {
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"file": fileName})
if err != nil {
return nil, err
Expand All @@ -173,7 +159,7 @@ func (g *gptscript) Parse(ctx context.Context, fileName string) ([]Node, error)
}

// ParseTool will parse the given string into a tool.
func (g *gptscript) ParseTool(ctx context.Context, toolDef string) ([]Node, error) {
func (g *GPTScript) ParseTool(ctx context.Context, toolDef string) ([]Node, error) {
out, err := g.runBasicCommand(ctx, "parse", map[string]any{"content": toolDef})
if err != nil {
return nil, err
Expand All @@ -192,7 +178,7 @@ func (g *gptscript) ParseTool(ctx context.Context, toolDef string) ([]Node, erro
}

// Fmt will format the given nodes into a string.
func (g *gptscript) Fmt(ctx context.Context, nodes []Node) (string, error) {
func (g *GPTScript) Fmt(ctx context.Context, nodes []Node) (string, error) {
for _, node := range nodes {
node.TextNode.combine()
}
Expand All @@ -206,7 +192,7 @@ func (g *gptscript) Fmt(ctx context.Context, nodes []Node) (string, error) {
}

// Version will return the output of `gptscript --version`
func (g *gptscript) Version(ctx context.Context) (string, error) {
func (g *GPTScript) Version(ctx context.Context) (string, error) {
out, err := g.runBasicCommand(ctx, "version", nil)
if err != nil {
return "", err
Expand All @@ -216,7 +202,7 @@ func (g *gptscript) Version(ctx context.Context) (string, error) {
}

// ListTools will list all the available tools.
func (g *gptscript) ListTools(ctx context.Context) (string, error) {
func (g *GPTScript) ListTools(ctx context.Context) (string, error) {
out, err := g.runBasicCommand(ctx, "list-tools", nil)
if err != nil {
return "", err
Expand All @@ -226,7 +212,7 @@ func (g *gptscript) ListTools(ctx context.Context) (string, error) {
}

// ListModels will list all the available models.
func (g *gptscript) ListModels(ctx context.Context) ([]string, error) {
func (g *GPTScript) ListModels(ctx context.Context) ([]string, error) {
out, err := g.runBasicCommand(ctx, "list-models", nil)
if err != nil {
return nil, err
Expand All @@ -235,17 +221,17 @@ func (g *gptscript) ListModels(ctx context.Context) ([]string, error) {
return strings.Split(strings.TrimSpace(out), "\n"), nil
}

func (g *gptscript) Confirm(ctx context.Context, resp AuthResponse) error {
func (g *GPTScript) Confirm(ctx context.Context, resp AuthResponse) error {
_, err := g.runBasicCommand(ctx, "confirm/"+resp.ID, resp)
return err
}

func (g *gptscript) PromptResponse(ctx context.Context, resp PromptResponse) error {
func (g *GPTScript) PromptResponse(ctx context.Context, resp PromptResponse) error {
_, err := g.runBasicCommand(ctx, "prompt-response/"+resp.ID, resp.Responses)
return err
}

func (g *gptscript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, error) {
func (g *GPTScript) runBasicCommand(ctx context.Context, requestPath string, body any) (string, error) {
run := &Run{
url: g.url,
requestPath: requestPath,
Expand Down
2 changes: 1 addition & 1 deletion gptscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

var g GPTScript
var g *GPTScript

func TestMain(m *testing.M) {
if os.Getenv("OPENAI_API_KEY") == "" && os.Getenv("GPTSCRIPT_URL") == "" {
Expand Down