add vector upsert ability

This commit is contained in:
Henry
2023-11-16 18:34:17 +00:00
parent 9205a29787
commit 44cadc1cc3
108 changed files with 10019 additions and 4726 deletions
+154
View File
@@ -332,6 +332,57 @@ export const getAvailableNodesForVariable = (nodes, edges, target, targetHandle)
return parentNodes
}
export const getUpsertDetails = (nodes, edges) => {
const vsNodes = nodes.filter(
(node) =>
node.data.category === 'Vector Stores' && !node.data.label.includes('Upsert') && !node.data.label.includes('Load Existing')
)
const vsNodeIds = vsNodes.map((vs) => vs.data.id)
const upsertNodes = []
const seenVsNodeIds = []
for (const edge of edges) {
if (vsNodeIds.includes(edge.source) || vsNodeIds.includes(edge.target)) {
const vsNode = vsNodes.find((node) => node.data.id === edge.source || node.data.id === edge.target)
if (!vsNode || seenVsNodeIds.includes(vsNode.data.id)) continue
seenVsNodeIds.push(vsNode.data.id)
// Found Vector Store Node, proceed to find connected Document Loader node
let connectedDocs = []
if (vsNode.data.inputs.document) connectedDocs = [...new Set(vsNode.data.inputs.document)]
if (connectedDocs.length) {
const innerNodes = [vsNode]
if (vsNode.data.inputs.embeddings) {
const embeddingsId = vsNode.data.inputs.embeddings.replace(/{{|}}/g, '').split('.')[0]
innerNodes.push(nodes.find((node) => node.data.id === embeddingsId))
}
for (const doc of connectedDocs) {
const docId = doc.replace(/{{|}}/g, '').split('.')[0]
const docNode = nodes.find((node) => node.data.id === docId)
if (docNode) innerNodes.push(docNode)
// Found Document Loader Node, proceed to find connected Text Splitter node
if (docNode && docNode.data.inputs.textSplitter) {
const textSplitterId = docNode.data.inputs.textSplitter.replace(/{{|}}/g, '').split('.')[0]
const textSplitterNode = nodes.find((node) => node.data.id === textSplitterId)
if (textSplitterNode) innerNodes.push(textSplitterNode)
}
}
upsertNodes.push({
vectorNode: vsNode,
nodes: innerNodes.reverse()
})
}
}
}
return upsertNodes
}
export const rearrangeToolsOrdering = (newValues, sourceNodeId) => {
// RequestsGet and RequestsPost have to be in order before other tools
newValues.push(`{{${sourceNodeId}.data.instance}}`)
@@ -454,3 +505,106 @@ export const formatDataGridRows = (rows) => {
return []
}
}
export const setLocalStorageChatflow = (chatflowid, chatId, chatHistory) => {
const chatDetails = localStorage.getItem(`${chatflowid}_INTERNAL`)
const obj = {}
if (chatId) obj.chatId = chatId
if (chatHistory) obj.chatHistory = chatHistory
if (!chatDetails) {
localStorage.setItem(`${chatflowid}_INTERNAL`, JSON.stringify(obj))
} else {
try {
const parsedChatDetails = JSON.parse(chatDetails)
localStorage.setItem(`${chatflowid}_INTERNAL`, JSON.stringify({ ...parsedChatDetails, ...obj }))
} catch (e) {
const chatId = chatDetails
obj.chatId = chatId
localStorage.setItem(`${chatflowid}_INTERNAL`, JSON.stringify(obj))
}
}
}
export const unshiftFiles = (configData) => {
const filesConfig = configData.find((config) => config.name === 'files')
if (filesConfig) {
configData = configData.filter((config) => config.name !== 'files')
configData.unshift(filesConfig)
}
return configData
}
export const getConfigExamplesForJS = (configData, bodyType, isMultiple, stopNodeId) => {
let finalStr = ''
configData = unshiftFiles(configData)
const loop = Math.min(configData.length, 4)
for (let i = 0; i < loop; i += 1) {
const config = configData[i]
let exampleVal = `"example"`
if (config.type === 'string') exampleVal = `"example"`
else if (config.type === 'boolean') exampleVal = `true`
else if (config.type === 'number') exampleVal = `1`
else if (config.type === 'json') exampleVal = `{ "key": "val" }`
else if (config.name === 'files') exampleVal = `input.files[0]`
finalStr += bodyType === 'json' ? `\n "${config.name}": ${exampleVal},` : `formData.append("${config.name}", ${exampleVal})\n`
if (i === loop - 1 && bodyType !== 'json')
finalStr += !isMultiple
? ``
: stopNodeId
? `formData.append("stopNodeId", "${stopNodeId}")\n`
: `formData.append("question", "Hey, how are you?")\n`
}
return finalStr
}
export const getConfigExamplesForPython = (configData, bodyType, isMultiple, stopNodeId) => {
let finalStr = ''
configData = unshiftFiles(configData)
const loop = Math.min(configData.length, 4)
for (let i = 0; i < loop; i += 1) {
const config = configData[i]
let exampleVal = `"example"`
if (config.type === 'string') exampleVal = `"example"`
else if (config.type === 'boolean') exampleVal = `true`
else if (config.type === 'number') exampleVal = `1`
else if (config.type === 'json') exampleVal = `{ "key": "val" }`
else if (config.name === 'files') continue
finalStr += bodyType === 'json' ? `\n "${config.name}": ${exampleVal},` : `\n "${config.name}": ${exampleVal},`
if (i === loop - 1 && bodyType !== 'json')
finalStr += !isMultiple
? `\n`
: stopNodeId
? `\n "stopNodeId": "${stopNodeId}"\n`
: `\n "question": "Hey, how are you?"\n`
}
return finalStr
}
export const getConfigExamplesForCurl = (configData, bodyType, isMultiple, stopNodeId) => {
let finalStr = ''
configData = unshiftFiles(configData)
const loop = Math.min(configData.length, 4)
for (let i = 0; i < loop; i += 1) {
const config = configData[i]
let exampleVal = `example`
if (config.type === 'string') exampleVal = bodyType === 'json' ? `"example"` : `example`
else if (config.type === 'boolean') exampleVal = `true`
else if (config.type === 'number') exampleVal = `1`
else if (config.type === 'json') exampleVal = `{key:val}`
else if (config.name === 'files')
exampleVal = `@/home/user1/Desktop/example${config.type.includes(',') ? config.type.split(',')[0] : config.type}`
finalStr += bodyType === 'json' ? `"${config.name}": ${exampleVal}` : `\n -F "${config.name}=${exampleVal}"`
if (i === loop - 1)
finalStr +=
bodyType === 'json'
? ` }`
: !isMultiple
? ``
: stopNodeId
? ` \\\n -F "stopNodeId=${stopNodeId}"`
: ` \\\n -F "question=Hey, how are you?"`
else finalStr += bodyType === 'json' ? `, ` : ` \\`
}
return finalStr
}