mirror of
https://github.com/yangjian102621/geekai.git
synced 2026-04-23 11:34:27 +08:00
完成文本审查服务开发
This commit is contained in:
33
api/service/moderation/baidu_moderation.go
Normal file
33
api/service/moderation/baidu_moderation.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package moderation
|
||||
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
|
||||
// * Use of this source code is governed by a Apache-2.0 license
|
||||
// * that can be found in the LICENSE file.
|
||||
// * @Author yangjian102621@163.com
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"geekai/core/types"
|
||||
)
|
||||
|
||||
type BaiduAIModeration struct {
|
||||
config types.ModerationBaiduConfig
|
||||
}
|
||||
|
||||
func NewBaiduAIModeration(sysConfig *types.SystemConfig) *BaiduAIModeration {
|
||||
return &BaiduAIModeration{
|
||||
config: sysConfig.Moderation.Baidu,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *BaiduAIModeration) UpdateConfig(config types.ModerationBaiduConfig) {
|
||||
s.config = config
|
||||
}
|
||||
|
||||
func (s *BaiduAIModeration) Moderate(text string) (types.ModerationResult, error) {
|
||||
return types.ModerationResult{}, errors.New("not implemented")
|
||||
}
|
||||
|
||||
var _ Service = (*BaiduAIModeration)(nil)
|
||||
58
api/service/moderation/gitee_moderation.go
Normal file
58
api/service/moderation/gitee_moderation.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package moderation
|
||||
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
|
||||
// * Use of this source code is governed by a Apache-2.0 license
|
||||
// * that can be found in the LICENSE file.
|
||||
// * @Author yangjian102621@163.com
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"geekai/core/types"
|
||||
|
||||
"github.com/imroc/req/v3"
|
||||
)
|
||||
|
||||
type GiteeAIModeration struct {
|
||||
config types.ModerationGiteeConfig
|
||||
apiURL string
|
||||
}
|
||||
|
||||
func NewGiteeAIModeration(sysConfig *types.SystemConfig) *GiteeAIModeration {
|
||||
return &GiteeAIModeration{
|
||||
config: sysConfig.Moderation.Gitee,
|
||||
apiURL: "https://ai.gitee.com/v1/moderations",
|
||||
}
|
||||
}
|
||||
|
||||
func (s *GiteeAIModeration) UpdateConfig(config types.ModerationGiteeConfig) {
|
||||
s.config = config
|
||||
}
|
||||
|
||||
type GiteeAIModerationResult struct {
|
||||
ID string `json:"id"`
|
||||
Model string `json:"model"`
|
||||
Results []types.ModerationResult `json:"results"`
|
||||
}
|
||||
|
||||
func (s *GiteeAIModeration) Moderate(text string) (types.ModerationResult, error) {
|
||||
|
||||
body := map[string]any{
|
||||
"input": text,
|
||||
"model": s.config.Model,
|
||||
}
|
||||
var res GiteeAIModerationResult
|
||||
r, err := req.C().R().SetHeader("Authorization", "Bearer "+s.config.ApiKey).SetBody(body).SetSuccessResult(&res).Post(s.apiURL)
|
||||
if err != nil {
|
||||
return types.ModerationResult{}, err
|
||||
}
|
||||
|
||||
if r.IsErrorState() {
|
||||
return types.ModerationResult{}, errors.New(r.String())
|
||||
}
|
||||
|
||||
return res.Results[0], nil
|
||||
}
|
||||
|
||||
var _ Service = (*GiteeAIModeration)(nil)
|
||||
58
api/service/moderation/moderation_manager.go
Normal file
58
api/service/moderation/moderation_manager.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package moderation
|
||||
|
||||
import (
|
||||
"geekai/core/types"
|
||||
|
||||
logger2 "geekai/logger"
|
||||
)
|
||||
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
|
||||
// * Use of this source code is governed by a Apache-2.0 license
|
||||
// * that can be found in the LICENSE file.
|
||||
// * @Author yangjian102621@163.com
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
var logger = logger2.GetLogger()
|
||||
|
||||
type Service interface {
|
||||
Moderate(text string) (types.ModerationResult, error)
|
||||
}
|
||||
|
||||
type ServiceManager struct {
|
||||
gitee *GiteeAIModeration
|
||||
baidu *BaiduAIModeration
|
||||
tencent *TencentAIModeration
|
||||
active string
|
||||
}
|
||||
|
||||
func NewServiceManager(gitee *GiteeAIModeration, baidu *BaiduAIModeration, tencent *TencentAIModeration) *ServiceManager {
|
||||
return &ServiceManager{
|
||||
gitee: gitee,
|
||||
baidu: baidu,
|
||||
tencent: tencent,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServiceManager) GetService() Service {
|
||||
switch s.active {
|
||||
case types.ModerationBaidu:
|
||||
return s.baidu
|
||||
case types.ModerationTencent:
|
||||
return s.tencent
|
||||
default:
|
||||
return s.gitee
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServiceManager) UpdateConfig(config types.ModerationConfig) {
|
||||
switch config.Active {
|
||||
case types.ModerationGitee:
|
||||
s.gitee.UpdateConfig(config.Gitee)
|
||||
case types.ModerationBaidu:
|
||||
s.baidu.UpdateConfig(config.Baidu)
|
||||
case types.ModerationTencent:
|
||||
s.tencent.UpdateConfig(config.Tencent)
|
||||
}
|
||||
s.active = config.Active
|
||||
}
|
||||
33
api/service/moderation/tencent_moderation.go
Normal file
33
api/service/moderation/tencent_moderation.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package moderation
|
||||
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
|
||||
// * Use of this source code is governed by a Apache-2.0 license
|
||||
// * that can be found in the LICENSE file.
|
||||
// * @Author yangjian102621@163.com
|
||||
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"geekai/core/types"
|
||||
)
|
||||
|
||||
type TencentAIModeration struct {
|
||||
config types.ModerationTencentConfig
|
||||
}
|
||||
|
||||
func NewTencentAIModeration(sysConfig *types.SystemConfig) *TencentAIModeration {
|
||||
return &TencentAIModeration{
|
||||
config: sysConfig.Moderation.Tencent,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TencentAIModeration) UpdateConfig(config types.ModerationTencentConfig) {
|
||||
s.config = config
|
||||
}
|
||||
|
||||
func (s *TencentAIModeration) Moderate(text string) (types.ModerationResult, error) {
|
||||
return types.ModerationResult{}, errors.New("not implemented")
|
||||
}
|
||||
|
||||
var _ Service = (*TencentAIModeration)(nil)
|
||||
Reference in New Issue
Block a user