mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 15:00:57 +03:00
add llamaindex
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import path from 'path'
|
||||
import { flatten } from 'lodash'
|
||||
import { storageContextFromDefaults, serviceContextFromDefaults, VectorStoreIndex, Document } from 'llamaindex'
|
||||
import { Document as LCDocument } from 'langchain/document'
|
||||
import { INode, INodeData, INodeOutputsValue, INodeParams } from '../../../src/Interface'
|
||||
import { getUserHome } from '../../../src'
|
||||
|
||||
class SimpleStoreUpsert_LlamaIndex_VectorStores implements INode {
|
||||
label: string
|
||||
name: string
|
||||
version: number
|
||||
description: string
|
||||
type: string
|
||||
icon: string
|
||||
category: string
|
||||
baseClasses: string[]
|
||||
tags: string[]
|
||||
inputs: INodeParams[]
|
||||
outputs: INodeOutputsValue[]
|
||||
|
||||
constructor() {
|
||||
this.label = 'SimpleStore'
|
||||
this.name = 'simpleStoreLlamaIndex'
|
||||
this.version = 1.0
|
||||
this.type = 'SimpleVectorStore'
|
||||
this.icon = 'simplevs.svg'
|
||||
this.category = 'Vector Stores'
|
||||
this.description = 'Upsert embedded data to local path and perform similarity search'
|
||||
this.baseClasses = [this.type, 'VectorIndexRetriever']
|
||||
this.tags = ['LlamaIndex']
|
||||
this.inputs = [
|
||||
{
|
||||
label: 'Document',
|
||||
name: 'document',
|
||||
type: 'Document',
|
||||
list: true,
|
||||
optional: true
|
||||
},
|
||||
{
|
||||
label: 'Chat Model',
|
||||
name: 'model',
|
||||
type: 'BaseChatModel_LlamaIndex'
|
||||
},
|
||||
{
|
||||
label: 'Embeddings',
|
||||
name: 'embeddings',
|
||||
type: 'BaseEmbedding_LlamaIndex'
|
||||
},
|
||||
{
|
||||
label: 'Base Path to store',
|
||||
name: 'basePath',
|
||||
description:
|
||||
'Path to store persist embeddings indexes with persistence. If not specified, default to same path where database is stored',
|
||||
type: 'string',
|
||||
optional: true
|
||||
},
|
||||
{
|
||||
label: 'Top K',
|
||||
name: 'topK',
|
||||
description: 'Number of top results to fetch. Default to 4',
|
||||
placeholder: '4',
|
||||
type: 'number',
|
||||
optional: true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
//@ts-ignore
|
||||
vectorStoreMethods = {
|
||||
async upsert(nodeData: INodeData): Promise<void> {
|
||||
const basePath = nodeData.inputs?.basePath as string
|
||||
const docs = nodeData.inputs?.document as LCDocument[]
|
||||
const embeddings = nodeData.inputs?.embeddings
|
||||
const model = nodeData.inputs?.model
|
||||
|
||||
let filePath = ''
|
||||
if (!basePath) filePath = path.join(getUserHome(), '.flowise', 'llamaindex')
|
||||
else filePath = basePath
|
||||
|
||||
const flattenDocs = docs && docs.length ? flatten(docs) : []
|
||||
const finalDocs = []
|
||||
for (let i = 0; i < flattenDocs.length; i += 1) {
|
||||
finalDocs.push(new LCDocument(flattenDocs[i]))
|
||||
}
|
||||
|
||||
const llamadocs: Document[] = []
|
||||
for (const doc of finalDocs) {
|
||||
llamadocs.push(new Document({ text: doc.pageContent, metadata: doc.metadata }))
|
||||
}
|
||||
|
||||
const serviceContext = serviceContextFromDefaults({ llm: model, embedModel: embeddings })
|
||||
const storageContext = await storageContextFromDefaults({ persistDir: filePath })
|
||||
|
||||
try {
|
||||
await VectorStoreIndex.fromDocuments(llamadocs, { serviceContext, storageContext })
|
||||
} catch (e) {
|
||||
throw new Error(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async init(nodeData: INodeData): Promise<any> {
|
||||
const basePath = nodeData.inputs?.basePath as string
|
||||
const embeddings = nodeData.inputs?.embeddings
|
||||
const model = nodeData.inputs?.model
|
||||
const topK = nodeData.inputs?.topK as string
|
||||
const k = topK ? parseFloat(topK) : 4
|
||||
|
||||
let filePath = ''
|
||||
if (!basePath) filePath = path.join(getUserHome(), '.flowise', 'llamaindex')
|
||||
else filePath = basePath
|
||||
|
||||
const serviceContext = serviceContextFromDefaults({ llm: model, embedModel: embeddings })
|
||||
const storageContext = await storageContextFromDefaults({ persistDir: filePath })
|
||||
|
||||
const index = await VectorStoreIndex.init({ storageContext, serviceContext })
|
||||
const retriever = index.asRetriever()
|
||||
retriever.similarityTopK = k
|
||||
|
||||
return retriever
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { nodeClass: SimpleStoreUpsert_LlamaIndex_VectorStores }
|
||||
@@ -0,0 +1,6 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-database" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
|
||||
<path d="M12 6m-8 0a8 3 0 1 0 16 0a8 3 0 1 0 -16 0"></path>
|
||||
<path d="M4 6v6a8 3 0 0 0 16 0v-6"></path>
|
||||
<path d="M4 12v6a8 3 0 0 0 16 0v-6"></path>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 451 B |
Reference in New Issue
Block a user