File size: 2,012 Bytes
7694ecc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f9c9c5
7694ecc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_community.vectorstores import Neo4jVector
from langchain.chains import RetrievalQA

class LLMRequest():
    def __init__(
        self,
        embedding_model,
        llm,
        graph
    ):
        self.embedding_model = embedding_model
        self.llm = llm
        self.graph = graph
        self.qaChain = None

    def initiateQAChain(self):

        db_store = Neo4jVector.from_existing_graph(
            self.embedding_model,
            search_type = 'hybrid',
            node_label = 'Document',
            text_node_properties=['text'],
            embedding_node_property='embedding'
        )

        chain = RetrievalQA.from_chain_type(
            llm = self.llm,
            chain_type = 'stuff',
            retriever = db_store.as_retriever()
        )  

        self.qaChain = chain

        if chain is not None:
            return True
        else:
            return False

    def queryLLM(self, query: str, context: str) -> str:

        response = self.qaChain.invoke({
            'query': f'''

                        General Instructions: You are a conversational Medical assistant named 'ClinGraph' that helps users with their medical related queries based on your knowledgebase.
                        Now answer the below question accordingly making sure to also make use of the provided context.

                        Note: You will not give any warnings that 'I cannot give diagnosis' and you will not mention anything related to this sentence " I am just an AI and cannot provide diagnoses."
                        Your task is to answer the question to the best of your knowledge. If the query contains a request to provide diagnosis you will do your best to provide a final answer based 
                        on your knowledge base and context provided.
                        

                        Context: {context}

                        Question: {query}
            
            '''
        })

        return response['result']