@@ -9,11 +9,17 @@ import (
99 "net/http"
1010 "strings"
1111
12- "github.com/APIParkLab/APIPark/module/router"
12+ "github.com/APIParkLab/APIPark/module/subscribe"
13+ subscribe_dto "github.com/APIParkLab/APIPark/module/subscribe/dto"
1314
1415 "github.com/APIParkLab/APIPark/model/plugin_model"
1516 "github.com/APIParkLab/APIPark/service/api"
1617
18+ ai_api_dto "github.com/APIParkLab/APIPark/module/ai-api/dto"
19+ router_dto "github.com/APIParkLab/APIPark/module/router/dto"
20+
21+ "github.com/APIParkLab/APIPark/module/router"
22+
1723 ai_api "github.com/APIParkLab/APIPark/module/ai-api"
1824
1925 "github.com/APIParkLab/APIPark/module/catalogue"
@@ -24,9 +30,6 @@ import (
2430
2531 service_dto "github.com/APIParkLab/APIPark/module/service/dto"
2632
27- ai_api_dto "github.com/APIParkLab/APIPark/module/ai-api/dto"
28- router_dto "github.com/APIParkLab/APIPark/module/router/dto"
29-
3033 ai_provider_local "github.com/APIParkLab/APIPark/ai-provider/local"
3134
3235 "github.com/eolinker/eosc/log"
@@ -44,7 +47,9 @@ type imlLocalModelController struct {
4447 serviceModule service.IServiceModule `autowired:""`
4548 catalogueModule catalogue.ICatalogueModule `autowired:""`
4649 aiAPIModule ai_api.IAPIModule `autowired:""`
50+ appModule service.IAppModule `autowired:""`
4751 routerModule router.IRouterModule `autowired:""`
52+ subscribeModule subscribe.ISubscribeModule `autowired:""`
4853 docModule service.IServiceDocModule `autowired:""`
4954 transaction store.ITransaction `autowired:""`
5055}
@@ -91,15 +96,8 @@ func (i *imlLocalModelController) Deploy(ctx *gin.Context) {
9196 "code" : - 1 , "msg" : "model is required" , "success" : "fail" ,
9297 })
9398 return
94-
9599 }
96- //err = i.initAILocalService(ctx, input.Model, input.Team)
97- //if err != nil {
98- // ctx.JSON(200, gin.H{
99- // "code": -1, "msg": err.Error(), "success": "fail",
100- // })
101- // return
102- //}
100+
103101 id := uuid .NewString ()
104102 p , err := i .module .Deploy (ctx , input .Model , id )
105103 if err != nil {
@@ -113,10 +111,11 @@ func (i *imlLocalModelController) Deploy(ctx *gin.Context) {
113111 go func () {
114112 select {
115113 case <- ctx .Writer .CloseNotify ():
116- log .Info ("client closed connection,close pipeline" )
117- ai_provider_local .CancelPipeline (input .Model , id )
114+
118115 case <- done :
116+
119117 }
118+ ai_provider_local .CancelPipeline (input .Model , id )
120119 }()
121120 var complete int64
122121 var total int64
@@ -177,29 +176,29 @@ func (i *imlLocalModelController) Deploy(ctx *gin.Context) {
177176}
178177
179178func (i * imlLocalModelController ) DeployStart (ctx * gin.Context , input * ai_local_dto.DeployInput ) error {
180- err := i .initAILocalService (ctx , input .Model , input .Team )
179+ fn , err := i .initAILocalService (ctx , input .Model , input .Team )
181180 if err != nil {
182181 return err
183182 }
184183 id := uuid .NewString ()
185- _ , err = i .module .Deploy (ctx , input .Model , id )
184+ _ , err = i .module .Deploy (ctx , input .Model , id , fn )
186185 if err != nil {
187186 return err
188187 }
189188 ai_provider_local .CancelPipeline (input .Model , id )
190189 return nil
191190}
192191
193- func (i * imlLocalModelController ) initAILocalService (ctx context.Context , model string , teamID string ) error {
194- err := i .transaction . Transaction (ctx , func ( ctx context. Context ) error {
195- catalogueInfo , err := i . catalogueModule . DefaultCatalogue ( ctx )
196- if err != nil {
197- return err
198- }
199- serviceId := uuid . NewString ( )
200- prefix := fmt . Sprintf ( "/%s" , serviceId [: 8 ])
201- providerId := "ollama"
202- info , err : = i .serviceModule .Create (ctx , teamID , & service_dto.CreateService {
192+ func (i * imlLocalModelController ) initAILocalService (ctx context.Context , model string , teamID string ) ( func () error , error ) {
193+ catalogueInfo , err := i .catalogueModule . DefaultCatalogue (ctx )
194+ if err != nil {
195+ return nil , err
196+ }
197+ serviceId := uuid . NewString ()
198+ prefix := fmt . Sprintf ( "/%s" , serviceId [: 8 ] )
199+ providerId := "ollama"
200+ err = i . transaction . Transaction ( ctx , func ( ctx context. Context ) error {
201+ _ , err = i .serviceModule .Create (ctx , teamID , & service_dto.CreateService {
203202 Id : serviceId ,
204203 Name : model ,
205204 Prefix : prefix ,
@@ -214,10 +213,10 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
214213 if err != nil {
215214 return err
216215 }
217- err = i .module .SaveCache (ctx , model , serviceId )
218- if err != nil {
219- return err
220- }
216+ return i .module .SaveCache (ctx , model , serviceId )
217+ })
218+
219+ return func () error {
221220 path := fmt .Sprintf ("/%s/chat" , strings .Trim (prefix , "/" ))
222221 timeout := 300000
223222 retry := 0
@@ -232,11 +231,11 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
232231 Type : "local" ,
233232 }
234233 name := "Demo AI API"
235- description := "A demo that shows you how to use a e a Chat API."
234+ description := "This is a demo that shows you how to use a Chat API."
236235 apiId := uuid .NewString ()
237236 err = i .aiAPIModule .Create (
238237 ctx ,
239- info . Id ,
238+ serviceId ,
240239 & ai_api_dto.CreateAPI {
241240 Id : apiId ,
242241 Name : name ,
@@ -262,11 +261,11 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
262261 plugins ["ai_formatter" ] = api.PluginSetting {
263262 Config : plugin_model.ConfigType {
264263 "model" : aiModel .Id ,
265- "provider" : info . Provider . Id ,
264+ "provider" : providerId ,
266265 "config" : aiModel .Config ,
267266 },
268267 }
269- _ , err = i .routerModule .Create (ctx , info . Id , & router_dto.Create {
268+ _ , err = i .routerModule .Create (ctx , serviceId , & router_dto.Create {
270269 Id : apiId ,
271270 Name : name ,
272271 Path : path ,
@@ -287,13 +286,19 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
287286 if err != nil {
288287 return err
289288 }
290-
291- return i .docModule .SaveServiceDoc (ctx , info .Id , & service_dto.SaveServiceDoc {
289+ apps , err := i .appModule .Search (ctx , teamID , "" )
290+ if err != nil {
291+ return err
292+ }
293+ for _ , app := range apps {
294+ i .subscribeModule .AddSubscriber (ctx , serviceId , & subscribe_dto.AddSubscriber {
295+ Application : app .Id ,
296+ })
297+ }
298+ return i .docModule .SaveServiceDoc (ctx , serviceId , & service_dto.SaveServiceDoc {
292299 Doc : "" ,
293300 })
294- })
295-
296- return err
301+ }, err
297302}
298303
299304func (i * imlLocalModelController ) Search (ctx * gin.Context , keyword string ) ([]* ai_local_dto.LocalModelItem , error ) {
0 commit comments