27 lines
602 B
Go
27 lines
602 B
Go
|
|
package ai
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
|
||
|
|
"haixun-backend/internal/svc"
|
||
|
|
"haixun-backend/internal/types"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ChatLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewChatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatLogic {
|
||
|
|
return &ChatLogic{ctx: ctx, svcCtx: svcCtx}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *ChatLogic) Chat(req *types.AIChatReq, token string) (*types.AIChatData, error) {
|
||
|
|
result, err := l.svcCtx.AI.GenerateText(l.ctx, toGenerateRequest(req, token))
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
return &types.AIChatData{Text: result.Text, FinishReason: result.FinishReason}, nil
|
||
|
|
}
|