Browse Source

fix:edit agent/create

jimmyyem 6 months ago
parent
commit
321efa6bca

+ 4 - 0
desc/wechat/agent.api

@@ -19,6 +19,10 @@ type (
 
         // examples | 对话案例 
         Examples  *string `json:"examples,optional"`
+
+		DatasetId *string `json:"dataset_id,optional"`
+
+		CollectionId *string `json:"collection_id,optional"`
     }
 
     // The response data of agent list | Agent列表数据

+ 4 - 0
ent/agent/agent.go

@@ -108,8 +108,12 @@ var (
 	ExamplesValidator func(string) error
 	// OrganizationIDValidator is a validator for the "organization_id" field. It is called by the builders before save.
 	OrganizationIDValidator func(uint64) error
+	// DefaultDatasetID holds the default value on creation for the "dataset_id" field.
+	DefaultDatasetID string
 	// DatasetIDValidator is a validator for the "dataset_id" field. It is called by the builders before save.
 	DatasetIDValidator func(string) error
+	// DefaultCollectionID holds the default value on creation for the "collection_id" field.
+	DefaultCollectionID string
 	// CollectionIDValidator is a validator for the "collection_id" field. It is called by the builders before save.
 	CollectionIDValidator func(string) error
 )

+ 24 - 0
ent/agent_create.go

@@ -131,12 +131,28 @@ func (ac *AgentCreate) SetDatasetID(s string) *AgentCreate {
 	return ac
 }
 
+// SetNillableDatasetID sets the "dataset_id" field if the given value is not nil.
+func (ac *AgentCreate) SetNillableDatasetID(s *string) *AgentCreate {
+	if s != nil {
+		ac.SetDatasetID(*s)
+	}
+	return ac
+}
+
 // SetCollectionID sets the "collection_id" field.
 func (ac *AgentCreate) SetCollectionID(s string) *AgentCreate {
 	ac.mutation.SetCollectionID(s)
 	return ac
 }
 
+// SetNillableCollectionID sets the "collection_id" field if the given value is not nil.
+func (ac *AgentCreate) SetNillableCollectionID(s *string) *AgentCreate {
+	if s != nil {
+		ac.SetCollectionID(*s)
+	}
+	return ac
+}
+
 // SetID sets the "id" field.
 func (ac *AgentCreate) SetID(u uint64) *AgentCreate {
 	ac.mutation.SetID(u)
@@ -221,6 +237,14 @@ func (ac *AgentCreate) defaults() error {
 		v := agent.DefaultExamples
 		ac.mutation.SetExamples(v)
 	}
+	if _, ok := ac.mutation.DatasetID(); !ok {
+		v := agent.DefaultDatasetID
+		ac.mutation.SetDatasetID(v)
+	}
+	if _, ok := ac.mutation.CollectionID(); !ok {
+		v := agent.DefaultCollectionID
+		ac.mutation.SetCollectionID(v)
+	}
 	return nil
 }
 

+ 2 - 2
ent/migrate/schema.go

