KarthikaRajagopal commited on
Commit
e20bb61
Β·
verified Β·
1 Parent(s): 6fc6a02

Delete rag.py

Browse files
Files changed (1) hide show
  1. rag.py +0 -95
rag.py DELETED
@@ -1,95 +0,0 @@
1
- import requests
2
- import os
3
- import google.generativeai as genai
4
-
5
- from typing import List
6
- from utils import encode_image
7
- from PIL import Image
8
-
9
- class Rag:
10
-
11
- def get_answer_from_gemini(self, query, imagePaths):
12
-
13
- print(f"Querying Gemini for query={query}, imagePaths={imagePaths}")
14
-
15
- try:
16
- genai.configure(api_key=os.environ['GEMINI_API_KEY'])
17
- model = genai.GenerativeModel('gemini-1.5-flash')
18
-
19
- images = [Image.open(path) for path in imagePaths]
20
-
21
- chat = model.start_chat()
22
-
23
- response = chat.send_message([*images, query])
24
-
25
- answer = response.text
26
-
27
- print(answer)
28
-
29
- return answer
30
-
31
- except Exception as e:
32
- print(f"An error occurred while querying Gemini: {e}")
33
- return f"Error: {str(e)}"
34
-
35
-
36
- def get_answer_from_openai(self, query, imagesPaths):
37
- print(f"Querying OpenAI for query={query}, imagesPaths={imagesPaths}")
38
-
39
- try:
40
- payload = self.__get_openai_api_payload(query, imagesPaths)
41
-
42
- headers = {
43
- "Content-Type": "application/json",
44
- "Authorization": f"Bearer {os.environ['OPENAI_API_KEY']}"
45
- }
46
-
47
- response = requests.post(
48
- url="https://api.openai.com/v1/chat/completions",
49
- headers=headers,
50
- json=payload
51
- )
52
- response.raise_for_status() # Raise an HTTPError for bad responses
53
-
54
- answer = response.json()["choices"][0]["message"]["content"]
55
-
56
- print(answer)
57
-
58
- return answer
59
-
60
- except Exception as e:
61
- print(f"An error occurred while querying OpenAI: {e}")
62
- return None
63
-
64
-
65
- def __get_openai_api_payload(self, query:str, imagesPaths:List[str]):
66
- image_payload = []
67
-
68
- for imagePath in imagesPaths:
69
- base64_image = encode_image(imagePath)
70
- image_payload.append({
71
- "type": "image_url",
72
- "image_url": {
73
- "url": f"data:image/jpeg;base64,{base64_image}"
74
- }
75
- })
76
-
77
- payload = {
78
- "model": "gpt-4o",
79
- "messages": [
80
- {
81
- "role": "user",
82
- "content": [
83
- {
84
- "type": "text",
85
- "text": query
86
- },
87
- *image_payload
88
- ]
89
- }
90
- ],
91
- "max_tokens": 1024
92
- }
93
-
94
- return payload
95
-