Skip to content

Commit

Permalink
调整岗位分析返回数据格式
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Dec 5, 2024
1 parent f2b23b5 commit e2e4462
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 42 deletions.
6 changes: 3 additions & 3 deletions internal/ai/internal/domain/jd.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ type JDEvaluation struct {

type JD struct {
Amount int64
TechScore *JDEvaluation
BizScore *JDEvaluation
PosScore *JDEvaluation
TechScore JDEvaluation
BizScore JDEvaluation
PosScore JDEvaluation
// 潜台词
Subtext string
}
21 changes: 9 additions & 12 deletions internal/ai/internal/integration/llm_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,22 +655,19 @@ func (s *LLMServiceSuite) TestHandler_AnalysisJD() {
return domain.LLMResponse{
Tokens: 1000,
Amount: 100,
Answer: `score: 6
这是技术前景`,
Answer: `{"score":6, "summary":["这是技术前景"]}`,
}, nil
case domain.AnalysisJDBiz:
return domain.LLMResponse{
Tokens: 100,
Amount: 200,
Answer: `score: 7
这是业务前景`,
Answer: `{"score":7, "summary":["这是业务前景"]}`,
}, nil
case domain.AnalysisJDPosition:
return domain.LLMResponse{
Tokens: 100,
Amount: 100,
Answer: `score: 8
这是公司地位`,
Answer: `{"score":8, "summary":["这是公司地位"]}`,
}, nil
case domain.AnalysisJDSubtext:
return domain.LLMResponse{
Expand All @@ -694,17 +691,17 @@ func (s *LLMServiceSuite) TestHandler_AnalysisJD() {
// 校验response写入的内容是否正确
assert.Equal(t, web.JDResponse{
Amount: 500,
TechScore: &web.JDEvaluation{
TechScore: web.JDEvaluation{
Score: 6,
Analysis: "这是技术前景",
Analysis: "- 这是技术前景",
},
BizScore: &web.JDEvaluation{
BizScore: web.JDEvaluation{
Score: 7,
Analysis: "这是业务前景",
Analysis: "- 这是业务前景",
},
PosScore: &web.JDEvaluation{
PosScore: web.JDEvaluation{
Score: 8,
Analysis: "这是公司地位",
Analysis: "- 这是公司地位",
},
Subtext: "这是我的分析",
}, resp)
Expand Down
55 changes: 36 additions & 19 deletions internal/ai/internal/service/jd_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@ package service

import (
"context"
"errors"
"fmt"
"strconv"
"encoding/json"
"regexp"
"strings"
"sync/atomic"

"github.com/gotomicro/ego/core/elog"

"github.com/ecodeclub/webook/internal/ai/internal/domain"
"github.com/ecodeclub/webook/internal/ai/internal/service/llm"
"github.com/lithammer/shortuuid/v4"
"golang.org/x/sync/errgroup"
)

// 最简单的提取方式
const jsonExpr = `\{(.|\n|\r)+\}`

type JDService interface {
// Evaluate 测评
Evaluate(ctx context.Context, uid int64, jd string) (domain.JD, error)
}

type jdSvc struct {
aiSvc llm.Service
aiSvc llm.Service
logger *elog.Component
expr *regexp.Regexp
}

func NewJDService(aiSvc llm.Service) JDService {
return &jdSvc{
aiSvc: aiSvc,
aiSvc: aiSvc,
logger: elog.DefaultLogger,
expr: regexp.MustCompile(jsonExpr),
}
}

func (j *jdSvc) Evaluate(ctx context.Context, uid int64, jd string) (domain.JD, error) {
var techJD, bizJD, positionJD *domain.JDEvaluation
var techJD, bizJD, positionJD domain.JDEvaluation
var amount int64
var subtext string
var eg errgroup.Group
Expand Down Expand Up @@ -89,7 +97,7 @@ func (j *jdSvc) Evaluate(ctx context.Context, uid int64, jd string) (domain.JD,
}, nil
}

func (j *jdSvc) analysisJd(ctx context.Context, uid int64, biz string, jd string) (int64, *domain.JDEvaluation, error) {
func (j *jdSvc) analysisJd(ctx context.Context, uid int64, biz string, jd string) (int64, domain.JDEvaluation, error) {
tid := shortuuid.New()
aiReq := domain.LLMRequest{
Uid: uid,
Expand All @@ -99,20 +107,29 @@ func (j *jdSvc) analysisJd(ctx context.Context, uid int64, biz string, jd string
}
resp, err := j.aiSvc.Invoke(ctx, aiReq)
if err != nil {
return 0, nil, err
}
answer := strings.SplitN(resp.Answer, "\n", 2)
if len(answer) != 2 {
return 0, nil, errors.New("不符合预期的大模型响应")
return 0, domain.JDEvaluation{}, err
}
score := answer[0]
scoreNum, err := strconv.ParseFloat(strings.TrimSpace(strings.TrimPrefix(score, "score:")), 64)
jsonStr := j.expr.FindString(resp.Answer)
var (
scoreResp ScoreResp
analysis string
)
err = json.Unmarshal([]byte(jsonStr), &scoreResp)
if err != nil {
return 0, nil, fmt.Errorf("分数返回的数据不对 %s", score)
j.logger.Error("不符合预期的大模型响应",
elog.FieldErr(err),
elog.String("resp", resp.Answer))
} else {
analysis = "- " + strings.Join(scoreResp.Summary, "\n- ")
}

return resp.Amount, &domain.JDEvaluation{
Score: scoreNum,
Analysis: strings.TrimSpace(strings.TrimPrefix(answer[1], "analysis:")),
return resp.Amount, domain.JDEvaluation{
Score: scoreResp.Score,
// 按照 Markdown 的写法,拼接起来
Analysis: analysis,
}, nil
}

type ScoreResp struct {
Score float64 `json:"score"`
Summary []string `json:"summary"`
}
50 changes: 50 additions & 0 deletions internal/ai/internal/service/jd_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2023 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package service

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
)

// TestJSONExpression 测试利用正则表达式提取 JSON 串
func TestJSONExpression(t *testing.T) {
testCases := []struct {
name string
input string
want string
}{
{
name: "本身就是JSON",
input: `{"abc": "bcd"}`,
want: `{"abc": "bcd"}`,
},
{
name: "有前缀后缀",
input: "```json{\"abc\": \"bcd\"}```",
want: `{"abc": "bcd"}`,
},
}

expr := regexp.MustCompile(jsonExpr)
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
val := expr.FindString(tc.input)
assert.Equal(t, tc.want, val)
})
}
}
4 changes: 2 additions & 2 deletions internal/ai/internal/web/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (h *Handler) AnalysisJd(ctx *ginx.Context, req JDRequest, sess session.Sess

}

func (h *Handler) newJD(jd *domain.JDEvaluation) *JDEvaluation {
return &JDEvaluation{
func (h *Handler) newJD(jd domain.JDEvaluation) JDEvaluation {
return JDEvaluation{
Score: jd.Score,
Analysis: jd.Analysis,
}
Expand Down
10 changes: 5 additions & 5 deletions internal/ai/internal/web/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type JDRequest struct {
}

type JDResponse struct {
Amount int64 `json:"amount"`
TechScore *JDEvaluation `json:"techScore"`
BizScore *JDEvaluation `json:"bizScore"`
PosScore *JDEvaluation `json:"posScore"`
Subtext string `json:"subtext"`
Amount int64 `json:"amount"`
TechScore JDEvaluation `json:"techScore"`
BizScore JDEvaluation `json:"bizScore"`
PosScore JDEvaluation `json:"posScore"`
Subtext string `json:"subtext"`
}

type JDEvaluation struct {
Expand Down
4 changes: 3 additions & 1 deletion ioc/private/nonsense/non_sense_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package nonsense

import (
"log/slog"

"github.com/gin-gonic/gin"
)

// NonSenseV1
var NonSenseV1 gin.HandlerFunc = func(ct *gin.Context) {
// 啥也不做
println("hello")
slog.Debug("进来了 NonSenseV1")
}

0 comments on commit e2e4462

Please sign in to comment.