Browse Source

fix:integrate fastgpt to agent API

jimmyyem 6 months ago
parent
commit
052243be8f

+ 15 - 8
desc/wechat/agent.api

@@ -162,6 +162,15 @@ type (
 		IsOwner *bool `json:"isOwner"`
 	}
 
+	DataSimpleInfo {
+		ID *string  `json:"id"`
+		Q *string `json:"q"`
+		A *string `json:"a"`
+		ChunkIndex *uint64 `json:"chunkIndex"`
+		DatasetId *string `json:"datasetId"`
+		CollectionId *string `json:"collectionId"`
+	}
+
 	// Get collection list request params | Collection列表请求参数
 	CollectionListReq {
 		PageNum   *int    `json:"pageNum" validate:"required,number,gt=0"`
@@ -199,7 +208,7 @@ type (
 		BaseDataInfo
 
 		// Agent information | Agent数据
-		Data []DataInfo `json:"data"`
+		Data []DataSimpleInfo `json:"data"`
 
 		PageNum   *int    `json:"pageNum" validate:"required,number,gt=0"`
 
@@ -213,10 +222,10 @@ type (
 		CollectionId *string `json:"collectionId" validate:"required"`
 
 		// Q
-		 Q *string `json:"q"`
+		 Q *string `json:"q" validate:"required"`
 
 		// A
-		 A *string `json:"a"`
+		 A *string `json:"a" validate:"required"`
 
 		// Indexes | 索引
 		//Indexes []IndexSingle `json:"indexes,optional"`
@@ -228,16 +237,14 @@ type (
 
 	// Data create request | 信息返回体
 	UpdateDataInfoReq {
-		CollectionId *string `json:"collectionId" validate:"required"`
-
 		// ID
-		ID *string `json:"id"`
+		DataId *string `json:"dataId" validate:"required"`
 
 		// Q
-		Q *string `json:"q"`
+		Q *string `json:"q" validate:"required"`
 
 		// A
-		A *string `json:"a"`
+		A *string `json:"a" validate:"required"`
 
 		// Indexes | 索引
 		//Indexes []IndexSingle `json:"indexes,optional"`

+ 2 - 2
hook/fastgpt/collection.go

@@ -128,10 +128,10 @@ type CollectionInfo struct {
 	Type          string    `json:"type"`
 	Name          string    `json:"name"`
 	TrainingType  string    `json:"trainingType"`
-	ChunkSize     int       `json:"chunkSize"`
+	ChunkSize     uint64    `json:"chunkSize"`
 	ChunkSplitter string    `json:"chunkSplitter"`
 	QaPrompt      string    `json:"qaPrompt"`
-	RawTextLength int       `json:"rawTextLength"`
+	RawTextLength uint64    `json:"rawTextLength"`
 	HashRawText   string    `json:"hashRawText"`
 	CreateTime    time.Time `json:"createTime"`
 	UpdateTime    time.Time `json:"updateTime"`

+ 4 - 4
hook/fastgpt/data.go

@@ -84,7 +84,7 @@ type DataListResp struct {
 			A            string `json:"a"`
 			ChunkIndex   uint64 `json:"chunkIndex"`
 		} `json:"data,optional"`
-		Total uint64 `json:"total"`
+		Total int `json:"total"`
 	} `json:"data,optional"`
 }
 
@@ -96,9 +96,9 @@ type DataDetail struct {
 }
 
 type UpdateDataReq struct {
-	ID      string  `json:"id"`
-	Q       string  `json:"q"`
-	A       string  `json:"a"`
+	DataId  string  `json:"dataId" validate:"required"`
+	Q       string  `json:"q" validate:"required"`
+	A       string  `json:"a" validate:"required"`
 	Indexes []Index `json:"indexes,omitempty,optional"`
 }
 

+ 10 - 10
hook/fastgpt/dataset.go

@@ -35,10 +35,10 @@ type DatasetInfo struct {
 		Model            string `json:"model"`
 		Name             string `json:"name"`
 		Avatar           string `json:"avatar"`
-		CharsPointsPrice int    `json:"charsPointsPrice"`
-		DefaultToken     int    `json:"defaultToken"`
-		MaxToken         int    `json:"maxToken"`
-		Weight           int    `json:"weight"`
+		CharsPointsPrice uint64 `json:"charsPointsPrice"`
+		DefaultToken     uint64 `json:"defaultToken"`
+		MaxToken         uint64 `json:"maxToken"`
+		Weight           uint64 `json:"weight"`
 		DefaultConfig    struct {
 		} `json:"defaultConfig"`
 		DbConfig struct {
@@ -50,11 +50,11 @@ type DatasetInfo struct {
 		Model                   string  `json:"model"`
 		Name                    string  `json:"name"`
 		Avatar                  string  `json:"avatar"`
-		MaxContext              int     `json:"maxContext"`
-		MaxResponse             int     `json:"maxResponse"`
-		QuoteMaxToken           int     `json:"quoteMaxToken"`
+		MaxContext              uint64  `json:"maxContext"`
+		MaxResponse             uint64  `json:"maxResponse"`
+		QuoteMaxToken           uint64  `json:"quoteMaxToken"`
 		MaxTemperature          float64 `json:"maxTemperature"`
-		CharsPointsPrice        int     `json:"charsPointsPrice"`
+		CharsPointsPrice        uint64  `json:"charsPointsPrice"`
 		Censor                  bool    `json:"censor"`
 		Vision                  bool    `json:"vision"`
 		DatasetProcess          bool    `json:"datasetProcess"`
@@ -78,9 +78,9 @@ type DatasetInfo struct {
 		HasWritePer  bool  `json:"hasWritePer"`
 		HasReadPer   bool  `json:"hasReadPer"`
 	} `json:"permission,optional"`
-	DefaultPermission int       `json:"defaultPermission.optional"`
+	DefaultPermission uint64    `json:"defaultPermission.optional"`
 	UpdateTime        time.Time `json:"updateTime,optional"`
-	V                 int       `json:"__v,optional"`
+	V                 uint64    `json:"__v,optional"`
 }
 
 type DatasetList struct {

+ 5 - 5
internal/logic/agent/create_agent_data_logic.go

@@ -2,9 +2,9 @@ package agent
 
 import (
 	"context"
-	"errors"
 	"fmt"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/zeromicro/go-zero/core/errorx"
 	"wechat-api/hook/fastgpt"
 
 	"wechat-api/internal/svc"
@@ -30,7 +30,7 @@ func (l *CreateAgentDataLogic) CreateAgentData(req *types.CreateDataInfoReq) (*t
 	params := fastgpt.CreateBulkDataReq{}
 	params.CollectionID = *req.CollectionId
 	params.TrainingMode = "chunk"
-	pair := make([]fastgpt.DataQuestion, 0)
+	pair := make([]fastgpt.DataQuestion, 0, 1)
 	pair = append(pair, fastgpt.DataQuestion{
 		Q: *req.Q,
 		A: *req.A,
@@ -39,10 +39,10 @@ func (l *CreateAgentDataLogic) CreateAgentData(req *types.CreateDataInfoReq) (*t
 
 	resp, err := fastgpt.CreateBulkData(&params)
 	if err != nil {
-		return nil, errors.New("fastgpt create data failed")
+		return nil, errorx.NewInvalidArgumentError("fastgpt create data failed " + err.Error())
 	}
 
-	if resp.Code == 0 {
+	if resp.Code == 200 {
 		return &types.BaseDataInfo{
 			Code: 0,
 			Msg:  errormsg.Success,
@@ -50,5 +50,5 @@ func (l *CreateAgentDataLogic) CreateAgentData(req *types.CreateDataInfoReq) (*t
 		}, nil
 	}
 
-	return nil, errors.New(resp.StatusText)
+	return nil, errorx.NewInvalidArgumentError(resp.StatusText)
 }

+ 11 - 21
internal/logic/agent/get_agent_collection_list_logic.go

@@ -2,8 +2,7 @@ package agent
 
 import (
 	"context"
-	"errors"
-	"fmt"
+	"github.com/zeromicro/go-zero/core/errorx"
 	"wechat-api/hook/fastgpt"
 
 	"wechat-api/internal/svc"
@@ -27,26 +26,23 @@ func NewGetAgentCollectionListLogic(ctx context.Context, svcCtx *svc.ServiceCont
 
 func (l *GetAgentCollectionListLogic) GetAgentCollectionList(req *types.CollectionListReq) (*types.CollectionListResp, error) {
 	collectionResp := types.CollectionListResp{}
+	collectionResp.PageNum = req.PageNum
+	collectionResp.PageSize = req.PageSize
+	var total int
 
-	var params fastgpt.GetCollectionListReq
+	params := fastgpt.GetCollectionListReq{}
 	params.DatasetId = *req.DatasetId
 	params.PageNum = *req.PageNum
-	params.PageSize = *req.PageNum
+	params.PageSize = *req.PageSize
 	resp, err := fastgpt.GetCollectionList(&params)
 	if err != nil {
-		return nil, err
+		return nil, errorx.NewInvalidArgumentError("fastgpt error" + err.Error())
 	}
 
-	for _, val := range resp.Data.Data {
-		fmt.Printf("id=%v, parentId=%v, total=%v \n", val.ID, val.ParentID, resp.Data.Total)
-	}
-
-	fmt.Printf("code=%v, message=%v, data=%v len=%v\n", resp.Code, resp.Message, resp.Data, len(resp.Data.Data))
-
-	if resp.Code == 200 && len(resp.Data.Data) > 0 {
-		collectionResp.Data = make([]types.CollectionSimpleInfo, 0, len(resp.Data.Data))
+	collectionResp.Data = make([]types.CollectionSimpleInfo, 0, 1)
+	collectionResp.Total = &total
+	if resp.Code == 200 && resp.Data.Total > 0 {
 		for _, val := range resp.Data.Data {
-			fmt.Printf("id=%v, parentId=%v \n", val.ID, val.ParentID)
 			collectionResp.Data = append(collectionResp.Data, types.CollectionSimpleInfo{
 				ID:             &val.ID,
 				ParentID:       &val.ParentID,
@@ -57,14 +53,8 @@ func (l *GetAgentCollectionListLogic) GetAgentCollectionList(req *types.Collecti
 				TrainingAmount: &val.TrainingAmount,
 			})
 		}
-
-		//collectionResp.Data = listData
-		collectionResp.PageNum = req.PageNum
-		collectionResp.PageSize = req.PageSize
 		collectionResp.Total = &resp.Data.Total
-
-		return &collectionResp, nil
 	}
 
-	return nil, errors.New(resp.Message)
+	return &collectionResp, nil
 }

+ 14 - 9
internal/logic/agent/get_agent_data_list_logic.go

@@ -2,8 +2,7 @@ package agent
 
 import (
 	"context"
-	"errors"
-	"fmt"
+	"github.com/zeromicro/go-zero/core/errorx"
 	"wechat-api/hook/fastgpt"
 
 	"wechat-api/internal/svc"
@@ -26,18 +25,25 @@ func NewGetAgentDataListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
 }
 
 func (l *GetAgentDataListLogic) GetAgentDataList(req *types.DataListReq) (*types.DataListResp, error) {
+	dataResp := types.DataListResp{}
+	dataResp.PageNum = req.PageNum
+	dataResp.PageSize = req.PageSize
+	var total int
+
 	var params fastgpt.GetDataListReq
 	params.CollectionId = *req.CollectionId
 	params.PageSize = *req.PageSize
 	params.PageNum = *req.PageNum
 	resp, err := fastgpt.GetDataList(&params)
 	if err != nil {
-		fmt.Printf("%v", err.Error())
+		return nil, errorx.NewInvalidArgumentError("fastgpt get data list failed " + err.Error())
 	}
-	dataResp := types.DataListResp{}
-	if resp.Code == 0 {
+
+	dataResp.Data = make([]types.DataSimpleInfo, 0, 1)
+	dataResp.Total = &total
+	if resp.Code == 200 && resp.Data.Total > 0 {
 		for _, val := range resp.Data.Data {
-			dataResp.Data = append(dataResp.Data, types.DataInfo{
+			dataResp.Data = append(dataResp.Data, types.DataSimpleInfo{
 				ID:           &val.ID,
 				Q:            &val.Q,
 				A:            &val.A,
@@ -46,9 +52,8 @@ func (l *GetAgentDataListLogic) GetAgentDataList(req *types.DataListReq) (*types
 				CollectionId: &val.CollectionID,
 			})
 		}
-
-		return &dataResp, nil
+		dataResp.Total = &resp.Data.Total
 	}
 
-	return nil, errors.New(resp.Message)
+	return &dataResp, nil
 }

+ 5 - 5
internal/logic/agent/update_agent_data_logic.go

@@ -2,8 +2,8 @@ package agent
 
 import (
 	"context"
-	"errors"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"github.com/zeromicro/go-zero/core/errorx"
 	"wechat-api/hook/fastgpt"
 
 	"wechat-api/internal/svc"
@@ -27,15 +27,15 @@ func NewUpdateAgentDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *U
 
 func (l *UpdateAgentDataLogic) UpdateAgentData(req *types.UpdateDataInfoReq) (*types.BaseDataInfo, error) {
 	params := fastgpt.UpdateDataReq{}
-	params.ID = *req.ID
+	params.DataId = *req.DataId
 	params.Q = *req.Q
 	params.A = *req.A
 	resp, err := fastgpt.UpdateData(&params)
 	if err != nil {
-		return nil, errors.New("fastgpt update data failed")
+		return nil, errorx.NewInvalidArgumentError("fastgpt update data failed " + err.Error())
 	}
 
-	if resp.Code == 0 {
+	if resp.Code == 200 {
 		return &types.BaseDataInfo{
 			Code: 0,
 			Msg:  errormsg.Success,
@@ -43,5 +43,5 @@ func (l *UpdateAgentDataLogic) UpdateAgentData(req *types.UpdateDataInfoReq) (*t
 		}, nil
 	}
 
-	return nil, errors.New(resp.StatusText)
+	return nil, errorx.NewInvalidArgumentError(resp.Message)
 }

+ 21 - 8
internal/types/types.go

@@ -527,6 +527,16 @@ type DataInfo struct {
 	IsOwner      *bool   `json:"isOwner"`
 }
 
+// swagger:model DataSimpleInfo
+type DataSimpleInfo struct {
+	ID           *string `json:"id"`
+	Q            *string `json:"q"`
+	A            *string `json:"a"`
+	ChunkIndex   *uint64 `json:"chunkIndex"`
+	DatasetId    *string `json:"datasetId"`
+	CollectionId *string `json:"collectionId"`
+}
+
 // Get collection list request params | Collection列表请求参数
 // swagger:model CollectionListReq
 type CollectionListReq struct {
@@ -573,7 +583,7 @@ type DataListReq struct {
 type DataListResp struct {
 	BaseDataInfo
 	// Agent information | Agent数据
-	Data []DataInfo `json:"data"`
+	Data []DataSimpleInfo `json:"data"`
 	// required : true
 	// min : 0
 	PageNum *int `json:"pageNum" validate:"required,number,gt=0"`
@@ -589,9 +599,11 @@ type CreateDataInfoReq struct {
 	// required : true
 	CollectionId *string `json:"collectionId" validate:"required"`
 	// Q
-	Q *string `json:"q"`
+	// required : true
+	Q *string `json:"q" validate:"required"`
 	// A
-	A *string `json:"a"`
+	// required : true
+	A *string `json:"a" validate:"required"`
 }
 
 //	IndexSingle {
@@ -601,14 +613,15 @@ type CreateDataInfoReq struct {
 // Data create request | 信息返回体
 // swagger:model UpdateDataInfoReq
 type UpdateDataInfoReq struct {
-	// required : true
-	CollectionId *string `json:"collectionId" validate:"required"`
 	// ID
-	ID *string `json:"id"`
+	// required : true
+	DataId *string `json:"dataId" validate:"required"`
 	// Q
-	Q *string `json:"q"`
+	// required : true
+	Q *string `json:"q" validate:"required"`
 	// A
-	A *string `json:"a"`
+	// required : true
+	A *string `json:"a" validate:"required"`
 }
 
 type LoginQRStatus struct {