mb-rag 1.1.56.post0__py3-none-any.whl → 1.1.58__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mb-rag might be problematic. Click here for more details.

mb_rag/basic.py CHANGED
@@ -1,306 +1,375 @@
1
- ## file for loading all models for chat/rag
2
-
3
- import os
4
- from langchain_core.messages import HumanMessage
5
- from mb_rag.utils.extra import check_package
6
- import base64
7
- from .utils.extra import check_package
8
- from typing import Any
9
- from .utils.all_data_extract import DocumentExtractor
10
-
11
- __all__ = [
12
- 'ModelFactory',
13
- ]
14
-
15
- class ModelFactory:
16
- """Factory class for creating different types of chatbot models"""
17
-
18
- def __init__(self, model_type: str = 'openai', model_name: str = "gpt-4o", **kwargs) -> Any:
19
- """
20
- Factory method to create any type of model
21
- Args:
22
- model_type (str): Type of model to create. Default is OpenAI. Options are openai, anthropic, google, ollama , groq
23
- model_name (str): Name of the model
24
- **kwargs: Additional arguments
25
- Returns:
26
- Any: Chatbot model
27
- """
28
- creators = {
29
- 'openai': self.create_openai,
30
- 'anthropic': self.create_anthropic,
31
- 'google': self.create_google,
32
- 'ollama': self.create_ollama,
33
- 'groq': self.create_groq,
34
- 'deepseek': self.create_deepseek,
35
- 'qwen' : self.create_qwen,
36
- 'hugging_face': self.create_hugging_face
37
- }
38
-
39
- self.model_type = model_type
40
- self.model_name = model_name
41
- model_data = creators.get(model_type)
42
- if not model_data:
43
- raise ValueError(f"Unsupported model type: {model_type}")
44
-
45
- try:
46
- self.model = model_data(model_name, **kwargs)
47
- except Exception as e:
48
- raise ValueError(f"Error creating {model_type} model: {str(e)}")
49
-
50
- @classmethod
51
- def create_openai(cls, model_name: str = "gpt-4o", **kwargs) -> Any:
52
- """
53
- Create OpenAI chatbot model
54
- Args:
55
- model_name (str): Name of the model
56
- **kwargs: Additional arguments
57
- Returns:
58
- ChatOpenAI: Chatbot model
59
- """
60
- if not check_package("openai"):
61
- raise ImportError("OpenAI package not found. Please install it using: pip install openai langchain-openai")
62
-
63
- from langchain_openai import ChatOpenAI
64
- kwargs["model_name"] = model_name
65
- return ChatOpenAI(**kwargs)
66
-
67
- @classmethod
68
- def create_anthropic(cls, model_name: str = "claude-3-opus-20240229", **kwargs) -> Any:
69
- """
70
- Create Anthropic chatbot model
71
- Args:
72
- model_name (str): Name of the model
73
- **kwargs: Additional arguments
74
- Returns:
75
- ChatAnthropic: Chatbot model
76
- """
77
- if not check_package("anthropic"):
78
- raise ImportError("Anthropic package not found. Please install it using: pip install anthropic langchain-anthropic")
79
-
80
- from langchain_anthropic import ChatAnthropic
81
- kwargs["model_name"] = model_name
82
- return ChatAnthropic(**kwargs)
83
-
84
- @classmethod
85
- def create_google(cls, model_name: str = "gemini-2.0-flash", **kwargs) -> Any:
86
- """
87
- Create Google chatbot model
88
- Args:
89
- model_name (str): Name of the model
90
- **kwargs: Additional arguments
91
- Returns:
92
- ChatGoogleGenerativeAI: Chatbot model
93
- """
94
- if not check_package("langchain_google_genai"):
95
- raise ImportError("langchain_google_genai package not found. Please install it using: pip install google-generativeai")
96
-
97
- from langchain_google_genai import ChatGoogleGenerativeAI
98
- kwargs["model"] = model_name
99
- return ChatGoogleGenerativeAI(**kwargs)
100
-
101
- @classmethod
102
- def create_ollama(cls, model_name: str = "llama3", **kwargs) -> Any:
103
- """
104
- Create Ollama chatbot model
105
- Args:
106
- model_name (str): Name of the model
107
- **kwargs: Additional arguments
108
- Returns:
109
- Ollama: Chatbot model
110
- """
111
- if not check_package("langchain_ollama"):
112
- raise ImportError("Langchain Community package not found. Please install it using: pip install langchain_ollama")
113
-
114
- from langchain_ollama import ChatOllama
115
-
116
- print(f"Current Ollama serve model is {os.system('ollama ps')}")
117
- kwargs["model"] = model_name
118
- return ChatOllama(**kwargs)
119
-
120
- @classmethod
121
- def create_groq(cls, model_name: str = "llama-3.3-70b-versatile", **kwargs) -> Any:
122
- """
123
- Create Groq chatbot model
124
- Args:
125
- model_name (str): Name of the model
126
- **kwargs: Additional arguments. Options are: temperature, groq_api_key, model_name
127
- Returns:
128
- ChatGroq: Chatbot model
129
- """
130
- if not check_package("langchain_groq"):
131
- raise ImportError("Langchain Groq package not found. Please install it using: pip install langchain-groq")
132
-
133
- from langchain_groq import ChatGroq
134
- kwargs["model"] = model_name
135
- return ChatGroq(**kwargs)
136
-
137
- @classmethod
138
- def create_deepseek(cls, model_name: str = "deepseek-chat", **kwargs) -> Any:
139
- """
140
- Create Deepseek chatbot model
141
- Args:
142
- model_name (str): Name of the model
143
- **kwargs: Additional arguments
144
- Returns:
145
- ChatDeepseek: Chatbot model
146
- """
147
- if not check_package("langchain_deepseek"):
148
- raise ImportError("Langchain Deepseek package not found. Please install it using: pip install langchain-deepseek")
149
-
150
- from langchain_deepseek import ChatDeepSeek
151
- kwargs["model"] = model_name
152
- return ChatDeepSeek(**kwargs)
153
-
154
- @classmethod
155
- def create_qwen(cls, model_name: str = "qwen", **kwargs) -> Any:
156
- """
157
- Create Qwen chatbot model
158
- Args:
159
- model_name (str): Name of the model
160
- **kwargs: Additional arguments
161
- Returns:
162
- ChatQwen: Chatbot model
163
- """
164
- if not check_package("langchain_community"):
165
- raise ImportError("Langchain Qwen package not found. Please install it using: pip install langchain_community")
166
-
167
- from langchain_community.chat_models.tongyi import ChatTongyi
168
- kwargs["model"] = model_name
169
- return ChatTongyi(streaming=True,**kwargs)
170
-
171
- @classmethod
172
- def create_hugging_face(cls, model_name: str = "Qwen/Qwen2.5-VL-7B-Instruct",model_function: str = "image-text-to-text",
173
- device='cpu',**kwargs) -> Any:
174
- """
175
- Create and load hugging face model.
176
- Args:
177
- model_name (str): Name of the model
178
- model_function (str): model function. Default is image-text-to-text.
179
- device (str): Device to use. Default is cpu
180
- **kwargs: Additional arguments
181
- Returns:
182
- ChatHuggingFace: Chatbot model
183
- """
184
- if not check_package("transformers"):
185
- raise ImportError("Transformers package not found. Please install it using: pip install transformers")
186
- if not check_package("langchain_huggingface"):
187
- raise ImportError("langchain_huggingface package not found. Please install it using: pip install langchain_huggingface")
188
- if not check_package("torch"):
189
- raise ImportError("Torch package not found. Please install it using: pip install torch")
190
-
191
- from langchain_huggingface import HuggingFacePipeline
192
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, AutoModelForImageTextToText,AutoProcessor
193
- import torch
194
-
195
- device = torch.device(device) if torch.cuda.is_available() else torch.device("cpu")
196
-
197
- temperature = kwargs.pop("temperature", 0.7)
198
- max_length = kwargs.pop("max_length", 1024)
199
-
200
- if model_function == "image-text-to-text":
201
- tokenizer = AutoProcessor.from_pretrained(model_name,trust_remote_code=True)
202
- model = AutoModelForImageTextToText.from_pretrained(
203
- model_name,
204
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
205
- device_map=device,
206
- trust_remote_code=True,
207
- **kwargs
208
- )
209
- else:
210
- tokenizer = AutoTokenizer.from_pretrained(model_name,trust_remote_code=True)
211
- model = AutoModelForCausalLM.from_pretrained(
212
- model_name,
213
- torch_dtype=torch.float16 if device == "cuda" else torch.float32,
214
- device_map=device,
215
- trust_remote_code=True,
216
- **kwargs)
217
-
218
- # Create pipeline
219
- pipe = pipeline(
220
- model_function,
221
- model=model,
222
- tokenizer=tokenizer,
223
- max_length=max_length,
224
- temperature=temperature
225
- )
226
-
227
- # Create and return LangChain HuggingFacePipeline
228
- return HuggingFacePipeline(pipeline=pipe)
229
-
230
- def _reset_model(self):
231
- """Reset the model"""
232
- self.model = self.model.reset()
233
-
234
- def invoke_query(self,query: str,file_path: str = None,get_content_only: bool = True,images: list = None,pydantic_model = None) -> str:
235
- """
236
- Invoke the model
237
- Args:
238
- query (str): Query to send to the model
239
- file_path (str): Path to text file to load. Default is None
240
- get_content_only (bool): Whether to return only content
241
- images (list): List of images to send to the model
242
- pydantic_model: Pydantic model for structured output
243
- Returns:
244
- str: Response from the model
245
- """
246
- if file_path:
247
- loader = DocumentExtractor()
248
- data = loader.get_data(file_path)
249
- query = query + "\n\n" + data
250
-
251
- structured_model = None
252
- if pydantic_model is not None:
253
- try:
254
- structured_model = self.model.with_structured_output(pydantic_model)
255
- except Exception as e:
256
- raise ValueError(f"Error with pydantic_model: {e}")
257
- if structured_model is None:
258
- structured_model = self.model
259
- else:
260
- print("Using structured model with pydantic schema. So get_content_only is set to False.")
261
- get_content_only = False # Override to get full response when using structured model
262
- if images:
263
- message = self._model_invoke_images(
264
- images=images,
265
- prompt=query)
266
- res = structured_model.invoke([message])
267
- else:
268
- res = structured_model.invoke(query)
269
- if get_content_only:
270
- try:
271
- return res.content
272
- except Exception:
273
- return res
274
- return res
275
-
276
- def _image_to_base64(self,image):
277
- with open(image, "rb") as f:
278
- return base64.b64encode(f.read()).decode('utf-8')
279
-
280
- def _model_invoke_images(self, images: list, prompt: str) -> str:
281
- """
282
- Function to invoke the model with images
283
- Args:
284
- images (list): List of images
285
- prompt (str): Prompt
286
- Returns:
287
- str: Output from the model
288
- """
289
- base64_images = [self._image_to_base64(image) for image in images]
290
- image_prompt_create = [{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_images[i]}"}} for i in range(len(images))]
291
- prompt_new = [{"type": "text", "text": prompt}, *image_prompt_create]
292
-
293
- message = HumanMessage(content=prompt_new)
294
- return message
295
-
296
- def _get_llm_metadata(self):
297
- """
298
- Returns Basic metadata about the LLM
299
- """
300
- print("Model Name: ", self.model)
301
- print("Model Temperature: ", self.model.temperature)
302
- print("Model Max Tokens: ", self.model.max_output_tokens)
303
- print("Model Top P: ", self.model.top_p)
304
- print("Model Top K: ", self.model.top_k)
305
- print("Model Input Schema:",self.model.input_schema)
306
-
1
+ ## file for loading all models for chat/rag
2
+
3
+ import os
4
+ from langchain_core.messages import HumanMessage
5
+ from mb_rag.utils.extra import check_package
6
+ import base64
7
+ from .utils.extra import check_package
8
+ from typing import Any
9
+ from .utils.all_data_extract import DocumentExtractor
10
+ import pandas as pd
11
+ from tqdm.auto import tqdm
12
+ from concurrent.futures import ThreadPoolExecutor, as_completed
13
+
14
+ __all__ = [
15
+ 'ModelFactory',
16
+ ]
17
+
18
+ class ModelFactory:
19
+ """Factory class for creating different types of chatbot models"""
20
+
21
+ def __init__(self, model_type: str = 'openai', model_name: str = "gpt-4o", **kwargs) -> Any:
22
+ """
23
+ Factory method to create any type of model
24
+ Args:
25
+ model_type (str): Type of model to create. Default is OpenAI. Options are openai, anthropic, google, ollama , groq
26
+ model_name (str): Name of the model
27
+ **kwargs: Additional arguments
28
+ Returns:
29
+ Any: Chatbot model
30
+ """
31
+ creators = {
32
+ 'openai': self.create_openai,
33
+ 'anthropic': self.create_anthropic,
34
+ 'google': self.create_google,
35
+ 'ollama': self.create_ollama,
36
+ 'groq': self.create_groq,
37
+ 'deepseek': self.create_deepseek,
38
+ 'qwen' : self.create_qwen,
39
+ 'hugging_face': self.create_hugging_face
40
+ }
41
+
42
+ self.model_type = model_type
43
+ self.model_name = model_name
44
+ model_data = creators.get(model_type)
45
+ if not model_data:
46
+ raise ValueError(f"Unsupported model type: {model_type}")
47
+
48
+ try:
49
+ self.model = model_data(model_name, **kwargs)
50
+ except Exception as e:
51
+ raise ValueError(f"Error creating {model_type} model: {str(e)}")
52
+
53
+ @classmethod
54
+ def create_openai(cls, model_name: str = "gpt-4o", **kwargs) -> Any:
55
+ """
56
+ Create OpenAI chatbot model
57
+ Args:
58
+ model_name (str): Name of the model
59
+ **kwargs: Additional arguments
60
+ Returns:
61
+ ChatOpenAI: Chatbot model
62
+ """
63
+ if not check_package("openai"):
64
+ raise ImportError("OpenAI package not found. Please install it using: pip install openai langchain-openai")
65
+
66
+ from langchain_openai import ChatOpenAI
67
+ kwargs["model_name"] = model_name
68
+ return ChatOpenAI(**kwargs)
69
+
70
+ @classmethod
71
+ def create_anthropic(cls, model_name: str = "claude-3-opus-20240229", **kwargs) -> Any:
72
+ """
73
+ Create Anthropic chatbot model
74
+ Args:
75
+ model_name (str): Name of the model
76
+ **kwargs: Additional arguments
77
+ Returns:
78
+ ChatAnthropic: Chatbot model
79
+ """
80
+ if not check_package("anthropic"):
81
+ raise ImportError("Anthropic package not found. Please install it using: pip install anthropic langchain-anthropic")
82
+
83
+ from langchain_anthropic import ChatAnthropic
84
+ kwargs["model_name"] = model_name
85
+ return ChatAnthropic(**kwargs)
86
+
87
+ @classmethod
88
+ def create_google(cls, model_name: str = "gemini-2.0-flash", **kwargs) -> Any:
89
+ """
90
+ Create Google chatbot model
91
+ Args:
92
+ model_name (str): Name of the model
93
+ **kwargs: Additional arguments
94
+ Returns:
95
+ ChatGoogleGenerativeAI: Chatbot model
96
+ """
97
+ if not check_package("langchain_google_genai"):
98
+ raise ImportError("langchain_google_genai package not found. Please install it using: pip install google-generativeai")
99
+
100
+ from langchain_google_genai import ChatGoogleGenerativeAI
101
+ kwargs["model"] = model_name
102
+ return ChatGoogleGenerativeAI(**kwargs)
103
+
104
+ @classmethod
105
+ def create_ollama(cls, model_name: str = "llama3", **kwargs) -> Any:
106
+ """
107
+ Create Ollama chatbot model
108
+ Args:
109
+ model_name (str): Name of the model
110
+ **kwargs: Additional arguments
111
+ Returns:
112
+ Ollama: Chatbot model
113
+ """
114
+ if not check_package("langchain_ollama"):
115
+ raise ImportError("Langchain Community package not found. Please install it using: pip install langchain_ollama")
116
+
117
+ from langchain_ollama import ChatOllama
118
+
119
+ print(f"Current Ollama serve model is {os.system('ollama ps')}")
120
+ kwargs["model"] = model_name
121
+ return ChatOllama(**kwargs)
122
+
123
+ @classmethod
124
+ def create_groq(cls, model_name: str = "llama-3.3-70b-versatile", **kwargs) -> Any:
125
+ """
126
+ Create Groq chatbot model
127
+ Args:
128
+ model_name (str): Name of the model
129
+ **kwargs: Additional arguments. Options are: temperature, groq_api_key, model_name
130
+ Returns:
131
+ ChatGroq: Chatbot model
132
+ """
133
+ if not check_package("langchain_groq"):
134
+ raise ImportError("Langchain Groq package not found. Please install it using: pip install langchain-groq")
135
+
136
+ from langchain_groq import ChatGroq
137
+ kwargs["model"] = model_name
138
+ return ChatGroq(**kwargs)
139
+
140
+ @classmethod
141
+ def create_deepseek(cls, model_name: str = "deepseek-chat", **kwargs) -> Any:
142
+ """
143
+ Create Deepseek chatbot model
144
+ Args:
145
+ model_name (str): Name of the model
146
+ **kwargs: Additional arguments
147
+ Returns:
148
+ ChatDeepseek: Chatbot model
149
+ """
150
+ if not check_package("langchain_deepseek"):
151
+ raise ImportError("Langchain Deepseek package not found. Please install it using: pip install langchain-deepseek")
152
+
153
+ from langchain_deepseek import ChatDeepSeek
154
+ kwargs["model"] = model_name
155
+ return ChatDeepSeek(**kwargs)
156
+
157
+ @classmethod
158
+ def create_qwen(cls, model_name: str = "qwen", **kwargs) -> Any:
159
+ """
160
+ Create Qwen chatbot model
161
+ Args:
162
+ model_name (str): Name of the model
163
+ **kwargs: Additional arguments
164
+ Returns:
165
+ ChatQwen: Chatbot model
166
+ """
167
+ if not check_package("langchain_community"):
168
+ raise ImportError("Langchain Qwen package not found. Please install it using: pip install langchain_community")
169
+
170
+ from langchain_community.chat_models.tongyi import ChatTongyi
171
+ kwargs["model"] = model_name
172
+ return ChatTongyi(streaming=True,**kwargs)
173
+
174
+ @classmethod
175
+ def create_hugging_face(cls, model_name: str = "Qwen/Qwen2.5-VL-7B-Instruct",model_function: str = "image-text-to-text",
176
+ device='cpu',**kwargs) -> Any:
177
+ """
178
+ Create and load hugging face model.
179
+ Args:
180
+ model_name (str): Name of the model
181
+ model_function (str): model function. Default is image-text-to-text.
182
+ device (str): Device to use. Default is cpu
183
+ **kwargs: Additional arguments
184
+ Returns:
185
+ ChatHuggingFace: Chatbot model
186
+ """
187
+ if not check_package("transformers"):
188
+ raise ImportError("Transformers package not found. Please install it using: pip install transformers")
189
+ if not check_package("langchain_huggingface"):
190
+ raise ImportError("langchain_huggingface package not found. Please install it using: pip install langchain_huggingface")
191
+ if not check_package("torch"):
192
+ raise ImportError("Torch package not found. Please install it using: pip install torch")
193
+
194
+ from langchain_huggingface import HuggingFacePipeline
195
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, AutoModelForImageTextToText,AutoProcessor
196
+ import torch
197
+
198
+ device = torch.device(device) if torch.cuda.is_available() else torch.device("cpu")
199
+
200
+ temperature = kwargs.pop("temperature", 0.7)
201
+ max_length = kwargs.pop("max_length", 1024)
202
+
203
+ if model_function == "image-text-to-text":
204
+ tokenizer = AutoProcessor.from_pretrained(model_name,trust_remote_code=True)
205
+ model = AutoModelForImageTextToText.from_pretrained(
206
+ model_name,
207
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
208
+ device_map=device,
209
+ trust_remote_code=True,
210
+ **kwargs
211
+ )
212
+ else:
213
+ tokenizer = AutoTokenizer.from_pretrained(model_name,trust_remote_code=True)
214
+ model = AutoModelForCausalLM.from_pretrained(
215
+ model_name,
216
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
217
+ device_map=device,
218
+ trust_remote_code=True,
219
+ **kwargs)
220
+
221
+ # Create pipeline
222
+ pipe = pipeline(
223
+ model_function,
224
+ model=model,
225
+ tokenizer=tokenizer,
226
+ max_length=max_length,
227
+ temperature=temperature
228
+ )
229
+
230
+ # Create and return LangChain HuggingFacePipeline
231
+ return HuggingFacePipeline(pipeline=pipe)
232
+
233
+ def _reset_model(self):
234
+ """Reset the model"""
235
+ self.model = self.model.reset()
236
+
237
+ def invoke_query(self,query: str,file_path: str = None,get_content_only: bool = True,images: list = None,pydantic_model = None) -> str:
238
+ """
239
+ Invoke the model
240
+ Args:
241
+ query (str): Query to send to the model
242
+ file_path (str): Path to text file to load. Default is None
243
+ get_content_only (bool): Whether to return only content
244
+ images (list): List of images to send to the model
245
+ pydantic_model: Pydantic model for structured output
246
+ Returns:
247
+ str: Response from the model
248
+ """
249
+ if file_path:
250
+ loader = DocumentExtractor()
251
+ data = loader.get_data(file_path)
252
+ query = query + "\n\n" + data
253
+
254
+ structured_model = None
255
+ if pydantic_model is not None:
256
+ try:
257
+ structured_model = self.model.with_structured_output(pydantic_model)
258
+ except Exception as e:
259
+ raise ValueError(f"Error with pydantic_model: {e}")
260
+ if structured_model is None:
261
+ structured_model = self.model
262
+ else:
263
+ print("Using structured model with pydantic schema. So get_content_only is set to False.")
264
+ get_content_only = False # Override to get full response when using structured model
265
+ if images:
266
+ message = self._model_invoke_images(
267
+ images=images,
268
+ prompt=query)
269
+ res = structured_model.invoke([message])
270
+ else:
271
+ res = structured_model.invoke(query)
272
+ if get_content_only:
273
+ try:
274
+ return res.content
275
+ except Exception:
276
+ return res
277
+ return res
278
+
279
+ def invoke_query_threads(self,query_list: list,get_content_only: bool = True,input_data: list = None,n_workers: int = 4,pydantic_model=None) -> pd.DataFrame:
280
+ """
281
+ Invoke the model with multiple threads (parallel queries).
282
+
283
+ Args:
284
+ query_list (list): List of queries to send to the model
285
+ get_content_only (bool): Whether to return only content
286
+ input_data (list): List of input data for the model
287
+ n_workers (int): Number of workers to use for threading
288
+ pydantic_model: Pydantic model for structured output
289
+
290
+ Returns:
291
+ pandas.DataFrame: Response from the model
292
+ """
293
+ if not isinstance(query_list, list):
294
+ raise ValueError("query_list must be a list of strings")
295
+ if input_data is not None and not isinstance(input_data, list):
296
+ raise ValueError("input_data should be a list of messages")
297
+ if input_data is not None and len(input_data) != len(query_list):
298
+ raise ValueError("Length of input_data should equal length of query_list")
299
+
300
+ print("Length of query_list:", len(query_list))
301
+
302
+ df = pd.DataFrame(query_list, columns=["query"])
303
+ df["response"] = None
304
+
305
+ structured_model = None
306
+ if pydantic_model is not None:
307
+ try:
308
+ structured_model = self.model.with_structured_output(pydantic_model)
309
+ print("Using structured model with Pydantic schema. Setting get_content_only=False.")
310
+ get_content_only = False
311
+ except Exception as e:
312
+ raise ValueError(f"Error initializing pydantic_model: {e}")
313
+ else:
314
+ structured_model = self.model
315
+
316
+ def process_one(i, query_data):
317
+ try:
318
+ if input_data is not None and len(input_data) > 0:
319
+ image_data = input_data[i]
320
+ message = self._model_invoke_images(images=image_data, prompt=query_data)
321
+ res = structured_model.invoke([message])
322
+ else:
323
+ res = structured_model.invoke(query_data)
324
+
325
+ if get_content_only:
326
+ try:
327
+ res = res.content
328
+ except Exception:
329
+ pass
330
+ return i, query_data, res
331
+ except Exception as e:
332
+ return i, query_data, f"Error: {e}"
333
+
334
+ # Run all queries concurrently
335
+ with ThreadPoolExecutor(max_workers=n_workers) as executor:
336
+ futures = [executor.submit(process_one, i, q) for i, q in enumerate(query_list)]
337
+ for future in tqdm(as_completed(futures), total=len(futures), desc="Processing queries"):
338
+ i, query_data, res = future.result()
339
+ df.at[i, "query"] = query_data
340
+ df.at[i, "response"] = res
341
+
342
+ df.sort_index(inplace=True)
343
+ return df
344
+
345
+ def _image_to_base64(self,image):
346
+ with open(image, "rb") as f:
347
+ return base64.b64encode(f.read()).decode('utf-8')
348
+
349
+ def _model_invoke_images(self, images: list, prompt: str) -> str:
350
+ """
351
+ Function to invoke the model with images
352
+ Args:
353
+ images (list): List of images
354
+ prompt (str): Prompt
355
+ Returns:
356
+ str: Output from the model
357
+ """
358
+ base64_images = [self._image_to_base64(image) for image in images]
359
+ image_prompt_create = [{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_images[i]}"}} for i in range(len(images))]
360
+ prompt_new = [{"type": "text", "text": prompt}, *image_prompt_create]
361
+
362
+ message = HumanMessage(content=prompt_new)
363
+ return message
364
+
365
+ def _get_llm_metadata(self):
366
+ """
367
+ Returns Basic metadata about the LLM
368
+ """
369
+ print("Model Name: ", self.model)
370
+ print("Model Temperature: ", self.model.temperature)
371
+ print("Model Max Tokens: ", self.model.max_output_tokens)
372
+ print("Model Top P: ", self.model.top_p)
373
+ print("Model Top K: ", self.model.top_k)
374
+ print("Model Input Schema:",self.model.input_schema)
375
+