File size: 6,821 Bytes
241c492
 
 
 
 
 
 
273089c
 
241c492
 
 
 
273089c
9832882
 
 
273089c
9832882
 
 
273089c
9832882
 
 
 
273089c
9832882
 
 
273089c
9832882
 
273089c
9832882
 
273089c
9832882
 
241c492
9832882
 
241c492
9832882
 
241c492
9832882
241c492
9832882
241c492
9832882
 
 
 
241c492
 
 
 
 
 
 
 
 
 
 
 
273089c
 
241c492
 
273089c
 
241c492
273089c
 
241c492
273089c
241c492
 
273089c
 
 
 
 
241c492
 
 
273089c
241c492
 
 
273089c
241c492
273089c
241c492
273089c
 
9832882
 
 
273089c
9832882
 
 
273089c
9832882
 
 
 
273089c
9832882
 
 
241c492
9832882
 
241c492
9832882
 
 
 
 
 
 
241c492
9832882
 
241c492
9832882
 
241c492
9832882
 
241c492
9832882
 
241c492
9832882
241c492
9832882
241c492
9832882
 
 
 
241c492
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273089c
241c492
273089c
 
 
241c492
273089c
 
 
241c492
273089c
241c492
273089c
 
241c492
273089c
 
 
241c492
273089c
241c492
273089c
 
 
241c492
273089c
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Import required libraries
import requests  # For making HTTP requests
import os  # For accessing environment variables
import google.generativeai as genai  # For interacting with Google's Generative AI APIs
from typing import List  # For type annotations
from utils import encode_image  # Utility function to encode images as base64
from PIL import Image  # For image processing

class Rag:
    """
    A class for interacting with Generative AI models (Gemini and OpenAI) to retrieve answers
    based on user queries and associated images.
    """

    def get_answer_from_gemini(self, query: str, imagePaths: List[str]) -> str:
        """
        Query the Gemini model with a text query and associated images.

        Args:
            query (str): The user's query.
            imagePaths (List[str]): List of file paths to images.

        Returns:
            str: The response text from the Gemini model.
        """
        print(f"Querying Gemini for query={query}, imagePaths={imagePaths}")

        try:
            # Configure the Gemini API client using the API key from environment variables
            genai.configure(api_key=os.environ['GEMINI_API_KEY'])

            # Initialize the Gemini generative model
            model = genai.GenerativeModel('gemini-1.5-flash')

            # Load images from the given paths
            images = [Image.open(path) for path in imagePaths]

            # Start a new chat session
            chat = model.start_chat()

            # Send the query and images to the model
            response = chat.send_message([*images, query])

            # Extract the response text
            answer = response.text

            print(answer)  # Log the answer

            return answer
        
        except Exception as e:
            # Handle and log any errors that occur
            print(f"An error occurred while querying Gemini: {e}")
            return f"Error: {str(e)}"

    def get_answer_from_openai(self, query: str, imagesPaths: List[str]) -> str:
        """
        Query OpenAI's GPT model with a text query and associated images.

        Args:
            query (str): The user's query.
            imagesPaths (List[str]): List of file paths to images.

        Returns:
            str: The response text from OpenAI.
        """
        print(f"Querying OpenAI for query={query}, imagesPaths={imagesPaths}")

        try:
            # Prepare the API payload with the query and images
            payload = self.__get_openai_api_payload(query, imagesPaths)

            # Define the HTTP headers for the OpenAI API request
            headers = {
                "Content-Type": "application/json",
                "Authorization": f"Bearer {os.environ['OPENAI_API_KEY']}"  # API key from environment variables
            }

            # Send a POST request to the OpenAI API
            response = requests.post(
                url="https://api.openai.com/v1/chat/completions",
                headers=headers,
                json=payload
            )
            response.raise_for_status()  # Raise an error for unsuccessful requests

            # Extract the content of the response
            answer = response.json()["choices"][0]["message"]["content"]

            print(answer)  # Log the answer

            return answer
        
        except Exception as e:
            # Handle and log any errors that occur
            print(f"An error occurred while querying OpenAI: {e}")
            return None
    # def get_answer_from_gemini(self, query: str, imagePaths: List[str]) -> str:
    #     """
    #     Query the Gemini model with a text query and associated images.

    #     Args:
    #         query (str): The user's query.
    #         imagePaths (List[str]): List of file paths to images.

    #     Returns:
    #         str: The response text from the Gemini model.
    #     """
    #     print(f"Querying Gemini for query={query}, imagePaths={imagePaths}")

    #     try:
    #         # Configure the Gemini API client using the API key from environment variables
    #         genai.configure(api_key=os.environ['GEMINI_API_KEY'])

    #         # Initialize the Gemini generative model
    #         model = genai.GenerativeModel('gemini-1.5-flash')

    #         # Load images from the given paths (skip missing files)
    #         images = []
    #         for path in imagePaths:
    #             if os.path.exists(path):
    #                 images.append(Image.open(path))
    #             else:
    #                 print(f"Warning: Image not found {path}, skipping.")

    #         # Start a new chat session
    #         chat = model.start_chat()

    #         # Construct the input for the model (handle cases with and without images)
    #         input_data = [query] if not images else [*images, query]

    #         # Send the query (and images, if any) to the model
    #         response = chat.send_message(input_data)

    #         # Extract the response text
    #         answer = response.text

    #         print(answer)  # Log the answer

    #         return answer

    #     except Exception as e:
    #         # Handle and log any errors that occur
    #         print(f"An error occurred while querying Gemini: {e}")
    #         return f"Error: {str(e)}"

    def __get_openai_api_payload(self, query: str, imagesPaths: List[str]) -> dict:
        """
        Prepare the payload for the OpenAI API request.

        Args:
            query (str): The user's query.
            imagesPaths (List[str]): List of file paths to images.

        Returns:
            dict: The payload for the OpenAI API request.
        """
        image_payload = []  # List to store encoded image data

        # Encode each image as base64 and prepare the payload
        for imagePath in imagesPaths:
            base64_image = encode_image(imagePath)  # Encode image in base64
            image_payload.append({
                "type": "image_url",
                "image_url": {
                    "url": f"data:image/jpeg;base64,{base64_image}"  # Embed image data as a URL
                }
            })

        # Create the complete payload for the API request
        payload = {
            "model": "gpt-4o",  # Specify the OpenAI model
            "messages": [
                {
                    "role": "user",  # Role of the message sender
                    "content": [
                        {
                            "type": "text",
                            "text": query  # Include the user's query
                        },
                        *image_payload  # Include the image data
                    ]
                }
            ],
            "max_tokens": 1024  # Limit the response length
        }

        return payload