File size: 2,340 Bytes
bc53764
 
 
 
 
 
 
 
 
 
 
 
 
 
b8b0b89
a164c8b
b8b0b89
 
 
bc53764
 
 
 
 
 
 
 
 
 
 
a8dafaa
bc53764
 
 
b8b0b89
 
 
 
 
 
429167b
b8b0b89
 
a8dafaa
429167b
b8b0b89
273c375
 
 
 
b8b0b89
bc53764
273c375
b8b0b89
273c375
 
 
 
3731d17
273c375
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
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:
    """
    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()
        return cls._instance

 
    def get_conversation_chain():
        """
        Create a conversational retrieval chain and a language model.

        Args:
        - instance: The instance of the ConversationChainSingleton class.

        Returns:
        - ConversationChain: The initialized conversation chain.
        """
        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