Skip to content

Commit 69da7eb

Browse files
添加functioncall抽象 (#38)
1 parent 90ecfe5 commit 69da7eb

24 files changed

+796
-64
lines changed

.DS_Store

-8 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ logs
3131
.sys
3232
**/local.yaml
3333
.tool-versions
34+
.DS_Store

cmd/platform/ioc/type.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ package ioc
1717
import (
1818
"github.com/ecodeclub/ai-gateway-go/internal/admin"
1919
igrpc "github.com/ecodeclub/ai-gateway-go/internal/grpc"
20+
"github.com/ecodeclub/ai-gateway-go/internal/pkg/template"
2021
"github.com/ecodeclub/ai-gateway-go/internal/repository"
2122
"github.com/ecodeclub/ai-gateway-go/internal/repository/cache"
2223
"github.com/ecodeclub/ai-gateway-go/internal/repository/dao"
2324
"github.com/ecodeclub/ai-gateway-go/internal/service"
25+
"github.com/ecodeclub/ai-gateway-go/internal/service/llm/fcall/invoke_llm"
2426
"github.com/google/wire"
2527
"github.com/gotomicro/ego/server/egin"
2628
"github.com/gotomicro/ego/server/egrpc"
@@ -59,16 +61,24 @@ var (
5961
service.NewBizConfigService,
6062
admin.NewBizConfigHandler,
6163
)
62-
6364
ProviderSet = wire.NewSet(
6465
dao.NewProviderDAO,
6566
repository.NewProviderRepository,
6667
InitProvider,
6768
admin.NewProviderHandler,
6869
)
70+
InvokeLLmSet = wire.NewSet(
71+
InitRender,
72+
invoke_llm.NewFcall,
73+
)
74+
6975
MockSet = wire.NewSet(admin.NewMockHandler)
7076
)
7177

78+
func InitRender() *template.DefaultRender {
79+
return template.NewDefaultRender(template.DefaultConfig())
80+
}
81+
7282
type App struct {
7383
GrpcSever *egrpc.Component
7484
GinServer *egin.Component

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ require (
99
github.com/ego-component/egorm v1.1.4
1010
github.com/gin-contrib/cors v1.7.6
1111
github.com/gin-gonic/gin v1.10.1
12+
github.com/google/uuid v1.6.0
1213
github.com/google/wire v0.6.0
1314
github.com/gotomicro/ego v1.2.3
1415
github.com/openai/openai-go v1.8.1
16+
github.com/pkg/errors v0.9.1
1517
github.com/redis/go-redis/v9 v9.3.0
1618
github.com/stretchr/testify v1.10.0
19+
github.com/tidwall/gjson v1.14.4
1720
go.uber.org/mock v0.3.0
1821
go.uber.org/zap v1.27.0
1922
golang.org/x/net v0.42.0
@@ -64,7 +67,6 @@ require (
6467
github.com/golang/protobuf v1.5.4 // indirect
6568
github.com/google/cel-go v0.11.3 // indirect
6669
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
67-
github.com/google/uuid v1.6.0 // indirect
6870
github.com/gotomicro/logrotate v0.0.0-20211108034117-46d53eedc960 // indirect
6971
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
7072
github.com/hashicorp/go-version v1.4.0 // indirect
@@ -90,7 +92,6 @@ require (
9092
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
9193
github.com/modern-go/reflect2 v1.0.2 // indirect
9294
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
93-
github.com/pkg/errors v0.9.1 // indirect
9495
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
9596
github.com/prometheus/client_golang v1.12.1 // indirect
9697
github.com/prometheus/client_model v0.2.0 // indirect
@@ -102,7 +103,6 @@ require (
102103
github.com/shirou/gopsutil/v3 v3.21.6 // indirect
103104
github.com/spf13/cast v1.4.1 // indirect
104105
github.com/stoewer/go-strcase v1.2.0 // indirect
105-
github.com/tidwall/gjson v1.14.4 // indirect
106106
github.com/tidwall/match v1.1.1 // indirect
107107
github.com/tidwall/pretty v1.2.1 // indirect
108108
github.com/tidwall/sjson v1.2.5 // indirect

internal/admin/invocation_config_vo.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ type InvocationConfigVersionVO struct {
5151
ModelProviderID int64 `json:"modelProviderID"`
5252
ModelProviderName string `json:"modelProviderName"`
5353

54-
Version string `json:"version"`
55-
Prompt string `json:"prompt"`
56-
SystemPrompt string `json:"systemPrompt"`
57-
JSONSchema string `json:"jsonSchema"`
58-
Attributes map[string]any `json:"attributes,omitempty"`
59-
Functions []FunctionVO `json:"functions,omitempty"`
60-
Temperature float32 `json:"temperature"`
61-
TopP float32 `json:"topP"`
62-
MaxTokens int `json:"maxTokens"`
63-
Status string `json:"status"`
64-
Ctime int64 `json:"ctime"`
65-
Utime int64 `json:"utime"`
54+
Version string `json:"version"`
55+
Prompt string `json:"prompt"`
56+
SystemPrompt string `json:"systemPrompt"`
57+
JSONSchema string `json:"jsonSchema"`
58+
Attributes domain.Attributes `json:"attributes,omitempty"`
59+
Functions []FunctionVO `json:"functions,omitempty"`
60+
Temperature float32 `json:"temperature"`
61+
TopP float32 `json:"topP"`
62+
MaxTokens int `json:"maxTokens"`
63+
Status string `json:"status"`
64+
Ctime int64 `json:"ctime"`
65+
Utime int64 `json:"utime"`
6666
}
6767

6868
type FunctionVO struct {

internal/domain/invocation_config.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
package domain
1616

1717
import (
18+
"encoding/json"
1819
"time"
20+
21+
"github.com/tidwall/gjson"
1922
)
2023

2124
type OwnerType string
@@ -65,7 +68,7 @@ type InvocationConfigVersion struct {
6568
Prompt string
6669
SystemPrompt string
6770
JSONSchema string
68-
Attributes map[string]any
71+
Attributes Attributes
6972
Functions []Function
7073
Temperature float32
7174
TopP float32
@@ -75,6 +78,24 @@ type InvocationConfigVersion struct {
7578
Utime time.Time
7679
}
7780

81+
type Attributes map[string]any
82+
83+
func (a Attributes) GetAttribute(expr string) map[string]any {
84+
if expr == "" {
85+
return a
86+
}
87+
attrJson := a.toJson()
88+
res := gjson.Get(attrJson, expr)
89+
attr := make(map[string]any)
90+
_ = json.Unmarshal([]byte(res.Raw), &attr)
91+
return attr
92+
}
93+
94+
func (a Attributes) toJson() string {
95+
aByte, _ := json.Marshal(a)
96+
return string(aByte)
97+
}
98+
7899
type Function struct {
79100
Name string `json:"name"`
80101
Definition string `json:"definition"`
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package template
2+
3+
import (
4+
"context"
5+
"testing"
6+
)
7+
8+
func TestFCall(t *testing.T) {
9+
ctx := NewContext(context.Background())
10+
if err := ctx.SetVariable(NewVariable("level", "advance")); err != nil {
11+
t.Fatalf("设置变量失败: %v", err)
12+
}
13+
14+
r := NewDefaultRender(DefaultConfig())
15+
// 使用内置 replace(old, new, s) 函数,把模板中的占位符 ${level} 替换为变量 .level
16+
tpl := `{{ replace "${level}" .level "{{ .name.${level} }}" }}`
17+
18+
out, err := r.Render(ctx, tpl)
19+
if err != nil {
20+
t.Fatalf("渲染失败: %v", err)
21+
}
22+
23+
expected := "{{ .name.advance }}"
24+
if out != expected {
25+
t.Fatalf("期望 %q, 实际 %q", expected, out)
26+
}
27+
}

internal/pkg/template/variable.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package template
1616

1717
// Variable 变量接口,简洁的名称+值设计
1818
type Variable interface {
19-
// Name 返回变量名称,用于模板中访问 {{ .name.xxx }}
19+
// Name 返回变量名称,用于模板中访问
2020
Name() string
2121
// Value 返回变量值,支持错误返回
2222
Value() (any, error)

internal/repository/dao/invocation_config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,13 @@ func (p *InvocationConfigDAO) ActivateVersion(ctx context.Context, id int64) err
154154
}).Error
155155
})
156156
}
157+
158+
func (p *InvocationConfigDAO) ActiveVersion(ctx context.Context, id int64) (InvocationConfigVersion, error) {
159+
var version InvocationConfigVersion
160+
db := p.db
161+
err := db.WithContext(ctx).
162+
Where("inv_id = ? and status = ? ", id, domain.InvocationCfgVersionStatusActive.String()).
163+
Order("utime desc").
164+
First(&version).Error
165+
return version, err
166+
}

internal/repository/invocation_config.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"encoding/json"
2121
"time"
2222

23+
"golang.org/x/sync/errgroup"
24+
2325
"github.com/ecodeclub/ekit/slice"
2426

2527
"github.com/ecodeclub/ai-gateway-go/internal/domain"
@@ -173,3 +175,31 @@ func (p *InvocationConfigRepo) GetVersionByID(ctx context.Context, id int64) (do
173175
func (p *InvocationConfigRepo) ActivateVersion(ctx context.Context, id int64) error {
174176
return p.dao.ActivateVersion(ctx, id)
175177
}
178+
179+
// GetActiveVersionByID 获取
180+
func (p *InvocationConfigRepo) GetActiveVersionByID(ctx context.Context, id int64) (domain.InvocationConfigVersion, error) {
181+
var (
182+
eg errgroup.Group
183+
config dao.InvocationConfig
184+
configVersion dao.InvocationConfigVersion
185+
)
186+
187+
eg.Go(func() error {
188+
var err error
189+
config, err = p.dao.GetByID(ctx, id)
190+
return err
191+
})
192+
193+
eg.Go(func() error {
194+
var err error
195+
configVersion, err = p.dao.ActiveVersion(ctx, id)
196+
return err
197+
})
198+
if err := eg.Wait(); err != nil {
199+
return domain.InvocationConfigVersion{}, err
200+
}
201+
domainCfg := p.toDomain(config)
202+
versionCfg := p.toDomainVersion(configVersion)
203+
versionCfg.Config = domainCfg
204+
return versionCfg, nil
205+
}

0 commit comments

Comments
 (0)