File size: 20,665 Bytes
94ddc94 093632f 83d1ea1 52ee99e 94ddc94 093632f 94ddc94 093632f 83d1ea1 093632f 6b7de2a 04f2d25 83d1ea1 52ee99e 093632f 94ddc94 093632f 94ddc94 093632f 32585a6 093632f 52ee99e 04f2d25 52ee99e 04f2d25 6b7de2a 093632f 83d1ea1 32585a6 83d1ea1 32585a6 04f2d25 7ba4670 04f2d25 18fc113 04f2d25 9b1726d 04f2d25 52ee99e 7ba4670 52ee99e 9b1726d 18fc113 9b1726d 52ee99e a4d8b4b 9b1726d a4d8b4b 04f2d25 7ba4670 32585a6 18fc113 32585a6 18fc113 32585a6 9b1726d 32585a6 093632f 9023f08 093632f 86ff876 093632f 9b1726d 093632f 7ba4670 83d1ea1 18fc113 83d1ea1 18fc113 83d1ea1 9b1726d 83d1ea1 093632f 18fc113 093632f 18fc113 093632f 18fc113 093632f 83d1ea1 d59e3c0 83d1ea1 d59e3c0 83d1ea1 d59e3c0 83d1ea1 093632f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | """
This class contains multiple LLMs and handles LLMs response
"""
import json
import time
from openai import OpenAI
import openai
import torch
import re
import anthropic
import os
import streamlit as st
from google.genai import types
from google import genai
class LLM:
def __init__(self, Core):
self.Core = Core
self.model = None
self.model_type = "openai" # valid values -> ["openai", "ollama"]
self.client = None
self.connect_to_llm()
def get_credential(self, key):
return os.getenv(key) or st.secrets.get(key)
def get_response(self, prompt, instructions):
if self.model_type == "openai":
response = self.get_message_openai(prompt, instructions)
# elif self.model_type == "ollama":
# response = self.get_message_ollama(prompt, instructions)
elif self.model_type == "inference":
response = self.get_message_inference(prompt, instructions)
elif self.model_type == "claude":
response = self.get_message_claude(prompt, instructions)
elif self.model_type == "google":
response = self.get_message_google(prompt, instructions)
else:
raise f"Invalid model type : {self.model_type}"
return response
def connect_to_llm(self):
"""
connect to selected llm -> ollama or openai connection
:return:
"""
if self.Core.model in self.Core.config_file["openai_models"]:
self.model_type = "openai"
elif self.Core.model in self.Core.config_file["inference_models"]:
self.model_type = "inference"
elif self.Core.model in self.Core.config_file["google_models"]:
self.model_type = "google"
# elif self.Core.model in self.Core.config_file["ollama_models"]:
# self.model_type = "ollama"
# self.client = ollama.Client()
elif self.Core.model in self.Core.config_file["claude_models"]:
self.model_type = "claude"
self.client = anthropic.Anthropic(
api_key=self.get_credential('claude_api_key'),
)
self.model = self.Core.model
# ==============================================================
def get_message_inference(self, prompt, instructions, max_retries=6):
"""
sending the prompt to openai LLM and get back the response
"""
api_key = self.get_credential('inference_api_key')
client = OpenAI(
base_url="https://api.inference.net/v1",
api_key=api_key,
)
for attempt in range(max_retries):
try:
if self.Core.reasoning_model:
response = client.chat.completions.create(
model=self.Core.model,
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": prompt}
],
reasoning_effort="medium",
n=1,
)
else:
response = client.chat.completions.create(
model=self.Core.model,
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": prompt}
],
n=1,
temperature=self.Core.temperature
)
tokens = {
'prompt_tokens': response.usage.prompt_tokens,
'completion_tokens': response.usage.completion_tokens,
'total_tokens': response.usage.total_tokens
}
# validating the JSON
self.Core.total_tokens['prompt_tokens'] += tokens['prompt_tokens']
self.Core.total_tokens['completion_tokens'] += tokens['completion_tokens']
self.Core.temp_token_counter += tokens['total_tokens']
try:
content = response.choices[0].message.content
# Extract JSON code block
output = json.loads(content)
if 'message' not in output or 'header' not in output:
print(f"'message' or 'header' is missing in response on attempt {attempt + 1}. Retrying...")
continue # Continue to next attempt
else:
if len(output["header"].strip()) > self.Core.config_file["header_limit"] or len(
output["message"].strip()) > self.Core.config_file["message_limit"]:
print(
f"'header' or 'message' is more than specified characters in response on attempt {attempt + 1}. Retrying...")
continue
return output
except json.JSONDecodeError:
print(f"Invalid JSON from LLM on attempt {attempt + 1}. Retrying...")
except openai.APIConnectionError as e:
print("The server could not be reached")
print(e.__cause__) # an underlying Exception, likely raised within httpx.
except openai.RateLimitError as e:
print("A 429 status code was received; we should back off a bit.")
except openai.APIStatusError as e:
print("Another non-200-range status code was received")
print(e.status_code)
print(e.response)
print("Max retries exceeded. Returning empty response.")
return None
# =========================================================================
def get_message_google(self, prompt, instructions, max_retries=6):
client = genai.Client(api_key=self.get_credential("Google_API"))
for attempt in range(max_retries):
try:
response = client.models.generate_content(
model=self.Core.model,
contents=prompt,
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_budget=0),
system_instruction=instructions,
temperature=self.Core.temperature,
response_mime_type="application/json"
))
# output = json.loads(str(response.text))
tokens = {
'prompt_tokens': response.usage_metadata.prompt_token_count,
'completion_tokens': response.usage_metadata.candidates_token_count,
'total_tokens': response.usage_metadata.total_token_count
}
# validating the JSON
self.Core.total_tokens['prompt_tokens'] += tokens['prompt_tokens']
self.Core.total_tokens['completion_tokens'] += tokens['completion_tokens']
self.Core.temp_token_counter += tokens['total_tokens']
output = self.preprocess_and_parse_json(response.text)
if 'message' not in output or 'header' not in output:
print(f"'message' or 'header' is missing in response on attempt {attempt + 1}. Retrying...")
continue # Continue to next attempt
else:
if len(output["header"].strip()) > self.Core.config_file["header_limit"] or len(
output["message"].strip()) > self.Core.config_file["message_limit"]:
print(
f"'header' or 'message' is more than specified characters in response on attempt {attempt + 1}. Retrying...")
continue
return output
except json.JSONDecodeError:
print(f"Invalid JSON from LLM on attempt {attempt + 1}. Retrying...")
except Exception as e:
print(f"Error in attempt {attempt}: {e}")
print("Max retries exceeded. Returning empty response.")
return None
# =========================================================================
def get_message_openai(self, prompt, instructions, max_retries=5):
"""
sending the prompt to openai LLM and get back the response
"""
openai.api_key = self.Core.api_key
client = OpenAI(api_key=self.Core.api_key)
for attempt in range(max_retries):
try:
if self.Core.reasoning_model:
response = client.chat.completions.create(
model=self.Core.model,
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": prompt}
],
reasoning_effort="minimal",
n=1,
)
else:
response = client.chat.completions.create(
model=self.Core.model,
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": prompt}
],
n=1,
temperature=self.Core.temperature
)
tokens = {
'prompt_tokens': response.usage.prompt_tokens,
'completion_tokens': response.usage.completion_tokens,
'total_tokens': response.usage.total_tokens
}
# validating the JSON
self.Core.total_tokens['prompt_tokens'] += tokens['prompt_tokens']
self.Core.total_tokens['completion_tokens'] += tokens['completion_tokens']
self.Core.temp_token_counter += tokens['total_tokens']
try:
content = response.choices[0].message.content
# Extract JSON code block
output = json.loads(content)
if 'message' not in output or 'header' not in output:
print(f"'message' or 'header' is missing in response on attempt {attempt + 1}. Retrying...")
continue # Continue to next attempt
else:
if len(output["header"].strip()) > self.Core.config_file["header_limit"] or len(
output["message"].strip()) > self.Core.config_file["message_limit"]:
print(
f"'header' or 'message' is more than specified characters in response on attempt {attempt + 1}. Retrying...")
continue
return output
except json.JSONDecodeError:
print(f"Invalid JSON from LLM on attempt {attempt + 1}. Retrying...")
except openai.APIConnectionError as e:
print("The server could not be reached")
print(e.__cause__) # an underlying Exception, likely raised within httpx.
except openai.RateLimitError as e:
print("A 429 status code was received; we should back off a bit.")
except openai.APIStatusError as e:
print("Another non-200-range status code was received")
print(e.status_code)
print(e.response)
print("Max retries exceeded. Returning empty response.")
return None
# ======================================================================
def get_message_ollama(self, prompt, instructions, max_retries=10):
"""
Send the prompt to the LLM and get back the response.
Includes handling for GPU memory issues by clearing cache and waiting before retry.
"""
prompt = instructions + "\n \n" + prompt
for attempt in range(max_retries):
try:
# Try generating the response
response = self.client.generate(model=self.model, prompt=prompt)
except Exception as e:
# This catches errors like the connection being forcibly closed
print(f"Error on attempt {attempt + 1}: {e}.")
try:
# Clear GPU cache if you're using PyTorch; this may help free up memory
torch.cuda.empty_cache()
print("Cleared GPU cache.")
except Exception as cache_err:
print("Failed to clear GPU cache:", cache_err)
# Wait a bit before retrying to allow memory to recover
time.sleep(2)
continue
try:
tokens = {
'prompt_tokens': 0,
'completion_tokens': 0,
'total_tokens': 0
}
try:
output = self.preprocess_and_parse_json(response.response)
if output is None:
continue
if 'message' not in output or 'header' not in output:
print(f"'message' or 'header' is missing in response on attempt {attempt + 1}. Retrying...")
continue # Continue to next attempt
else:
if len(output["header"].strip()) > self.Core.config_file["header_limit"] or len(
output["message"].strip()) > self.Core.config_file["message_limit"]:
print(
f"'header' or 'message' is more than specified characters in response on attempt {attempt + 1}. Retrying...")
continue
else:
return output
except json.JSONDecodeError:
print(f"Invalid JSON from LLM on attempt {attempt + 1}. Retrying...")
except Exception as parse_error:
print("Error processing output:", parse_error)
print("Max retries exceeded. Returning empty response.")
return None
def get_message_claude(self, prompt, instructions, max_retries=6):
"""
send prompt to claude LLM and get back the response
:param prompt:
:param instructions:
:return:
"""
for attempt in range(max_retries):
try:
message = self.client.messages.create(
model=self.model,
max_tokens=4096,
system = instructions,
messages=[
{"role": "user", "content": prompt + "\nHere is the JSON requested:\n"}
],
temperature=self.Core.temperature
)
# Try generating the response
response = message.content[0].text
tokens = {
'prompt_tokens': message.usage.input_tokens,
'completion_tokens': message.usage.output_tokens,
'total_tokens': message.usage.output_tokens + message.usage.input_tokens
}
self.Core.total_tokens['prompt_tokens'] += tokens['prompt_tokens']
self.Core.total_tokens['completion_tokens'] += tokens['completion_tokens']
self.Core.temp_token_counter += tokens['total_tokens']
try:
output = self.preprocess_and_parse_json_claude(response)
if output is None:
continue
if 'message' not in output or 'header' not in output:
print(f"'message' or 'header' is missing in response on attempt {attempt + 1}. Retrying...")
continue # Continue to next attempt
else:
if len(output["header"].strip()) > self.Core.config_file["header_limit"] or len(
output["message"].strip()) > self.Core.config_file["message_limit"]:
print(
f"'header' or 'message' is more than specified characters in response on attempt {attempt + 1}. Retrying...")
continue
else:
return output
except json.JSONDecodeError:
print(f"Invalid JSON from LLM on attempt {attempt + 1}. Retrying...")
except Exception as parse_error:
print("Error processing output:", parse_error)
print("Max retries exceeded. Returning empty response.")
return None
# ======================================================================
def preprocess_and_parse_json(self, response: str):
"""
Remove <think> blocks, extract JSON (from ```json fences or first {...} block),
and parse. Includes a repair pass to handle common LLM issues like trailing commas.
"""
def extract_json(text: str) -> str:
# Remove <think>...</think>
text = re.sub(r'<think>.*?</think>', '', text, flags=re.DOTALL).strip()
# Prefer fenced code if present
fence = re.search(r'```(?:json)?(.*?)```', text, flags=re.DOTALL | re.IGNORECASE)
if fence:
return fence.group(1).strip()
# Otherwise, grab the first {...} block
brace = re.search(r'\{.*\}', text, flags=re.DOTALL)
return brace.group(0).strip() if brace else text.strip()
def normalize_quotes(text: str) -> str:
return (text
.replace('\ufeff', '') # strip BOM if present
.replace('“', '"').replace('”', '"')
.replace('‘', "'").replace('’', "'"))
def strip_comments(text: str) -> str:
# Remove // line comments and /* block comments */
text = re.sub(r'//.*?$', '', text, flags=re.MULTILINE)
text = re.sub(r'/\*.*?\*/', '', text, flags=re.DOTALL)
return text
def remove_trailing_commas(text: str) -> str:
# Remove commas before } or ]
return re.sub(r',(\s*[}\]])', r'\1', text)
raw = extract_json(response)
raw = normalize_quotes(raw)
try:
return json.loads(raw)
except json.JSONDecodeError:
# Repair pass
repaired = strip_comments(raw)
repaired = remove_trailing_commas(repaired)
repaired = repaired.strip()
try:
return json.loads(repaired)
except json.JSONDecodeError as e:
print(f"Failed to parse JSON: {e}")
# print('Offending text:', repaired)
return None
# ===============================================================
# def preprocess_and_parse_json_claude(self, response: str):
# """
# process claude response and extract JSON
# :param response:
# :return:
# """
# json_start = response.index("{")
# json_end = response.rfind("}")
# parsed_response = json.loads(response[json_start:json_end + 1])
# return parsed_response
#
def preprocess_and_parse_json_claude(self, response: str):
"""
Process Claude response and extract JSON content safely
"""
try:
json_start = response.index("{")
json_end = response.rfind("}")
json_string = response[json_start:json_end + 1]
parsed_response = json.loads(json_string)
if not isinstance(parsed_response, dict):
raise ValueError(f"Parsed response is not a dict: {parsed_response}")
return parsed_response
except ValueError as ve:
raise ValueError(f"Could not extract JSON from Claude response: {ve}\nOriginal response: {response}")
except json.JSONDecodeError as je:
raise ValueError(f"Failed to parse JSON from string: {json_string}\nError: {je}")
|