From ae3387cedaafb0700115546b370e86eed8cf27f0 Mon Sep 17 00:00:00 2001 From: chungyau97 Date: Mon, 5 Jun 2023 21:44:37 +0700 Subject: [PATCH 1/2] add returnJSONStr --- .../prompts/PromptTemplate/PromptTemplate.ts | 7 +-- packages/components/src/utils.ts | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/packages/components/nodes/prompts/PromptTemplate/PromptTemplate.ts b/packages/components/nodes/prompts/PromptTemplate/PromptTemplate.ts index f976d64c..cfa2c488 100644 --- a/packages/components/nodes/prompts/PromptTemplate/PromptTemplate.ts +++ b/packages/components/nodes/prompts/PromptTemplate/PromptTemplate.ts @@ -1,5 +1,5 @@ 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' class PromptTemplate_Prompts implements INode { @@ -46,11 +46,12 @@ class PromptTemplate_Prompts implements INode { async init(nodeData: INodeData): Promise { const template = nodeData.inputs?.template as string - const promptValuesStr = nodeData.inputs?.promptValues as string + let promptValuesStr = nodeData.inputs?.promptValues as string let promptValues: ICommonObject = {} if (promptValuesStr) { - promptValues = JSON.parse(promptValuesStr.replace(/\s/g, '')) + promptValuesStr = promptValuesStr.replace(/\s/g, '') + promptValues = JSON.parse(returnJSONStr(promptValuesStr)) } const inputVariables = getInputVariables(template) diff --git a/packages/components/src/utils.ts b/packages/components/src/utils.ts index 08d32bab..05ff2e85 100644 --- a/packages/components/src/utils.ts +++ b/packages/components/src/utils.ts @@ -234,3 +234,55 @@ export class CustomChainHandler extends BaseCallbackHandler { this.socketIO.to(this.socketIOClientId).emit('end') } } + +export const returnJSONStr = (jsonStr: string): string => { + let jsonStrArray = jsonStr.split(':') + // jsonStrArray = jsonStrArray.split('"') + console.log(`jsonStrArray: ${JSON.stringify(jsonStrArray)} length: ${jsonStrArray.length}`) + + let wholeString = '' + for (let i = 0; i < jsonStrArray.length; i++) { + // console.log(`element: ${jsonStrArray[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]) + } + } + console.log(`wholeString: ${wholeString}`) + return wholeString +} + +const handleEscapeDoubleQuote = (value: string): string => { + console.log(`value: ${value}`) + 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 '': + console.log(`nothing`) + newValue += '"' + break + case '}': + console.log(`}`) + newValue += '"}' + break + default: + console.log(`default`) + newValue += '\\"' + valueArray[i] + '\\"' + } + } else { + newValue += valueArray[i] + } + + console.log(`valueArray[i]: ${valueArray[i]}`) + console.log(`newValue: ${newValue}`) + } + } + return newValue === '' ? value : newValue +} From f7dd021b38b030167a53a355550018c302a1fba7 Mon Sep 17 00:00:00 2001 From: chungyau97 Date: Mon, 5 Jun 2023 21:59:42 +0700 Subject: [PATCH 2/2] remove console.log --- packages/components/src/utils.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/packages/components/src/utils.ts b/packages/components/src/utils.ts index 05ff2e85..22a09d34 100644 --- a/packages/components/src/utils.ts +++ b/packages/components/src/utils.ts @@ -237,12 +237,9 @@ export class CustomChainHandler extends BaseCallbackHandler { export const returnJSONStr = (jsonStr: string): string => { let jsonStrArray = jsonStr.split(':') - // jsonStrArray = jsonStrArray.split('"') - console.log(`jsonStrArray: ${JSON.stringify(jsonStrArray)} length: ${jsonStrArray.length}`) let wholeString = '' for (let i = 0; i < jsonStrArray.length; i++) { - // console.log(`element: ${jsonStrArray[i]}`) if (jsonStrArray[i].includes(',') && jsonStrArray[i + 1] !== undefined) { const splitValueAndTitle = jsonStrArray[i].split(',') const value = splitValueAndTitle[0] @@ -252,12 +249,10 @@ export const returnJSONStr = (jsonStr: string): string => { wholeString += wholeString === '' ? jsonStrArray[i] + ':' : handleEscapeDoubleQuote(jsonStrArray[i]) } } - console.log(`wholeString: ${wholeString}`) return wholeString } const handleEscapeDoubleQuote = (value: string): string => { - console.log(`value: ${value}`) let newValue = '' if (value.includes('"')) { const valueArray = value.split('"') @@ -265,23 +260,17 @@ const handleEscapeDoubleQuote = (value: string): string => { if ((i + 1) % 2 !== 0) { switch (valueArray[i]) { case '': - console.log(`nothing`) newValue += '"' break case '}': - console.log(`}`) newValue += '"}' break default: - console.log(`default`) newValue += '\\"' + valueArray[i] + '\\"' } } else { newValue += valueArray[i] } - - console.log(`valueArray[i]: ${valueArray[i]}`) - console.log(`newValue: ${newValue}`) } } return newValue === '' ? value : newValue