You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

70 lines
1.6 KiB

// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package ai
import (
"context"
"errors"
"github.com/youruser/base/internal/svc"
"github.com/youruser/base/internal/types"
"github.com/youruser/base/model"
"github.com/zeromicro/go-zero/core/logx"
)
type AiConversationCreateLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 创建对话
func NewAiConversationCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AiConversationCreateLogic {
return &AiConversationCreateLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *AiConversationCreateLogic) AiConversationCreate(req *types.AIConversationCreateRequest) (resp *types.AIConversationInfo, err error) {
userId, _ := l.ctx.Value("userId").(int64)
if userId == 0 {
return nil, errors.New("unauthorized")
}
title := req.Title
if title == "" {
title = "新对话"
}
conv := &model.AIConversation{
UserId: userId,
Title: title,
ModelId: req.ModelId,
}
// Look up model to get provider ID
if req.ModelId != "" {
aiModel, err := model.AIModelFindByModelId(l.ctx, l.svcCtx.DB, req.ModelId)
if err == nil {
conv.ProviderId = aiModel.ProviderId
}
}
_, err = model.AIConversationInsert(l.ctx, l.svcCtx.DB, conv)
if err != nil {
return nil, err
}
return &types.AIConversationInfo{
Id: conv.Id,
Title: conv.Title,
ModelId: conv.ModelId,
ProviderId: conv.ProviderId,
CreatedAt: conv.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: conv.UpdatedAt.Format("2006-01-02 15:04:05"),
}, nil
}