Skip to content

Commit 4e0239d

Browse files
authored
Merge pull request #259 from APIParkLab/feature/1.6-liujian
Feature/1.6 liujian
2 parents 92e4781 + 2574b28 commit 4e0239d

File tree

9 files changed

+88
-35
lines changed

9 files changed

+88
-35
lines changed

ai-provider/model-runtime/model-providers/bailian/bailian.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
provider: bailian
22
label:
33
zh_Hans: 阿里云百炼
4-
en_US: bailian
4+
en_US: BaiLian
55
icon_small:
66
en_US: icon_s_en.svg
77
icon_large:

app/ai-event-handler/nsq.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func convertInt(value interface{}) int {
8181

8282
func genAIKey(key string, provider string) string {
8383
keys := strings.Split(key, "@")
84-
return strings.TrimSuffix(keys[0], fmt.Sprintf("-%s", provider))
84+
return strings.TrimPrefix(keys[0], fmt.Sprintf("%s-", provider))
8585
}
8686

8787
// HandleMessage 处理从 NSQ 读取的消息

controller/ai-local/iml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
267267
})
268268

269269
return func() error {
270-
path := fmt.Sprintf("/%s/chat", strings.Trim(prefix, "/"))
270+
path := fmt.Sprintf("/%s/chat/completions", strings.Trim(prefix, "/"))
271271
timeout := 300000
272272
retry := 0
273273
aiPrompt := &ai_api_dto.AiPrompt{

controller/system/iml.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -395,36 +395,36 @@ func (i *imlInitController) createAIService(ctx context.Context, teamID string,
395395
if err != nil {
396396
return err
397397
}
398-
path := fmt.Sprintf("/%s/demo_translation_api", strings.Trim(input.Prefix, "/"))
398+
path := fmt.Sprintf("/%s/chat/completions", strings.Trim(input.Prefix, "/"))
399399
timeout := 300000
400400
retry := 0
401401
aiPrompt := &ai_api_dto.AiPrompt{
402-
Variables: []*ai_api_dto.AiPromptVariable{
403-
{
404-
Key: "source_lang",
405-
Description: "",
406-
Require: true,
407-
},
408-
{
409-
Key: "target_lang",
410-
Description: "",
411-
Require: true,
412-
},
413-
{
414-
Key: "text",
415-
Description: "",
416-
Require: true,
417-
},
418-
},
419-
Prompt: "You need to translate {{source_lang}} into {{target_lang}}, and the following is the content that needs to be translated.\n---\n{{text}}",
402+
//Variables: []*ai_api_dto.AiPromptVariable{
403+
// {
404+
// Key: "source_lang",
405+
// Description: "",
406+
// Require: true,
407+
// },
408+
// {
409+
// Key: "target_lang",
410+
// Description: "",
411+
// Require: true,
412+
// },
413+
// {
414+
// Key: "text",
415+
// Description: "",
416+
// Require: true,
417+
// },
418+
//},
419+
//Prompt: "You need to translate {{source_lang}} into {{target_lang}}, and the following is the content that needs to be translated.\n---\n{{text}}",
420420
}
421421
aiModel := &ai_api_dto.AiModel{
422422
Id: m.ID(),
423423
Config: m.DefaultConfig(),
424424
Provider: providerId,
425425
}
426-
name := "Demo Translation API"
427-
description := "A demo that shows you how to use a prompt to create a Translation API."
426+
name := "Demo Chat API"
427+
description := "A demo that shows you how to use a prompt to create a Chat API."
428428
apiId := uuid.New().String()
429429
err = i.aiAPIModule.Create(
430430
ctx,

module/ai-api/schema.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,32 @@ func genOperation(summary string, description string, variables []*ai_api_dto.Ai
2323
operation := openapi3.NewOperation()
2424
operation.Summary = summary
2525
operation.Description = description
26+
operation.AddParameter(&openapi3.Parameter{
27+
Name: "Authorization",
28+
In: "header",
29+
Required: true,
30+
Example: "{your_apipark_apikey}",
31+
})
2632
operation.RequestBody = genRequestBody(variables)
2733
operation.Responses = &openapi3.Responses{}
2834
operation.Responses.Set("200", genResponse())
2935
return operation
3036
}
3137

38+
func genRequestHeaders() openapi3.Parameters {
39+
return openapi3.Parameters{
40+
{
41+
Value: &openapi3.Parameter{
42+
Name: "Authorization",
43+
In: "header",
44+
Description: "your_apipark_apikey", // 替换Prompt的变量列表
45+
Required: true,
46+
Example: "your_apipark_apikey",
47+
},
48+
},
49+
}
50+
}
51+
3252
func genRequestParameters(variables []*ai_api_dto.AiPromptVariable) openapi3.Parameters {
3353
return openapi3.Parameters{
3454
{

module/ai-balance/iml.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ func (i *imlBalanceModule) getLocalBalances(ctx context.Context, v string) ([]*g
268268
var has bool
269269
v, has = i.settingService.Get(ctx, "system.ai_model.ollama_address")
270270
if !has {
271-
return nil, fmt.Errorf("ollama address not found")
271+
//return nil, fmt.Errorf("ollama address not found")
272+
return nil, nil
272273
}
273274
}
274275

@@ -294,7 +295,8 @@ func (i *imlBalanceModule) getBalances(ctx context.Context) ([]*gateway.DynamicR
294295
}
295296
v, has := i.settingService.Get(ctx, "system.ai_model.ollama_address")
296297
if !has {
297-
return nil, fmt.Errorf("ollama address not found")
298+
//return nil, fmt.Errorf("ollama address not found")
299+
return nil, nil
298300
}
299301
releases := make([]*gateway.DynamicRelease, 0, len(balances))
300302
for _, item := range balances {
@@ -305,6 +307,10 @@ func (i *imlBalanceModule) getBalances(ctx context.Context) ([]*gateway.DynamicR
305307
continue
306308
}
307309
base = fmt.Sprintf("%s://%s%s", p.URI().Scheme(), p.URI().Host(), p.URI().Path())
310+
} else {
311+
if v == "" {
312+
continue
313+
}
308314
}
309315
releases = append(releases, newRelease(item, base))
310316
}

module/ai-local/iml.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,8 @@ func (i *imlLocalModel) getLocalModels(ctx context.Context, v string) ([]*gatewa
614614
var has bool
615615
v, has = i.settingService.Get(ctx, "system.ai_model.ollama_address")
616616
if !has {
617-
return nil, errors.New("ollama_address not set")
617+
//return nil, errors.New("ollama_address not set")
618+
return nil, nil
618619
}
619620
}
620621
provider := ai_provider_local.ProviderLocal

module/ai/iml.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"sort"
9+
"strings"
910
"time"
1011

1112
ai_model "github.com/APIParkLab/APIPark/service/ai-model"
@@ -200,6 +201,10 @@ func (i *imlProviderModule) Delete(ctx context.Context, id string) error {
200201
}
201202

202203
func (i *imlProviderModule) AddProvider(ctx context.Context, input *ai_dto.NewProvider) (*ai_dto.SimpleProvider, error) {
204+
_, has := model_runtime.GetProvider(strings.ToLower(input.Name))
205+
if has {
206+
return nil, fmt.Errorf("provider `%s` duplicate", input.Name)
207+
}
203208
// uuid = name
204209
if has := i.providerService.CheckUuidDuplicate(ctx, input.Name); has {
205210
return nil, fmt.Errorf("provider `%s` duplicate", input.Name)
@@ -241,7 +246,7 @@ func (i *imlProviderModule) SimpleProvider(ctx context.Context, id string) (*ai_
241246

242247
func (i *imlProviderModule) ConfiguredProviders(ctx context.Context, keyword string) ([]*ai_dto.ConfiguredProviderItem, error) {
243248
// 获取已配置的AI服务商
244-
list, err := i.providerService.Search(ctx, keyword, nil, "update_at")
249+
list, err := i.providerService.Search(ctx, keyword, nil, "update_at desc")
245250
if err != nil {
246251
return nil, fmt.Errorf("get provider list error:%v", err)
247252
}

module/service/iml.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,36 @@ func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *ser
396396
}
397397
}
398398
}
399-
400-
err := i.serviceModelMappingService.Save(ctx, &service_model_mapping.Save{
401-
Sid: input.Id,
402-
Content: input.ModelMapping,
403-
})
399+
err := i.serviceService.Create(ctx, mo)
404400
if err != nil {
405401
return err
406402
}
407-
return i.serviceService.Create(ctx, mo)
403+
if input.ModelMapping != "" {
404+
m := make(map[string]string)
405+
err = json.Unmarshal([]byte(input.ModelMapping), &m)
406+
if err != nil {
407+
return err
408+
}
409+
err = i.serviceModelMappingService.Save(ctx, &service_model_mapping.Save{
410+
Sid: input.Id,
411+
Content: input.ModelMapping,
412+
})
413+
if err != nil {
414+
return err
415+
}
416+
client, err := i.clusterService.GatewayClient(ctx, cluster.DefaultClusterID)
417+
if err != nil {
418+
return err
419+
}
420+
err = client.Hash().Online(ctx, &gateway.HashRelease{
421+
HashKey: fmt.Sprintf("%s:%s", gateway.KeyServiceMapping, input.Id),
422+
HashMap: m,
423+
})
424+
if err != nil {
425+
return err
426+
}
427+
}
428+
return nil
408429
})
409430
if err != nil {
410431
return nil, err
@@ -473,7 +494,7 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d
473494

474495
}
475496
}
476-
if input.ModelMapping != nil {
497+
if input.ModelMapping != nil && *input.ModelMapping != "" {
477498
m := make(map[string]string)
478499
err = json.Unmarshal([]byte(*input.ModelMapping), &m)
479500
if err != nil {

0 commit comments

Comments
 (0)