LalitMahale commited on
Commit
ba1cae1
·
1 Parent(s): f2e2bb3

gemini added

Browse files
Files changed (3) hide show
  1. .env +1 -0
  2. main.py +3 -3
  3. utils/rag.py +42 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ Google_api = 'AIzaSyB3wI2r6ZgQnYQ3V39PX5S0zWSRqy5ldYw'
main.py CHANGED
@@ -4,7 +4,7 @@ from utils.convert_embedding import GetEmbedding
4
  import random
5
  import pickle
6
  import os
7
-
8
 
9
 
10
  # def dump_user_question(query):
@@ -35,10 +35,10 @@ def process(user_query:str):
35
  score = similarity_scores[0,index]
36
  print(f"Index : {index}:\tscore:{score} \tquery: {user_query}")
37
 
38
- if float(score) > 0.45 :
39
  final_output = random.choice(answer)
40
  else:
41
- final_output = "Sorry, I did not understand."
42
 
43
  return final_output
44
 
 
4
  import random
5
  import pickle
6
  import os
7
+ from utils.rag import RAG
8
 
9
 
10
  # def dump_user_question(query):
 
35
  score = similarity_scores[0,index]
36
  print(f"Index : {index}:\tscore:{score} \tquery: {user_query}")
37
 
38
+ if float(score) > 0.60 :
39
  final_output = random.choice(answer)
40
  else:
41
+ final_output = RAG().pipeline(query=user_query)
42
 
43
  return final_output
44
 
utils/rag.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_google_genai import GoogleGenerativeAI
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ from dotenv import load_dotenv
5
+ import os
6
+ load_dotenv()
7
+
8
+
9
+ class RAG:
10
+ def __init__(self):
11
+ self.url = 'https://lalitmahale.github.io'
12
+ self.llm = GoogleGenerativeAI(google_api_key=os.getenv("Google_api"),model="gemini-pro")
13
+
14
+
15
+ def get_data(self):
16
+ try:
17
+ res = requests.get(self.url)
18
+ soup = BeautifulSoup(res.content, "html.parser")
19
+ return soup.get_text()
20
+ except Exception as e:
21
+ print(e)
22
+
23
+ def clean_text(self):
24
+ return self.get_data().replace("\n","")
25
+
26
+ def prompt(self):
27
+ return """You are a helpfull assistant for me. understand the below context and give answer for user question.
28
+
29
+ context : {context}\n\nQuestion : {question}\n\nGive proper answer for this questions."""
30
+
31
+
32
+ def pipeline(self,query):
33
+ try:
34
+ prompt = self.prompt().format(context = self.clean_text(),question = query)
35
+ return self.llm.invoke(prompt)
36
+ except Exception as e:
37
+ print(e)
38
+
39
+
40
+ if __name__ == "__main__":
41
+ res = RAG().pipeline("who is lalit mahale")
42
+ print(res)