11package cli
22
33import (
4- "context"
54 "fmt"
65 "io"
76 "os"
@@ -12,15 +11,12 @@ import (
1211 "github.com/gptscript-ai/gptscript/pkg/builtin"
1312 "github.com/gptscript-ai/gptscript/pkg/cache"
1413 "github.com/gptscript-ai/gptscript/pkg/confirm"
15- "github.com/gptscript-ai/gptscript/pkg/engine "
14+ "github.com/gptscript-ai/gptscript/pkg/gptscript "
1615 "github.com/gptscript-ai/gptscript/pkg/input"
17- "github.com/gptscript-ai/gptscript/pkg/llm"
1816 "github.com/gptscript-ai/gptscript/pkg/loader"
1917 "github.com/gptscript-ai/gptscript/pkg/monitor"
2018 "github.com/gptscript-ai/gptscript/pkg/mvl"
2119 "github.com/gptscript-ai/gptscript/pkg/openai"
22- "github.com/gptscript-ai/gptscript/pkg/repos/runtimes"
23- "github.com/gptscript-ai/gptscript/pkg/runner"
2420 "github.com/gptscript-ai/gptscript/pkg/server"
2521 "github.com/gptscript-ai/gptscript/pkg/types"
2622 "github.com/gptscript-ai/gptscript/pkg/version"
@@ -50,8 +46,6 @@ type GPTScript struct {
5046 Server bool `usage:"Start server"`
5147 ListenAddress string `usage:"Server listen address" default:"127.0.0.1:9090"`
5248 Chdir string `usage:"Change current working directory" short:"C"`
53-
54- _client llm.Client `usage:"-"`
5549}
5650
5751func New () * cobra.Command {
@@ -78,33 +72,6 @@ func (r *GPTScript) Customize(cmd *cobra.Command) {
7872 }
7973}
8074
81- func (r * GPTScript ) getClient (ctx context.Context ) (llm.Client , error ) {
82- if r ._client != nil {
83- return r ._client , nil
84- }
85-
86- cacheClient , err := cache .New (cache .Options (r .CacheOptions ))
87- if err != nil {
88- return nil , err
89- }
90-
91- oaClient , err := openai .NewClient (openai .Options (r .OpenAIOptions ), openai.Options {
92- Cache : cacheClient ,
93- })
94- if err != nil {
95- return nil , err
96- }
97-
98- registry := llm .NewRegistry ()
99-
100- if err := registry .AddClient (ctx , oaClient ); err != nil {
101- return nil , err
102- }
103-
104- r ._client = registry
105- return r ._client , nil
106- }
107-
10875func (r * GPTScript ) listTools () error {
10976 var lines []string
11077 for _ , tool := range builtin .ListTools () {
@@ -114,24 +81,6 @@ func (r *GPTScript) listTools() error {
11481 return nil
11582}
11683
117- func (r * GPTScript ) listModels (ctx context.Context ) error {
118- c , err := r .getClient (ctx )
119- if err != nil {
120- return err
121- }
122-
123- models , err := c .ListModels (ctx )
124- if err != nil {
125- return err
126- }
127-
128- for _ , model := range models {
129- fmt .Println (model )
130- }
131-
132- return nil
133- }
134-
13584func (r * GPTScript ) Pre (* cobra.Command , []string ) error {
13685 // chdir as soon as possible
13786 if r .Chdir != "" {
@@ -164,37 +113,50 @@ func (r *GPTScript) Pre(*cobra.Command, []string) error {
164113}
165114
166115func (r * GPTScript ) Run (cmd * cobra.Command , args []string ) error {
167- defer engine .CloseDaemons ()
168-
169- if r .ListModels {
170- return r .listModels (cmd .Context ())
171- }
172-
173- if r .ListTools {
174- return r .listTools ()
116+ gptOpt := gptscript.Options {
117+ Cache : cache .Options (r .CacheOptions ),
118+ OpenAI : openai .Options (r .OpenAIOptions ),
119+ Monitor : monitor .Options (r .DisplayOptions ),
120+ Quiet : r .Quiet ,
121+ Env : os .Environ (),
175122 }
176123
177124 if r .Server {
178- c , err := r .getClient (cmd .Context ())
179- if err != nil {
180- return err
181- }
182- s , err := server .New (c , server.Options {
125+ s , err := server .New (& server.Options {
183126 ListenAddress : r .ListenAddress ,
127+ GPTScript : gptOpt ,
184128 })
185129 if err != nil {
186130 return err
187131 }
132+ defer s .Close ()
188133 return s .Start (cmd .Context ())
189134 }
190135
136+ gptScript , err := gptscript .New (& gptOpt )
137+ if err != nil {
138+ return err
139+ }
140+ defer gptScript .Close ()
141+
142+ if r .ListModels {
143+ models , err := gptScript .ListModels (cmd .Context ())
144+ if err != nil {
145+ return err
146+ }
147+ fmt .Println (strings .Join (models , "\n " ))
148+ }
149+
150+ if r .ListTools {
151+ return r .listTools ()
152+ }
153+
191154 if len (args ) == 0 {
192155 return cmd .Help ()
193156 }
194157
195158 var (
196159 prg types.Program
197- err error
198160 )
199161
200162 if args [0 ] == "-" {
@@ -227,21 +189,6 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
227189 return assemble .Assemble (prg , out )
228190 }
229191
230- client , err := r .getClient (cmd .Context ())
231- if err != nil {
232- return err
233- }
234-
235- runner , err := runner .New (client , runner.Options {
236- MonitorFactory : monitor .NewConsole (monitor .Options (r .DisplayOptions ), monitor.Options {
237- DisplayProgress : ! * r .Quiet ,
238- }),
239- RuntimeManager : runtimes .Default (cache .Complete (cache .Options (r .CacheOptions )).CacheDir ),
240- })
241- if err != nil {
242- return err
243- }
244-
245192 toolInput , err := input .FromCLI (r .Input , args )
246193 if err != nil {
247194 return err
@@ -251,7 +198,7 @@ func (r *GPTScript) Run(cmd *cobra.Command, args []string) error {
251198 if r .Confirm {
252199 ctx = confirm .WithConfirm (ctx , confirm.TextPrompt {})
253200 }
254- s , err := runner .Run (ctx , prg , os .Environ (), toolInput )
201+ s , err := gptScript .Run (ctx , prg , os .Environ (), toolInput )
255202 if err != nil {
256203 return err
257204 }
0 commit comments