Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:ecodeclub/webook into mitten_add_cor…
Browse files Browse the repository at this point in the history
…rect_post
  • Loading branch information
倪诗梦 authored and 倪诗梦 committed Aug 11, 2024
2 parents ce555bd + 989415e commit b43528d
Show file tree
Hide file tree
Showing 79 changed files with 4,628 additions and 857 deletions.
12 changes: 10 additions & 2 deletions internal/ai/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,16 @@ func InitQuestionExamineHandler(
// log -> cfg -> credit -> record -> question_examine -> platform
builder := biz.NewQuestionExamineBizHandlerBuilder()
common = append(common, builder)
res := biz.NewCombinedBizHandler("question_examine", common, platform)
return res
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,
Expand Down
1 change: 1 addition & 0 deletions internal/ai/internal/domain/llm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package domain

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

type LLMRequest struct {
Biz string
Expand Down
89 changes: 87 additions & 2 deletions internal/ai/internal/integration/llm_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s *LLMServiceSuite) SetupSuite() {
db := testioc.InitDB()
s.db = db
err := dao.InitTables(db)
require.NoError(s.T(), err)
s.NoError(err)
s.logDao = dao.NewGORMLLMLogDAO(db)

// 先插入 BizConfig
Expand All @@ -56,7 +56,16 @@ func (s *LLMServiceSuite) SetupSuite() {
Ctime: now,
Utime: now,
}).Error
assert.NoError(s.T(), err)
s.NoError(err)
err = s.db.Create(&dao.BizConfig{
Biz: domain.BizCaseExamine,
MaxInput: 100,
PromptTemplate: "这是案例 %s,这是用户输入 %s",
KnowledgeId: knowledgeId,
Ctime: now,
Utime: now,
}).Error
s.NoError(err)
}

func (s *LLMServiceSuite) TearDownSuite() {
Expand Down Expand Up @@ -154,6 +163,82 @@ func (s *LLMServiceSuite) TestService() {
}, creditLogModel)
},
},
{
name: "案例测试-成功",
req: domain.LLMRequest{
Biz: domain.BizCaseExamine,
Uid: 123,
Tid: "13",
Input: []string{
"案例1",
"用户输入1",
},
},
assertFunc: assert.NoError,
before: func(t *testing.T,
ctrl *gomock.Controller) (llmHandler.Handler, credit.Service) {
llmHdl := hdlmocks.NewMockHandler(ctrl)
llmHdl.EXPECT().Handle(gomock.Any(), gomock.Any()).
Return(domain.LLMResponse{
Tokens: 100,
Amount: 100,
Answer: "aians",
}, nil)
creditSvc := creditmocks.NewMockService(ctrl)
creditSvc.EXPECT().GetCreditsByUID(gomock.Any(), gomock.Any()).Return(credit.Credit{
TotalAmount: 1000,
}, nil)
creditSvc.EXPECT().TryDeductCredits(gomock.Any(), gomock.Any()).Return(11, nil)
creditSvc.EXPECT().ConfirmDeductCredits(gomock.Any(), int64(123), int64(11)).Return(nil)
return llmHdl, creditSvc
},
after: func(t *testing.T, resp domain.LLMResponse) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
// 校验response写入的内容是否正确
assert.Equal(t, domain.LLMResponse{
Tokens: 100,
Amount: 100,
Answer: "aians",
}, resp)
var logModel dao.LLMRecord
err := s.db.WithContext(ctx).Where("tid = ?", "13").First(&logModel).Error
require.NoError(t, err)
logModel.Id = 0
s.assertLog(dao.LLMRecord{
Id: 0,
Tid: "13",
Uid: 123,
Biz: domain.BizCaseExamine,
Tokens: 100,
Amount: 100,
KnowledgeId: knowledgeId,
Input: sqlx.JsonColumn[[]string]{
Valid: true,
Val: []string{
"案例1",
"用户输入1",
},
},
Status: 1,
PromptTemplate: sqlx.NewNullString("这是案例 %s,这是用户输入 %s"),
Answer: sqlx.NewNullString("aians"),
}, logModel)
// 校验credit写入的内容是否正确
var creditLogModel dao.LLMCredit
err = s.db.WithContext(ctx).Where("tid = ?", "13").First(&creditLogModel).Error
require.NoError(t, err)
assert.True(t, creditLogModel.Id != 0)
creditLogModel.Id = 0
s.assertCreditLog(dao.LLMCredit{
Tid: "13",
Uid: 123,
Biz: domain.BizCaseExamine,
Amount: 100,
Status: 1,
}, creditLogModel)
},
},
{
name: "积分不足",
req: domain.LLMRequest{
Expand Down
2 changes: 2 additions & 0 deletions internal/ai/internal/integration/startup/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func InitModule(db *egorm.Component,

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,
})
}
Expand Down
4 changes: 3 additions & 1 deletion 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.

33 changes: 33 additions & 0 deletions internal/ai/internal/service/llm/handler/biz/case_examine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package biz

import (
"context"
"fmt"
"unicode/utf8"

"github.com/ecodeclub/webook/internal/ai/internal/domain"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm/handler"
)

type CaseExamineBizHandlerBuilder struct {
}

func NewCaseExamineBizHandlerBuilder() *CaseExamineBizHandlerBuilder {
return &CaseExamineBizHandlerBuilder{}
}

