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