create_fastgpt_logic.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package fastgpt
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "go.mongodb.org/mongo-driver/bson/primitive"
  7. "time"
  8. apps "wechat-api/mongo_model/apps"
  9. team_members "wechat-api/mongo_model/team_members"
  10. teams "wechat-api/mongo_model/teams"
  11. users "wechat-api/mongo_model/users"
  12. "wechat-api/internal/svc"
  13. "wechat-api/internal/types"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type CreateFastgptLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewCreateFastgptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateFastgptLogic {
  22. return &CreateFastgptLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func (l *CreateFastgptLogic) CreateFastgpt(req *types.CreateInfo) (resp *types.BaseMsgResp, err error) {
  28. //创建 fastgpt 用户
  29. user_info := &users.Users{
  30. Status: "active",
  31. Username: req.UserName,
  32. Password: "838b0ad79fa89e8b3a0cfdc6eab90ac24d20e30ca862855b93a4c736f8cdfedf",
  33. Avatar: "/icon/human.svg",
  34. Balance: int32(200000),
  35. PromotionRate: int32(15),
  36. Timezone: "Asia/Shanghai",
  37. }
  38. err = l.svcCtx.MongoModel.UsersModel.Insert(context.TODO(), user_info)
  39. if err != nil {
  40. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  41. }
  42. // 创建团队
  43. teams_info := &teams.Teams{
  44. Name: *req.Title,
  45. OwnerID: user_info.ID,
  46. DefaultPermission: int32(0),
  47. Avatar: "/icon/logo.svg",
  48. CreateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC),
  49. Balance: int32(999900000),
  50. }
  51. err = l.svcCtx.MongoModel.TeamsModel.Insert(context.TODO(), teams_info)
  52. if err != nil {
  53. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  54. }
  55. // 创建团队关系
  56. team_members_info := &team_members.TeamMembers{
  57. TeamID: teams_info.ID,
  58. UserID: user_info.ID,
  59. Name: "Owner",
  60. Role: "owner",
  61. Status: "active",
  62. CreateTime: time.Date(2024, 7, 10, 12, 0, 18, 197000000, time.UTC),
  63. DefaultTeam: true,
  64. Version: int32(0),
  65. }
  66. err = l.svcCtx.MongoModel.TeamMembersModel.Insert(context.TODO(), team_members_info)
  67. if err != nil {
  68. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  69. }
  70. // 创建app
  71. apps_info := &apps.Apps{
  72. ParentID: nil,
  73. TeamID: teams_info.ID,
  74. TmbID: team_members_info.ID,
  75. Name: "默认智能体",
  76. Type: "simple",
  77. Version: "v2",
  78. Avatar: "/imgs/app/avatar/simple.svg",
  79. Intro: "",
  80. TeamTags: []string{},
  81. Modules: []apps.AppModule{
  82. {
  83. NodeID: "userGuide",
  84. Name: "系统配置",
  85. Intro: "",
  86. FlowNodeType: "userGuide",
  87. Position: apps.Position{
  88. X: 531.242273606555,
  89. Y: -486.761172954975,
  90. },
  91. Version: "481",
  92. Inputs: []apps.AppInput{},
  93. Outputs: []apps.AppOutput{},
  94. },
  95. {
  96. NodeID: "workflowStartNodeId",
  97. Name: "流程开始",
  98. Intro: "",
  99. Avatar: "core/workflow/template/workflowStart",
  100. FlowNodeType: "workflowStart",
  101. Position: apps.Position{
  102. X: 558.40823764155,
  103. Y: 123.723874291941,
  104. },
  105. Version: "481",
  106. Inputs: []apps.AppInput{
  107. {
  108. Key: "userChatInput",
  109. RenderTypeList: []string{"reference", "textarea"},
  110. ValueType: "string",
  111. Label: "workflow:user_question",
  112. ToolDescription: "workflow:user_question",
  113. Required: true,
  114. },
  115. },
  116. Outputs: []apps.AppOutput{
  117. {
  118. ID: "userChatInput",
  119. Key: "userChatInput",
  120. Label: "common:core.module.input.label.user question",
  121. Type: "static",
  122. ValueType: "string",
  123. },
  124. {
  125. ID: "userFiles",
  126. Key: "userFiles",
  127. Label: "app:workflow.user_file_input",
  128. Description: "app:workflow.user_file_input_desc",
  129. Type: "static",
  130. ValueType: "arrayString",
  131. },
  132. },
  133. },
  134. {
  135. NodeID: "7BdojPlukIQw",
  136. Name: "AI 对话",
  137. Intro: "AI 大模型对话",
  138. Avatar: "core/workflow/template/aiChat",
  139. FlowNodeType: "chatNode",
  140. ShowStatus: true,
  141. Position: apps.Position{
  142. X: 1106.32383879608,
  143. Y: -350.603067468347,
  144. },
  145. Version: "4813",
  146. Inputs: []apps.AppInput{
  147. {
  148. Key: "model",
  149. RenderTypeList: []string{"settingLLMModel", "reference"},
  150. ValueType: "string",
  151. Value: "DeepSeek-V3",
  152. },
  153. {
  154. Key: "temperature",
  155. RenderTypeList: []string{"hidden"},
  156. ValueType: "number",
  157. Value: int32(0),
  158. Min: getInt32(0),
  159. Max: getInt32(10),
  160. Step: getInt32(1),
  161. },
  162. {
  163. Key: "maxToken",
  164. RenderTypeList: []string{"hidden"},
  165. ValueType: "number",
  166. Value: int32(2000),
  167. Min: getInt32(100),
  168. Max: getInt32(4000),
  169. Step: getInt32(50),
  170. },
  171. {
  172. Key: "isResponseAnswerText",
  173. RenderTypeList: []string{"hidden"},
  174. ValueType: "boolean",
  175. Value: true,
  176. },
  177. {
  178. Key: "aiChatQuoteRole",
  179. RenderTypeList: []string{"hidden"},
  180. ValueType: "string",
  181. Value: "system",
  182. },
  183. {
  184. Key: "quoteTemplate",
  185. RenderTypeList: []string{"hidden"},
  186. ValueType: "string",
  187. },
  188. {
  189. Key: "quotePrompt",
  190. RenderTypeList: []string{"hidden"},
  191. ValueType: "string",
  192. },
  193. {
  194. Key: "systemPrompt",
  195. RenderTypeList: []string{"textarea", "reference"},
  196. Max: getInt32(3000),
  197. ValueType: "string",
  198. Label: "core.ai.Prompt",
  199. Description: "core.app.tip.systemPromptTip",
  200. Placeholder: "core.app.tip.chatNodeSystemPromptTip",
  201. Value: "",
  202. },
  203. {
  204. Key: "history",
  205. RenderTypeList: []string{"numberInput", "reference"},
  206. ValueType: "chatHistory",
  207. Label: "core.module.input.label.chat history",
  208. Required: true,
  209. Min: getInt32(0),
  210. Max: getInt32(30),
  211. Value: int32(6),
  212. },
  213. {
  214. Key: "userChatInput",
  215. RenderTypeList: []string{"reference", "textarea"},
  216. ValueType: "string",
  217. Label: "common:core.module.input.label.user question",
  218. Required: true,
  219. ToolDescription: "common:core.module.input.label.user question",
  220. Value: []interface{}{"workflowStartNodeId", "userChatInput"},
  221. },
  222. {
  223. Key: "quoteQA",
  224. RenderTypeList: []string{"settingDatasetQuotePrompt"},
  225. Label: "",
  226. DebugLabel: "common:core.module.Dataset quote.label",
  227. Description: "",
  228. ValueType: "datasetQuote",
  229. },
  230. {
  231. Key: "fileUrlList",
  232. RenderTypeList: []string{"reference", "input"},
  233. Label: "app:file_quote_link",
  234. DebugLabel: "app:file_quote_link",
  235. ValueType: "arrayString",
  236. Value: [][]interface{}{{"workflowStartNodeId", "userFiles"}},
  237. },
  238. {
  239. Key: "aiChatVision",
  240. RenderTypeList: []string{"hidden"},
  241. ValueType: "boolean",
  242. Value: true,
  243. },
  244. },
  245. Outputs: []apps.AppOutput{
  246. {
  247. ID: "history",
  248. Key: "history",
  249. Required: true,
  250. Label: "common:core.module.output.label.New context",
  251. Description: "common:core.module.output.description.New context",
  252. ValueType: "chatHistory",
  253. ValueDesc: "{\n obj: System | Human | AI;\n value: string;\n}[]",
  254. Type: "static",
  255. },
  256. {
  257. ID: "answerText",
  258. Key: "answerText",
  259. Required: true,
  260. Label: "common:core.module.output.label.Ai response content",
  261. Description: "common:core.module.output.description.Ai response content",
  262. ValueType: "string",
  263. Type: "static",
  264. },
  265. },
  266. },
  267. },
  268. Edges: []apps.Edge{
  269. {
  270. Source: "workflowStartNodeId",
  271. Target: "7BdojPlukIQw",
  272. SourceHandle: "workflowStartNodeId-source-right",
  273. TargetHandle: "7BdojPlukIQw-target-left",
  274. },
  275. },
  276. PluginData: apps.PluginData{
  277. ID: mustParseObjectID("67da46b29667c5bf21203554"),
  278. NodeVersion: "67da46d29667c5bf2120361a",
  279. },
  280. InheritPermission: true,
  281. VersionNumber: int32(0),
  282. ChatConfig: apps.ChatConfig{
  283. WelcomeText: "",
  284. Variables: []interface{}{},
  285. QuestionGuide: false,
  286. TTSConfig: apps.TTSConfig{
  287. Type: "web",
  288. },
  289. WhisperConfig: apps.WhisperConfig{
  290. Open: false,
  291. AutoSend: false,
  292. AutoTTSResponse: false,
  293. },
  294. ScheduledTriggerConfig: nil,
  295. ChatInputGuide: apps.ChatInputGuide{
  296. Open: false,
  297. TextList: []string{},
  298. CustomUrl: "",
  299. },
  300. Instruction: "",
  301. ID: mustParseObjectID("67da46d29667c5bf2120361d"),
  302. },
  303. UpdateTime: time.Date(2025, 3, 19, 4, 24, 4, 394000000, time.UTC),
  304. ScheduledTriggerConfig: nil,
  305. ScheduledTriggerNextTime: nil,
  306. }
  307. err = l.svcCtx.MongoModel.AppsModel.Insert(context.TODO(), apps_info)
  308. if err != nil {
  309. return nil, errorx.NewInvalidArgumentError("fastgpt create failed " + err.Error())
  310. }
  311. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  312. }
  313. // 添加 mustParseObjectID 函数
  314. func mustParseObjectID(hex string) primitive.ObjectID {
  315. id, err := primitive.ObjectIDFromHex(hex)
  316. if err != nil {
  317. panic(err) // 或者根据需要处理错误
  318. }
  319. return id
  320. }
  321. func getInt32(n int32) *int32 {
  322. num := int32(n)
  323. return &num
  324. }