q-koa 13.0.3 → 13.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -594,12 +594,11 @@ exports.sql = async (ctx) => {
594
594
 
595
595
  exports.gpt = async (ctx) => {
596
596
  const { app, appName } = getAppByCtx(ctx)
597
- const appConfig = getConfig(app)
598
- const { content } = ctx.request.body
599
- if (!content) return ctx.ERROR('?content')
600
- if (!app.service.common) throw new Error(`common可能没有service.js`)
601
- const result = await app.service.common.gpt({
602
- content,
603
- })
604
- return ctx.SUCCESS(result)
597
+ const { content = '' } = ctx.request.body
598
+
599
+ if (!content) return ctx.ERROR('没有内容')
600
+
601
+ const res = await app.service.common.gpt({ app, content })
602
+
603
+ ctx.SUCCESS(res)
605
604
  }
@@ -1,42 +1,36 @@
1
+ const { getConfig, ServiceError } = require('q-koa')
1
2
  const axios = require('axios')
2
3
  const XLSX = require('xlsx')
3
4
  const moment = require('moment')
4
5
 
5
- exports.gpt = async ({ content }) => {
6
- let result = ''
7
- let messages = [
8
- {
9
- content,
10
- role: 'user',
11
- },
12
- ]
6
+ exports.gpt = async ({ app, content }) => {
7
+ if (!content) throw new ServiceError('?content')
13
8
 
14
- for (let i = 0; i < 1000; i++) {
15
- try {
16
- const { choices } = await axios
17
- .post('https://gpt.likangjian.cn:3001/gpt/chat', {
18
- messages,
19
- })
20
- .then((res) => res.data)
21
- console.log(choices.finish_reason, 'REPLY:', choices[0].message.content)
22
- result += choices[0].message.content
23
- if (choices.finish_reason === 'stop' || !choices.finish_reason) {
24
- break
25
- }
26
- messages = [
27
- ...messages,
28
- choices[0].message,
29
- {
30
- content: '继续',
31
- role: 'user',
9
+ const appConfig = getConfig(app)
10
+ const { ai_key } = await appConfig.getObject('base')
11
+ const url =
12
+ 'https://dashscope.aliyuncs.com/api/v1/apps/69c314f8f87e4a2080af950e5997bdc6/completion'
13
+
14
+ const {
15
+ output: { text },
16
+ } = await axios
17
+ .post(
18
+ url,
19
+ {
20
+ input: {
21
+ prompt: content,
32
22
  },
33
- ]
34
- } catch (e) {
35
- throw new Error('ai开小差了')
36
- }
37
- }
23
+ },
24
+ {
25
+ headers: {
26
+ Authorization: `Bearer ${ai_key}`,
27
+ 'Content-Type': 'application/json',
28
+ },
29
+ }
30
+ )
31
+ .then((res) => res.data)
38
32
 
39
- return result
33
+ return text
40
34
  }
41
35
 
42
36
  exports.formatXLSX = ({ header, data, fileName: _fileName }) => {
@@ -38,11 +38,12 @@ module.exports = class Singleton {
38
38
  throw new Error(result.msg)
39
39
  }
40
40
  } catch (e) {
41
+ const message = ` ${no} ${e.message || '物流服务暂不可用'}`
41
42
  push({
42
43
  appName: 'express',
43
- message: '物流服务暂不可用',
44
+ message,
44
45
  })
45
- throw new Error('物流服务暂不可用')
46
+ throw new Error(message)
46
47
  }
47
48
  }
48
49
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "q-koa",
3
- "version": "13.0.3",
3
+ "version": "13.0.5",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {