generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support more chain types, implement vector search powered by huggingf…
…ace inference api (#34) * Implement ChainFactory for chain singletons * Add in-memory vector search powered by huggingface inference api * Add todo items for unlimited context search
- Loading branch information
1 parent
24defc6
commit ab2b5e5
Showing
10 changed files
with
242 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { BaseLanguageModel } from "langchain/base_language"; | ||
import { | ||
BaseChain, | ||
ConversationChain, | ||
ConversationalRetrievalQAChain, | ||
LLMChainInput, | ||
} from "langchain/chains"; | ||
import { BaseRetriever } from "langchain/schema"; | ||
|
||
|
||
export interface ConversationalRetrievalChainParams { | ||
llm: BaseLanguageModel; | ||
retriever: BaseRetriever; | ||
options?: { | ||
questionGeneratorTemplate?: string; | ||
qaTemplate?: string; | ||
returnSourceDocuments?: boolean; | ||
} | ||
} | ||
|
||
// Add new chain types here | ||
export const LLM_CHAIN = 'llm_chain'; | ||
export const CONVERSATIONAL_RETRIEVAL_QA_CHAIN = 'conversational_retrieval_chain'; | ||
export const SUPPORTED_CHAIN_TYPES = new Set([ | ||
LLM_CHAIN, | ||
CONVERSATIONAL_RETRIEVAL_QA_CHAIN, | ||
]); | ||
|
||
class ChainFactory { | ||
private static instances: Map<string, BaseChain> = new Map(); | ||
|
||
public static getLLMChain(args: LLMChainInput): BaseChain { | ||
let instance = ChainFactory.instances.get(LLM_CHAIN); | ||
if (!instance) { | ||
instance = new ConversationChain(args as LLMChainInput); | ||
console.log('New chain created: ', instance._chainType()); | ||
ChainFactory.instances.set(LLM_CHAIN, instance); | ||
} | ||
return instance; | ||
} | ||
|
||
public static getRetrievalChain( | ||
args: ConversationalRetrievalChainParams | ||
): ConversationalRetrievalQAChain { | ||
let instance = ChainFactory.instances.get(CONVERSATIONAL_RETRIEVAL_QA_CHAIN); | ||
if (!instance) { | ||
const argsRetrieval = args as ConversationalRetrievalChainParams; | ||
instance = ConversationalRetrievalQAChain.fromLLM( | ||
argsRetrieval.llm, argsRetrieval.retriever, argsRetrieval.options | ||
); | ||
console.log('New chain created: ', instance._chainType()); | ||
ChainFactory.instances.set(CONVERSATIONAL_RETRIEVAL_QA_CHAIN, instance); | ||
} | ||
return instance as ConversationalRetrievalQAChain; | ||
} | ||
} | ||
|
||
export default ChainFactory; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.