oceansweep
commited on
Commit
•
644abaa
1
Parent(s):
1ceea1c
Update App_Function_Libraries/LLM_API_Calls.py
Browse files- App_Function_Libraries/LLM_API_Calls.py +965 -976
App_Function_Libraries/LLM_API_Calls.py
CHANGED
@@ -1,977 +1,966 @@
|
|
1 |
-
# Summarization_General_Lib.py
|
2 |
-
#########################################
|
3 |
-
# General Summarization Library
|
4 |
-
# This library is used to perform summarization.
|
5 |
-
#
|
6 |
-
####
|
7 |
-
####################
|
8 |
-
# Function List
|
9 |
-
#
|
10 |
-
# 1. extract_text_from_segments(segments: List[Dict]) -> str
|
11 |
-
# 2. chat_with_openai(api_key, file_path, custom_prompt_arg)
|
12 |
-
# 3. chat_with_anthropic(api_key, file_path, model, custom_prompt_arg, max_retries=3, retry_delay=5)
|
13 |
-
# 4. chat_with_cohere(api_key, file_path, model, custom_prompt_arg)
|
14 |
-
# 5. chat_with_groq(api_key, input_data, custom_prompt_arg, system_prompt=None):
|
15 |
-
# 6. chat_with_openrouter(api_key, input_data, custom_prompt_arg, system_prompt=None)
|
16 |
-
# 7. chat_with_huggingface(api_key, input_data, custom_prompt_arg, system_prompt=None)
|
17 |
-
# 8. chat_with_deepseek(api_key, input_data, custom_prompt_arg, system_prompt=None)
|
18 |
-
# 9. chat_with_vllm(input_data, custom_prompt_input, api_key=None, vllm_api_url="http://127.0.0.1:8000/v1/chat/completions", system_prompt=None)
|
19 |
-
#
|
20 |
-
#
|
21 |
-
####################
|
22 |
-
#
|
23 |
-
# Import necessary libraries
|
24 |
-
import json
|
25 |
-
import logging
|
26 |
-
import os
|
27 |
-
import time
|
28 |
-
from typing import List
|
29 |
-
|
30 |
-
import requests
|
31 |
-
#
|
32 |
-
# Import 3rd-Party Libraries
|
33 |
-
from requests import RequestException
|
34 |
-
#
|
35 |
-
# Import Local libraries
|
36 |
-
from App_Function_Libraries.Utils.Utils import load_and_log_configs
|
37 |
-
#
|
38 |
-
#######################################################################################################################
|
39 |
-
# Function Definitions
|
40 |
-
#
|
41 |
-
|
42 |
-
#FIXME: Update to include full arguments
|
43 |
-
|
44 |
-
def extract_text_from_segments(segments):
|
45 |
-
logging.debug(f"Segments received: {segments}")
|
46 |
-
logging.debug(f"Type of segments: {type(segments)}")
|
47 |
-
|
48 |
-
text = ""
|
49 |
-
|
50 |
-
if isinstance(segments, list):
|
51 |
-
for segment in segments:
|
52 |
-
logging.debug(f"Current segment: {segment}")
|
53 |
-
logging.debug(f"Type of segment: {type(segment)}")
|
54 |
-
if 'Text' in segment:
|
55 |
-
text += segment['Text'] + " "
|
56 |
-
else:
|
57 |
-
logging.warning(f"Skipping segment due to missing 'Text' key: {segment}")
|
58 |
-
else:
|
59 |
-
logging.warning(f"Unexpected type of 'segments': {type(segments)}")
|
60 |
-
|
61 |
-
return text.strip()
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
def get_openai_embeddings(input_data: str, model: str) -> List[float]:
|
66 |
-
"""
|
67 |
-
Get embeddings for the input text from OpenAI API.
|
68 |
-
|
69 |
-
Args:
|
70 |
-
input_data (str): The input text to get embeddings for.
|
71 |
-
model (str): The model to use for generating embeddings.
|
72 |
-
|
73 |
-
Returns:
|
74 |
-
List[float]: The embeddings generated by the API.
|
75 |
-
"""
|
76 |
-
loaded_config_data = load_and_log_configs()
|
77 |
-
api_key = loaded_config_data['api_keys']['openai']
|
78 |
-
|
79 |
-
if not api_key:
|
80 |
-
logging.error("OpenAI: API key not found or is empty")
|
81 |
-
raise ValueError("OpenAI: API Key Not Provided/Found in Config file or is empty")
|
82 |
-
|
83 |
-
logging.debug(f"OpenAI: Using API Key: {api_key[:5]}...{api_key[-5:]}")
|
84 |
-
logging.debug(f"OpenAI: Raw input data (first 500 chars): {str(input_data)[:500]}...")
|
85 |
-
logging.debug(f"OpenAI: Using model: {model}")
|
86 |
-
|
87 |
-
headers = {
|
88 |
-
'Authorization': f'Bearer {api_key}',
|
89 |
-
'Content-Type': 'application/json'
|
90 |
-
}
|
91 |
-
|
92 |
-
request_data = {
|
93 |
-
"input": input_data,
|
94 |
-
"model": model,
|
95 |
-
}
|
96 |
-
|
97 |
-
try:
|
98 |
-
logging.debug("OpenAI: Posting request to embeddings API")
|
99 |
-
response = requests.post('https://api.openai.com/v1/embeddings', headers=headers, json=request_data)
|
100 |
-
logging.debug(f"Full API response data: {response}")
|
101 |
-
if response.status_code == 200:
|
102 |
-
response_data = response.json()
|
103 |
-
if 'data' in response_data and len(response_data['data']) > 0:
|
104 |
-
embedding = response_data['data'][0]['embedding']
|
105 |
-
logging.debug("OpenAI: Embeddings retrieved successfully")
|
106 |
-
return embedding
|
107 |
-
else:
|
108 |
-
logging.warning("OpenAI: Embedding data not found in the response")
|
109 |
-
raise ValueError("OpenAI: Embedding data not available in the response")
|
110 |
-
else:
|
111 |
-
logging.error(f"OpenAI: Embeddings request failed with status code {response.status_code}")
|
112 |
-
logging.error(f"OpenAI: Error response: {response.text}")
|
113 |
-
raise ValueError(f"OpenAI: Failed to retrieve embeddings. Status code: {response.status_code}")
|
114 |
-
except requests.RequestException as e:
|
115 |
-
logging.error(f"OpenAI: Error making API request: {str(e)}", exc_info=True)
|
116 |
-
raise ValueError(f"OpenAI: Error making API request: {str(e)}")
|
117 |
-
except Exception as e:
|
118 |
-
logging.error(f"OpenAI: Unexpected error: {str(e)}", exc_info=True)
|
119 |
-
raise ValueError(f"OpenAI: Unexpected error occurred: {str(e)}")
|
120 |
-
|
121 |
-
|
122 |
-
def chat_with_openai(api_key, input_data, custom_prompt_arg, temp=None, system_message=None):
|
123 |
-
loaded_config_data = load_and_log_configs()
|
124 |
-
openai_api_key = api_key
|
125 |
-
try:
|
126 |
-
# API key validation
|
127 |
-
if not openai_api_key:
|
128 |
-
logging.info("OpenAI: API key not provided as parameter")
|
129 |
-
logging.info("OpenAI: Attempting to use API key from config file")
|
130 |
-
openai_api_key = loaded_config_data['api_keys']['openai']
|
131 |
-
|
132 |
-
if not openai_api_key:
|
133 |
-
logging.error("OpenAI: API key not found or is empty")
|
134 |
-
return "OpenAI: API Key Not Provided/Found in Config file or is empty"
|
135 |
-
|
136 |
-
logging.debug(f"OpenAI: Using API Key: {openai_api_key[:5]}...{openai_api_key[-5:]}")
|
137 |
-
|
138 |
-
# Input data handling
|
139 |
-
logging.debug(f"OpenAI: Raw input data type: {type(input_data)}")
|
140 |
-
logging.debug(f"OpenAI: Raw input data (first 500 chars): {str(input_data)[:500]}...")
|
141 |
-
|
142 |
-
if isinstance(input_data, str):
|
143 |
-
if input_data.strip().startswith('{'):
|
144 |
-
# It's likely a JSON string
|
145 |
-
logging.debug("OpenAI: Parsing provided JSON string data for summarization")
|
146 |
-
try:
|
147 |
-
data = json.loads(input_data)
|
148 |
-
except json.JSONDecodeError as e:
|
149 |
-
logging.error(f"OpenAI: Error parsing JSON string: {str(e)}")
|
150 |
-
return f"OpenAI: Error parsing JSON input: {str(e)}"
|
151 |
-
elif os.path.isfile(input_data):
|
152 |
-
logging.debug("OpenAI: Loading JSON data from file for summarization")
|
153 |
-
with open(input_data, 'r') as file:
|
154 |
-
data = json.load(file)
|
155 |
-
else:
|
156 |
-
logging.debug("OpenAI: Using provided string data for summarization")
|
157 |
-
data = input_data
|
158 |
-
else:
|
159 |
-
data = input_data
|
160 |
-
|
161 |
-
logging.debug(f"OpenAI: Processed data type: {type(data)}")
|
162 |
-
logging.debug(f"OpenAI: Processed data (first 500 chars): {str(data)[:500]}...")
|
163 |
-
|
164 |
-
# Text extraction
|
165 |
-
if isinstance(data, dict):
|
166 |
-
if 'summary' in data:
|
167 |
-
logging.debug("OpenAI: Summary already exists in the loaded data")
|
168 |
-
return data['summary']
|
169 |
-
elif 'segments' in data:
|
170 |
-
text = extract_text_from_segments(data['segments'])
|
171 |
-
else:
|
172 |
-
text = json.dumps(data) # Convert dict to string if no specific format
|
173 |
-
elif isinstance(data, list):
|
174 |
-
text = extract_text_from_segments(data)
|
175 |
-
elif isinstance(data, str):
|
176 |
-
text = data
|
177 |
-
else:
|
178 |
-
raise ValueError(f"OpenAI: Invalid input data format: {type(data)}")
|
179 |
-
|
180 |
-
logging.debug(f"OpenAI: Extracted text (first 500 chars): {text[:500]}...")
|
181 |
-
logging.debug(f"OpenAI: Custom prompt: {custom_prompt_arg}")
|
182 |
-
|
183 |
-
openai_model = loaded_config_data['models']['openai'] or "gpt-4o"
|
184 |
-
logging.debug(f"OpenAI: Using model: {openai_model}")
|
185 |
-
|
186 |
-
headers = {
|
187 |
-
'Authorization': f'Bearer {openai_api_key}',
|
188 |
-
'Content-Type': 'application/json'
|
189 |
-
}
|
190 |
-
|
191 |
-
logging.debug(
|
192 |
-
f"OpenAI API Key: {openai_api_key[:5]}...{openai_api_key[-5:] if openai_api_key else None}")
|
193 |
-
logging.debug("openai: Preparing data + prompt for submittal")
|
194 |
-
openai_prompt = f"{text} \n\n\n\n{custom_prompt_arg}"
|
195 |
-
if temp is None:
|
196 |
-
temp = 0.7
|
197 |
-
if system_message is None:
|
198 |
-
system_message = "You are a helpful AI assistant who does whatever the user requests."
|
199 |
-
temp = float(temp)
|
200 |
-
data = {
|
201 |
-
"model": openai_model,
|
202 |
-
"messages": [
|
203 |
-
{"role": "system", "content": system_message},
|
204 |
-
{"role": "user", "content": openai_prompt}
|
205 |
-
],
|
206 |
-
"max_tokens": 4096,
|
207 |
-
"temperature": temp
|
208 |
-
}
|
209 |
-
|
210 |
-
logging.debug("OpenAI: Posting request")
|
211 |
-
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
|
212 |
-
logging.debug(f"Full API response data: {response}")
|
213 |
-
if response.status_code == 200:
|
214 |
-
response_data = response.json()
|
215 |
-
logging.debug(response_data)
|
216 |
-
if 'choices' in response_data and len(response_data['choices']) > 0:
|
217 |
-
chat_response = response_data['choices'][0]['message']['content'].strip()
|
218 |
-
logging.debug("openai: Chat Sent successfully")
|
219 |
-
logging.debug(f"openai: Chat response: {chat_response}")
|
220 |
-
return chat_response
|
221 |
-
else:
|
222 |
-
logging.warning("openai: Chat response not found in the response data")
|
223 |
-
return "openai: Chat not available"
|
224 |
-
else:
|
225 |
-
logging.error(f"OpenAI: Chat request failed with status code {response.status_code}")
|
226 |
-
logging.error(f"OpenAI: Error response: {response.text}")
|
227 |
-
return f"OpenAI: Failed to process chat response. Status code: {response.status_code}"
|
228 |
-
except json.JSONDecodeError as e:
|
229 |
-
logging.error(f"OpenAI: Error decoding JSON: {str(e)}", exc_info=True)
|
230 |
-
return f"OpenAI: Error decoding JSON input: {str(e)}"
|
231 |
-
except requests.RequestException as e:
|
232 |
-
logging.error(f"OpenAI: Error making API request: {str(e)}", exc_info=True)
|
233 |
-
return f"OpenAI: Error making API request: {str(e)}"
|
234 |
-
except Exception as e:
|
235 |
-
logging.error(f"OpenAI: Unexpected error: {str(e)}", exc_info=True)
|
236 |
-
return f"OpenAI: Unexpected error occurred: {str(e)}"
|
237 |
-
|
238 |
-
|
239 |
-
def chat_with_anthropic(api_key, input_data, model, custom_prompt_arg, max_retries=3, retry_delay=5, system_prompt=None):
|
240 |
-
try:
|
241 |
-
loaded_config_data = load_and_log_configs()
|
242 |
-
global anthropic_api_key
|
243 |
-
anthropic_api_key = api_key
|
244 |
-
# API key validation
|
245 |
-
if not api_key:
|
246 |
-
logging.info("Anthropic: API key not provided as parameter")
|
247 |
-
logging.info("Anthropic: Attempting to use API key from config file")
|
248 |
-
anthropic_api_key = loaded_config_data['api_keys']['anthropic']
|
249 |
-
|
250 |
-
if not api_key or api_key.strip() == "":
|
251 |
-
logging.error("Anthropic: API key not found or is empty")
|
252 |
-
return "Anthropic: API Key Not Provided/Found in Config file or is empty"
|
253 |
-
|
254 |
-
logging.debug(f"Anthropic: Using API Key: {api_key[:5]}...{api_key[-5:]}")
|
255 |
-
|
256 |
-
if system_prompt is not None:
|
257 |
-
logging.debug("Anthropic: Using provided system prompt")
|
258 |
-
pass
|
259 |
-
else:
|
260 |
-
system_prompt = "You are a helpful assistant"
|
261 |
-
|
262 |
-
logging.debug(f"AnthropicAI: Loaded data: {input_data}")
|
263 |
-
logging.debug(f"AnthropicAI: Type of data: {type(input_data)}")
|
264 |
-
|
265 |
-
anthropic_model = loaded_config_data['models']['anthropic']
|
266 |
-
|
267 |
-
headers = {
|
268 |
-
'x-api-key': anthropic_api_key,
|
269 |
-
'anthropic-version': '2023-06-01',
|
270 |
-
'Content-Type': 'application/json'
|
271 |
-
}
|
272 |
-
|
273 |
-
anthropic_user_prompt = custom_prompt_arg
|
274 |
-
logging.debug(f"Anthropic: User Prompt is {anthropic_user_prompt}")
|
275 |
-
user_message = {
|
276 |
-
"role": "user",
|
277 |
-
"content": f"{input_data} \n\n\n\n{anthropic_user_prompt}"
|
278 |
-
}
|
279 |
-
|
280 |
-
data = {
|
281 |
-
"model": model,
|
282 |
-
"max_tokens": 4096, # max _possible_ tokens to return
|
283 |
-
"messages": [user_message],
|
284 |
-
"stop_sequences": ["\n\nHuman:"],
|
285 |
-
"temperature": 0.1,
|
286 |
-
"top_k": 0,
|
287 |
-
"top_p": 1.0,
|
288 |
-
"metadata": {
|
289 |
-
"user_id": "example_user_id",
|
290 |
-
},
|
291 |
-
"stream": False,
|
292 |
-
"system": f"{system_prompt}"
|
293 |
-
}
|
294 |
-
|
295 |
-
for attempt in range(max_retries):
|
296 |
-
try:
|
297 |
-
logging.debug("anthropic: Posting request to API")
|
298 |
-
response = requests.post('https://api.anthropic.com/v1/messages', headers=headers, json=data)
|
299 |
-
logging.debug(f"Full API response data: {response}")
|
300 |
-
# Check if the status code indicates success
|
301 |
-
if response.status_code == 200:
|
302 |
-
logging.debug("anthropic: Post submittal successful")
|
303 |
-
response_data = response.json()
|
304 |
-
try:
|
305 |
-
chat_response = response_data['content'][0]['text'].strip()
|
306 |
-
logging.debug("anthropic: Chat request successful")
|
307 |
-
print("Chat request processed successfully.")
|
308 |
-
return chat_response
|
309 |
-
except (IndexError, KeyError) as e:
|
310 |
-
logging.debug("anthropic: Unexpected data in response")
|
311 |
-
print("Unexpected response format from Anthropic API:", response.text)
|
312 |
-
return None
|
313 |
-
elif response.status_code == 500: # Handle internal server error specifically
|
314 |
-
logging.debug("anthropic: Internal server error")
|
315 |
-
print("Internal server error from API. Retrying may be necessary.")
|
316 |
-
time.sleep(retry_delay)
|
317 |
-
else:
|
318 |
-
logging.debug(
|
319 |
-
f"anthropic: Failed to process chat request, status code {response.status_code}: {response.text}")
|
320 |
-
print(f"Failed to process chat request, status code {response.status_code}: {response.text}")
|
321 |
-
return None
|
322 |
-
|
323 |
-
except RequestException as e:
|
324 |
-
logging.error(f"anthropic: Network error during attempt {attempt + 1}/{max_retries}: {str(e)}")
|
325 |
-
if attempt < max_retries - 1:
|
326 |
-
time.sleep(retry_delay)
|
327 |
-
else:
|
328 |
-
return f"anthropic: Network error: {str(e)}"
|
329 |
-
except Exception as e:
|
330 |
-
logging.error(f"anthropic: Error in processing: {str(e)}")
|
331 |
-
return f"anthropic: Error occurred while processing summary with Anthropic: {str(e)}"
|
332 |
-
|
333 |
-
|
334 |
-
# Summarize with Cohere
|
335 |
-
def chat_with_cohere(api_key, input_data, model, custom_prompt_arg, system_prompt=None):
|
336 |
-
loaded_config_data = load_and_log_configs()
|
337 |
-
if api_key is not None:
|
338 |
-
logging.debug(f"Cohere Chat: API Key from parameter: {api_key[:3]}...{api_key[-3:]}")
|
339 |
-
logging.debug(f"Cohere Chat: Cohere API Key from config: {loaded_config_data['api_keys']['cohere']}")
|
340 |
-
try:
|
341 |
-
# API key validation
|
342 |
-
if api_key is None:
|
343 |
-
logging.info("Cohere Chat: API key not provided as parameter")
|
344 |
-
logging.info("Cohere Chat: Attempting to use API key from config file")
|
345 |
-
cohere_api_key = loaded_config_data.get('api_keys', {}).get('cohere')
|
346 |
-
if not cohere_api_key:
|
347 |
-
logging.error("Cohere Chat: API key not found or is empty")
|
348 |
-
return "Cohere Chat: API Key Not Provided/Found in Config file or is empty"
|
349 |
-
|
350 |
-
logging.debug(f"Cohere Chat: Using API Key: {cohere_api_key[:3]}...{cohere_api_key[-3:]}")
|
351 |
-
|
352 |
-
logging.debug(f"Cohere Chat: Loaded data: {input_data}")
|
353 |
-
logging.debug(f"Cohere Chat: Type of data: {type(input_data)}")
|
354 |
-
|
355 |
-
# Ensure model is set
|
356 |
-
if not model:
|
357 |
-
model = loaded_config_data['models']['cohere']
|
358 |
-
logging.debug(f"Cohere Chat: Using model: {model}")
|
359 |
-
|
360 |
-
headers = {
|
361 |
-
'accept': 'application/json',
|
362 |
-
'content-type': 'application/json',
|
363 |
-
'Authorization': f'Bearer {cohere_api_key}'
|
364 |
-
}
|
365 |
-
|
366 |
-
# Ensure system_prompt is set
|
367 |
-
if not system_prompt:
|
368 |
-
system_prompt = "You are a helpful assistant"
|
369 |
-
logging.debug(f"Cohere Chat: System Prompt being sent is: '{system_prompt}'")
|
370 |
-
|
371 |
-
cohere_prompt = input_data
|
372 |
-
if custom_prompt_arg:
|
373 |
-
cohere_prompt += f"\n\n{custom_prompt_arg}"
|
374 |
-
logging.debug(f"Cohere Chat: User Prompt being sent is: '{cohere_prompt}'")
|
375 |
-
|
376 |
-
data = {
|
377 |
-
"chat_history": [
|
378 |
-
{"role": "SYSTEM", "message": system_prompt},
|
379 |
-
],
|
380 |
-
"message": cohere_prompt,
|
381 |
-
"model": model,
|
382 |
-
"connectors": [{"id": "web-search"}]
|
383 |
-
}
|
384 |
-
logging.debug(f"Cohere Chat: Request data: {json.dumps(data, indent=2)}")
|
385 |
-
|
386 |
-
logging.debug("cohere chat: Submitting request to API endpoint")
|
387 |
-
print("cohere chat: Submitting request to API endpoint")
|
388 |
-
|
389 |
-
try:
|
390 |
-
response = requests.post('https://api.cohere.ai/v1/chat', headers=headers, json=data)
|
391 |
-
logging.debug(f"Cohere Chat: Raw API response: {response.text}")
|
392 |
-
except requests.RequestException as e:
|
393 |
-
logging.error(f"Cohere Chat: Error making API request: {str(e)}")
|
394 |
-
return f"Cohere Chat: Error making API request: {str(e)}"
|
395 |
-
|
396 |
-
if response.status_code == 200:
|
397 |
-
try:
|
398 |
-
response_data = response.json()
|
399 |
-
except json.JSONDecodeError:
|
400 |
-
logging.error("Cohere Chat: Failed to decode JSON response")
|
401 |
-
return "Cohere Chat: Failed to decode JSON response"
|
402 |
-
|
403 |
-
if response_data is None:
|
404 |
-
logging.error("Cohere Chat: No response data received.")
|
405 |
-
return "Cohere Chat: No response data received."
|
406 |
-
|
407 |
-
logging.debug(f"cohere chat: Full API response data: {json.dumps(response_data, indent=2)}")
|
408 |
-
|
409 |
-
if 'text' in response_data:
|
410 |
-
chat_response = response_data['text'].strip()
|
411 |
-
logging.debug("Cohere Chat: Chat request successful")
|
412 |
-
print("Cohere Chat request processed successfully.")
|
413 |
-
return chat_response
|
414 |
-
else:
|
415 |
-
logging.error("Cohere Chat: Expected 'text' key not found in API response.")
|
416 |
-
return "Cohere Chat: Expected data not found in API response."
|
417 |
-
else:
|
418 |
-
logging.error(f"Cohere Chat: API request failed with status code {response.status_code}: {response.text}")
|
419 |
-
print(f"Cohere Chat: Failed to process chat response, status code {response.status_code}: {response.text}")
|
420 |
-
return f"Cohere Chat: API request failed: {response.text}"
|
421 |
-
|
422 |
-
except Exception as e:
|
423 |
-
logging.error(f"Cohere Chat: Error in processing: {str(e)}", exc_info=True)
|
424 |
-
return f"Cohere Chat: Error occurred while processing chat request with Cohere: {str(e)}"
|
425 |
-
|
426 |
-
|
427 |
-
# https://console.groq.com/docs/quickstart
|
428 |
-
def chat_with_groq(api_key, input_data, custom_prompt_arg, temp=None, system_message=None):
|
429 |
-
logging.debug("Groq: Summarization process starting...")
|
430 |
-
try:
|
431 |
-
logging.debug("Groq: Loading and validating configurations")
|
432 |
-
loaded_config_data = load_and_log_configs()
|
433 |
-
if loaded_config_data is None:
|
434 |
-
logging.error("Failed to load configuration data")
|
435 |
-
groq_api_key = None
|
436 |
-
else:
|
437 |
-
# Prioritize the API key passed as a parameter
|
438 |
-
if api_key and api_key.strip():
|
439 |
-
groq_api_key = api_key
|
440 |
-
logging.info("Groq: Using API key provided as parameter")
|
441 |
-
else:
|
442 |
-
# If no parameter is provided, use the key from the config
|
443 |
-
groq_api_key = loaded_config_data['api_keys'].get('groq')
|
444 |
-
if groq_api_key:
|
445 |
-
logging.info("Groq: Using API key from config file")
|
446 |
-
else:
|
447 |
-
logging.warning("Groq: No API key found in config file")
|
448 |
-
|
449 |
-
# Final check to ensure we have a valid API key
|
450 |
-
if not groq_api_key or not groq_api_key.strip():
|
451 |
-
logging.error("Anthropic: No valid API key available")
|
452 |
-
# You might want to raise an exception here or handle this case as appropriate for your application
|
453 |
-
# For example: raise ValueError("No valid Anthropic API key available")
|
454 |
-
|
455 |
-
logging.debug(f"Groq: Using API Key: {groq_api_key[:5]}...{groq_api_key[-5:]}")
|
456 |
-
|
457 |
-
# Transcript data handling & Validation
|
458 |
-
if isinstance(input_data, str) and os.path.isfile(input_data):
|
459 |
-
logging.debug("Groq: Loading json data for summarization")
|
460 |
-
with open(input_data, 'r') as file:
|
461 |
-
data = json.load(file)
|
462 |
-
else:
|
463 |
-
logging.debug("Groq: Using provided string data for summarization")
|
464 |
-
data = input_data
|
465 |
-
|
466 |
-
# DEBUG - Debug logging to identify sent data
|
467 |
-
logging.debug(f"Groq: Loaded data: {data[:500]}...(snipped to first 500 chars)")
|
468 |
-
logging.debug(f"Groq: Type of data: {type(data)}")
|
469 |
-
|
470 |
-
if isinstance(data, dict) and 'summary' in data:
|
471 |
-
# If the loaded data is a dictionary and already contains a summary, return it
|
472 |
-
logging.debug("Groq: Summary already exists in the loaded data")
|
473 |
-
return data['summary']
|
474 |
-
|
475 |
-
# If the loaded data is a list of segment dictionaries or a string, proceed with summarization
|
476 |
-
if isinstance(data, list):
|
477 |
-
segments = data
|
478 |
-
text = extract_text_from_segments(segments)
|
479 |
-
elif isinstance(data, str):
|
480 |
-
text = data
|
481 |
-
else:
|
482 |
-
raise ValueError("Groq: Invalid input data format")
|
483 |
-
|
484 |
-
# Set the model to be used
|
485 |
-
groq_model = loaded_config_data['models']['groq']
|
486 |
-
|
487 |
-
if temp is None:
|
488 |
-
temp = 0.2
|
489 |
-
temp = float(temp)
|
490 |
-
if system_message is None:
|
491 |
-
system_message = "You are a helpful AI assistant who does whatever the user requests."
|
492 |
-
|
493 |
-
headers = {
|
494 |
-
'Authorization': f'Bearer {groq_api_key}',
|
495 |
-
'Content-Type': 'application/json'
|
496 |
-
}
|
497 |
-
|
498 |
-
groq_prompt = f"{text} \n\n\n\n{custom_prompt_arg}"
|
499 |
-
logging.debug("groq: Prompt being sent is {groq_prompt}")
|
500 |
-
|
501 |
-
data = {
|
502 |
-
"messages": [
|
503 |
-
{
|
504 |
-
"role": "system",
|
505 |
-
"content": system_message,
|
506 |
-
},
|
507 |
-
{
|
508 |
-
"role": "user",
|
509 |
-
"content": groq_prompt,
|
510 |
-
}
|
511 |
-
],
|
512 |
-
"model": groq_model,
|
513 |
-
"temperature": temp
|
514 |
-
}
|
515 |
-
|
516 |
-
logging.debug("groq: Submitting request to API endpoint")
|
517 |
-
print("groq: Submitting request to API endpoint")
|
518 |
-
response = requests.post('https://api.groq.com/openai/v1/chat/completions', headers=headers, json=data)
|
519 |
-
|
520 |
-
response_data = response.json()
|
521 |
-
logging.debug(f"Full API response data: {response_data}")
|
522 |
-
|
523 |
-
if response.status_code == 200:
|
524 |
-
logging.debug(response_data)
|
525 |
-
if 'choices' in response_data and len(response_data['choices']) > 0:
|
526 |
-
summary = response_data['choices'][0]['message']['content'].strip()
|
527 |
-
logging.debug("groq: Chat request successful")
|
528 |
-
print("Groq: Chat request successful.")
|
529 |
-
return summary
|
530 |
-
else:
|
531 |
-
logging.error("Groq(chat): Expected data not found in API response.")
|
532 |
-
return "Groq(chat): Expected data not found in API response."
|
533 |
-
else:
|
534 |
-
logging.error(f"groq: API request failed with status code {response.status_code}: {response.text}")
|
535 |
-
return f"groq: API request failed: {response.text}"
|
536 |
-
|
537 |
-
except Exception as e:
|
538 |
-
logging.error("groq: Error in processing: %s", str(e))
|
539 |
-
return f"groq: Error occurred while processing summary with groq: {str(e)}"
|
540 |
-
|
541 |
-
|
542 |
-
def chat_with_openrouter(api_key, input_data, custom_prompt_arg, temp=None, system_message=None):
|
543 |
-
import requests
|
544 |
-
import json
|
545 |
-
global openrouter_model, openrouter_api_key
|
546 |
-
try:
|
547 |
-
logging.debug("OpenRouter: Loading and validating configurations")
|
548 |
-
loaded_config_data = load_and_log_configs()
|
549 |
-
if loaded_config_data is None:
|
550 |
-
logging.error("Failed to load configuration data")
|
551 |
-
openrouter_api_key = None
|
552 |
-
else:
|
553 |
-
# Prioritize the API key passed as a parameter
|
554 |
-
if api_key and api_key.strip():
|
555 |
-
openrouter_api_key = api_key
|
556 |
-
logging.info("OpenRouter: Using API key provided as parameter")
|
557 |
-
else:
|
558 |
-
# If no parameter is provided, use the key from the config
|
559 |
-
openrouter_api_key = loaded_config_data['api_keys'].get('openrouter')
|
560 |
-
if openrouter_api_key:
|
561 |
-
logging.info("OpenRouter: Using API key from config file")
|
562 |
-
else:
|
563 |
-
logging.warning("OpenRouter: No API key found in config file")
|
564 |
-
|
565 |
-
# Model Selection validation
|
566 |
-
logging.debug("OpenRouter: Validating model selection")
|
567 |
-
loaded_config_data = load_and_log_configs()
|
568 |
-
openrouter_model = loaded_config_data['models']['openrouter']
|
569 |
-
logging.debug(f"OpenRouter: Using model from config file: {openrouter_model}")
|
570 |
-
|
571 |
-
# Final check to ensure we have a valid API key
|
572 |
-
if not openrouter_api_key or not openrouter_api_key.strip():
|
573 |
-
logging.error("OpenRouter: No valid API key available")
|
574 |
-
raise ValueError("No valid Anthropic API key available")
|
575 |
-
except Exception as e:
|
576 |
-
logging.error("OpenRouter: Error in processing: %s", str(e))
|
577 |
-
return f"OpenRouter: Error occurred while processing config file with OpenRouter: {str(e)}"
|
578 |
-
|
579 |
-
logging.debug(f"OpenRouter: Using API Key: {openrouter_api_key[:5]}...{openrouter_api_key[-5:]}")
|
580 |
-
|
581 |
-
logging.debug(f"OpenRouter: Using Model: {openrouter_model}")
|
582 |
-
|
583 |
-
if isinstance(input_data, str) and os.path.isfile(input_data):
|
584 |
-
logging.debug("OpenRouter: Loading json data for summarization")
|
585 |
-
with open(input_data, 'r') as file:
|
586 |
-
data = json.load(file)
|
587 |
-
else:
|
588 |
-
logging.debug("OpenRouter: Using provided string data for summarization")
|
589 |
-
data = input_data
|
590 |
-
|
591 |
-
# DEBUG - Debug logging to identify sent data
|
592 |
-
logging.debug(f"OpenRouter: Loaded data: {data[:500]}...(snipped to first 500 chars)")
|
593 |
-
logging.debug(f"OpenRouter: Type of data: {type(data)}")
|
594 |
-
|
595 |
-
if isinstance(data, dict) and 'summary' in data:
|
596 |
-
# If the loaded data is a dictionary and already contains a summary, return it
|
597 |
-
logging.debug("OpenRouter: Summary already exists in the loaded data")
|
598 |
-
return data['summary']
|
599 |
-
|
600 |
-
# If the loaded data is a list of segment dictionaries or a string, proceed with summarization
|
601 |
-
if isinstance(data, list):
|
602 |
-
segments = data
|
603 |
-
text = extract_text_from_segments(segments)
|
604 |
-
elif isinstance(data, str):
|
605 |
-
text = data
|
606 |
-
else:
|
607 |
-
raise ValueError("OpenRouter: Invalid input data format")
|
608 |
-
|
609 |
-
openrouter_prompt = f"{input_data} \n\n\n\n{custom_prompt_arg}"
|
610 |
-
logging.debug(f"openrouter: User Prompt being sent is {openrouter_prompt}")
|
611 |
-
|
612 |
-
if temp is None:
|
613 |
-
temp = 0.1
|
614 |
-
temp = float(temp)
|
615 |
-
if system_message is None:
|
616 |
-
system_message = "You are a helpful AI assistant who does whatever the user requests."
|
617 |
-
|
618 |
-
try:
|
619 |
-
logging.debug("OpenRouter: Submitting request to API endpoint")
|
620 |
-
print("OpenRouter: Submitting request to API endpoint")
|
621 |
-
response = requests.post(
|
622 |
-
url="https://openrouter.ai/api/v1/chat/completions",
|
623 |
-
headers={
|
624 |
-
"Authorization": f"Bearer {openrouter_api_key}",
|
625 |
-
},
|
626 |
-
data=json.dumps({
|
627 |
-
"model": openrouter_model,
|
628 |
-
"messages": [
|
629 |
-
{"role": "system", "content": system_message},
|
630 |
-
{"role": "user", "content": openrouter_prompt}
|
631 |
-
],
|
632 |
-
"temperature": temp
|
633 |
-
})
|
634 |
-
)
|
635 |
-
|
636 |
-
response_data = response.json()
|
637 |
-
logging.debug("Full API Response Data: %s", response_data)
|
638 |
-
|
639 |
-
if response.status_code == 200:
|
640 |
-
if 'choices' in response_data and len(response_data['choices']) > 0:
|
641 |
-
summary = response_data['choices'][0]['message']['content'].strip()
|
642 |
-
logging.debug("openrouter: Chat request successful")
|
643 |
-
print("openrouter: Chat request successful.")
|
644 |
-
return summary
|
645 |
-
else:
|
646 |
-
logging.error("openrouter: Expected data not found in API response.")
|
647 |
-
return "openrouter: Expected data not found in API response."
|
648 |
-
else:
|
649 |
-
logging.error(f"openrouter: API request failed with status code {response.status_code}: {response.text}")
|
650 |
-
return f"openrouter: API request failed: {response.text}"
|
651 |
-
except Exception as e:
|
652 |
-
logging.error("openrouter: Error in processing: %s", str(e))
|
653 |
-
return f"openrouter: Error occurred while processing chat request with openrouter: {str(e)}"
|
654 |
-
|
655 |
-
|
656 |
-
# FIXME: This function is not yet implemented properly
|
657 |
-
def chat_with_huggingface(api_key, input_data, custom_prompt_arg, system_prompt=None, temp=None):
|
658 |
-
loaded_config_data = load_and_log_configs()
|
659 |
-
logging.debug(f"huggingface Chat: Chat request process starting...")
|
660 |
-
try:
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
-
|
679 |
-
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
logging.debug(f"
|
687 |
-
|
688 |
-
|
689 |
-
|
690 |
-
"
|
691 |
-
|
692 |
-
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
728 |
-
|
729 |
-
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
-
|
739 |
-
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
744 |
-
|
745 |
-
|
746 |
-
|
747 |
-
logging
|
748 |
-
|
749 |
-
|
750 |
-
|
751 |
-
|
752 |
-
|
753 |
-
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
|
772 |
-
|
773 |
-
|
774 |
-
|
775 |
-
|
776 |
-
|
777 |
-
|
778 |
-
|
779 |
-
|
780 |
-
|
781 |
-
|
782 |
-
|
783 |
-
|
784 |
-
|
785 |
-
|
786 |
-
|
787 |
-
|
788 |
-
|
789 |
-
|
790 |
-
|
791 |
-
|
792 |
-
|
793 |
-
|
794 |
-
|
795 |
-
|
796 |
-
|
797 |
-
|
798 |
-
]
|
799 |
-
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
|
805 |
-
|
806 |
-
|
807 |
-
|
808 |
-
|
809 |
-
|
810 |
-
|
811 |
-
|
812 |
-
|
813 |
-
|
814 |
-
|
815 |
-
|
816 |
-
|
817 |
-
|
818 |
-
|
819 |
-
|
820 |
-
|
821 |
-
|
822 |
-
|
823 |
-
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
-
|
829 |
-
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
-
|
839 |
-
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
870 |
-
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
876 |
-
|
877 |
-
|
878 |
-
|
879 |
-
|
880 |
-
"
|
881 |
-
|
882 |
-
|
883 |
-
|
884 |
-
|
885 |
-
|
886 |
-
|
887 |
-
|
888 |
-
|
889 |
-
|
890 |
-
|
891 |
-
|
892 |
-
|
893 |
-
|
894 |
-
|
895 |
-
|
896 |
-
|
897 |
-
|
898 |
-
|
899 |
-
|
900 |
-
|
901 |
-
|
902 |
-
|
903 |
-
|
904 |
-
|
905 |
-
|
906 |
-
|
907 |
-
|
908 |
-
|
909 |
-
|
910 |
-
|
911 |
-
|
912 |
-
|
913 |
-
|
914 |
-
|
915 |
-
|
916 |
-
|
917 |
-
#
|
918 |
-
#
|
919 |
-
#
|
920 |
-
#
|
921 |
-
#
|
922 |
-
#
|
923 |
-
#
|
924 |
-
#
|
925 |
-
# logging.
|
926 |
-
#
|
927 |
-
#
|
928 |
-
#
|
929 |
-
# logging.
|
930 |
-
#
|
931 |
-
#
|
932 |
-
#
|
933 |
-
# )
|
934 |
-
#
|
935 |
-
# if isinstance(
|
936 |
-
#
|
937 |
-
#
|
938 |
-
#
|
939 |
-
#
|
940 |
-
#
|
941 |
-
#
|
942 |
-
#
|
943 |
-
#
|
944 |
-
#
|
945 |
-
#
|
946 |
-
#
|
947 |
-
#
|
948 |
-
#
|
949 |
-
#
|
950 |
-
#
|
951 |
-
#
|
952 |
-
#
|
953 |
-
#
|
954 |
-
#
|
955 |
-
#
|
956 |
-
#
|
957 |
-
#
|
958 |
-
#
|
959 |
-
#
|
960 |
-
#
|
961 |
-
|
962 |
-
|
963 |
-
|
964 |
-
#
|
965 |
-
#
|
966 |
-
# {"role": "system", "content": f"{system_prompt}"},
|
967 |
-
# {"role": "user", "content": f"{text} \n\n\n\n{custom_prompt}"}
|
968 |
-
# ]
|
969 |
-
# )
|
970 |
-
# vllm_summary = completion.choices[0].message.content
|
971 |
-
# return vllm_summary
|
972 |
-
|
973 |
-
|
974 |
-
|
975 |
-
#
|
976 |
-
#
|
977 |
#######################################################################################################################
|
|
|
1 |
+
# Summarization_General_Lib.py
|
2 |
+
#########################################
|
3 |
+
# General Summarization Library
|
4 |
+
# This library is used to perform summarization.
|
5 |
+
#
|
6 |
+
####
|
7 |
+
####################
|
8 |
+
# Function List
|
9 |
+
#
|
10 |
+
# 1. extract_text_from_segments(segments: List[Dict]) -> str
|
11 |
+
# 2. chat_with_openai(api_key, file_path, custom_prompt_arg)
|
12 |
+
# 3. chat_with_anthropic(api_key, file_path, model, custom_prompt_arg, max_retries=3, retry_delay=5)
|
13 |
+
# 4. chat_with_cohere(api_key, file_path, model, custom_prompt_arg)
|
14 |
+
# 5. chat_with_groq(api_key, input_data, custom_prompt_arg, system_prompt=None):
|
15 |
+
# 6. chat_with_openrouter(api_key, input_data, custom_prompt_arg, system_prompt=None)
|
16 |
+
# 7. chat_with_huggingface(api_key, input_data, custom_prompt_arg, system_prompt=None)
|
17 |
+
# 8. chat_with_deepseek(api_key, input_data, custom_prompt_arg, system_prompt=None)
|
18 |
+
# 9. chat_with_vllm(input_data, custom_prompt_input, api_key=None, vllm_api_url="http://127.0.0.1:8000/v1/chat/completions", system_prompt=None)
|
19 |
+
#
|
20 |
+
#
|
21 |
+
####################
|
22 |
+
#
|
23 |
+
# Import necessary libraries
|
24 |
+
import json
|
25 |
+
import logging
|
26 |
+
import os
|
27 |
+
import time
|
28 |
+
from typing import List
|
29 |
+
|
30 |
+
import requests
|
31 |
+
#
|
32 |
+
# Import 3rd-Party Libraries
|
33 |
+
from requests import RequestException
|
34 |
+
#
|
35 |
+
# Import Local libraries
|
36 |
+
from App_Function_Libraries.Utils.Utils import load_and_log_configs
|
37 |
+
#
|
38 |
+
#######################################################################################################################
|
39 |
+
# Function Definitions
|
40 |
+
#
|
41 |
+
|
42 |
+
#FIXME: Update to include full arguments
|
43 |
+
|
44 |
+
def extract_text_from_segments(segments):
|
45 |
+
logging.debug(f"Segments received: {segments}")
|
46 |
+
logging.debug(f"Type of segments: {type(segments)}")
|
47 |
+
|
48 |
+
text = ""
|
49 |
+
|
50 |
+
if isinstance(segments, list):
|
51 |
+
for segment in segments:
|
52 |
+
logging.debug(f"Current segment: {segment}")
|
53 |
+
logging.debug(f"Type of segment: {type(segment)}")
|
54 |
+
if 'Text' in segment:
|
55 |
+
text += segment['Text'] + " "
|
56 |
+
else:
|
57 |
+
logging.warning(f"Skipping segment due to missing 'Text' key: {segment}")
|
58 |
+
else:
|
59 |
+
logging.warning(f"Unexpected type of 'segments': {type(segments)}")
|
60 |
+
|
61 |
+
return text.strip()
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
def get_openai_embeddings(input_data: str, model: str) -> List[float]:
|
66 |
+
"""
|
67 |
+
Get embeddings for the input text from OpenAI API.
|
68 |
+
|
69 |
+
Args:
|
70 |
+
input_data (str): The input text to get embeddings for.
|
71 |
+
model (str): The model to use for generating embeddings.
|
72 |
+
|
73 |
+
Returns:
|
74 |
+
List[float]: The embeddings generated by the API.
|
75 |
+
"""
|
76 |
+
loaded_config_data = load_and_log_configs()
|
77 |
+
api_key = loaded_config_data['api_keys']['openai']
|
78 |
+
|
79 |
+
if not api_key:
|
80 |
+
logging.error("OpenAI: API key not found or is empty")
|
81 |
+
raise ValueError("OpenAI: API Key Not Provided/Found in Config file or is empty")
|
82 |
+
|
83 |
+
logging.debug(f"OpenAI: Using API Key: {api_key[:5]}...{api_key[-5:]}")
|
84 |
+
logging.debug(f"OpenAI: Raw input data (first 500 chars): {str(input_data)[:500]}...")
|
85 |
+
logging.debug(f"OpenAI: Using model: {model}")
|
86 |
+
|
87 |
+
headers = {
|
88 |
+
'Authorization': f'Bearer {api_key}',
|
89 |
+
'Content-Type': 'application/json'
|
90 |
+
}
|
91 |
+
|
92 |
+
request_data = {
|
93 |
+
"input": input_data,
|
94 |
+
"model": model,
|
95 |
+
}
|
96 |
+
|
97 |
+
try:
|
98 |
+
logging.debug("OpenAI: Posting request to embeddings API")
|
99 |
+
response = requests.post('https://api.openai.com/v1/embeddings', headers=headers, json=request_data)
|
100 |
+
logging.debug(f"Full API response data: {response}")
|
101 |
+
if response.status_code == 200:
|
102 |
+
response_data = response.json()
|
103 |
+
if 'data' in response_data and len(response_data['data']) > 0:
|
104 |
+
embedding = response_data['data'][0]['embedding']
|
105 |
+
logging.debug("OpenAI: Embeddings retrieved successfully")
|
106 |
+
return embedding
|
107 |
+
else:
|
108 |
+
logging.warning("OpenAI: Embedding data not found in the response")
|
109 |
+
raise ValueError("OpenAI: Embedding data not available in the response")
|
110 |
+
else:
|
111 |
+
logging.error(f"OpenAI: Embeddings request failed with status code {response.status_code}")
|
112 |
+
logging.error(f"OpenAI: Error response: {response.text}")
|
113 |
+
raise ValueError(f"OpenAI: Failed to retrieve embeddings. Status code: {response.status_code}")
|
114 |
+
except requests.RequestException as e:
|
115 |
+
logging.error(f"OpenAI: Error making API request: {str(e)}", exc_info=True)
|
116 |
+
raise ValueError(f"OpenAI: Error making API request: {str(e)}")
|
117 |
+
except Exception as e:
|
118 |
+
logging.error(f"OpenAI: Unexpected error: {str(e)}", exc_info=True)
|
119 |
+
raise ValueError(f"OpenAI: Unexpected error occurred: {str(e)}")
|
120 |
+
|
121 |
+
|
122 |
+
def chat_with_openai(api_key, input_data, custom_prompt_arg, temp=None, system_message=None):
|
123 |
+
loaded_config_data = load_and_log_configs()
|
124 |
+
openai_api_key = api_key
|
125 |
+
try:
|
126 |
+
# API key validation
|
127 |
+
if not openai_api_key:
|
128 |
+
logging.info("OpenAI: API key not provided as parameter")
|
129 |
+
logging.info("OpenAI: Attempting to use API key from config file")
|
130 |
+
openai_api_key = loaded_config_data['api_keys']['openai']
|
131 |
+
|
132 |
+
if not openai_api_key:
|
133 |
+
logging.error("OpenAI: API key not found or is empty")
|
134 |
+
return "OpenAI: API Key Not Provided/Found in Config file or is empty"
|
135 |
+
|
136 |
+
logging.debug(f"OpenAI: Using API Key: {openai_api_key[:5]}...{openai_api_key[-5:]}")
|
137 |
+
|
138 |
+
# Input data handling
|
139 |
+
logging.debug(f"OpenAI: Raw input data type: {type(input_data)}")
|
140 |
+
logging.debug(f"OpenAI: Raw input data (first 500 chars): {str(input_data)[:500]}...")
|
141 |
+
|
142 |
+
if isinstance(input_data, str):
|
143 |
+
if input_data.strip().startswith('{'):
|
144 |
+
# It's likely a JSON string
|
145 |
+
logging.debug("OpenAI: Parsing provided JSON string data for summarization")
|
146 |
+
try:
|
147 |
+
data = json.loads(input_data)
|
148 |
+
except json.JSONDecodeError as e:
|
149 |
+
logging.error(f"OpenAI: Error parsing JSON string: {str(e)}")
|
150 |
+
return f"OpenAI: Error parsing JSON input: {str(e)}"
|
151 |
+
elif os.path.isfile(input_data):
|
152 |
+
logging.debug("OpenAI: Loading JSON data from file for summarization")
|
153 |
+
with open(input_data, 'r') as file:
|
154 |
+
data = json.load(file)
|
155 |
+
else:
|
156 |
+
logging.debug("OpenAI: Using provided string data for summarization")
|
157 |
+
data = input_data
|
158 |
+
else:
|
159 |
+
data = input_data
|
160 |
+
|
161 |
+
logging.debug(f"OpenAI: Processed data type: {type(data)}")
|
162 |
+
logging.debug(f"OpenAI: Processed data (first 500 chars): {str(data)[:500]}...")
|
163 |
+
|
164 |
+
# Text extraction
|
165 |
+
if isinstance(data, dict):
|
166 |
+
if 'summary' in data:
|
167 |
+
logging.debug("OpenAI: Summary already exists in the loaded data")
|
168 |
+
return data['summary']
|
169 |
+
elif 'segments' in data:
|
170 |
+
text = extract_text_from_segments(data['segments'])
|
171 |
+
else:
|
172 |
+
text = json.dumps(data) # Convert dict to string if no specific format
|
173 |
+
elif isinstance(data, list):
|
174 |
+
text = extract_text_from_segments(data)
|
175 |
+
elif isinstance(data, str):
|
176 |
+
text = data
|
177 |
+
else:
|
178 |
+
raise ValueError(f"OpenAI: Invalid input data format: {type(data)}")
|
179 |
+
|
180 |
+
logging.debug(f"OpenAI: Extracted text (first 500 chars): {text[:500]}...")
|
181 |
+
logging.debug(f"OpenAI: Custom prompt: {custom_prompt_arg}")
|
182 |
+
|
183 |
+
openai_model = loaded_config_data['models']['openai'] or "gpt-4o"
|
184 |
+
logging.debug(f"OpenAI: Using model: {openai_model}")
|
185 |
+
|
186 |
+
headers = {
|
187 |
+
'Authorization': f'Bearer {openai_api_key}',
|
188 |
+
'Content-Type': 'application/json'
|
189 |
+
}
|
190 |
+
|
191 |
+
logging.debug(
|
192 |
+
f"OpenAI API Key: {openai_api_key[:5]}...{openai_api_key[-5:] if openai_api_key else None}")
|
193 |
+
logging.debug("openai: Preparing data + prompt for submittal")
|
194 |
+
openai_prompt = f"{text} \n\n\n\n{custom_prompt_arg}"
|
195 |
+
if temp is None:
|
196 |
+
temp = 0.7
|
197 |
+
if system_message is None:
|
198 |
+
system_message = "You are a helpful AI assistant who does whatever the user requests."
|
199 |
+
temp = float(temp)
|
200 |
+
data = {
|
201 |
+
"model": openai_model,
|
202 |
+
"messages": [
|
203 |
+
{"role": "system", "content": system_message},
|
204 |
+
{"role": "user", "content": openai_prompt}
|
205 |
+
],
|
206 |
+
"max_tokens": 4096,
|
207 |
+
"temperature": temp
|
208 |
+
}
|
209 |
+
|
210 |
+
logging.debug("OpenAI: Posting request")
|
211 |
+
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
|
212 |
+
logging.debug(f"Full API response data: {response}")
|
213 |
+
if response.status_code == 200:
|
214 |
+
response_data = response.json()
|
215 |
+
logging.debug(response_data)
|
216 |
+
if 'choices' in response_data and len(response_data['choices']) > 0:
|
217 |
+
chat_response = response_data['choices'][0]['message']['content'].strip()
|
218 |
+
logging.debug("openai: Chat Sent successfully")
|
219 |
+
logging.debug(f"openai: Chat response: {chat_response}")
|
220 |
+
return chat_response
|
221 |
+
else:
|
222 |
+
logging.warning("openai: Chat response not found in the response data")
|
223 |
+
return "openai: Chat not available"
|
224 |
+
else:
|
225 |
+
logging.error(f"OpenAI: Chat request failed with status code {response.status_code}")
|
226 |
+
logging.error(f"OpenAI: Error response: {response.text}")
|
227 |
+
return f"OpenAI: Failed to process chat response. Status code: {response.status_code}"
|
228 |
+
except json.JSONDecodeError as e:
|
229 |
+
logging.error(f"OpenAI: Error decoding JSON: {str(e)}", exc_info=True)
|
230 |
+
return f"OpenAI: Error decoding JSON input: {str(e)}"
|
231 |
+
except requests.RequestException as e:
|
232 |
+
logging.error(f"OpenAI: Error making API request: {str(e)}", exc_info=True)
|
233 |
+
return f"OpenAI: Error making API request: {str(e)}"
|
234 |
+
except Exception as e:
|
235 |
+
logging.error(f"OpenAI: Unexpected error: {str(e)}", exc_info=True)
|
236 |
+
return f"OpenAI: Unexpected error occurred: {str(e)}"
|
237 |
+
|
238 |
+
|
239 |
+
def chat_with_anthropic(api_key, input_data, model, custom_prompt_arg, max_retries=3, retry_delay=5, system_prompt=None):
|
240 |
+
try:
|
241 |
+
loaded_config_data = load_and_log_configs()
|
242 |
+
global anthropic_api_key
|
243 |
+
anthropic_api_key = api_key
|
244 |
+
# API key validation
|
245 |
+
if not api_key:
|
246 |
+
logging.info("Anthropic: API key not provided as parameter")
|
247 |
+
logging.info("Anthropic: Attempting to use API key from config file")
|
248 |
+
anthropic_api_key = loaded_config_data['api_keys']['anthropic']
|
249 |
+
|
250 |
+
if not api_key or api_key.strip() == "":
|
251 |
+
logging.error("Anthropic: API key not found or is empty")
|
252 |
+
return "Anthropic: API Key Not Provided/Found in Config file or is empty"
|
253 |
+
|
254 |
+
logging.debug(f"Anthropic: Using API Key: {api_key[:5]}...{api_key[-5:]}")
|
255 |
+
|
256 |
+
if system_prompt is not None:
|
257 |
+
logging.debug("Anthropic: Using provided system prompt")
|
258 |
+
pass
|
259 |
+
else:
|
260 |
+
system_prompt = "You are a helpful assistant"
|
261 |
+
|
262 |
+
logging.debug(f"AnthropicAI: Loaded data: {input_data}")
|
263 |
+
logging.debug(f"AnthropicAI: Type of data: {type(input_data)}")
|
264 |
+
|
265 |
+
anthropic_model = loaded_config_data['models']['anthropic']
|
266 |
+
|
267 |
+
headers = {
|
268 |
+
'x-api-key': anthropic_api_key,
|
269 |
+
'anthropic-version': '2023-06-01',
|
270 |
+
'Content-Type': 'application/json'
|
271 |
+
}
|
272 |
+
|
273 |
+
anthropic_user_prompt = custom_prompt_arg
|
274 |
+
logging.debug(f"Anthropic: User Prompt is {anthropic_user_prompt}")
|
275 |
+
user_message = {
|
276 |
+
"role": "user",
|
277 |
+
"content": f"{input_data} \n\n\n\n{anthropic_user_prompt}"
|
278 |
+
}
|
279 |
+
|
280 |
+
data = {
|
281 |
+
"model": model,
|
282 |
+
"max_tokens": 4096, # max _possible_ tokens to return
|
283 |
+
"messages": [user_message],
|
284 |
+
"stop_sequences": ["\n\nHuman:"],
|
285 |
+
"temperature": 0.1,
|
286 |
+
"top_k": 0,
|
287 |
+
"top_p": 1.0,
|
288 |
+
"metadata": {
|
289 |
+
"user_id": "example_user_id",
|
290 |
+
},
|
291 |
+
"stream": False,
|
292 |
+
"system": f"{system_prompt}"
|
293 |
+
}
|
294 |
+
|
295 |
+
for attempt in range(max_retries):
|
296 |
+
try:
|
297 |
+
logging.debug("anthropic: Posting request to API")
|
298 |
+
response = requests.post('https://api.anthropic.com/v1/messages', headers=headers, json=data)
|
299 |
+
logging.debug(f"Full API response data: {response}")
|
300 |
+
# Check if the status code indicates success
|
301 |
+
if response.status_code == 200:
|
302 |
+
logging.debug("anthropic: Post submittal successful")
|
303 |
+
response_data = response.json()
|
304 |
+
try:
|
305 |
+
chat_response = response_data['content'][0]['text'].strip()
|
306 |
+
logging.debug("anthropic: Chat request successful")
|
307 |
+
print("Chat request processed successfully.")
|
308 |
+
return chat_response
|
309 |
+
except (IndexError, KeyError) as e:
|
310 |
+
logging.debug("anthropic: Unexpected data in response")
|
311 |
+
print("Unexpected response format from Anthropic API:", response.text)
|
312 |
+
return None
|
313 |
+
elif response.status_code == 500: # Handle internal server error specifically
|
314 |
+
logging.debug("anthropic: Internal server error")
|
315 |
+
print("Internal server error from API. Retrying may be necessary.")
|
316 |
+
time.sleep(retry_delay)
|
317 |
+
else:
|
318 |
+
logging.debug(
|
319 |
+
f"anthropic: Failed to process chat request, status code {response.status_code}: {response.text}")
|
320 |
+
print(f"Failed to process chat request, status code {response.status_code}: {response.text}")
|
321 |
+
return None
|
322 |
+
|
323 |
+
except RequestException as e:
|
324 |
+
logging.error(f"anthropic: Network error during attempt {attempt + 1}/{max_retries}: {str(e)}")
|
325 |
+
if attempt < max_retries - 1:
|
326 |
+
time.sleep(retry_delay)
|
327 |
+
else:
|
328 |
+
return f"anthropic: Network error: {str(e)}"
|
329 |
+
except Exception as e:
|
330 |
+
logging.error(f"anthropic: Error in processing: {str(e)}")
|
331 |
+
return f"anthropic: Error occurred while processing summary with Anthropic: {str(e)}"
|
332 |
+
|
333 |
+
|
334 |
+
# Summarize with Cohere
|
335 |
+
def chat_with_cohere(api_key, input_data, model, custom_prompt_arg, system_prompt=None):
|
336 |
+
loaded_config_data = load_and_log_configs()
|
337 |
+
if api_key is not None:
|
338 |
+
logging.debug(f"Cohere Chat: API Key from parameter: {api_key[:3]}...{api_key[-3:]}")
|
339 |
+
logging.debug(f"Cohere Chat: Cohere API Key from config: {loaded_config_data['api_keys']['cohere']}")
|
340 |
+
try:
|
341 |
+
# API key validation
|
342 |
+
if api_key is None:
|
343 |
+
logging.info("Cohere Chat: API key not provided as parameter")
|
344 |
+
logging.info("Cohere Chat: Attempting to use API key from config file")
|
345 |
+
cohere_api_key = loaded_config_data.get('api_keys', {}).get('cohere')
|
346 |
+
if not cohere_api_key:
|
347 |
+
logging.error("Cohere Chat: API key not found or is empty")
|
348 |
+
return "Cohere Chat: API Key Not Provided/Found in Config file or is empty"
|
349 |
+
|
350 |
+
logging.debug(f"Cohere Chat: Using API Key: {cohere_api_key[:3]}...{cohere_api_key[-3:]}")
|
351 |
+
|
352 |
+
logging.debug(f"Cohere Chat: Loaded data: {input_data}")
|
353 |
+
logging.debug(f"Cohere Chat: Type of data: {type(input_data)}")
|
354 |
+
|
355 |
+
# Ensure model is set
|
356 |
+
if not model:
|
357 |
+
model = loaded_config_data['models']['cohere']
|
358 |
+
logging.debug(f"Cohere Chat: Using model: {model}")
|
359 |
+
|
360 |
+
headers = {
|
361 |
+
'accept': 'application/json',
|
362 |
+
'content-type': 'application/json',
|
363 |
+
'Authorization': f'Bearer {cohere_api_key}'
|
364 |
+
}
|
365 |
+
|
366 |
+
# Ensure system_prompt is set
|
367 |
+
if not system_prompt:
|
368 |
+
system_prompt = "You are a helpful assistant"
|
369 |
+
logging.debug(f"Cohere Chat: System Prompt being sent is: '{system_prompt}'")
|
370 |
+
|
371 |
+
cohere_prompt = input_data
|
372 |
+
if custom_prompt_arg:
|
373 |
+
cohere_prompt += f"\n\n{custom_prompt_arg}"
|
374 |
+
logging.debug(f"Cohere Chat: User Prompt being sent is: '{cohere_prompt}'")
|
375 |
+
|
376 |
+
data = {
|
377 |
+
"chat_history": [
|
378 |
+
{"role": "SYSTEM", "message": system_prompt},
|
379 |
+
],
|
380 |
+
"message": cohere_prompt,
|
381 |
+
"model": model,
|
382 |
+
"connectors": [{"id": "web-search"}]
|
383 |
+
}
|
384 |
+
logging.debug(f"Cohere Chat: Request data: {json.dumps(data, indent=2)}")
|
385 |
+
|
386 |
+
logging.debug("cohere chat: Submitting request to API endpoint")
|
387 |
+
print("cohere chat: Submitting request to API endpoint")
|
388 |
+
|
389 |
+
try:
|
390 |
+
response = requests.post('https://api.cohere.ai/v1/chat', headers=headers, json=data)
|
391 |
+
logging.debug(f"Cohere Chat: Raw API response: {response.text}")
|
392 |
+
except requests.RequestException as e:
|
393 |
+
logging.error(f"Cohere Chat: Error making API request: {str(e)}")
|
394 |
+
return f"Cohere Chat: Error making API request: {str(e)}"
|
395 |
+
|
396 |
+
if response.status_code == 200:
|
397 |
+
try:
|
398 |
+
response_data = response.json()
|
399 |
+
except json.JSONDecodeError:
|
400 |
+
logging.error("Cohere Chat: Failed to decode JSON response")
|
401 |
+
return "Cohere Chat: Failed to decode JSON response"
|
402 |
+
|
403 |
+
if response_data is None:
|
404 |
+
logging.error("Cohere Chat: No response data received.")
|
405 |
+
return "Cohere Chat: No response data received."
|
406 |
+
|
407 |
+
logging.debug(f"cohere chat: Full API response data: {json.dumps(response_data, indent=2)}")
|
408 |
+
|
409 |
+
if 'text' in response_data:
|
410 |
+
chat_response = response_data['text'].strip()
|
411 |
+
logging.debug("Cohere Chat: Chat request successful")
|
412 |
+
print("Cohere Chat request processed successfully.")
|
413 |
+
return chat_response
|
414 |
+
else:
|
415 |
+
logging.error("Cohere Chat: Expected 'text' key not found in API response.")
|
416 |
+
return "Cohere Chat: Expected data not found in API response."
|
417 |
+
else:
|
418 |
+
logging.error(f"Cohere Chat: API request failed with status code {response.status_code}: {response.text}")
|
419 |
+
print(f"Cohere Chat: Failed to process chat response, status code {response.status_code}: {response.text}")
|
420 |
+
return f"Cohere Chat: API request failed: {response.text}"
|
421 |
+
|
422 |
+
except Exception as e:
|
423 |
+
logging.error(f"Cohere Chat: Error in processing: {str(e)}", exc_info=True)
|
424 |
+
return f"Cohere Chat: Error occurred while processing chat request with Cohere: {str(e)}"
|
425 |
+
|
426 |
+
|
427 |
+
# https://console.groq.com/docs/quickstart
|
428 |
+
def chat_with_groq(api_key, input_data, custom_prompt_arg, temp=None, system_message=None):
|
429 |
+
logging.debug("Groq: Summarization process starting...")
|
430 |
+
try:
|
431 |
+
logging.debug("Groq: Loading and validating configurations")
|
432 |
+
loaded_config_data = load_and_log_configs()
|
433 |
+
if loaded_config_data is None:
|
434 |
+
logging.error("Failed to load configuration data")
|
435 |
+
groq_api_key = None
|
436 |
+
else:
|
437 |
+
# Prioritize the API key passed as a parameter
|
438 |
+
if api_key and api_key.strip():
|
439 |
+
groq_api_key = api_key
|
440 |
+
logging.info("Groq: Using API key provided as parameter")
|
441 |
+
else:
|
442 |
+
# If no parameter is provided, use the key from the config
|
443 |
+
groq_api_key = loaded_config_data['api_keys'].get('groq')
|
444 |
+
if groq_api_key:
|
445 |
+
logging.info("Groq: Using API key from config file")
|
446 |
+
else:
|
447 |
+
logging.warning("Groq: No API key found in config file")
|
448 |
+
|
449 |
+
# Final check to ensure we have a valid API key
|
450 |
+
if not groq_api_key or not groq_api_key.strip():
|
451 |
+
logging.error("Anthropic: No valid API key available")
|
452 |
+
# You might want to raise an exception here or handle this case as appropriate for your application
|
453 |
+
# For example: raise ValueError("No valid Anthropic API key available")
|
454 |
+
|
455 |
+
logging.debug(f"Groq: Using API Key: {groq_api_key[:5]}...{groq_api_key[-5:]}")
|
456 |
+
|
457 |
+
# Transcript data handling & Validation
|
458 |
+
if isinstance(input_data, str) and os.path.isfile(input_data):
|
459 |
+
logging.debug("Groq: Loading json data for summarization")
|
460 |
+
with open(input_data, 'r') as file:
|
461 |
+
data = json.load(file)
|
462 |
+
else:
|
463 |
+
logging.debug("Groq: Using provided string data for summarization")
|
464 |
+
data = input_data
|
465 |
+
|
466 |
+
# DEBUG - Debug logging to identify sent data
|
467 |
+
logging.debug(f"Groq: Loaded data: {data[:500]}...(snipped to first 500 chars)")
|
468 |
+
logging.debug(f"Groq: Type of data: {type(data)}")
|
469 |
+
|
470 |
+
if isinstance(data, dict) and 'summary' in data:
|
471 |
+
# If the loaded data is a dictionary and already contains a summary, return it
|
472 |
+
logging.debug("Groq: Summary already exists in the loaded data")
|
473 |
+
return data['summary']
|
474 |
+
|
475 |
+
# If the loaded data is a list of segment dictionaries or a string, proceed with summarization
|
476 |
+
if isinstance(data, list):
|
477 |
+
segments = data
|
478 |
+
text = extract_text_from_segments(segments)
|
479 |
+
elif isinstance(data, str):
|
480 |
+
text = data
|
481 |
+
else:
|
482 |
+
raise ValueError("Groq: Invalid input data format")
|
483 |
+
|
484 |
+
# Set the model to be used
|
485 |
+
groq_model = loaded_config_data['models']['groq']
|
486 |
+
|
487 |
+
if temp is None:
|
488 |
+
temp = 0.2
|
489 |
+
temp = float(temp)
|
490 |
+
if system_message is None:
|
491 |
+
system_message = "You are a helpful AI assistant who does whatever the user requests."
|
492 |
+
|
493 |
+
headers = {
|
494 |
+
'Authorization': f'Bearer {groq_api_key}',
|
495 |
+
'Content-Type': 'application/json'
|
496 |
+
}
|
497 |
+
|
498 |
+
groq_prompt = f"{text} \n\n\n\n{custom_prompt_arg}"
|
499 |
+
logging.debug("groq: Prompt being sent is {groq_prompt}")
|
500 |
+
|
501 |
+
data = {
|
502 |
+
"messages": [
|
503 |
+
{
|
504 |
+
"role": "system",
|
505 |
+
"content": system_message,
|
506 |
+
},
|
507 |
+
{
|
508 |
+
"role": "user",
|
509 |
+
"content": groq_prompt,
|
510 |
+
}
|
511 |
+
],
|
512 |
+
"model": groq_model,
|
513 |
+
"temperature": temp
|
514 |
+
}
|
515 |
+
|
516 |
+
logging.debug("groq: Submitting request to API endpoint")
|
517 |
+
print("groq: Submitting request to API endpoint")
|
518 |
+
response = requests.post('https://api.groq.com/openai/v1/chat/completions', headers=headers, json=data)
|
519 |
+
|
520 |
+
response_data = response.json()
|
521 |
+
logging.debug(f"Full API response data: {response_data}")
|
522 |
+
|
523 |
+
if response.status_code == 200:
|
524 |
+
logging.debug(response_data)
|
525 |
+
if 'choices' in response_data and len(response_data['choices']) > 0:
|
526 |
+
summary = response_data['choices'][0]['message']['content'].strip()
|
527 |
+
logging.debug("groq: Chat request successful")
|
528 |
+
print("Groq: Chat request successful.")
|
529 |
+
return summary
|
530 |
+
else:
|
531 |
+
logging.error("Groq(chat): Expected data not found in API response.")
|
532 |
+
return "Groq(chat): Expected data not found in API response."
|
533 |
+
else:
|
534 |
+
logging.error(f"groq: API request failed with status code {response.status_code}: {response.text}")
|
535 |
+
return f"groq: API request failed: {response.text}"
|
536 |
+
|
537 |
+
except Exception as e:
|
538 |
+
logging.error("groq: Error in processing: %s", str(e))
|
539 |
+
return f"groq: Error occurred while processing summary with groq: {str(e)}"
|
540 |
+
|
541 |
+
|
542 |
+
def chat_with_openrouter(api_key, input_data, custom_prompt_arg, temp=None, system_message=None):
|
543 |
+
import requests
|
544 |
+
import json
|
545 |
+
global openrouter_model, openrouter_api_key
|
546 |
+
try:
|
547 |
+
logging.debug("OpenRouter: Loading and validating configurations")
|
548 |
+
loaded_config_data = load_and_log_configs()
|
549 |
+
if loaded_config_data is None:
|
550 |
+
logging.error("Failed to load configuration data")
|
551 |
+
openrouter_api_key = None
|
552 |
+
else:
|
553 |
+
# Prioritize the API key passed as a parameter
|
554 |
+
if api_key and api_key.strip():
|
555 |
+
openrouter_api_key = api_key
|
556 |
+
logging.info("OpenRouter: Using API key provided as parameter")
|
557 |
+
else:
|
558 |
+
# If no parameter is provided, use the key from the config
|
559 |
+
openrouter_api_key = loaded_config_data['api_keys'].get('openrouter')
|
560 |
+
if openrouter_api_key:
|
561 |
+
logging.info("OpenRouter: Using API key from config file")
|
562 |
+
else:
|
563 |
+
logging.warning("OpenRouter: No API key found in config file")
|
564 |
+
|
565 |
+
# Model Selection validation
|
566 |
+
logging.debug("OpenRouter: Validating model selection")
|
567 |
+
loaded_config_data = load_and_log_configs()
|
568 |
+
openrouter_model = loaded_config_data['models']['openrouter']
|
569 |
+
logging.debug(f"OpenRouter: Using model from config file: {openrouter_model}")
|
570 |
+
|
571 |
+
# Final check to ensure we have a valid API key
|
572 |
+
if not openrouter_api_key or not openrouter_api_key.strip():
|
573 |
+
logging.error("OpenRouter: No valid API key available")
|
574 |
+
raise ValueError("No valid Anthropic API key available")
|
575 |
+
except Exception as e:
|
576 |
+
logging.error("OpenRouter: Error in processing: %s", str(e))
|
577 |
+
return f"OpenRouter: Error occurred while processing config file with OpenRouter: {str(e)}"
|
578 |
+
|
579 |
+
logging.debug(f"OpenRouter: Using API Key: {openrouter_api_key[:5]}...{openrouter_api_key[-5:]}")
|
580 |
+
|
581 |
+
logging.debug(f"OpenRouter: Using Model: {openrouter_model}")
|
582 |
+
|
583 |
+
if isinstance(input_data, str) and os.path.isfile(input_data):
|
584 |
+
logging.debug("OpenRouter: Loading json data for summarization")
|
585 |
+
with open(input_data, 'r') as file:
|
586 |
+
data = json.load(file)
|
587 |
+
else:
|
588 |
+
logging.debug("OpenRouter: Using provided string data for summarization")
|
589 |
+
data = input_data
|
590 |
+
|
591 |
+
# DEBUG - Debug logging to identify sent data
|
592 |
+
logging.debug(f"OpenRouter: Loaded data: {data[:500]}...(snipped to first 500 chars)")
|
593 |
+
logging.debug(f"OpenRouter: Type of data: {type(data)}")
|
594 |
+
|
595 |
+
if isinstance(data, dict) and 'summary' in data:
|
596 |
+
# If the loaded data is a dictionary and already contains a summary, return it
|
597 |
+
logging.debug("OpenRouter: Summary already exists in the loaded data")
|
598 |
+
return data['summary']
|
599 |
+
|
600 |
+
# If the loaded data is a list of segment dictionaries or a string, proceed with summarization
|
601 |
+
if isinstance(data, list):
|
602 |
+
segments = data
|
603 |
+
text = extract_text_from_segments(segments)
|
604 |
+
elif isinstance(data, str):
|
605 |
+
text = data
|
606 |
+
else:
|
607 |
+
raise ValueError("OpenRouter: Invalid input data format")
|
608 |
+
|
609 |
+
openrouter_prompt = f"{input_data} \n\n\n\n{custom_prompt_arg}"
|
610 |
+
logging.debug(f"openrouter: User Prompt being sent is {openrouter_prompt}")
|
611 |
+
|
612 |
+
if temp is None:
|
613 |
+
temp = 0.1
|
614 |
+
temp = float(temp)
|
615 |
+
if system_message is None:
|
616 |
+
system_message = "You are a helpful AI assistant who does whatever the user requests."
|
617 |
+
|
618 |
+
try:
|
619 |
+
logging.debug("OpenRouter: Submitting request to API endpoint")
|
620 |
+
print("OpenRouter: Submitting request to API endpoint")
|
621 |
+
response = requests.post(
|
622 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
623 |
+
headers={
|
624 |
+
"Authorization": f"Bearer {openrouter_api_key}",
|
625 |
+
},
|
626 |
+
data=json.dumps({
|
627 |
+
"model": openrouter_model,
|
628 |
+
"messages": [
|
629 |
+
{"role": "system", "content": system_message},
|
630 |
+
{"role": "user", "content": openrouter_prompt}
|
631 |
+
],
|
632 |
+
"temperature": temp
|
633 |
+
})
|
634 |
+
)
|
635 |
+
|
636 |
+
response_data = response.json()
|
637 |
+
logging.debug("Full API Response Data: %s", response_data)
|
638 |
+
|
639 |
+
if response.status_code == 200:
|
640 |
+
if 'choices' in response_data and len(response_data['choices']) > 0:
|
641 |
+
summary = response_data['choices'][0]['message']['content'].strip()
|
642 |
+
logging.debug("openrouter: Chat request successful")
|
643 |
+
print("openrouter: Chat request successful.")
|
644 |
+
return summary
|
645 |
+
else:
|
646 |
+
logging.error("openrouter: Expected data not found in API response.")
|
647 |
+
return "openrouter: Expected data not found in API response."
|
648 |
+
else:
|
649 |
+
logging.error(f"openrouter: API request failed with status code {response.status_code}: {response.text}")
|
650 |
+
return f"openrouter: API request failed: {response.text}"
|
651 |
+
except Exception as e:
|
652 |
+
logging.error("openrouter: Error in processing: %s", str(e))
|
653 |
+
return f"openrouter: Error occurred while processing chat request with openrouter: {str(e)}"
|
654 |
+
|
655 |
+
|
656 |
+
# FIXME: This function is not yet implemented properly
|
657 |
+
def chat_with_huggingface(api_key, input_data, custom_prompt_arg, system_prompt=None, temp=None):
|
658 |
+
loaded_config_data = load_and_log_configs()
|
659 |
+
logging.debug(f"huggingface Chat: Chat request process starting...")
|
660 |
+
try:
|
661 |
+
huggingface_api_key = global_huggingface_api_key
|
662 |
+
|
663 |
+
headers = {
|
664 |
+
"Authorization": f"Bearer {huggingface_api_key}"
|
665 |
+
}
|
666 |
+
|
667 |
+
# Setup model
|
668 |
+
huggingface_model = loaded_config_data['models']['huggingface']
|
669 |
+
|
670 |
+
API_URL = f"https://api-inference.huggingface.co/models/{huggingface_model}/v1/chat/completions"
|
671 |
+
if temp is None:
|
672 |
+
temp = 1.0
|
673 |
+
temp = float(temp)
|
674 |
+
huggingface_prompt = f"{custom_prompt_arg}\n\n\n{input_data}"
|
675 |
+
logging.debug(f"HuggingFace chat: Prompt being sent is {huggingface_prompt}")
|
676 |
+
data = {
|
677 |
+
"model": f"{huggingface_model}",
|
678 |
+
"messages": [{"role": "user", "content": f"{huggingface_prompt}"}],
|
679 |
+
"max_tokens": 4096,
|
680 |
+
"stream": False,
|
681 |
+
"temperature": temp
|
682 |
+
}
|
683 |
+
|
684 |
+
logging.debug("HuggingFace Chat: Submitting request...")
|
685 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
686 |
+
logging.debug(f"Full API response data: {response.text}")
|
687 |
+
|
688 |
+
if response.status_code == 200:
|
689 |
+
response_json = response.json()
|
690 |
+
if "choices" in response_json and len(response_json["choices"]) > 0:
|
691 |
+
generated_text = response_json["choices"][0]["message"]["content"]
|
692 |
+
logging.debug("HuggingFace Chat: Chat request successful")
|
693 |
+
print("HuggingFace Chat: Chat request successful.")
|
694 |
+
return generated_text.strip()
|
695 |
+
else:
|
696 |
+
logging.error("HuggingFace Chat: No generated text in the response")
|
697 |
+
return "HuggingFace Chat: No generated text in the response"
|
698 |
+
else:
|
699 |
+
logging.error(
|
700 |
+
f"HuggingFace Chat: Chat request failed with status code {response.status_code}: {response.text}")
|
701 |
+
return f"HuggingFace Chat: Failed to process chat request, status code {response.status_code}: {response.text}"
|
702 |
+
except Exception as e:
|
703 |
+
logging.error(f"HuggingFace Chat: Error in processing: {str(e)}")
|
704 |
+
print(f"HuggingFace Chat: Error occurred while processing chat request with huggingface: {str(e)}")
|
705 |
+
return None
|
706 |
+
|
707 |
+
|
708 |
+
def chat_with_deepseek(api_key, input_data, custom_prompt_arg, temp=None, system_message=None):
|
709 |
+
logging.debug("DeepSeek: Summarization process starting...")
|
710 |
+
try:
|
711 |
+
logging.debug("DeepSeek: Loading and validating configurations")
|
712 |
+
loaded_config_data = load_and_log_configs()
|
713 |
+
if loaded_config_data is None:
|
714 |
+
logging.error("Failed to load configuration data")
|
715 |
+
deepseek_api_key = None
|
716 |
+
else:
|
717 |
+
# Prioritize the API key passed as a parameter
|
718 |
+
if api_key and api_key.strip():
|
719 |
+
deepseek_api_key = api_key
|
720 |
+
logging.info("DeepSeek: Using API key provided as parameter")
|
721 |
+
else:
|
722 |
+
# If no parameter is provided, use the key from the config
|
723 |
+
deepseek_api_key = loaded_config_data['api_keys'].get('deepseek')
|
724 |
+
if deepseek_api_key:
|
725 |
+
logging.info("DeepSeek: Using API key from config file")
|
726 |
+
else:
|
727 |
+
logging.warning("DeepSeek: No API key found in config file")
|
728 |
+
|
729 |
+
# Final check to ensure we have a valid API key
|
730 |
+
if not deepseek_api_key or not deepseek_api_key.strip():
|
731 |
+
logging.error("DeepSeek: No valid API key available")
|
732 |
+
# You might want to raise an exception here or handle this case as appropriate for your application
|
733 |
+
# For example: raise ValueError("No valid deepseek API key available")
|
734 |
+
|
735 |
+
|
736 |
+
logging.debug(f"DeepSeek: Using API Key: {deepseek_api_key[:5]}...{deepseek_api_key[-5:]}")
|
737 |
+
|
738 |
+
# Input data handling
|
739 |
+
if isinstance(input_data, str) and os.path.isfile(input_data):
|
740 |
+
logging.debug("DeepSeek: Loading json data for summarization")
|
741 |
+
with open(input_data, 'r') as file:
|
742 |
+
data = json.load(file)
|
743 |
+
else:
|
744 |
+
logging.debug("DeepSeek: Using provided string data for summarization")
|
745 |
+
data = input_data
|
746 |
+
|
747 |
+
# DEBUG - Debug logging to identify sent data
|
748 |
+
logging.debug(f"DeepSeek: Loaded data: {data[:500]}...(snipped to first 500 chars)")
|
749 |
+
logging.debug(f"DeepSeek: Type of data: {type(data)}")
|
750 |
+
|
751 |
+
if isinstance(data, dict) and 'summary' in data:
|
752 |
+
# If the loaded data is a dictionary and already contains a summary, return it
|
753 |
+
logging.debug("DeepSeek: Summary already exists in the loaded data")
|
754 |
+
return data['summary']
|
755 |
+
|
756 |
+
# Text extraction
|
757 |
+
if isinstance(data, list):
|
758 |
+
segments = data
|
759 |
+
text = extract_text_from_segments(segments)
|
760 |
+
elif isinstance(data, str):
|
761 |
+
text = data
|
762 |
+
else:
|
763 |
+
raise ValueError("DeepSeek: Invalid input data format")
|
764 |
+
|
765 |
+
deepseek_model = loaded_config_data['models']['deepseek'] or "deepseek-chat"
|
766 |
+
|
767 |
+
if temp is None:
|
768 |
+
temp = 0.1
|
769 |
+
temp = float(temp)
|
770 |
+
if system_message is None:
|
771 |
+
system_message = "You are a helpful AI assistant who does whatever the user requests."
|
772 |
+
|
773 |
+
headers = {
|
774 |
+
'Authorization': f'Bearer {api_key}',
|
775 |
+
'Content-Type': 'application/json'
|
776 |
+
}
|
777 |
+
|
778 |
+
logging.debug(
|
779 |
+
f"Deepseek API Key: {api_key[:5]}...{api_key[-5:] if api_key else None}")
|
780 |
+
logging.debug("DeepSeek: Preparing data + prompt for submittal")
|
781 |
+
deepseek_prompt = f"{text} \n\n\n\n{custom_prompt_arg}"
|
782 |
+
data = {
|
783 |
+
"model": deepseek_model,
|
784 |
+
"messages": [
|
785 |
+
{"role": "system", "content": system_message},
|
786 |
+
{"role": "user", "content": deepseek_prompt}
|
787 |
+
],
|
788 |
+
"stream": False,
|
789 |
+
"temperature": temp
|
790 |
+
}
|
791 |
+
|
792 |
+
logging.debug("DeepSeek: Posting request")
|
793 |
+
response = requests.post('https://api.deepseek.com/chat/completions', headers=headers, json=data)
|
794 |
+
logging.debug(f"Full API response data: {response}")
|
795 |
+
if response.status_code == 200:
|
796 |
+
response_data = response.json()
|
797 |
+
logging.debug(response_data)
|
798 |
+
if 'choices' in response_data and len(response_data['choices']) > 0:
|
799 |
+
summary = response_data['choices'][0]['message']['content'].strip()
|
800 |
+
logging.debug("DeepSeek: Chat request successful")
|
801 |
+
return summary
|
802 |
+
else:
|
803 |
+
logging.warning("DeepSeek: Chat response not found in the response data")
|
804 |
+
return "DeepSeek: Chat response not available"
|
805 |
+
else:
|
806 |
+
logging.error(f"DeepSeek: Chat request failed with status code {response.status_code}")
|
807 |
+
logging.error(f"DeepSeek: Error response: {response.text}")
|
808 |
+
return f"DeepSeek: Failed to chat request summary. Status code: {response.status_code}"
|
809 |
+
except Exception as e:
|
810 |
+
logging.error(f"DeepSeek: Error in processing: {str(e)}", exc_info=True)
|
811 |
+
return f"DeepSeek: Error occurred while processing chat request: {str(e)}"
|
812 |
+
|
813 |
+
|
814 |
+
def chat_with_mistral(api_key, input_data, custom_prompt_arg, temp=None, system_message=None):
|
815 |
+
logging.debug("Mistral: Chat request made")
|
816 |
+
try:
|
817 |
+
logging.debug("Mistral: Loading and validating configurations")
|
818 |
+
loaded_config_data = load_and_log_configs()
|
819 |
+
if loaded_config_data is None:
|
820 |
+
logging.error("Failed to load configuration data")
|
821 |
+
mistral_api_key = None
|
822 |
+
else:
|
823 |
+
# Prioritize the API key passed as a parameter
|
824 |
+
if api_key and api_key.strip():
|
825 |
+
mistral_api_key = api_key
|
826 |
+
logging.info("Mistral: Using API key provided as parameter")
|
827 |
+
else:
|
828 |
+
# If no parameter is provided, use the key from the config
|
829 |
+
mistral_api_key = loaded_config_data['api_keys'].get('mistral')
|
830 |
+
if mistral_api_key:
|
831 |
+
logging.info("Mistral: Using API key from config file")
|
832 |
+
else:
|
833 |
+
logging.warning("Mistral: No API key found in config file")
|
834 |
+
|
835 |
+
# Final check to ensure we have a valid API key
|
836 |
+
if not mistral_api_key or not mistral_api_key.strip():
|
837 |
+
logging.error("Mistral: No valid API key available")
|
838 |
+
return "Mistral: No valid API key available"
|
839 |
+
|
840 |
+
logging.debug(f"Mistral: Using API Key: {mistral_api_key[:5]}...{mistral_api_key[-5:]}")
|
841 |
+
|
842 |
+
logging.debug("Mistral: Using provided string data")
|
843 |
+
data = input_data
|
844 |
+
|
845 |
+
# Text extraction
|
846 |
+
if isinstance(input_data, list):
|
847 |
+
text = extract_text_from_segments(input_data)
|
848 |
+
elif isinstance(input_data, str):
|
849 |
+
text = input_data
|
850 |
+
else:
|
851 |
+
raise ValueError("Mistral: Invalid input data format")
|
852 |
+
|
853 |
+
mistral_model = loaded_config_data['models'].get('mistral', "mistral-large-latest")
|
854 |
+
|
855 |
+
temp = float(temp) if temp is not None else 0.2
|
856 |
+
if system_message is None:
|
857 |
+
system_message = "You are a helpful AI assistant who does whatever the user requests."
|
858 |
+
|
859 |
+
headers = {
|
860 |
+
'Authorization': f'Bearer {mistral_api_key}',
|
861 |
+
'Content-Type': 'application/json'
|
862 |
+
}
|
863 |
+
|
864 |
+
logging.debug(
|
865 |
+
f"Deepseek API Key: {mistral_api_key[:5]}...{mistral_api_key[-5:] if mistral_api_key else None}")
|
866 |
+
logging.debug("Mistral: Preparing data + prompt for submittal")
|
867 |
+
mistral_prompt = f"{custom_prompt_arg}\n\n\n\n{text} "
|
868 |
+
data = {
|
869 |
+
"model": mistral_model,
|
870 |
+
"messages": [
|
871 |
+
{"role": "system",
|
872 |
+
"content": system_message},
|
873 |
+
{"role": "user",
|
874 |
+
"content": mistral_prompt}
|
875 |
+
],
|
876 |
+
"temperature": temp,
|
877 |
+
"top_p": 1,
|
878 |
+
"max_tokens": 4096,
|
879 |
+
"stream": False,
|
880 |
+
"safe_prompt": False
|
881 |
+
}
|
882 |
+
|
883 |
+
logging.debug("Mistral: Posting request")
|
884 |
+
response = requests.post('https://api.mistral.ai/v1/chat/completions', headers=headers, json=data)
|
885 |
+
logging.debug(f"Full API response data: {response}")
|
886 |
+
if response.status_code == 200:
|
887 |
+
response_data = response.json()
|
888 |
+
logging.debug(response_data)
|
889 |
+
if 'choices' in response_data and len(response_data['choices']) > 0:
|
890 |
+
summary = response_data['choices'][0]['message']['content'].strip()
|
891 |
+
logging.debug("Mistral: request successful")
|
892 |
+
return summary
|
893 |
+
else:
|
894 |
+
logging.warning("Mistral: Chat response not found in the response data")
|
895 |
+
return "Mistral: Chat response not available"
|
896 |
+
else:
|
897 |
+
logging.error(f"Mistral: Chat request failed with status code {response.status_code}")
|
898 |
+
logging.error(f"Mistral: Error response: {response.text}")
|
899 |
+
return f"Mistral: Failed to process summary. Status code: {response.status_code}. Error: {response.text}"
|
900 |
+
except Exception as e:
|
901 |
+
logging.error(f"Mistral: Error in processing: {str(e)}", exc_info=True)
|
902 |
+
return f"Mistral: Error occurred while processing Chat: {str(e)}"
|
903 |
+
|
904 |
+
|
905 |
+
|
906 |
+
# Stashed in here since OpenAI usage.... #FIXME
|
907 |
+
# FIXME - https://docs.vllm.ai/en/latest/getting_started/quickstart.html .... Great docs.
|
908 |
+
# def chat_with_vllm(input_data, custom_prompt_input, api_key=None, vllm_api_url="http://127.0.0.1:8000/v1/chat/completions", system_prompt=None):
|
909 |
+
# loaded_config_data = load_and_log_configs()
|
910 |
+
# llm_model = loaded_config_data['models']['vllm']
|
911 |
+
# # API key validation
|
912 |
+
# if api_key is None:
|
913 |
+
# logging.info("vLLM: API key not provided as parameter")
|
914 |
+
# logging.info("vLLM: Attempting to use API key from config file")
|
915 |
+
# api_key = loaded_config_data['api_keys']['llama']
|
916 |
+
#
|
917 |
+
# if api_key is None or api_key.strip() == "":
|
918 |
+
# logging.info("vLLM: API key not found or is empty")
|
919 |
+
# vllm_client = OpenAI(
|
920 |
+
# base_url=vllm_api_url,
|
921 |
+
# api_key=custom_prompt_input
|
922 |
+
# )
|
923 |
+
#
|
924 |
+
# if isinstance(input_data, str) and os.path.isfile(input_data):
|
925 |
+
# logging.debug("vLLM: Loading json data for summarization")
|
926 |
+
# with open(input_data, 'r') as file:
|
927 |
+
# data = json.load(file)
|
928 |
+
# else:
|
929 |
+
# logging.debug("vLLM: Using provided string data for summarization")
|
930 |
+
# data = input_data
|
931 |
+
#
|
932 |
+
# logging.debug(f"vLLM: Loaded data: {data}")
|
933 |
+
# logging.debug(f"vLLM: Type of data: {type(data)}")
|
934 |
+
#
|
935 |
+
# if isinstance(data, dict) and 'summary' in data:
|
936 |
+
# # If the loaded data is a dictionary and already contains a summary, return it
|
937 |
+
# logging.debug("vLLM: Summary already exists in the loaded data")
|
938 |
+
# return data['summary']
|
939 |
+
#
|
940 |
+
# # If the loaded data is a list of segment dictionaries or a string, proceed with summarization
|
941 |
+
# if isinstance(data, list):
|
942 |
+
# segments = data
|
943 |
+
# text = extract_text_from_segments(segments)
|
944 |
+
# elif isinstance(data, str):
|
945 |
+
# text = data
|
946 |
+
# else:
|
947 |
+
# raise ValueError("Invalid input data format")
|
948 |
+
#
|
949 |
+
#
|
950 |
+
# custom_prompt = custom_prompt_input
|
951 |
+
#
|
952 |
+
# completion = client.chat.completions.create(
|
953 |
+
# model=llm_model,
|
954 |
+
# messages=[
|
955 |
+
# {"role": "system", "content": f"{system_prompt}"},
|
956 |
+
# {"role": "user", "content": f"{text} \n\n\n\n{custom_prompt}"}
|
957 |
+
# ]
|
958 |
+
# )
|
959 |
+
# vllm_summary = completion.choices[0].message.content
|
960 |
+
# return vllm_summary
|
961 |
+
|
962 |
+
|
963 |
+
|
964 |
+
#
|
965 |
+
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
966 |
#######################################################################################################################
|