Merge pull request #613 from Yongtae723/features/add_HyDERtriver

Add Hyde retriever
This commit is contained in:
Henry Heng 2023-07-25 16:39:04 +01:00 committed by GitHub
commit e2d3f27842
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,121 @@
import { VectorStore } from 'langchain/vectorstores/base'
import { INode, INodeData, INodeParams } from '../../../src/Interface'
import { HydeRetriever, HydeRetrieverOptions, PromptKey } from 'langchain/retrievers/hyde'
import { BaseLanguageModel } from 'langchain/base_language'
import { PromptTemplate } from 'langchain/prompts'
class HydeRetriever_Retrievers implements INode {
label: string
name: string
description: string
type: string
icon: string
category: string
baseClasses: string[]
inputs: INodeParams[]
constructor() {
this.label = 'Hyde Retriever'
this.name = 'HydeRetriever'
this.type = 'HydeRetriever'
this.icon = 'hyderetriever.svg'
this.category = 'Retrievers'
this.description = 'Use HyDE retriever to retrieve from a vector store'
this.baseClasses = [this.type, 'BaseRetriever']
this.inputs = [
{
label: 'Language Model',
name: 'model',
type: 'BaseLanguageModel'
},
{
label: 'Vector Store',
name: 'vectorStore',
type: 'VectorStore'
},
{
label: 'Prompt Key',
name: 'promptKey',
type: 'options',
options: [
{
label: 'websearch',
name: 'websearch'
},
{
label: 'scifact',
name: 'scifact'
},
{
label: 'arguana',
name: 'arguana'
},
{
label: 'trec-covid',
name: 'trec-covid'
},
{
label: 'fiqa',
name: 'fiqa'
},
{
label: 'dbpedia-entity',
name: 'dbpedia-entity'
},
{
label: 'trec-news',
name: 'trec-news'
},
{
label: 'mr-tydi',
name: 'mr-tydi'
}
],
default: 'websearch'
},
{
label: 'Custom Prompt',
name: 'customPrompt',
description: 'If custom prompt is used, this will override Prompt Key',
placeholder: 'Please write a passage to answer the question\nQuestion: {question}\nPassage:',
type: 'string',
rows: 4,
additionalParams: true,
optional: true
},
{
label: 'Top K',
name: 'topK',
description: 'Number of top results to fetch. Default to 4',
placeholder: '4',
type: 'number',
default: 4,
additionalParams: true,
optional: true
}
]
}
async init(nodeData: INodeData): Promise<any> {
const llm = nodeData.inputs?.model as BaseLanguageModel
const vectorStore = nodeData.inputs?.vectorStore as VectorStore
const promptKey = nodeData.inputs?.promptKey as PromptKey
const customPrompt = nodeData.inputs?.customPrompt as string
const topK = nodeData.inputs?.topK as string
const k = topK ? parseInt(topK, 10) : 4
const obj: HydeRetrieverOptions<any> = {
llm,
vectorStore,
k
}
if (customPrompt) obj.promptTemplate = PromptTemplate.fromTemplate(customPrompt)
else if (promptKey) obj.promptTemplate = promptKey
const retriever = new HydeRetriever(obj)
return retriever
}
}
module.exports = { nodeClass: HydeRetriever_Retrievers }

View File

@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-database-export" 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="M4 6c0 1.657 3.582 3 8 3s8 -1.343 8 -3s-3.582 -3 -8 -3s-8 1.343 -8 3"></path>
<path d="M4 6v6c0 1.657 3.582 3 8 3c1.118 0 2.183 -.086 3.15 -.241"></path>
<path d="M20 12v-6"></path>
<path d="M4 12v6c0 1.657 3.582 3 8 3c.157 0 .312 -.002 .466 -.005"></path>
<path d="M16 19h6"></path>
<path d="M19 16l3 3l-3 3"></path>
</svg>

After

Width:  |  Height:  |  Size: 647 B