apps.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_'
  2. function randomString (length) {
  3. let result = ''
  4. for (let i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)]
  5. return result
  6. }
  7. // https://www.notion.so/55773516a0194781ae211792a44a3663?pvs=4
  8. const VirtualData = new Array(10).fill().map((_, index) => {
  9. const date = new Date(Date.now() - index * 24 * 60 * 60 * 1000)
  10. return {
  11. date: `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`,
  12. conversation_count: Math.floor(Math.random() * 10) + index,
  13. terminal_count: Math.floor(Math.random() * 10) + index,
  14. token_count: Math.floor(Math.random() * 10) + index,
  15. total_price: Math.floor(Math.random() * 10) + index,
  16. }
  17. })
  18. const registerAPI = function (app) {
  19. const apps = [{
  20. id: '1',
  21. name: 'chat app',
  22. mode: 'chat',
  23. description: 'description01',
  24. enable_site: true,
  25. enable_api: true,
  26. api_rpm: 60,
  27. api_rph: 3600,
  28. is_demo: false,
  29. model_config: {
  30. provider: 'OPENAI',
  31. model_id: 'gpt-3.5-turbo',
  32. configs: {
  33. prompt_template: '你是我的解梦小助手,请参考 {{book}} 回答我有关梦境的问题。在回答前请称呼我为 {{myName}}。',
  34. prompt_variables: [
  35. {
  36. key: 'book',
  37. name: '书',
  38. value: '《梦境解析》',
  39. type: 'string',
  40. description: '请具体说下书名'
  41. },
  42. {
  43. key: 'myName',
  44. name: 'your name',
  45. value: 'Book',
  46. type: 'string',
  47. description: 'please tell me your name'
  48. }
  49. ],
  50. completion_params: {
  51. max_token: 16,
  52. temperature: 1, // 0-2
  53. top_p: 1,
  54. presence_penalty: 1, // -2-2
  55. frequency_penalty: 1, // -2-2
  56. }
  57. }
  58. },
  59. site: {
  60. access_token: '1000',
  61. title: 'site 01',
  62. author: 'John',
  63. default_language: 'zh-Hans-CN',
  64. customize_domain: 'http://customize_domain',
  65. theme: 'theme',
  66. customize_token_strategy: 'must',
  67. prompt_public: true
  68. }
  69. },
  70. {
  71. id: '2',
  72. name: 'completion app',
  73. mode: 'completion', // genertation text
  74. description: 'description 02', // genertation text
  75. enable_site: false,
  76. enable_api: false,
  77. api_rpm: 60,
  78. api_rph: 3600,
  79. is_demo: false,
  80. model_config: {
  81. provider: 'OPENAI',
  82. model_id: 'text-davinci-003',
  83. configs: {
  84. prompt_template: '你是我的翻译小助手,请把以下内容 {{langA}} 翻译成 {{langB}},以下的内容:',
  85. prompt_variables: [
  86. {
  87. key: 'langA',
  88. name: '原始语音',
  89. value: '中文',
  90. type: 'string',
  91. description: '这是中文格式的原始语音'
  92. },
  93. {
  94. key: 'langB',
  95. name: '目标语言',
  96. value: '英语',
  97. type: 'string',
  98. description: '这是英语格式的目标语言'
  99. }
  100. ],
  101. completion_params: {
  102. max_token: 16,
  103. temperature: 1, // 0-2
  104. top_p: 1,
  105. presence_penalty: 1, // -2-2
  106. frequency_penalty: 1, // -2-2
  107. }
  108. }
  109. },
  110. site: {
  111. access_token: '2000',
  112. title: 'site 02',
  113. author: 'Mark',
  114. default_language: 'en-US',
  115. customize_domain: 'http://customize_domain',
  116. theme: 'theme',
  117. customize_token_strategy: 'must',
  118. prompt_public: false
  119. }
  120. },
  121. ]
  122. const apikeys = [{
  123. id: '111121312313132',
  124. token: 'sk-DEFGHJKMNPQRSTWXYZabcdefhijk1234',
  125. last_used_at: '1679212138000',
  126. created_at: '1673316000000'
  127. }, {
  128. id: '43441242131223123',
  129. token: 'sk-EEFGHJKMNPQRSTWXYZabcdefhijk5678',
  130. last_used_at: '1679212721000',
  131. created_at: '1679212731000'
  132. }]
  133. // create app
  134. app.post('/apps', async (req, res) => {
  135. apps.push({
  136. id: apps.length + 1 + '',
  137. ...req.body,
  138. })
  139. res.send({
  140. result: 'success'
  141. })
  142. })
  143. // app list
  144. app.get('/apps', async (req, res) => {
  145. res.send({
  146. data: apps
  147. })
  148. })
  149. // app detail
  150. app.get('/apps/:id', async (req, res) => {
  151. const item = apps.find(item => item.id === req.params.id) || apps[0]
  152. res.send(item)
  153. })
  154. // update app name
  155. app.post('/apps/:id/name', async (req, res) => {
  156. const item = apps.find(item => item.id === req.params.id)
  157. item.name = req.body.name
  158. res.send(item || null)
  159. })
  160. // update app site-enable status
  161. app.post('/apps/:id/site-enable', async (req, res) => {
  162. const item = apps.find(item => item.id === req.params.id)
  163. console.log(item)
  164. item.enable_site = req.body.enable_site
  165. res.send(item || null)
  166. })
  167. // update app api-enable status
  168. app.post('/apps/:id/api-enable', async (req, res) => {
  169. const item = apps.find(item => item.id === req.params.id)
  170. console.log(item)
  171. item.enable_api = req.body.enable_api
  172. res.send(item || null)
  173. })
  174. // update app rate-limit
  175. app.post('/apps/:id/rate-limit', async (req, res) => {
  176. const item = apps.find(item => item.id === req.params.id)
  177. console.log(item)
  178. item.api_rpm = req.body.api_rpm
  179. item.api_rph = req.body.api_rph
  180. res.send(item || null)
  181. })
  182. // update app url including code
  183. app.post('/apps/:id/site/access-token-reset', async (req, res) => {
  184. const item = apps.find(item => item.id === req.params.id)
  185. console.log(item)
  186. item.site.access_token = randomString(12)
  187. res.send(item || null)
  188. })
  189. // update app config
  190. app.post('/apps/:id/site', async (req, res) => {
  191. const item = apps.find(item => item.id === req.params.id)
  192. console.log(item)
  193. item.name = req.body.title
  194. item.description = req.body.description
  195. item.prompt_public = req.body.prompt_public
  196. item.default_language = req.body.default_language
  197. res.send(item || null)
  198. })
  199. // get statistics daily-conversations
  200. app.get('/apps/:id/statistics/daily-conversations', async (req, res) => {
  201. const item = apps.find(item => item.id === req.params.id)
  202. if (item) {
  203. res.send({
  204. data: VirtualData
  205. })
  206. } else {
  207. res.send({
  208. data: []
  209. })
  210. }
  211. })
  212. // get statistics daily-end-users
  213. app.get('/apps/:id/statistics/daily-end-users', async (req, res) => {
  214. const item = apps.find(item => item.id === req.params.id)
  215. if (item) {
  216. res.send({
  217. data: VirtualData
  218. })
  219. } else {
  220. res.send({
  221. data: []
  222. })
  223. }
  224. })
  225. // get statistics token-costs
  226. app.get('/apps/:id/statistics/token-costs', async (req, res) => {
  227. const item = apps.find(item => item.id === req.params.id)
  228. if (item) {
  229. res.send({
  230. data: VirtualData
  231. })
  232. } else {
  233. res.send({
  234. data: []
  235. })
  236. }
  237. })
  238. // update app model config
  239. app.post('/apps/:id/model-config', async (req, res) => {
  240. const item = apps.find(item => item.id === req.params.id)
  241. console.log(item)
  242. item.model_config = req.body
  243. res.send(item || null)
  244. })
  245. // get api keys list
  246. app.get('/apps/:id/api-keys', async (req, res) => {
  247. res.send({
  248. data: apikeys
  249. })
  250. })
  251. // del api key
  252. app.delete('/apps/:id/api-keys/:api_key_id', async (req, res) => {
  253. res.send({
  254. result: 'success'
  255. })
  256. })
  257. // create api key
  258. app.post('/apps/:id/api-keys', async (req, res) => {
  259. res.send({
  260. id: 'e2424241313131',
  261. token: 'sk-GEFGHJKMNPQRSTWXYZabcdefhijk0124',
  262. created_at: '1679216688962'
  263. })
  264. })
  265. // get completion-conversations
  266. app.get('/apps/:id/completion-conversations', async (req, res) => {
  267. const data = {
  268. data: [{
  269. id: 1,
  270. from_end_user_id: 'user 1',
  271. summary: 'summary1',
  272. created_at: '2023-10-11',
  273. annotated: true,
  274. message_count: 100,
  275. user_feedback_stats: {
  276. like: 4, dislike: 5
  277. },
  278. admin_feedback_stats: {
  279. like: 1, dislike: 2
  280. },
  281. message: {
  282. message: 'message1',
  283. query: 'question1',
  284. answer: 'answer1'
  285. }
  286. }, {
  287. id: 12,
  288. from_end_user_id: 'user 2',
  289. summary: 'summary2',
  290. created_at: '2023-10-01',
  291. annotated: false,
  292. message_count: 10,
  293. user_feedback_stats: {
  294. like: 2, dislike: 20
  295. },
  296. admin_feedback_stats: {
  297. like: 12, dislike: 21
  298. },
  299. message: {
  300. message: 'message2',
  301. query: 'question2',
  302. answer: 'answer2'
  303. }
  304. }, {
  305. id: 13,
  306. from_end_user_id: 'user 3',
  307. summary: 'summary3',
  308. created_at: '2023-10-11',
  309. annotated: false,
  310. message_count: 20,
  311. user_feedback_stats: {
  312. like: 2, dislike: 0
  313. },
  314. admin_feedback_stats: {
  315. like: 0, dislike: 21
  316. },
  317. message: {
  318. message: 'message3',
  319. query: 'question3',
  320. answer: 'answer3'
  321. }
  322. }],
  323. total: 200
  324. }
  325. res.send(data)
  326. })
  327. // get chat-conversations
  328. app.get('/apps/:id/chat-conversations', async (req, res) => {
  329. const data = {
  330. data: [{
  331. id: 1,
  332. from_end_user_id: 'user 1',
  333. summary: 'summary1',
  334. created_at: '2023-10-11',
  335. read_at: '2023-10-12',
  336. annotated: true,
  337. message_count: 100,
  338. user_feedback_stats: {
  339. like: 4, dislike: 5
  340. },
  341. admin_feedback_stats: {
  342. like: 1, dislike: 2
  343. },
  344. message: {
  345. message: 'message1',
  346. query: 'question1',
  347. answer: 'answer1'
  348. }
  349. }, {
  350. id: 12,
  351. from_end_user_id: 'user 2',
  352. summary: 'summary2',
  353. created_at: '2023-10-01',
  354. annotated: false,
  355. message_count: 10,
  356. user_feedback_stats: {
  357. like: 2, dislike: 20
  358. },
  359. admin_feedback_stats: {
  360. like: 12, dislike: 21
  361. },
  362. message: {
  363. message: 'message2',
  364. query: 'question2',
  365. answer: 'answer2'
  366. }
  367. }, {
  368. id: 13,
  369. from_end_user_id: 'user 3',
  370. summary: 'summary3',
  371. created_at: '2023-10-11',
  372. annotated: false,
  373. message_count: 20,
  374. user_feedback_stats: {
  375. like: 2, dislike: 0
  376. },
  377. admin_feedback_stats: {
  378. like: 0, dislike: 21
  379. },
  380. message: {
  381. message: 'message3',
  382. query: 'question3',
  383. answer: 'answer3'
  384. }
  385. }],
  386. total: 200
  387. }
  388. res.send(data)
  389. })
  390. // get completion-conversation detail
  391. app.get('/apps/:id/completion-conversations/:cid', async (req, res) => {
  392. const data =
  393. {
  394. id: 1,
  395. from_end_user_id: 'user 1',
  396. summary: 'summary1',
  397. created_at: '2023-10-11',
  398. annotated: true,
  399. message: {
  400. message: 'question1',
  401. // query: 'question1',
  402. answer: 'answer1',
  403. annotation: {
  404. content: '这是一段纠正的内容'
  405. }
  406. },
  407. model_config: {
  408. provider: 'openai',
  409. model_id: 'model_id',
  410. configs: {
  411. prompt_template: '你是我的翻译小助手,请把以下内容 {{langA}} 翻译成 {{langB}},以下的内容:{{content}}'
  412. }
  413. }
  414. }
  415. res.send(data)
  416. })
  417. // get chat-conversation detail
  418. app.get('/apps/:id/chat-conversations/:cid', async (req, res) => {
  419. const data =
  420. {
  421. id: 1,
  422. from_end_user_id: 'user 1',
  423. summary: 'summary1',
  424. created_at: '2023-10-11',
  425. annotated: true,
  426. message: {
  427. message: 'question1',
  428. // query: 'question1',
  429. answer: 'answer1',
  430. created_at: '2023-08-09 13:00',
  431. provider_response_latency: 130,
  432. message_tokens: 230
  433. },
  434. model_config: {
  435. provider: 'openai',
  436. model_id: 'model_id',
  437. configs: {
  438. prompt_template: '你是我的翻译小助手,请把以下内容 {{langA}} 翻译成 {{langB}},以下的内容:{{content}}'
  439. }
  440. }
  441. }
  442. res.send(data)
  443. })
  444. // get chat-conversation message list
  445. app.get('/apps/:id/chat-messages', async (req, res) => {
  446. const data = {
  447. data: [{
  448. id: 1,
  449. created_at: '2023-10-11 07:09',
  450. message: '请说说人为什么会做梦?' + req.query.conversation_id,
  451. answer: '梦境通常是个人内心深处的反映,很难确定每个人梦境的确切含义,因为它们可能会受到梦境者的文化背景、生活经验和情感状态等多种因素的影响。',
  452. provider_response_latency: 450,
  453. answer_tokens: 200,
  454. annotation: {
  455. content: 'string',
  456. account: {
  457. id: 'string',
  458. name: 'string',
  459. email: 'string'
  460. }
  461. },
  462. feedbacks: {
  463. rating: 'like',
  464. content: 'string',
  465. from_source: 'log'
  466. }
  467. }, {
  468. id: 2,
  469. created_at: '2023-10-11 8:23',
  470. message: '夜里经常做梦会影响次日的精神状态吗?',
  471. answer: '总之,这个梦境可能与梦境者的个人经历和情感状态有关,但在一般情况下,它可能表示一种强烈的情感反应,包括愤怒、不满和对于正义和自由的渴望。',
  472. provider_response_latency: 400,
  473. answer_tokens: 250,
  474. annotation: {
  475. content: 'string',
  476. account: {
  477. id: 'string',
  478. name: 'string',
  479. email: 'string'
  480. }
  481. },
  482. // feedbacks: {
  483. // rating: 'like',
  484. // content: 'string',
  485. // from_source: 'log'
  486. // }
  487. }, {
  488. id: 3,
  489. created_at: '2023-10-11 10:20',
  490. message: '梦见在山上手撕鬼子,大师解解梦',
  491. answer: '但是,一般来说,“手撕鬼子”这个场景可能是梦境者对于过去历史上的战争、侵略以及对于自己国家和族群的保护与维护的情感反应。在梦中,你可能会感到自己充满力量和勇气,去对抗那些看似强大的侵略者。',
  492. provider_response_latency: 288,
  493. answer_tokens: 100,
  494. annotation: {
  495. content: 'string',
  496. account: {
  497. id: 'string',
  498. name: 'string',
  499. email: 'string'
  500. }
  501. },
  502. feedbacks: {
  503. rating: 'dislike',
  504. content: 'string',
  505. from_source: 'log'
  506. }
  507. }],
  508. limit: 20,
  509. has_more: true
  510. }
  511. res.send(data)
  512. })
  513. app.post('/apps/:id/annotations', async (req, res) => {
  514. res.send({ result: 'success' })
  515. })
  516. app.post('/apps/:id/feedbacks', async (req, res) => {
  517. res.send({ result: 'success' })
  518. })
  519. }
  520. module.exports = registerAPI