mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-09-18 01:06:39 +08:00
chore: remove dead code
This commit is contained in:
parent
e9467341fa
commit
3f74b94784
@ -3,7 +3,6 @@ package core
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"chatplus/core/types"
|
"chatplus/core/types"
|
||||||
"chatplus/service/fun"
|
|
||||||
"chatplus/store/model"
|
"chatplus/store/model"
|
||||||
"chatplus/utils"
|
"chatplus/utils"
|
||||||
"chatplus/utils/resp"
|
"chatplus/utils/resp"
|
||||||
@ -39,10 +38,9 @@ type AppServer struct {
|
|||||||
ChatSession *types.LMap[string, *types.ChatSession] //map[sessionId]UserId
|
ChatSession *types.LMap[string, *types.ChatSession] //map[sessionId]UserId
|
||||||
ChatClients *types.LMap[string, *types.WsClient] // map[sessionId]Websocket 连接集合
|
ChatClients *types.LMap[string, *types.WsClient] // map[sessionId]Websocket 连接集合
|
||||||
ReqCancelFunc *types.LMap[string, context.CancelFunc] // HttpClient 请求取消 handle function
|
ReqCancelFunc *types.LMap[string, context.CancelFunc] // HttpClient 请求取消 handle function
|
||||||
Functions map[string]fun.Function
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(appConfig *types.AppConfig, functions map[string]fun.Function) *AppServer {
|
func NewServer(appConfig *types.AppConfig) *AppServer {
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
gin.DefaultWriter = io.Discard
|
gin.DefaultWriter = io.Discard
|
||||||
return &AppServer{
|
return &AppServer{
|
||||||
@ -53,7 +51,6 @@ func NewServer(appConfig *types.AppConfig, functions map[string]fun.Function) *A
|
|||||||
ChatSession: types.NewLMap[string, *types.ChatSession](),
|
ChatSession: types.NewLMap[string, *types.ChatSession](),
|
||||||
ChatClients: types.NewLMap[string, *types.WsClient](),
|
ChatClients: types.NewLMap[string, *types.WsClient](),
|
||||||
ReqCancelFunc: types.NewLMap[string, context.CancelFunc](),
|
ReqCancelFunc: types.NewLMap[string, context.CancelFunc](),
|
||||||
Functions: functions,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,47 +24,3 @@ type Property struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
|
||||||
FuncZaoBao = "zao_bao" // 每日早报
|
|
||||||
FuncHeadLine = "headline" // 今日头条
|
|
||||||
FuncWeibo = "weibo_hot" // 微博热搜
|
|
||||||
FuncImage = "draw_image" // AI 绘画
|
|
||||||
)
|
|
||||||
|
|
||||||
var InnerFunctions = []Function{
|
|
||||||
{
|
|
||||||
Name: FuncZaoBao,
|
|
||||||
Description: "每日早报,获取当天新闻事件列表",
|
|
||||||
Parameters: Parameters{
|
|
||||||
|
|
||||||
Type: "object",
|
|
||||||
Properties: map[string]Property{},
|
|
||||||
Required: []string{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: FuncWeibo,
|
|
||||||
Description: "新浪微博热搜榜,微博当日热搜榜单",
|
|
||||||
Parameters: Parameters{
|
|
||||||
Type: "object",
|
|
||||||
Properties: map[string]Property{},
|
|
||||||
Required: []string{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
|
||||||
Name: FuncImage,
|
|
||||||
Description: "AI 绘画工具,根据输入的绘图描述用 AI 工具进行绘画",
|
|
||||||
Parameters: Parameters{
|
|
||||||
Type: "object",
|
|
||||||
Properties: map[string]Property{
|
|
||||||
"prompt": {
|
|
||||||
Type: "string",
|
|
||||||
Description: "提示词,请自动将该参数翻译成英文。",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Required: []string{},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
@ -267,11 +267,8 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio
|
|||||||
} else {
|
} else {
|
||||||
// calculate the tokens of current request, to prevent to exceeding the max tokens num
|
// calculate the tokens of current request, to prevent to exceeding the max tokens num
|
||||||
tokens := req.MaxTokens
|
tokens := req.MaxTokens
|
||||||
for _, f := range types.InnerFunctions {
|
tks, _ := utils.CalcTokens(utils.JsonEncode(req.Tools), req.Model)
|
||||||
tks, _ := utils.CalcTokens(utils.JsonEncode(f), req.Model)
|
|
||||||
tokens += tks
|
tokens += tks
|
||||||
}
|
|
||||||
|
|
||||||
// loading the role context
|
// loading the role context
|
||||||
var messages []types.Message
|
var messages []types.Message
|
||||||
err := utils.JsonDecode(role.Context, &messages)
|
err := utils.JsonDecode(role.Context, &messages)
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
"chatplus/handler/chatimpl"
|
"chatplus/handler/chatimpl"
|
||||||
logger2 "chatplus/logger"
|
logger2 "chatplus/logger"
|
||||||
"chatplus/service"
|
"chatplus/service"
|
||||||
"chatplus/service/fun"
|
|
||||||
"chatplus/service/mj"
|
"chatplus/service/mj"
|
||||||
"chatplus/service/oss"
|
"chatplus/service/oss"
|
||||||
"chatplus/service/payment"
|
"chatplus/service/payment"
|
||||||
@ -115,9 +114,6 @@ func main() {
|
|||||||
return xdb.NewWithBuffer(cBuff)
|
return xdb.NewWithBuffer(cBuff)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
// 创建函数
|
|
||||||
fx.Provide(fun.NewFunctions),
|
|
||||||
|
|
||||||
// 创建控制器
|
// 创建控制器
|
||||||
fx.Provide(handler.NewChatRoleHandler),
|
fx.Provide(handler.NewChatRoleHandler),
|
||||||
fx.Provide(handler.NewUserHandler),
|
fx.Provide(handler.NewUserHandler),
|
||||||
|
@ -1,116 +0,0 @@
|
|||||||
package fun
|
|
||||||
|
|
||||||
import (
|
|
||||||
"chatplus/core/types"
|
|
||||||
"chatplus/service/oss"
|
|
||||||
"chatplus/store/model"
|
|
||||||
"chatplus/utils"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/imroc/req/v3"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AI 绘画函数
|
|
||||||
|
|
||||||
type FuncImage struct {
|
|
||||||
name string
|
|
||||||
db *gorm.DB
|
|
||||||
uploadManager *oss.UploaderManager
|
|
||||||
proxyURL string
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewImageFunc(db *gorm.DB, manager *oss.UploaderManager, config *types.AppConfig) FuncImage {
|
|
||||||
return FuncImage{
|
|
||||||
db: db,
|
|
||||||
name: "DALL-E3 绘画",
|
|
||||||
uploadManager: manager,
|
|
||||||
proxyURL: config.ProxyURL,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type imgReq struct {
|
|
||||||
Model string `json:"model"`
|
|
||||||
Prompt string `json:"prompt"`
|
|
||||||
N int `json:"n"`
|
|
||||||
Size string `json:"size"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type imgRes struct {
|
|
||||||
Created int64 `json:"created"`
|
|
||||||
Data []struct {
|
|
||||||
RevisedPrompt string `json:"revised_prompt"`
|
|
||||||
Url string `json:"url"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ErrRes struct {
|
|
||||||
Error struct {
|
|
||||||
Code interface{} `json:"code"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
Param interface{} `json:"param"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
} `json:"error"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FuncImage) Invoke(params map[string]interface{}) (string, error) {
|
|
||||||
logger.Infof("绘画参数:%+v", params)
|
|
||||||
prompt := utils.InterfaceToString(params["prompt"])
|
|
||||||
// get image generation API KEY
|
|
||||||
var apiKey model.ApiKey
|
|
||||||
tx := f.db.Where("platform = ? AND type = ?", types.OpenAI, "img").Order("last_used_at ASC").First(&apiKey)
|
|
||||||
if tx.Error != nil {
|
|
||||||
return "", fmt.Errorf("error with get generation API KEY: %v", tx.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// get image generation api URL
|
|
||||||
var conf model.Config
|
|
||||||
var chatConfig types.ChatConfig
|
|
||||||
tx = f.db.Where("marker", "chat").First(&conf)
|
|
||||||
if tx.Error != nil {
|
|
||||||
return "", fmt.Errorf("error with get chat configs: %v", tx.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
err := utils.JsonDecode(conf.Config, &chatConfig)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("error with decode chat config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
apiURL := chatConfig.DallApiURL
|
|
||||||
if utils.IsEmptyValue(apiURL) {
|
|
||||||
apiURL = "https://api.openai.com/v1/images/generations"
|
|
||||||
}
|
|
||||||
imgNum := chatConfig.DallImgNum
|
|
||||||
if imgNum <= 0 {
|
|
||||||
imgNum = 1
|
|
||||||
}
|
|
||||||
var res imgRes
|
|
||||||
var errRes ErrRes
|
|
||||||
r, err := req.C().SetProxyURL(f.proxyURL).R().SetHeader("Content-Type", "application/json").
|
|
||||||
SetHeader("Authorization", "Bearer "+apiKey.Value).
|
|
||||||
SetBody(imgReq{
|
|
||||||
Model: "dall-e-3",
|
|
||||||
Prompt: prompt,
|
|
||||||
N: imgNum,
|
|
||||||
Size: "1024x1024",
|
|
||||||
}).
|
|
||||||
SetErrorResult(&errRes).
|
|
||||||
SetSuccessResult(&res).Post(apiURL)
|
|
||||||
if err != nil || r.IsErrorState() {
|
|
||||||
return "", fmt.Errorf("error with http request: %v%v%s", err, r.Err, errRes.Error.Message)
|
|
||||||
}
|
|
||||||
// 存储图片
|
|
||||||
imgURL, err := f.uploadManager.GetUploadHandler().PutImg(res.Data[0].Url, false)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("下载图片失败: %s", err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
//logger.Info(imgURL)
|
|
||||||
return fmt.Sprintf("\n\n\n", imgURL), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FuncImage) Name() string {
|
|
||||||
return f.name
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Function = &FuncImage{}
|
|
@ -1,40 +0,0 @@
|
|||||||
package fun
|
|
||||||
|
|
||||||
import (
|
|
||||||
"chatplus/core/types"
|
|
||||||
logger2 "chatplus/logger"
|
|
||||||
"chatplus/service/oss"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Function interface {
|
|
||||||
Invoke(map[string]interface{}) (string, error)
|
|
||||||
Name() string
|
|
||||||
}
|
|
||||||
|
|
||||||
var logger = logger2.GetLogger()
|
|
||||||
|
|
||||||
type resVo struct {
|
|
||||||
Code types.BizCode `json:"code"`
|
|
||||||
Message string `json:"message"`
|
|
||||||
Data struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
UpdatedAt string `json:"updated_at"`
|
|
||||||
Items []dataItem `json:"items"`
|
|
||||||
} `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type dataItem struct {
|
|
||||||
Title string `json:"title"`
|
|
||||||
Url string `json:"url"`
|
|
||||||
Remark string `json:"remark"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewFunctions(config *types.AppConfig, db *gorm.DB, manager *oss.UploaderManager) map[string]Function {
|
|
||||||
return map[string]Function{
|
|
||||||
types.FuncZaoBao: NewZaoBao(config.ApiConfig),
|
|
||||||
types.FuncWeibo: NewWeiboHot(config.ApiConfig),
|
|
||||||
types.FuncHeadLine: NewHeadLines(config.ApiConfig),
|
|
||||||
types.FuncImage: NewImageFunc(db, manager, config),
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,58 +0,0 @@
|
|||||||
package fun
|
|
||||||
|
|
||||||
import (
|
|
||||||
"chatplus/core/types"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"github.com/imroc/req/v3"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 今日头条函数实现
|
|
||||||
|
|
||||||
type FuncHeadlines struct {
|
|
||||||
name string
|
|
||||||
config types.ChatPlusApiConfig
|
|
||||||
client *req.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHeadLines(config types.ChatPlusApiConfig) FuncHeadlines {
|
|
||||||
return FuncHeadlines{
|
|
||||||
name: "今日头条",
|
|
||||||
config: config,
|
|
||||||
client: req.C().SetTimeout(10 * time.Second)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FuncHeadlines) Invoke(map[string]interface{}) (string, error) {
|
|
||||||
if f.config.Token == "" {
|
|
||||||
return "", errors.New("无效的 API Token")
|
|
||||||
}
|
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/api/headline/fetch", f.config.ApiURL)
|
|
||||||
var res resVo
|
|
||||||
r, err := f.client.R().
|
|
||||||
SetHeader("AppId", f.config.AppId).
|
|
||||||
SetHeader("Authorization", fmt.Sprintf("Bearer %s", f.config.Token)).
|
|
||||||
SetSuccessResult(&res).Get(url)
|
|
||||||
if err != nil || r.IsErrorState() {
|
|
||||||
return "", fmt.Errorf("%v%v", err, r.Err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Code != types.Success {
|
|
||||||
return "", errors.New(res.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
builder := make([]string, 0)
|
|
||||||
builder = append(builder, fmt.Sprintf("**%s**,最新更新:%s", res.Data.Title, res.Data.UpdatedAt))
|
|
||||||
for i, v := range res.Data.Items {
|
|
||||||
builder = append(builder, fmt.Sprintf("%d、 [%s](%s) [%s]", i+1, v.Title, v.Url, v.Remark))
|
|
||||||
}
|
|
||||||
return strings.Join(builder, "\n\n"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FuncHeadlines) Name() string {
|
|
||||||
return f.name
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Function = &FuncHeadlines{}
|
|
@ -1,58 +0,0 @@
|
|||||||
package fun
|
|
||||||
|
|
||||||
import (
|
|
||||||
"chatplus/core/types"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"github.com/imroc/req/v3"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 微博热搜函数实现
|
|
||||||
|
|
||||||
type FuncWeiboHot struct {
|
|
||||||
name string
|
|
||||||
config types.ChatPlusApiConfig
|
|
||||||
client *req.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewWeiboHot(config types.ChatPlusApiConfig) FuncWeiboHot {
|
|
||||||
return FuncWeiboHot{
|
|
||||||
name: "微博热搜",
|
|
||||||
config: config,
|
|
||||||
client: req.C().SetTimeout(10 * time.Second)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FuncWeiboHot) Invoke(map[string]interface{}) (string, error) {
|
|
||||||
if f.config.Token == "" {
|
|
||||||
return "", errors.New("无效的 API Token")
|
|
||||||
}
|
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/api/weibo/fetch", f.config.ApiURL)
|
|
||||||
var res resVo
|
|
||||||
r, err := f.client.R().
|
|
||||||
SetHeader("AppId", f.config.AppId).
|
|
||||||
SetHeader("Authorization", fmt.Sprintf("Bearer %s", f.config.Token)).
|
|
||||||
SetSuccessResult(&res).Get(url)
|
|
||||||
if err != nil || r.IsErrorState() {
|
|
||||||
return "", fmt.Errorf("%v%v", err, r.Err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Code != types.Success {
|
|
||||||
return "", errors.New(res.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
builder := make([]string, 0)
|
|
||||||
builder = append(builder, fmt.Sprintf("**%s**,最新更新:%s", res.Data.Title, res.Data.UpdatedAt))
|
|
||||||
for i, v := range res.Data.Items {
|
|
||||||
builder = append(builder, fmt.Sprintf("%d、 [%s](%s) [热度:%s]", i+1, v.Title, v.Url, v.Remark))
|
|
||||||
}
|
|
||||||
return strings.Join(builder, "\n\n"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FuncWeiboHot) Name() string {
|
|
||||||
return f.name
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Function = &FuncWeiboHot{}
|
|
@ -1,59 +0,0 @@
|
|||||||
package fun
|
|
||||||
|
|
||||||
import (
|
|
||||||
"chatplus/core/types"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"github.com/imroc/req/v3"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 每日早报函数实现
|
|
||||||
|
|
||||||
type FuncZaoBao struct {
|
|
||||||
name string
|
|
||||||
config types.ChatPlusApiConfig
|
|
||||||
client *req.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewZaoBao(config types.ChatPlusApiConfig) FuncZaoBao {
|
|
||||||
return FuncZaoBao{
|
|
||||||
name: "每日早报",
|
|
||||||
config: config,
|
|
||||||
client: req.C().SetTimeout(10 * time.Second)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FuncZaoBao) Invoke(map[string]interface{}) (string, error) {
|
|
||||||
if f.config.Token == "" {
|
|
||||||
return "", errors.New("无效的 API Token")
|
|
||||||
}
|
|
||||||
|
|
||||||
url := fmt.Sprintf("%s/api/zaobao/fetch", f.config.ApiURL)
|
|
||||||
var res resVo
|
|
||||||
r, err := f.client.R().
|
|
||||||
SetHeader("AppId", f.config.AppId).
|
|
||||||
SetHeader("Authorization", fmt.Sprintf("Bearer %s", f.config.Token)).
|
|
||||||
SetSuccessResult(&res).Get(url)
|
|
||||||
if err != nil || r.IsErrorState() {
|
|
||||||
return "", fmt.Errorf("%v%v", err, r.Err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.Code != types.Success {
|
|
||||||
return "", errors.New(res.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
builder := make([]string, 0)
|
|
||||||
builder = append(builder, fmt.Sprintf("**%s 早报:**", res.Data.UpdatedAt))
|
|
||||||
for _, v := range res.Data.Items {
|
|
||||||
builder = append(builder, v.Title)
|
|
||||||
}
|
|
||||||
builder = append(builder, fmt.Sprintf("%s", res.Data.Title))
|
|
||||||
return strings.Join(builder, "\n\n"), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f FuncZaoBao) Name() string {
|
|
||||||
return f.name
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Function = &FuncZaoBao{}
|
|
@ -104,7 +104,7 @@ func (b *Bot) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// ignore messages for self
|
// ignore messages for self
|
||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author == nil || m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +136,7 @@ func (b *Bot) messageUpdate(s *discordgo.Session, m *discordgo.MessageUpdate) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
// ignore messages for self
|
// ignore messages for self
|
||||||
if m.Author.ID == s.State.User.ID {
|
if m.Author == nil || m.Author.ID == s.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user