Merge pull request #259 from APIParkLab/feature/1.6-liujian

Feature/1.6 liujian
This commit is contained in:
Dot.L
2025-03-14 19:17:25 +08:00
committed by GitHub
9 changed files with 88 additions and 35 deletions
@@ -1,7 +1,7 @@
provider: bailian
label:
zh_Hans: 阿里云百炼
en_US: bailian
en_US: BaiLian
icon_small:
en_US: icon_s_en.svg
icon_large:
+1 -1
View File
@@ -81,7 +81,7 @@ func convertInt(value interface{}) int {
func genAIKey(key string, provider string) string {
keys := strings.Split(key, "@")
return strings.TrimSuffix(keys[0], fmt.Sprintf("-%s", provider))
return strings.TrimPrefix(keys[0], fmt.Sprintf("%s-", provider))
}
// HandleMessage 处理从 NSQ 读取的消息
+1 -1
View File
@@ -267,7 +267,7 @@ func (i *imlLocalModelController) initAILocalService(ctx context.Context, model
})
return func() error {
path := fmt.Sprintf("/%s/chat", strings.Trim(prefix, "/"))
path := fmt.Sprintf("/%s/chat/completions", strings.Trim(prefix, "/"))
timeout := 300000
retry := 0
aiPrompt := &ai_api_dto.AiPrompt{
+21 -21
View File
@@ -395,36 +395,36 @@ func (i *imlInitController) createAIService(ctx context.Context, teamID string,
if err != nil {
return err
}
path := fmt.Sprintf("/%s/demo_translation_api", strings.Trim(input.Prefix, "/"))
path := fmt.Sprintf("/%s/chat/completions", strings.Trim(input.Prefix, "/"))
timeout := 300000
retry := 0
aiPrompt := &ai_api_dto.AiPrompt{
Variables: []*ai_api_dto.AiPromptVariable{
{
Key: "source_lang",
Description: "",
Require: true,
},
{
Key: "target_lang",
Description: "",
Require: true,
},
{
Key: "text",
Description: "",
Require: true,
},
},
Prompt: "You need to translate {{source_lang}} into {{target_lang}}, and the following is the content that needs to be translated.\n---\n{{text}}",
//Variables: []*ai_api_dto.AiPromptVariable{
// {
// Key: "source_lang",
// Description: "",
// Require: true,
// },
// {
// Key: "target_lang",
// Description: "",
// Require: true,
// },
// {
// Key: "text",
// Description: "",
// Require: true,
// },
//},
//Prompt: "You need to translate {{source_lang}} into {{target_lang}}, and the following is the content that needs to be translated.\n---\n{{text}}",
}
aiModel := &ai_api_dto.AiModel{
Id: m.ID(),
Config: m.DefaultConfig(),
Provider: providerId,
}
name := "Demo Translation API"
description := "A demo that shows you how to use a prompt to create a Translation API."
name := "Demo Chat API"
description := "A demo that shows you how to use a prompt to create a Chat API."
apiId := uuid.New().String()
err = i.aiAPIModule.Create(
ctx,
+20
View File
@@ -23,12 +23,32 @@ func genOperation(summary string, description string, variables []*ai_api_dto.Ai
operation := openapi3.NewOperation()
operation.Summary = summary
operation.Description = description
operation.AddParameter(&openapi3.Parameter{
Name: "Authorization",
In: "header",
Required: true,
Example: "{your_apipark_apikey}",
})
operation.RequestBody = genRequestBody(variables)
operation.Responses = &openapi3.Responses{}
operation.Responses.Set("200", genResponse())
return operation
}
func genRequestHeaders() openapi3.Parameters {
return openapi3.Parameters{
{
Value: &openapi3.Parameter{
Name: "Authorization",
In: "header",
Description: "your_apipark_apikey", // 替换Prompt的变量列表
Required: true,
Example: "your_apipark_apikey",
},
},
}
}
func genRequestParameters(variables []*ai_api_dto.AiPromptVariable) openapi3.Parameters {
return openapi3.Parameters{
{
+8 -2
View File
@@ -268,7 +268,8 @@ func (i *imlBalanceModule) getLocalBalances(ctx context.Context, v string) ([]*g
var has bool
v, has = i.settingService.Get(ctx, "system.ai_model.ollama_address")
if !has {
return nil, fmt.Errorf("ollama address not found")
//return nil, fmt.Errorf("ollama address not found")
return nil, nil
}
}
@@ -294,7 +295,8 @@ func (i *imlBalanceModule) getBalances(ctx context.Context) ([]*gateway.DynamicR
}
v, has := i.settingService.Get(ctx, "system.ai_model.ollama_address")
if !has {
return nil, fmt.Errorf("ollama address not found")
//return nil, fmt.Errorf("ollama address not found")
return nil, nil
}
releases := make([]*gateway.DynamicRelease, 0, len(balances))
for _, item := range balances {
@@ -305,6 +307,10 @@ func (i *imlBalanceModule) getBalances(ctx context.Context) ([]*gateway.DynamicR
continue
}
base = fmt.Sprintf("%s://%s%s", p.URI().Scheme(), p.URI().Host(), p.URI().Path())
} else {
if v == "" {
continue
}
}
releases = append(releases, newRelease(item, base))
}
+2 -1
View File
@@ -614,7 +614,8 @@ func (i *imlLocalModel) getLocalModels(ctx context.Context, v string) ([]*gatewa
var has bool
v, has = i.settingService.Get(ctx, "system.ai_model.ollama_address")
if !has {
return nil, errors.New("ollama_address not set")
//return nil, errors.New("ollama_address not set")
return nil, nil
}
}
provider := ai_provider_local.ProviderLocal
+6 -1
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"sort"
"strings"
"time"
ai_model "github.com/APIParkLab/APIPark/service/ai-model"
@@ -200,6 +201,10 @@ func (i *imlProviderModule) Delete(ctx context.Context, id string) error {
}
func (i *imlProviderModule) AddProvider(ctx context.Context, input *ai_dto.NewProvider) (*ai_dto.SimpleProvider, error) {
_, has := model_runtime.GetProvider(strings.ToLower(input.Name))
if has {
return nil, fmt.Errorf("provider `%s` duplicate", input.Name)
}
// uuid = name
if has := i.providerService.CheckUuidDuplicate(ctx, input.Name); has {
return nil, fmt.Errorf("provider `%s` duplicate", input.Name)
@@ -241,7 +246,7 @@ func (i *imlProviderModule) SimpleProvider(ctx context.Context, id string) (*ai_
func (i *imlProviderModule) ConfiguredProviders(ctx context.Context, keyword string) ([]*ai_dto.ConfiguredProviderItem, error) {
// 获取已配置的AI服务商
list, err := i.providerService.Search(ctx, keyword, nil, "update_at")
list, err := i.providerService.Search(ctx, keyword, nil, "update_at desc")
if err != nil {
return nil, fmt.Errorf("get provider list error:%v", err)
}
+28 -7
View File
@@ -396,15 +396,36 @@ func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *ser
}
}
}
err := i.serviceModelMappingService.Save(ctx, &service_model_mapping.Save{
Sid: input.Id,
Content: input.ModelMapping,
})
err := i.serviceService.Create(ctx, mo)
if err != nil {
return err
}
return i.serviceService.Create(ctx, mo)
if input.ModelMapping != "" {
m := make(map[string]string)
err = json.Unmarshal([]byte(input.ModelMapping), &m)
if err != nil {
return err
}
err = i.serviceModelMappingService.Save(ctx, &service_model_mapping.Save{
Sid: input.Id,
Content: input.ModelMapping,
})
if err != nil {
return err
}
client, err := i.clusterService.GatewayClient(ctx, cluster.DefaultClusterID)
if err != nil {
return err
}
err = client.Hash().Online(ctx, &gateway.HashRelease{
HashKey: fmt.Sprintf("%s:%s", gateway.KeyServiceMapping, input.Id),
HashMap: m,
})
if err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
@@ -473,7 +494,7 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d
}
}
if input.ModelMapping != nil {
if input.ModelMapping != nil && *input.ModelMapping != "" {
m := make(map[string]string)
err = json.Unmarshal([]byte(*input.ModelMapping), &m)
if err != nil {