func (h *CaseExamineBizHandlerBuilder) Next(next handler.Handler) handler.Handler {
return handler.HandleFunc(func(ctx context.Context, req domain.LLMRequest) (domain.LLMResponse, error) {
title := req.Input[0]
userInput := req.Input[1]
userInputLen := utf8.RuneCount([]byte(userInput))

if userInputLen > req.Config.MaxInput {
return domain.LLMResponse{}, fmt.Errorf("输入太长,最常不超过 %d,现有长度 %d", req.Config.MaxInput, userInputLen)
}
// 把 input 和 prompt 结合起来
prompt := fmt.Sprintf(req.Config.PromptTemplate, title, userInput)
req.Prompt = prompt
return next.Handle(ctx, req)
})
}
56 changes: 39 additions & 17 deletions internal/bff/internal/integration/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (c *CollectionHandlerTestSuite) SetupSuite() {
Biz: web.QuestionSetBiz,
QuestionSet: 4,
},
{
Biz: web.CaseSetBiz,
CaseSet: 5,
},
}, nil
}).AnyTimes()
queSvc.EXPECT().GetPubByIDs(gomock.Any(), gomock.Any()).
Expand Down Expand Up @@ -112,7 +116,19 @@ func (c *CollectionHandlerTestSuite) SetupSuite() {
}
}), nil
}).AnyTimes()
handler, _ := st.InitHandler(intrSvc, caseSvc, queSvc, queSetSvc, examSvc)
caseSetSvc := casemocks.NewMockCaseSetService(ctrl)
caseSetSvc.EXPECT().GetByIds(gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, ids []int64) ([]cases.CaseSet, error) {
return slice.Map(ids, func(idx int, src int64) cases.CaseSet {
return cases.CaseSet{
ID: src,
Title: fmt.Sprintf("这是案例集%d", src),
}
}), nil
}).AnyTimes()
handler, _ := st.InitHandler(&interactive.Module{Svc: intrSvc},
&cases.Module{Svc: caseSvc, SetSvc: caseSetSvc},
&baguwen.Module{Svc: queSvc, SetSvc: queSetSvc, ExamSvc: examSvc})
econf.Set("server", map[string]any{"contextTimeout": "1s"})
server := egin.Load("server").Build()
server.Use(func(ctx *gin.Context) {
Expand All @@ -128,7 +144,7 @@ func (c *CollectionHandlerTestSuite) SetupSuite() {
func (c *CollectionHandlerTestSuite) Test_Handler() {
t := c.T()
req, err := http.NewRequest(http.MethodPost,
"/interactive/collection/info", iox.NewJSONReader(web.CollectionInfoReq{
"/interactive/collection/records", iox.NewJSONReader(web.CollectionInfoReq{
ID: 1,
Offset: 0,
Limit: 10,
Expand All @@ -147,9 +163,9 @@ func (c *CollectionHandlerTestSuite) Test_Handler() {
},
{
Question: web.Question{
ID: 2,
Title: "这是题目2",
Result: 2 % 4,
ID: 2,
Title: "这是题目2",
ExamineResult: 2 % 4,
},
},
{
Expand All @@ -158,14 +174,14 @@ func (c *CollectionHandlerTestSuite) Test_Handler() {
Title: "这是题集3",
Questions: []web.Question{
{
ID: 33,
Title: "这是题目33",
Result: 33 % 4,
ID: 33,
Title: "这是题目33",
ExamineResult: 33 % 4,
},
{
ID: 36,
Title: "这是题目36",
Result: 36 % 4,
ID: 36,
Title: "这是题目36",
ExamineResult: 36 % 4,
},
},
},
Expand All @@ -176,18 +192,24 @@ func (c *CollectionHandlerTestSuite) Test_Handler() {
Title: "这是题集4",
Questions: []web.Question{
{
ID: 44,
Title: "这是题目44",
Result: 44 % 4,
ID: 44,
Title: "这是题目44",
ExamineResult: 44 % 4,
},
{
ID: 48,
Title: "这是题目48",
Result: 48 % 4,
ID: 48,
Title: "这是题目48",
ExamineResult: 48 % 4,
},
},
},
},
{
CaseSet: web.CaseSet{
ID: 5,
Title: "这是案例集5",
},
},
}, recorder.MustScan().Data)

}
Expand Down
11 changes: 6 additions & 5 deletions internal/bff/internal/integration/startup/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
"github.com/google/wire"
)

func InitHandler(intrSvc interactive.Service,
caseSvc cases.Service,
queSvc baguwen.Service,
queSetSvc baguwen.QuestionSetService,
examSvc baguwen.ExamService) (*web.Handler, error) {
func InitHandler(intrModule *interactive.Module,
caseModule *cases.Module,
queSvc *baguwen.Module) (*web.Handler, error) {
wire.Build(
web.NewHandler,
wire.FieldsOf(new(*interactive.Module), "Svc"),
wire.FieldsOf(new(*baguwen.Module), "Svc", "SetSvc", "ExamSvc"),
wire.FieldsOf(new(*cases.Module), "Svc", "SetSvc"),
)
return new(web.Handler), nil
}
Expand Down
16 changes: 11 additions & 5 deletions internal/bff/internal/integration/startup/wire_gen.go

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

Loading

0 comments on commit b43528d

Please sign in to comment.