import json
import logging
from typing import List, Optional
import torch
from nltk.tokenize import sent_tokenize
# Assuming these are defined elsewhere in your code
USER_PROMPT = ""
SYSTEM_PROMPT = ""
from pydantic import BaseModel
class ClaimAssessment(BaseModel):
claim_supported: str
need_calculation: str
calculation: str
class ChonkieChunker: # Mocked for brevity
def __init__(self, model_name, chunk_size):
pass
def chunk_text(self, text):
return [text]
logger = logging.getLogger(__name__)
class LLMCheck:
def __init__(self, model_id, tensor_parallel_size=1, max_tokens=512, cache_dir=None, enable_prefix_caching=False, max_model_len=None, online_api_url: Optional[str] = None):
from vllm import LLM, SamplingParams
from vllm.sampling_params import GuidedDecodingParams
logger.info("Reminder: Please set the CUDA device before initializing the LLMCheck object.")
self.model_id = model_id
self.tensor_parallel_size = tensor_parallel_size
self.max_tokens = max_tokens
self.max_model_len = 32768 if max_model_len is None else max_model_len # max input length (prompt + doc)
self.default_chunk_size = self.max_model_len - 300 # reserve some space (hard coded) for the claim to be checked
self.cache_dir = cache_dir
self.user_prompt = USER_PROMPT
self.system_prompt = SYSTEM_PROMPT
self.enable_prefix_caching = enable_prefix_caching
self.online_api_url = online_api_url
# Check if CUDA is available and get compute capability
if torch.cuda.is_available():
compute_capability = torch.cuda.get_device_capability()
if compute_capability[0] >= 8:
self.dtype = torch.bfloat16
logger.info("Using bfloat16 for LLM initialization.")
else:
self.dtype = torch.float16
logger.info(f"GPU compute capability {compute_capability} < 8.0. Using float16 for LLM initialization.")
else:
if torch.cpu.is_available() and hasattr(torch.cpu, 'is_bf16_supported') and torch.cpu.is_bf16_supported():
self.dtype = torch.bfloat16
logger.info("CUDA not available. Using bfloat16 on CPU.")
else:
self.dtype = torch.float32
logger.info("CUDA not available and CPU doesn't support bfloat16. Using float32 for LLM initialization.")
if self.online_api_url is None:
self.llm = LLM(
model=self.model_id,
dtype=self.dtype,
download_dir=self.cache_dir,
trust_remote_code=True if self.model_id == 'bespokelabs/Bespoke-MiniCheck-7B' else False,
tensor_parallel_size=self.tensor_parallel_size,
seed=2024,
max_model_len=self.max_model_len, # need to be adjusted based on the GPU memory available
enable_prefix_caching=self.enable_prefix_caching
)
self.tokenizer = self.llm.get_tokenizer()
self.tokenizer.padding_side = "left"
else:
self.llm = None # No local LLM
self.tokenizer = None # No tokenizer needed for online inference.
terminators = []
if self.tokenizer:
terminators = [
self.tokenizer.eos_token_id,
#self.tokenizer.convert_tokens_to_ids("<|im_end|>")
]
self.json_schema = ClaimAssessment.model_json_schema()
self.guided_decoding_params = GuidedDecodingParams(json=self.json_schema)
self.sampling_params = SamplingParams(
temperature=0.7,
top_k=20,
top_p=0.8,
max_tokens=self.max_tokens,
stop_token_ids=terminators,
logprobs=5,
guided_decoding=self.guided_decoding_params
)
def sent_tokenize_with_newlines(self, text):
blocks = text.split('\n')
tokenized_blocks = [sent_tokenize(block) for block in blocks]
tokenized_text = []
for block in tokenized_blocks:
tokenized_text.extend(block)
tokenized_text.append('\n')
return tokenized_text[:-1]
def apply_chat_template(self, doc, claim):
user_prompt = self.user_prompt.replace("[DOCUMENT]", doc).replace("[CLAIM]", claim)
message = [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": user_prompt},
]
if self.tokenizer:
text = self.tokenizer.apply_chat_template(message, add_generation_prompt=True, tokenize=False)
else:
text = self._build_raw_prompt(message) # Build the raw prompt string if no tokenizer.
return text
def _build_raw_prompt(self, messages): # Replace with your prompt formatting for your online API
prompt = ""
for message in messages:
if message["role"] == "system":
prompt += f"<<SYS>>{message['content']}<</SYS>>\n\n"
elif message["role"] == "user":
prompt += f"[INST] {message['content']} [/INST]"
return prompt
def get_support_prob(self, response_text):
"""Extracts the probability of the first 'yes' token from all logprobs."""
import math
try:
response_json = json.loads(response_text)
logprobs = response_json.get("logprobs", [])
for token_logprobs in logprobs:
for token_prob in token_logprobs.values():
if token_prob.get("decoded_token", "").strip().lower() == 'yes':
return math.exp(token_prob["logprob"]) # Return on the first yes
except (json.JSONDecodeError, KeyError) as e:
logger.warning(f"Error parsing logprobs from online response: {e}")
return 0
def get_json_output(self, response_text):
try:
return json.loads(response_text)
except json.JSONDecodeError:
logger.warning(f"Failed to parse JSON from response")
return {
"claim_supported": "no",
"need_calculation": "no",
"calculation": ""
}
def get_all_chunks_per_doc(self, doc, claim):
if not hasattr(self, 'doc_chunk_cache'):
self.doc_chunk_cache = {}
if doc in self.doc_chunk_cache:
doc_chunks = self.doc_chunk_cache[doc]
else:
chonkie = ChonkieChunker(model_name=self.model_id, chunk_size=self.default_chunk_size)
doc_chunks = [chunk.strip() for chunk in chonkie.chunk_text(doc) if chunk.strip()]
self.doc_chunk_cache[doc] = doc_chunks
if len(doc_chunks) == 0:
doc_chunks = ['']
claim_repeat = [claim] * len(doc_chunks)
return {'doc_chunks': doc_chunks, 'claim_repeat': claim_repeat}
def _online_generate(self, prompts: List[str]) -> List[str]: # Returns list of text responses
import requests
"""Calls an external API to generate text from prompts."""
if not self.online_api_url:
raise ValueError("online_api_url must be set for online inference.")
responses = []
for prompt in prompts:
try:
payload = {"prompt": prompt, "sampling_params": self.sampling_params.json()} # Or adjust based on API
response = requests.post(self.online_api_url, json=payload, timeout=60)
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
responses.append(response.text) # Assuming API returns plain text
except requests.exceptions.RequestException as e:
logger.error(f"Error during online inference: {e}")
responses.append('{"claim_supported": "no", "need_calculation": "no", "calculation": ""}') # Default JSON response on error.
return responses
def _offline_generate(self, prompts: List[str]):
from vllm import LLM, SamplingParams
responses = self.llm.generate(
prompts,
self.sampling_params,
)
return responses
def score(self, docs: List[str], claims: List[str], chunk_size=None) -> List[float]:
if not hasattr(self, 'doc_chunk_cache'):
self.doc_chunk_cache = {}
self.chunk_size = chunk_size if chunk_size else self.default_chunk_size
assert self.chunk_size < self.max_model_len, \
"chunk_size must be less than max_model_len so that MiniCheck can process the claim"
all_prompts = []
doc_claim_indices = []
for index, (doc, claim) in enumerate(zip(docs, claims)):
chunks = self.get_all_chunks_per_doc(doc, claim)
doc_chunks = chunks['doc_chunks']
claim_repeat = chunks['claim_repeat']
prompts = [self.apply_chat_template(doc_chunk, claim) for doc_chunk, claim in zip(doc_chunks, claim_repeat)]
all_prompts.extend(prompts)
doc_claim_indices.extend([index] * len(prompts))
if self.online_api_url:
responses = self._online_generate(all_prompts) # List of strings (JSON or plain text)
else:
responses = self._offline_generate(all_prompts) # Assuming vllm generate returns a list of objects with .text
if not self.online_api_url:
json_outputs = [self.get_json_output(responses[idx].outputs[0].text) for idx in range(len(responses))]
probs_per_chunk = [self.get_support_prob(responses[idx].outputs[0].text) for idx in range(len(responses))]
else:
json_outputs = [self.get_json_output(response) for response in responses] # Parse JSON strings
probs_per_chunk = [self.get_support_prob(response) for response in responses]
result_dict = {}
for index, (json_output, prob) in zip(doc_claim_indices, zip(json_outputs, probs_per_chunk)):
if index not in result_dict:
result_dict[index] = {
"claim_supported": False,
"need_calculation": False,
"calculations": [],
"support_probs": []
}
if json_output.get("claim_supported", "no").lower() == "yes":
result_dict[index]["claim_supported"] = True
if json_output.get("need_calculation", "no").lower() == "yes":
result_dict[index]["need_calculation"] = True
calc = json_output.get("calculation", "").strip()
if calc and calc not in result_dict[index]["calculations"]:
result_dict[index]["calculations"].append(calc)
result_dict[index]["support_probs"].append(prob)
probs_per_doc_claim_pair = [result_dict[index] for index in range(len(docs))]
pred_label, max_support_prob, used_chunk, support_prob_per_chunk = [], [], [], []
need_calculation, calculations = [], []
for idx in range(len(probs_per_doc_claim_pair)):
doc = docs[idx]
claim = claims[idx]
result = probs_per_doc_claim_pair[idx]
support_probs = result["support_probs"]
is_supported = result["claim_supported"]
needs_calc = result["need_calculation"]
calc_list = result["calculations"]
pred_label.append(1 if is_supported else 0)
max_support_prob.append(max(support_probs) if support_probs else 0.0)
used_chunk.append(self.get_all_chunks_per_doc(doc, claim)['doc_chunks'])
support_prob_per_chunk.append(support_probs)
need_calculation.append(needs_calc)
calculations.append(calc_list)
return pred_label, max_support_prob, need_calculation, calculations, used_chunk, support_prob_per_chunk