Feature/add ability to upload file from chat (#3059)

add ability to upload file from chat
This commit is contained in:
Henry Heng
2024-08-25 13:22:48 +01:00
committed by GitHub
parent e8f5f07735
commit 66acd0c000
37 changed files with 1111 additions and 259 deletions
@@ -1,12 +1,12 @@
import { flatten } from 'lodash'
import { IndexingResult, INode, INodeOutputsValue, INodeParams, INodeData, ICommonObject } from '../../../src/Interface'
import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { FLOWISE_CHATID, getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils'
import { Embeddings } from '@langchain/core/embeddings'
import { Document } from '@langchain/core/documents'
import { UpstashVectorStore } from '@langchain/community/vectorstores/upstash'
import { Index as UpstashIndex } from '@upstash/vector'
import { index } from '../../../src/indexing'
import { resolveVectorStoreOrRetriever } from '../VectorStoreUtils'
import { howToUseFileUpload, resolveVectorStoreOrRetriever } from '../VectorStoreUtils'
type UpstashVectorStoreParams = {
index: UpstashIndex
@@ -29,7 +29,7 @@ class Upstash_VectorStores implements INode {
constructor() {
this.label = 'Upstash Vector'
this.name = 'upstash'
this.version = 1.0
this.version = 2.0
this.type = 'Upstash'
this.icon = 'upstash.svg'
this.category = 'Vector Stores'
@@ -63,6 +63,18 @@ class Upstash_VectorStores implements INode {
description: 'Keep track of the record to prevent duplication',
optional: true
},
{
label: 'File Upload',
name: 'fileUpload',
description: 'Allow file upload on the chat',
hint: {
label: 'How to use',
value: howToUseFileUpload
},
type: 'boolean',
additionalParams: true,
optional: true
},
{
label: 'Upstash Metadata Filter',
name: 'upstashMetadataFilter',
@@ -100,6 +112,7 @@ class Upstash_VectorStores implements INode {
const docs = nodeData.inputs?.document as Document[]
const embeddings = nodeData.inputs?.embeddings as Embeddings
const recordManager = nodeData.inputs?.recordManager
const isFileUploadEnabled = nodeData.inputs?.fileUpload as boolean
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const UPSTASH_VECTOR_REST_URL = getCredentialParam('UPSTASH_VECTOR_REST_URL', credentialData, nodeData)
@@ -114,6 +127,9 @@ class Upstash_VectorStores implements INode {
const finalDocs = []
for (let i = 0; i < flattenDocs.length; i += 1) {
if (flattenDocs[i] && flattenDocs[i].pageContent) {
if (isFileUploadEnabled && options.chatId) {
flattenDocs[i].metadata = { ...flattenDocs[i].metadata, [FLOWISE_CHATID]: options.chatId }
}
finalDocs.push(new Document(flattenDocs[i]))
}
}
@@ -186,6 +202,7 @@ class Upstash_VectorStores implements INode {
async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> {
const upstashMetadataFilter = nodeData.inputs?.upstashMetadataFilter
const embeddings = nodeData.inputs?.embeddings as Embeddings
const isFileUploadEnabled = nodeData.inputs?.fileUpload as boolean
const credentialData = await getCredentialData(nodeData.credential ?? '', options)
const UPSTASH_VECTOR_REST_URL = getCredentialParam('UPSTASH_VECTOR_REST_URL', credentialData, nodeData)
@@ -203,6 +220,10 @@ class Upstash_VectorStores implements INode {
if (upstashMetadataFilter) {
obj.filter = upstashMetadataFilter
}
if (isFileUploadEnabled && options.chatId) {
if (upstashMetadataFilter) obj.filter += ` OR ${FLOWISE_CHATID} = "${options.chatId}" OR HAS NOT FIELD ${FLOWISE_CHATID}`
else obj.filter = `${FLOWISE_CHATID} = "${options.chatId}" OR HAS NOT FIELD ${FLOWISE_CHATID}`
}
const vectorStore = await UpstashVectorStore.fromExistingIndex(embeddings, obj)