devme commited on
Commit
4c54676
·
verified ·
1 Parent(s): c76ff4a

Update src/index.js

Browse files
Files changed (1) hide show
  1. src/index.js +16 -1
src/index.js CHANGED
@@ -10,6 +10,16 @@ app.use(bodyParser.json())
10
  app.use(bodyParser.json({ limit: '50mb' }))
11
  app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
12
 
 
 
 
 
 
 
 
 
 
 
13
  app.get('/api/v1/models', async (req, res) => {
14
  try {
15
  const response = await axios.get('https://chat.qwenlm.ai/api/models',
@@ -82,7 +92,12 @@ app.post('/api/v1/chat/completions', async (req, res) => {
82
 
83
  const lists = decodeText.split('\n').filter(item => item.trim() !== '')
84
  for (const item of lists) {
85
- const decodeJson = JSON.parse(item.replace(/^data: /, ''))
 
 
 
 
 
86
  let content = decodeJson.choices[0].delta.content
87
 
88
  if (backContent === null) {
 
10
  app.use(bodyParser.json({ limit: '50mb' }))
11
  app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))
12
 
13
+
14
+ const isJson = (str) => {
15
+ try {
16
+ JSON.parse(str)
17
+ return true
18
+ } catch (error) {
19
+ return false
20
+ }
21
+ }
22
+
23
  app.get('/api/v1/models', async (req, res) => {
24
  try {
25
  const response = await axios.get('https://chat.qwenlm.ai/api/models',
 
92
 
93
  const lists = decodeText.split('\n').filter(item => item.trim() !== '')
94
  for (const item of lists) {
95
+ const decodeJson = isJson(item.replace(/^data: /, '')) ? JSON.parse(item.replace(/^data: /, '')) : null
96
+
97
+ if (decodeJson === null) {
98
+ continue
99
+ }
100
+
101
  let content = decodeJson.choices[0].delta.content
102
 
103
  if (backContent === null) {