create_app_logic.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package fastgpt
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  7. "net/http"
  8. "strconv"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type CreateAppLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewCreateAppLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAppLogic {
  19. return &CreateAppLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *CreateAppLogic) CreateApp(req *types.CreateAppsReq) (resp *types.BaseMsgResp, err error) {
  25. organizationId := l.ctx.Value("organizationId").(uint64)
  26. organizationIdStr := strconv.FormatUint(organizationId, 10)
  27. token, err := getToken(organizationIdStr)
  28. if err != nil {
  29. return nil, fmt.Errorf("invalid token")
  30. }
  31. AppCreate(req.Type, req.Name, token)
  32. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  33. }
  34. func AppCreate(apps_type string, name string, token string) {
  35. url := "https://fastgpt.gkscrm.com/api/core/app/create"
  36. jsonData := ""
  37. if apps_type == "simple" {
  38. jsonData = fmt.Sprintf(`{
  39. "parentId": "",
  40. "avatar": "core/app/type/simpleFill",
  41. "name": "%s",
  42. "type": "simple",
  43. "modules": [
  44. {
  45. "nodeId": "userGuide",
  46. "name": "common:core.module.template.system_config",
  47. "intro": "common:core.module.template.config_params",
  48. "avatar": "core/workflow/template/systemConfig",
  49. "flowNodeType": "userGuide",
  50. "position": {
  51. "x": 531.2422736065552,
  52. "y": -486.7611729549753
  53. },
  54. "version": "481",
  55. "inputs": [
  56. {
  57. "key": "welcomeText",
  58. "renderTypeList": [
  59. "hidden"
  60. ],
  61. "valueType": "string",
  62. "label": "core.app.Welcome Text",
  63. "value": ""
  64. },
  65. {
  66. "key": "variables",
  67. "renderTypeList": [
  68. "hidden"
  69. ],
  70. "valueType": "any",
  71. "label": "core.app.Chat Variable",
  72. "value": []
  73. },
  74. {
  75. "key": "questionGuide",
  76. "valueType": "object",
  77. "renderTypeList": [
  78. "hidden"
  79. ],
  80. "label": "core.app.Question Guide",
  81. "value": {
  82. "open": false
  83. }
  84. },
  85. {
  86. "key": "tts",
  87. "renderTypeList": [
  88. "hidden"
  89. ],
  90. "valueType": "any",
  91. "label": "",
  92. "value": {
  93. "type": "web"
  94. }
  95. },
  96. {
  97. "key": "whisper",
  98. "renderTypeList": [
  99. "hidden"
  100. ],
  101. "valueType": "any",
  102. "label": "",
  103. "value": {
  104. "open": false,
  105. "autoSend": false,
  106. "autoTTSResponse": false
  107. }
  108. },
  109. {
  110. "key": "scheduleTrigger",
  111. "renderTypeList": [
  112. "hidden"
  113. ],
  114. "valueType": "any",
  115. "label": "",
  116. "value": null
  117. }
  118. ],
  119. "outputs": []
  120. },
  121. {
  122. "nodeId": "448745",
  123. "name": "common:core.module.template.work_start",
  124. "intro": "",
  125. "avatar": "core/workflow/template/workflowStart",
  126. "flowNodeType": "workflowStart",
  127. "position": {
  128. "x": 558.4082376415505,
  129. "y": 123.72387429194112
  130. },
  131. "version": "481",
  132. "inputs": [
  133. {
  134. "key": "userChatInput",
  135. "renderTypeList": [
  136. "reference",
  137. "textarea"
  138. ],
  139. "valueType": "string",
  140. "label": "common:core.module.input.label.user question",
  141. "required": true,
  142. "toolDescription": "common:core.module.input.label.user question"
  143. }
  144. ],
  145. "outputs": [
  146. {
  147. "id": "userChatInput",
  148. "key": "userChatInput",
  149. "label": "core.module.input.label.user question",
  150. "valueType": "string",
  151. "type": "static"
  152. }
  153. ]
  154. },
  155. {
  156. "nodeId": "loOvhld2ZTKa",
  157. "name": "common:core.module.template.ai_chat",
  158. "intro": "common:core.module.template.ai_chat_intro",
  159. "avatar": "core/workflow/template/aiChat",
  160. "flowNodeType": "chatNode",
  161. "showStatus": true,
  162. "position": {
  163. "x": 1097.7317280958762,
  164. "y": -244.16014496351386
  165. },
  166. "version": "481",
  167. "inputs": [
  168. {
  169. "key": "model",
  170. "renderTypeList": [
  171. "settingLLMModel",
  172. "reference"
  173. ],
  174. "label": "core.module.input.label.aiModel",
  175. "valueType": "string",
  176. "value": "gpt-4o-mini"
  177. },
  178. {
  179. "key": "temperature",
  180. "renderTypeList": [
  181. "hidden"
  182. ],
  183. "label": "",
  184. "value": 0,
  185. "valueType": "number",
  186. "min": 0,
  187. "max": 10,
  188. "step": 1
  189. },
  190. {
  191. "key": "maxToken",
  192. "renderTypeList": [
  193. "hidden"
  194. ],
  195. "label": "",
  196. "value": 2000,
  197. "valueType": "number",
  198. "min": 100,
  199. "max": 4000,
  200. "step": 50
  201. },
  202. {
  203. "key": "isResponseAnswerText",
  204. "renderTypeList": [
  205. "hidden"
  206. ],
  207. "label": "",
  208. "value": true,
  209. "valueType": "boolean"
  210. },
  211. {
  212. "key": "quoteTemplate",
  213. "renderTypeList": [
  214. "hidden"
  215. ],
  216. "label": "",
  217. "valueType": "string"
  218. },
  219. {
  220. "key": "quotePrompt",
  221. "renderTypeList": [
  222. "hidden"
  223. ],
  224. "label": "",
  225. "valueType": "string"
  226. },
  227. {
  228. "key": "systemPrompt",
  229. "renderTypeList": [
  230. "textarea",
  231. "reference"
  232. ],
  233. "max": 3000,
  234. "valueType": "string",
  235. "label": "core.ai.Prompt",
  236. "description": "core.app.tip.systemPromptTip",
  237. "placeholder": "core.app.tip.chatNodeSystemPromptTip",
  238. "value": ""
  239. },
  240. {
  241. "key": "history",
  242. "renderTypeList": [
  243. "numberInput",
  244. "reference"
  245. ],
  246. "valueType": "chatHistory",
  247. "label": "core.module.input.label.chat history",
  248. "required": true,
  249. "min": 0,
  250. "max": 30,
  251. "value": 6
  252. },
  253. {
  254. "key": "userChatInput",
  255. "renderTypeList": [
  256. "reference",
  257. "textarea"
  258. ],
  259. "valueType": "string",
  260. "label": "common:core.module.input.label.user question",
  261. "required": true,
  262. "toolDescription": "common:core.module.input.label.user question",
  263. "value": [
  264. "448745",
  265. "userChatInput"
  266. ]
  267. },
  268. {
  269. "key": "quoteQA",
  270. "renderTypeList": [
  271. "settingDatasetQuotePrompt"
  272. ],
  273. "label": "",
  274. "debugLabel": "common:core.module.Dataset quote.label",
  275. "description": "",
  276. "valueType": "datasetQuote"
  277. }
  278. ],
  279. "outputs": [
  280. {
  281. "id": "history",
  282. "key": "history",
  283. "label": "core.module.output.label.New context",
  284. "description": "core.module.output.description.New context",
  285. "valueType": "chatHistory",
  286. "type": "static"
  287. },
  288. {
  289. "id": "answerText",
  290. "key": "answerText",
  291. "label": "core.module.output.label.Ai response content",
  292. "description": "core.module.output.description.Ai response content",
  293. "valueType": "string",
  294. "type": "static"
  295. }
  296. ]
  297. }
  298. ],
  299. "edges": [
  300. {
  301. "source": "448745",
  302. "target": "loOvhld2ZTKa",
  303. "sourceHandle": "448745-source-right",
  304. "targetHandle": "loOvhld2ZTKa-target-left"
  305. }
  306. ]
  307. }`, name)
  308. } else {
  309. jsonData = fmt.Sprintf(`{"parentId":"","avatar":"core/app/type/workflowFill","name":"%s","type":"advanced","modules":[{"nodeId":"userGuide","name":"common:core.module.template.system_config","intro":"common:core.module.template.system_config_info","avatar":"core/workflow/template/systemConfig","flowNodeType":"userGuide","position":{"x":262.2732338817093,"y":-476.00241136598146},"version":"481","inputs":[{"key":"welcomeText","renderTypeList":["hidden"],"valueType":"string","label":"core.app.Welcome Text","value":""},{"key":"variables","renderTypeList":["hidden"],"valueType":"any","label":"core.app.Chat Variable","value":[]},{"key":"questionGuide","valueType":"any","renderTypeList":["hidden"],"label":"core.app.Question Guide","value":{"open":false}},{"key":"tts","renderTypeList":["hidden"],"valueType":"any","label":"","value":{"type":"web"}},{"key":"whisper","renderTypeList":["hidden"],"valueType":"any","label":"","value":{"open":false,"autoSend":false,"autoTTSResponse":false}},{"key":"scheduleTrigger","renderTypeList":["hidden"],"valueType":"any","label":"","value":null}],"outputs":[]},{"nodeId":"448745","name":"common:core.module.template.work_start","intro":"","avatar":"core/workflow/template/workflowStart","flowNodeType":"workflowStart","position":{"x":632.368838596004,"y":-347.7446492944009},"version":"481","inputs":[{"key":"userChatInput","renderTypeList":["reference","textarea"],"valueType":"string","label":"common:core.module.input.label.user question","required":true,"toolDescription":"common:core.module.input.label.user question"}],"outputs":[{"id":"userChatInput","key":"userChatInput","label":"common:core.module.input.label.user question","type":"static","valueType":"string"}]}],"edges":[]}`, name)
  310. }
  311. requestBody := []byte(jsonData)
  312. // 创建一个新的请求
  313. req1, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
  314. if err != nil {
  315. fmt.Printf("Error creating request: %v\n", err)
  316. return
  317. }
  318. // 设置请求头
  319. req1.Header.Set("Content-Type", "application/json")
  320. req1.Header.Set("Cookie", fmt.Sprintf("fastgpt_token=%s", token))
  321. // 使用 http.Client 发送请求
  322. client := &http.Client{}
  323. resp1, err := client.Do(req1)
  324. logx.Info("AppCreate resp1:", resp1)
  325. logx.Info("AppCreate err:", err)
  326. if err != nil {
  327. fmt.Printf("Error sending request: %v\n", err)
  328. return
  329. }
  330. defer resp1.Body.Close()
  331. return
  332. }