Skip to content

Commit

Permalink
AI 重构
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash committed Jul 18, 2024
1 parent 897f0bf commit cd2472e
Show file tree
Hide file tree
Showing 66 changed files with 1,568 additions and 1,222 deletions.
4 changes: 4 additions & 0 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ qywechat:
chatRobot:
webhookURL: "your/webhookURL"

zhipu:
apikey: ''
knowledgeId: ''

mysql:
dsn: "webook:webook@tcp(mysql8:3306)/webook?charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&loc=Local&timeout=1s&readTimeout=3s&writeTimeout=3s"

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/ecodeclub/webook

go 1.22.2

toolchain go1.22.5
go 1.22.5

require (
github.com/ecodeclub/ecache v0.0.0-20240111145855-75679834beca
Expand Down
68 changes: 68 additions & 0 deletions internal/ai/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 ai

import (
"github.com/ecodeclub/webook/internal/ai/internal/service/gpt/handler"
"github.com/ecodeclub/webook/internal/ai/internal/service/gpt/handler/biz"
"github.com/ecodeclub/webook/internal/ai/internal/service/gpt/handler/config"
"github.com/ecodeclub/webook/internal/ai/internal/service/gpt/handler/credit"
"github.com/ecodeclub/webook/internal/ai/internal/service/gpt/handler/gpt/zhipu"
"github.com/ecodeclub/webook/internal/ai/internal/service/gpt/handler/log"
"github.com/ecodeclub/webook/internal/ai/internal/service/gpt/handler/record"
"github.com/gotomicro/ego/core/econf"
)

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

func InitZhipu() *zhipu.Handler {
type Config struct {
APIKey string `yaml:"apikey"`
Price float64 `yaml:"price"`
}
var cfg Config
err := econf.UnmarshalKey("zhipu", &cfg)
if err != nil {
panic(err)
}
h, err := zhipu.NewHandler(cfg.APIKey, cfg.Price)
if err != nil {
panic(err)
}
return h
}

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

func InitCommonHandlers(log *log.HandlerBuilder,
cfg *config.HandlerBuilder,
credit *credit.HandlerBuilder,
record *record.HandlerBuilder) []handler.Builder {
return []handler.Builder{log, cfg, credit, record}
}
86 changes: 52 additions & 34 deletions internal/ai/internal/domain/gpt.go
Original file line number Diff line number Diff line change
@@ -1,72 +1,90 @@
package domain

const BizQuestionExamine = "question_examine"

type GPTRequest struct {
Biz string
Uid int64
// 请求id
Tid string
// 用户的输入
Input []string
BizConfig GPTBiz
Input []string
// Prompt 将 input 和 PromptTemplate 结合之后生成的正儿八经的 Prompt
Prompt string
// 业务相关的配置
Config BizConfig
}

type GPTResponse struct {
// 花费的token
Tokens int
Tokens int64
// 花费的金额
Amount int64
// gpt的回答
Answer string
}

type GPTBiz struct {
// 业务名称
Biz string
// 每个token的钱 分为单位
AmountPerToken float64
// 每个token的积分
CreditPerToken float64
// 一次最多返回多少Tokens
MaxTokensPerTime int
type BizConfig struct {
// 允许的最长输入
// 这里我们不用计算 token,只需要简单约束一下字符串长度就可以
MaxInput int
// 使用的知识库
KnowledgeId string
// 提示词。虽然这里只有一个 PromptTemplate 字段,
// 但是在部分业务里面,它是一个 json
// 这里一般使用 %s
// 后续考虑 key value 的形式
PromptTemplate string
}

type GPTCreditLog struct {
type GPTCredit struct {
Id int64
Tid string
Uid int64
Biz string
Tokens int64
Amount int64
Credit int64
Status GPTLogStatus
Prompt string
Answer string
Status CreditStatus
Ctime int64
Utime int64
}

type GPTLog struct {
Id int64
Tid string
Uid int64
Biz string
Tokens int64
Amount int64
Status GPTLogStatus
Prompt string
Answer string
Ctime int64
Utime int64
type GPTRecord struct {
Id int64
Tid string
Uid int64
Biz string
Tokens int64
Amount int64
Input []string
Status RecordStatus
KnowledgeId string
PromptTemplate string
Answer string
Ctime int64
Utime int64
}

type CreditStatus uint8

const (
CreditStatusProcessing CreditStatus = iota
CreditStatusSuccess
CreditStatusFailed
)

func (g CreditStatus) ToUint8() uint8 {
return uint8(g)
}

type GPTLogStatus uint8
type RecordStatus uint8

func (g GPTLogStatus) ToUint8() uint8 {
func (g RecordStatus) ToUint8() uint8 {
return uint8(g)
}

const (
ProcessingStatus GPTLogStatus = 0
SuccessStatus GPTLogStatus = 1
FailLogStatus GPTLogStatus = 2
RecordStatusProcessing RecordStatus = 0
RecordStatusSuccess RecordStatus = 1
RecordStatusFailed RecordStatus = 2
)
Loading

0 comments on commit cd2472e

Please sign in to comment.