Skip to content

Commit

Permalink
多次调用解决 AI 评测问题
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Sep 27, 2024
1 parent f5225a1 commit 66d4734
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 249 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ cover.out

local_test.go

.DS_Store
.DS_Store
*.jsonl
local/
31 changes: 3 additions & 28 deletions internal/ai/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ai

import (
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/biz"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/config"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/credit"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/log"
Expand All @@ -25,14 +24,9 @@ import (
"github.com/gotomicro/ego/core/econf"
)

func InitHandlerFacade(common []handler.Builder,
zhipu *zhipu.Handler) *biz.FacadeHandler {
que := InitQuestionExamineHandler(common, zhipu)
c := InitCaseExamineHandler(common, zhipu)
return biz.NewHandler(map[string]handler.Handler{
que.Biz(): que,
c.Biz(): c,
})
func InitCompositionHandlerUsingZhipu(common []handler.Builder,
root *zhipu.Handler) handler.Handler {
return handler.NewCompositionHandler(common, root)
}

func InitZhipu() *zhipu.Handler {
Expand All @@ -51,25 +45,6 @@ func InitZhipu() *zhipu.Handler {
return h
}

func InitQuestionExamineHandler(
common []handler.Builder,
// platform 就是真正的出口
platform handler.Handler) *biz.CompositionHandler {
// log -> cfg -> credit -> record -> question_examine -> platform
builder := biz.NewQuestionExamineBizHandlerBuilder()
common = append(common, builder)
return biz.NewCombinedBizHandler("question_examine", common, platform)

}
func InitCaseExamineHandler(
common []handler.Builder,
// platform 就是真正的出口
platform handler.Handler) *biz.CompositionHandler {
builder := biz.NewCaseExamineBizHandlerBuilder()
common = append(common, builder)
return biz.NewCombinedBizHandler("case_examine", common, platform)
}

func InitCommonHandlers(log *log.HandlerBuilder,
cfg *config.HandlerBuilder,
credit *credit.HandlerBuilder,
Expand Down
21 changes: 19 additions & 2 deletions internal/ai/internal/domain/llm.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package domain

import (
"fmt"

"github.com/ecodeclub/ekit/slice"
)

const BizQuestionExamine = "question_examine"
const BizCaseExamine = "case_examine"

Expand All @@ -10,10 +16,21 @@ type LLMRequest struct {
Tid string
// 用户的输入
Input []string
// Prompt 将 input 和 PromptTemplate 结合之后生成的正儿八经的 Prompt
Prompt string
// 业务相关的配置
Config BizConfig

// prompt 将 input 和 PromptTemplate 结合之后生成的正儿八经的 Prompt
prompt string
}

func (req LLMRequest) Prompt() string {
if req.prompt == "" {
args := slice.Map(req.Input, func(idx int, src string) any {
return src
})
req.prompt = fmt.Sprintf(req.Config.PromptTemplate, args...)
}
return req.prompt
}

type LLMResponse struct {
Expand Down
13 changes: 6 additions & 7 deletions internal/ai/internal/integration/llm_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/ecodeclub/ekit/sqlx"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm"
llmHandler "github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler"
hdlmocks "github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/mocks"

"github.com/ecodeclub/webook/internal/ai/internal/domain"
Expand Down Expand Up @@ -85,7 +84,7 @@ func (s *LLMServiceSuite) TestService() {
testCases := []struct {
name string
req domain.LLMRequest
before func(t *testing.T, ctrl *gomock.Controller) (llmHandler.Handler, credit.Service)
before func(t *testing.T, ctrl *gomock.Controller) (*hdlmocks.MockHandler, credit.Service)
assertFunc assert.ErrorAssertionFunc
after func(t *testing.T, resp domain.LLMResponse)
}{
Expand All @@ -103,7 +102,7 @@ func (s *LLMServiceSuite) TestService() {
},
assertFunc: assert.NoError,
before: func(t *testing.T,
ctrl *gomock.Controller) (llmHandler.Handler, credit.Service) {
ctrl *gomock.Controller) (*hdlmocks.MockHandler, credit.Service) {
llmHdl := hdlmocks.NewMockHandler(ctrl)
llmHdl.EXPECT().Handle(gomock.Any(), gomock.Any()).
Return(domain.LLMResponse{
Expand Down Expand Up @@ -179,7 +178,7 @@ func (s *LLMServiceSuite) TestService() {
},
assertFunc: assert.NoError,
before: func(t *testing.T,
ctrl *gomock.Controller) (llmHandler.Handler, credit.Service) {
ctrl *gomock.Controller) (*hdlmocks.MockHandler, credit.Service) {
llmHdl := hdlmocks.NewMockHandler(ctrl)
llmHdl.EXPECT().Handle(gomock.Any(), gomock.Any()).
Return(domain.LLMResponse{
Expand Down Expand Up @@ -254,7 +253,7 @@ func (s *LLMServiceSuite) TestService() {
},
},
before: func(t *testing.T,
ctrl *gomock.Controller) (llmHandler.Handler, credit.Service) {
ctrl *gomock.Controller) (*hdlmocks.MockHandler, credit.Service) {
llmHdl := hdlmocks.NewMockHandler(ctrl)
creditSvc := creditmocks.NewMockService(ctrl)
creditSvc.EXPECT().GetCreditsByUID(gomock.Any(), gomock.Any()).Return(credit.Credit{
Expand Down Expand Up @@ -301,7 +300,7 @@ func (s *LLMServiceSuite) TestService() {
},
},
before: func(t *testing.T,
ctrl *gomock.Controller) (llmHandler.Handler, credit.Service) {
ctrl *gomock.Controller) (*hdlmocks.MockHandler, credit.Service) {
llmHdl := hdlmocks.NewMockHandler(ctrl)
llmHdl.EXPECT().Handle(gomock.Any(), gomock.Any()).
Return(domain.LLMResponse{}, errors.New("调用失败"))
Expand Down Expand Up @@ -366,7 +365,7 @@ func (s *LLMServiceSuite) TestService() {
},
assertFunc: assert.Error,
before: func(t *testing.T,
ctrl *gomock.Controller) (llmHandler.Handler, credit.Service) {
ctrl *gomock.Controller) (*hdlmocks.MockHandler, credit.Service) {
llmHdl := hdlmocks.NewMockHandler(ctrl)
llmHdl.EXPECT().Handle(gomock.Any(), gomock.Any()).
Return(domain.LLMResponse{
Expand Down
16 changes: 6 additions & 10 deletions internal/ai/internal/integration/startup/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package startup
import (
"sync"

hdlmocks "github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/mocks"

"github.com/ecodeclub/webook/internal/ai"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/biz"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/config"
aicredit "github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/credit"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler/log"
Expand All @@ -23,7 +24,7 @@ import (
)

func InitModule(db *egorm.Component,
hdl handler.Handler,
hdl *hdlmocks.MockHandler,
creditSvc *credit.Module) (*ai.Module, error) {
wire.Build(
llm.NewLLMService,
Expand All @@ -41,21 +42,16 @@ func InitModule(db *egorm.Component,
aicredit.NewHandlerBuilder,

ai.InitCommonHandlers,
InitHandlerFacade,
InitRootHandler,

wire.Struct(new(ai.Module), "*"),
wire.FieldsOf(new(*credit.Module), "Svc"),
)
return new(ai.Module), nil
}

func InitHandlerFacade(common []handler.Builder, llm handler.Handler) *biz.FacadeHandler {
que := ai.InitQuestionExamineHandler(common, llm)
ca := ai.InitCaseExamineHandler(common, llm)
return biz.NewHandler(map[string]handler.Handler{
ca.Biz(): ca,
que.Biz(): que,
})
func InitRootHandler(common []handler.Builder, hdl *hdlmocks.MockHandler) handler.Handler {
return handler.NewCompositionHandler(common, hdl)
}

var daoOnce = sync.Once{}
Expand Down
19 changes: 7 additions & 12 deletions internal/ai/internal/integration/startup/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 0 additions & 34 deletions internal/ai/internal/service/llm/handler/biz/case_examine.go

This file was deleted.

33 changes: 0 additions & 33 deletions internal/ai/internal/service/llm/handler/biz/facade.go

This file was deleted.

48 changes: 0 additions & 48 deletions internal/ai/internal/service/llm/handler/biz/question_examine.go

This file was deleted.

Loading

0 comments on commit 66d4734

Please sign in to comment.