| """ | |
| Module: conversation_chain_singleton | |
| This module provides a singleton class, ConversationChainSingleton, for managing a conversation chain instance. | |
| Dependencies: | |
| - langchain.memory: Module providing memory functionalities for conversation chains. | |
| - langchain.chains: Module providing conversation chain functionalities. | |
| - langchain.llms: Module providing language model functionalities, particularly from HuggingFaceHub. | |
| Classes: | |
| - ConversationChainSingleton: A singleton class for managing a conversation chain instance. | |
| """ | |
| from langchain.memory import ConversationBufferMemory | |
| from langchain.chains import ConversationChain | |
| from langchain.llms import HuggingFaceHub | |
| class ConversationChainSingleton: | |
| def __init__(self) -> None: | |
| pass | |
| def conversation_chain(self, text): | |
| """ | |
| Create a conversational retrieval chain and a language model. | |
| Returns: | |
| - ConversationChain: The initialized conversation chain. | |
| """ | |
| print(text) | |
| llm = HuggingFaceHub( | |
| repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1", | |
| model_kwargs={"max_length": 1048, "temperature": 0.2, "max_new_tokens": 256, "top_p": 0.95, "repetition_penalty": 1.0}, | |
| ) | |
| memory = ConversationBufferMemory(memory_key="history", return_messages=True) | |
| conversation_chain = ConversationChain( | |
| llm=llm, verbose=True, memory=memory | |
| ) | |
| return conversation_chain | |
| """ | |
| A singleton class for managing a conversation chain instance. | |
| Attributes: | |
| - _instance: Private attribute holding the singleton instance. | |
| - conversation_chain: The conversation chain instance. | |
| Methods: | |
| - __new__(cls, *args, **kwargs): Creates a new instance of the ConversationChainSingleton class. | |
| - get_conversation_chain(self): Returns the conversation chain instance. | |
| - get_conversation_chain(): Creates and returns a conversational retrieval chain and a language model. | |
| _instance = None | |
| def __new__(cls, *args, **kwargs): | |
| if not cls._instance: | |
| cls._instance = super(ConversationChainSingleton, cls).__new__(cls) | |
| # Initialize your conversation chain here | |
| cls._instance.conversation_chain = cls.get_conversation_chain(cls._instance) | |
| return cls._instance | |
| """ | |