-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
66 changed files
with
1,568 additions
and
1,222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.