1
+ from langchain .embeddings import SentenceTransformerEmbeddings #HuggingFaceInstructEmbeddings
2
+ from langchain .vectorstores import FAISS
3
+ import os
4
+ import copy
5
+ import pprint
6
+ #import google.generativeai as palm
7
+ from langchain .llms import GooglePalm
8
+ from langchain import PromptTemplate
9
+ from langchain .chains import RetrievalQA
10
+
11
+ PALM_API = "AIzaSyAIzDH7NVopxUvOL8PAqBnKZqdmAoXeS28"
12
+ #palm.configure(api_key=PALM_API)
13
+
14
+
15
+ def getmodel ():
16
+ "test"
17
+ PALM_API = "AIzaSyAIzDH7NVopxUvOL8PAqBnKZqdmAoXeS28"
18
+ embeddings = SentenceTransformerEmbeddings (model_name = "all-MiniLM-L6-v2" )
19
+ db = FAISS .load_local ("faiss" , embeddings )
20
+ retriever = db .as_retriever (search_kwargs = {'k' : 10 })
21
+ #prompt=getprompt()
22
+ llm = GooglePalm (google_api_key = PALM_API ,temperature = 0.00003 ,max_output_tokens = 512 )
23
+ qa_llm = RetrievalQA .from_chain_type (llm = llm ,
24
+ chain_type = 'refine' ,
25
+ retriever = retriever ,
26
+ return_source_documents = True ,
27
+ #chain_type_kwargs={'prompt': prompt},
28
+ verbose = True )
29
+ return qa_llm
30
+
31
+ def getprompt ():
32
+ template = """Use the information to elaborate in points about the user's query.
33
+ If user mentions something not in the 'Context', just answer that you don't know.
34
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
35
+
36
+ Context: {context}
37
+
38
+ Query: {question}
39
+
40
+ Only return the helpful answer below and nothing else.
41
+
42
+ Helpful answer:
43
+ """
44
+ prompt = PromptTemplate (
45
+ template = template ,
46
+ input_variables = ['context' , 'question' ])
47
+ return prompt
48
+
49
+ def parseresult (result ):
50
+
51
+ PARSED = copy .deepcopy (result )
52
+ docs = PARSED ['source_documents' ]
53
+ sourcepage = []
54
+ for d in docs :
55
+ sourcepage .append (d .metadata ['page' ])
56
+ PARSED ['source_pages' ]= copy .deepcopy (sourcepage )
57
+ del sourcepage ,result
58
+ return PARSED
59
+
60
+ def getsources (result ):
61
+ sources = []
62
+ for s in result ['source_documents' ]:
63
+ sources .append (f"{ s .metadata } " )
64
+ return sources
65
+
66
+ def EXTRACT ():
67
+ print (f"{ '>>>' * 17 } QUERY DOCS{ '<<<' * 17 } " )
68
+ try :
69
+ llm = getmodel ()
70
+ except :
71
+ print ("CANNOT LOAD MODEL OR DATABASE" )
72
+ print (f"{ '###' * 40 } " )
73
+ return
74
+ while True :
75
+ print (f"{ '###' * 40 } " )
76
+ prompt = input ("(To stop querying enter exit) \n Query : " )
77
+
78
+ if prompt :
79
+ if prompt .find ('exit' )== 0 :
80
+ return
81
+ else :
82
+ pass
83
+
84
+ try :
85
+ result = parseresult (llm (prompt ))
86
+ sources = getsources (result )
87
+ result = result ["result" ]
88
+ except :
89
+ result = 'Error ocurred!'
90
+ sources = []
91
+ print (f"{ '!!!' * 40 } " )
92
+ print (f"QUERY: { prompt } " )
93
+ print (f"{ '###' * 40 } " )
94
+ print ("RESULT:" )
95
+ #print(f"{'###'*40}")
96
+ print (result )
97
+ print (f"{ '$$$' * 40 } " )
98
+ print ("SOURCES:" )
99
+ #print(f"{'$$$'*40}")
100
+ print (sources )
101
+ print (f"{ '>>>' * 40 } " )
0 commit comments