mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 17:01:00 +03:00
Merge pull request #259 from FlowiseAI/bugfix/UnexpectedTokenInJSONError
bugfix/UnexpectedTokenInJSONError
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { ICommonObject, INode, INodeData, INodeParams, PromptTemplate } from '../../../src/Interface'
|
import { ICommonObject, INode, INodeData, INodeParams, PromptTemplate } from '../../../src/Interface'
|
||||||
import { getBaseClasses, getInputVariables } from '../../../src/utils'
|
import { getBaseClasses, getInputVariables, returnJSONStr } from '../../../src/utils'
|
||||||
import { PromptTemplateInput } from 'langchain/prompts'
|
import { PromptTemplateInput } from 'langchain/prompts'
|
||||||
|
|
||||||
class PromptTemplate_Prompts implements INode {
|
class PromptTemplate_Prompts implements INode {
|
||||||
@@ -46,11 +46,12 @@ class PromptTemplate_Prompts implements INode {
|
|||||||
|
|
||||||
async init(nodeData: INodeData): Promise<any> {
|
async init(nodeData: INodeData): Promise<any> {
|
||||||
const template = nodeData.inputs?.template as string
|
const template = nodeData.inputs?.template as string
|
||||||
const promptValuesStr = nodeData.inputs?.promptValues as string
|
let promptValuesStr = nodeData.inputs?.promptValues as string
|
||||||
|
|
||||||
let promptValues: ICommonObject = {}
|
let promptValues: ICommonObject = {}
|
||||||
if (promptValuesStr) {
|
if (promptValuesStr) {
|
||||||
promptValues = JSON.parse(promptValuesStr.replace(/\s/g, ''))
|
promptValuesStr = promptValuesStr.replace(/\s/g, '')
|
||||||
|
promptValues = JSON.parse(returnJSONStr(promptValuesStr))
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputVariables = getInputVariables(template)
|
const inputVariables = getInputVariables(template)
|
||||||
|
|||||||
@@ -234,3 +234,44 @@ export class CustomChainHandler extends BaseCallbackHandler {
|
|||||||
this.socketIO.to(this.socketIOClientId).emit('end')
|
this.socketIO.to(this.socketIOClientId).emit('end')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const returnJSONStr = (jsonStr: string): string => {
|
||||||
|
let jsonStrArray = jsonStr.split(':')
|
||||||
|
|
||||||
|
let wholeString = ''
|
||||||
|
for (let i = 0; i < jsonStrArray.length; i++) {
|
||||||
|
if (jsonStrArray[i].includes(',') && jsonStrArray[i + 1] !== undefined) {
|
||||||
|
const splitValueAndTitle = jsonStrArray[i].split(',')
|
||||||
|
const value = splitValueAndTitle[0]
|
||||||
|
const newTitle = splitValueAndTitle[1]
|
||||||
|
wholeString += handleEscapeDoubleQuote(value) + ',' + newTitle + ':'
|
||||||
|
} else {
|
||||||
|
wholeString += wholeString === '' ? jsonStrArray[i] + ':' : handleEscapeDoubleQuote(jsonStrArray[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return wholeString
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleEscapeDoubleQuote = (value: string): string => {
|
||||||
|
let newValue = ''
|
||||||
|
if (value.includes('"')) {
|
||||||
|
const valueArray = value.split('"')
|
||||||
|
for (let i = 0; i < valueArray.length; i++) {
|
||||||
|
if ((i + 1) % 2 !== 0) {
|
||||||
|
switch (valueArray[i]) {
|
||||||
|
case '':
|
||||||
|
newValue += '"'
|
||||||
|
break
|
||||||
|
case '}':
|
||||||
|
newValue += '"}'
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
newValue += '\\"' + valueArray[i] + '\\"'
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
newValue += valueArray[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newValue === '' ? value : newValue
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user