@@ -21,8 +21,8 @@ var (
 		{Name: "background", Type: field.TypeString, Nullable: true, Size: 1000, Comment: "background | 背景介绍", Default: ""},
 		{Name: "examples", Type: field.TypeString, Nullable: true, Size: 5000, Comment: "examples | 对话案例", Default: ""},
 		{Name: "organization_id", Type: field.TypeUint64, Comment: "organization_id | 租户ID"},
-		{Name: "dataset_id", Type: field.TypeString, Size: 255, Comment: "dataset_id | 知识库ID"},
-		{Name: "collection_id", Type: field.TypeString, Size: 255, Comment: "collection_id | 集合ID"},
+		{Name: "dataset_id", Type: field.TypeString, Size: 255, Comment: "dataset_id | 知识库ID", Default: ""},
+		{Name: "collection_id", Type: field.TypeString, Size: 255, Comment: "collection_id | 集合ID", Default: ""},
 	}
 	// AgentTable holds the schema information for the "agent" table.
 	AgentTable = &schema.Table{

+ 4 - 0
ent/runtime/runtime.go

@@ -81,10 +81,14 @@ func init() {
 	agent.OrganizationIDValidator = agentDescOrganizationID.Validators[0].(func(uint64) error)
 	// agentDescDatasetID is the schema descriptor for dataset_id field.
 	agentDescDatasetID := agentFields[6].Descriptor()
+	// agent.DefaultDatasetID holds the default value on creation for the dataset_id field.
+	agent.DefaultDatasetID = agentDescDatasetID.Default.(string)
 	// agent.DatasetIDValidator is a validator for the "dataset_id" field. It is called by the builders before save.
 	agent.DatasetIDValidator = agentDescDatasetID.Validators[0].(func(string) error)
 	// agentDescCollectionID is the schema descriptor for collection_id field.
 	agentDescCollectionID := agentFields[7].Descriptor()
+	// agent.DefaultCollectionID holds the default value on creation for the collection_id field.
+	agent.DefaultCollectionID = agentDescCollectionID.Default.(string)
 	// agent.CollectionIDValidator is a validator for the "collection_id" field. It is called by the builders before save.
 	agent.CollectionIDValidator = agentDescCollectionID.Validators[0].(func(string) error)
 	batchmsgMixin := schema.BatchMsg{}.Mixin()

+ 2 - 2
ent/schema/agent.go

@@ -24,8 +24,8 @@ func (Agent) Fields() []ent.Field {
 		field.String("background").Optional().Default("").MaxLen(1000).Comment("background | 背景介绍"),
 		field.String("examples").Optional().Default("").MaxLen(5000).Comment("examples | 对话案例"),
 		field.Uint64("organization_id").Positive().Comment("organization_id | 租户ID"),
-		field.String("dataset_id").MaxLen(255).Comment("dataset_id | 知识库ID"),
-		field.String("collection_id").MaxLen(255).Comment("collection_id | 集合ID"),
+		field.String("dataset_id").Default("").MaxLen(255).Comment("dataset_id | 知识库ID"),
+		field.String("collection_id").Default("").MaxLen(255).Comment("collection_id | 集合ID"),
 	}
 }
 

+ 5 - 4
internal/logic/agent/create_agent_logic.go

@@ -3,6 +3,7 @@ package agent
 import (
 	"context"
 	"errors"
+	"github.com/zeromicro/go-zero/core/errorx"
 	"wechat-api/hook/fastgpt"
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
@@ -55,9 +56,9 @@ func (l *CreateAgentLogic) CreateAgent(req *types.AgentInfo) (*types.BaseMsgResp
 	datasetReq.VectorModel = "text-embedding-ada-002"
 	datasetResp, err := fastgpt.CreateDataset(&datasetReq)
 	if err != nil {
-		return nil, errors.New("create dataset failed")
+		return nil, errorx.NewInvalidArgumentError("fastgpt create dataset failed " + err.Error())
 	}
-	if datasetResp.Code != 0 {
+	if datasetResp.Code != 200 {
 		return nil, errors.New(datasetResp.Message)
 	}
 
@@ -69,8 +70,8 @@ func (l *CreateAgentLogic) CreateAgent(req *types.AgentInfo) (*types.BaseMsgResp
 	if err != nil {
 		return nil, errors.New("create dataset failed")
 	}
-	if collectionResp.Code != 0 {
-		return nil, errors.New(collectionResp.Message)
+	if collectionResp.Code != 200 {
+		return nil, errorx.NewInvalidArgumentError("fastgpt create collection failed " + err.Error())
 	}
 
 	_, err = l.svcCtx.DB.Agent.UpdateOneID(agent.ID).

+ 7 - 5
internal/logic/agent/get_agent_by_id_logic.go

@@ -47,11 +47,13 @@ func (l *GetAgentByIdLogic) GetAgentById(req *types.IDReq) (*types.AgentInfoResp
 				CreatedAt: pointy.GetPointer(data.CreatedAt.UnixMilli()),
 				UpdatedAt: pointy.GetPointer(data.UpdatedAt.UnixMilli()),
 			},
-			Name:       &data.Name,
-			Role:       &data.Role,
-			Status:     &data.Status,
-			Background: &data.Background,
-			Examples:   &data.Examples,
+			Name:         &data.Name,
+			Role:         &data.Role,
+			Status:       &data.Status,
+			Background:   &data.Background,
+			Examples:     &data.Examples,
+			DatasetId:    &data.DatasetID,
+			CollectionId: &data.CollectionID,
 		},
 	}, nil
 }

+ 7 - 5
internal/logic/agent/get_agent_list_logic.go

@@ -63,11 +63,13 @@ func (l *GetAgentListLogic) GetAgentList(req *types.AgentListReq) (*types.AgentL
 					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
 					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
 				},
-				Name:       &v.Name,
-				Role:       &v.Role,
-				Status:     &v.Status,
-				Background: &v.Background,
-				Examples:   &v.Examples,
+				Name:         &v.Name,
+				Role:         &v.Role,
+				Status:       &v.Status,
+				Background:   &v.Background,
+				Examples:     &v.Examples,
+				DatasetId:    &v.DatasetID,
+				CollectionId: &v.CollectionID,
 			})
 	}
 

+ 3 - 1
internal/types/types.go

@@ -382,7 +382,9 @@ type AgentInfo struct {
 	// background | 背景介绍
 	Background *string `json:"background,optional"`
 	// examples | 对话案例
-	Examples *string `json:"examples,optional"`
+	Examples     *string `json:"examples,optional"`
+	DatasetId    *string `json:"dataset_id,optional"`
+	CollectionId *string `json:"collection_id,optional"`
 }
 
 // The response data of agent list | Agent列表数据