Buckets:
| import copy | |
| import os | |
| import warnings | |
| from functools import lru_cache | |
| import pipmaster as pm # Pipmaster for dynamic library install | |
| # install specific modules | |
| if not pm.is_installed("transformers"): | |
| pm.install("transformers") | |
| if not pm.is_installed("torch"): | |
| pm.install("torch") | |
| if not pm.is_installed("numpy"): | |
| pm.install("numpy") | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| from tenacity import ( | |
| retry, | |
| stop_after_attempt, | |
| wait_exponential, | |
| retry_if_exception_type, | |
| ) | |
| from lightrag.exceptions import ( | |
| APIConnectionError, | |
| RateLimitError, | |
| APITimeoutError, | |
| ) | |
| import torch | |
| import numpy as np | |
| from lightrag.utils import TruncatedResponse, wrap_embedding_func_with_attrs | |
| os.environ["TOKENIZERS_PARALLELISM"] = "false" | |
| def initialize_hf_model(model_name): | |
| hf_tokenizer = AutoTokenizer.from_pretrained( | |
| model_name, device_map="auto", trust_remote_code=True | |
| ) | |
| hf_model = AutoModelForCausalLM.from_pretrained( | |
| model_name, device_map="auto", trust_remote_code=True | |
| ) | |
| if hf_tokenizer.pad_token is None: | |
| hf_tokenizer.pad_token = hf_tokenizer.eos_token | |
| return hf_model, hf_tokenizer | |
| async def hf_model_if_cache( | |
| model, | |
| prompt, | |
| system_prompt=None, | |
| history_messages=[], | |
| enable_cot: bool = False, | |
| **kwargs, | |
| ) -> str: | |
| if enable_cot: | |
| from lightrag.utils import logger | |
| logger.debug( | |
| "enable_cot=True is not supported for Hugging Face local models and will be ignored." | |
| ) | |
| model_name = model | |
| hf_model, hf_tokenizer = initialize_hf_model(model_name) | |
| messages = [] | |
| if system_prompt: | |
| messages.append({"role": "system", "content": system_prompt}) | |
| messages.extend(history_messages) | |
| messages.append({"role": "user", "content": prompt}) | |
| kwargs.pop("hashing_kv", None) | |
| max_tokens = kwargs.pop("max_tokens", 512) | |
| max_new_tokens = kwargs.pop("max_new_tokens", max_tokens) | |
| input_prompt = "" | |
| try: | |
| input_prompt = hf_tokenizer.apply_chat_template( | |
| messages, tokenize=False, add_generation_prompt=True | |
| ) | |
| except Exception: | |
| try: | |
| ori_message = copy.deepcopy(messages) | |
| if messages[0]["role"] == "system": | |
| messages[1]["content"] = ( | |
| "<system>" | |
| + messages[0]["content"] | |
| + "</system>\n" | |
| + messages[1]["content"] | |
| ) | |
| messages = messages[1:] | |
| input_prompt = hf_tokenizer.apply_chat_template( | |
| messages, tokenize=False, add_generation_prompt=True | |
| ) | |
| except Exception: | |
| len_message = len(ori_message) | |
| for msgid in range(len_message): | |
| input_prompt = ( | |
| input_prompt | |
| + "<" | |
| + ori_message[msgid]["role"] | |
| + ">" | |
| + ori_message[msgid]["content"] | |
| + "</" | |
| + ori_message[msgid]["role"] | |
| + ">\n" | |
| ) | |
| input_ids = hf_tokenizer( | |
| input_prompt, return_tensors="pt", padding=True, truncation=True | |
| ) | |
| # Move to wherever the model actually is, rather than assuming CUDA. | |
| # hf_model is loaded with device_map="auto" (see initialize_hf_model), | |
| # so hf_model.device already reflects accelerate's placement. | |
| inputs = {k: v.to(hf_model.device) for k, v in input_ids.items()} | |
| output = hf_model.generate( | |
| **inputs, | |
| max_new_tokens=max_new_tokens, | |
| num_return_sequences=1, | |
| early_stopping=True, | |
| ) | |
| generated_ids = output[0][len(inputs["input_ids"][0]) :] | |
| response_text = hf_tokenizer.decode(generated_ids, skip_special_tokens=True) | |
| eos_token_id = getattr( | |
| getattr(hf_model, "generation_config", None), "eos_token_id", None | |
| ) | |
| if eos_token_id is None: | |
| eos_token_id = getattr(hf_tokenizer, "eos_token_id", None) | |
| eos_token_ids = ( | |
| set(eos_token_id) | |
| if isinstance(eos_token_id, (list, tuple, set)) | |
| else {eos_token_id} | |
| if eos_token_id is not None | |
| else set() | |
| ) | |
| last_token_id = generated_ids[-1].item() if len(generated_ids) else None | |
| if ( | |
| max_new_tokens is not None | |
| and len(generated_ids) >= max_new_tokens | |
| and last_token_id not in eos_token_ids | |
| ): | |
| response_text = TruncatedResponse(response_text) | |
| return response_text | |
| async def hf_model_complete( | |
| prompt, | |
| system_prompt=None, | |
| history_messages=[], | |
| keyword_extraction=False, | |
| entity_extraction=False, | |
| enable_cot: bool = False, | |
| **kwargs, | |
| ) -> str: | |
| """Run local Hugging Face inference with LightRAG-compatible shims. | |
| Structured output note: | |
| - This adapter does not support OpenAI-style ``response_format`` JSON mode. | |
| - If callers pass ``response_format``, it is stripped before generation. | |
| - Deprecated ``keyword_extraction`` and ``entity_extraction`` booleans are | |
| accepted only as compatibility shims; they emit warnings and are ignored. | |
| """ | |
| # HuggingFace local inference has no JSON mode; drop response_format and | |
| # warn when legacy shim flags are set. | |
| if kwargs.pop("keyword_extraction", False) or keyword_extraction: | |
| warnings.warn( | |
| "hf_model_complete(keyword_extraction=True) is deprecated; " | |
| "pass response_format={'type': 'json_object'} instead.", | |
| DeprecationWarning, | |
| stacklevel=2, | |
| ) | |
| if kwargs.pop("entity_extraction", False) or entity_extraction: | |
| warnings.warn( | |
| "hf_model_complete(entity_extraction=True) is deprecated; " | |
| "pass response_format={'type': 'json_object'} instead.", | |
| DeprecationWarning, | |
| stacklevel=2, | |
| ) | |
| kwargs.pop("response_format", None) | |
| model_name = kwargs["hashing_kv"].global_config["llm_model_name"] | |
| result = await hf_model_if_cache( | |
| model_name, | |
| prompt, | |
| system_prompt=system_prompt, | |
| history_messages=history_messages, | |
| enable_cot=enable_cot, | |
| **kwargs, | |
| ) | |
| return result | |
| async def hf_embed( | |
| texts: list[str], | |
| tokenizer, | |
| embed_model, | |
| context: str = "document", | |
| query_prefix: str | None = None, | |
| document_prefix: str | None = None, | |
| ) -> np.ndarray: | |
| """Generate embeddings for a list of texts using a Hugging Face model. | |
| Args: | |
| texts (list[str]): List of input texts to embed. | |
| tokenizer: Hugging Face tokenizer. | |
| embed_model: Hugging Face model for generating embeddings. | |
| context (str): Context indicating whether the texts are "query" or "document". | |
| query_prefix (str | None): Optional prefix to add to query texts. | |
| document_prefix (str | None): Optional prefix to add to document texts. | |
| Returns: | |
| np.ndarray: Array of embeddings. | |
| """ | |
| # Detect the appropriate device | |
| if torch.cuda.is_available(): | |
| device = next(embed_model.parameters()).device # Use CUDA if available | |
| elif torch.backends.mps.is_available(): | |
| device = torch.device("mps") # Use MPS for Apple Silicon | |
| else: | |
| device = torch.device("cpu") # Fallback to CPU | |
| # Move the model to the detected device | |
| embed_model = embed_model.to(device) | |
| # Apply context-based prefixes if provided | |
| if context == "query" and query_prefix: | |
| texts = [query_prefix + text for text in texts] | |
| elif context == "document" and document_prefix: | |
| texts = [document_prefix + text for text in texts] | |
| # Tokenize the input texts and move them to the same device | |
| encoded_texts = tokenizer( | |
| texts, return_tensors="pt", padding=True, truncation=True | |
| ).to(device) | |
| # Perform inference | |
| with torch.no_grad(): | |
| attention_mask = encoded_texts["attention_mask"] | |
| outputs = embed_model( | |
| input_ids=encoded_texts["input_ids"], | |
| attention_mask=attention_mask, | |
| ) | |
| # Plain .mean(dim=1) counts padding-token hidden states, so the same | |
| # text's embedding shifts depending on what else is in the batch. | |
| # Weight by attention_mask instead. The reduction runs in float32 | |
| # regardless of the model's own dtype: accumulating in fp16/bf16 | |
| # risks the summed hidden states overflowing to infinity on long | |
| # inputs, and token counts above ~2048 (fp16) or ~256 (bf16) can't | |
| # be represented exactly, biasing the mean. clamp_min(1) keeps a | |
| # fully-masked row finite (all-padding input) rather than dividing | |
| # by zero. The result is cast back to the original hidden-state | |
| # dtype so output dtype behaviour is unchanged. | |
| mask = attention_mask.unsqueeze(-1).to(torch.float32) | |
| hidden_fp32 = outputs.last_hidden_state.to(torch.float32) | |
| summed = (hidden_fp32 * mask).sum(dim=1) | |
| counts = mask.sum(dim=1).clamp_min(1) | |
| embeddings = (summed / counts).to(outputs.last_hidden_state.dtype) | |
| # Convert embeddings to NumPy | |
| if embeddings.dtype == torch.bfloat16: | |
| return embeddings.detach().to(torch.float32).cpu().numpy() | |
| else: | |
| return embeddings.detach().cpu().numpy() | |
Xet Storage Details
- Size:
- 9.67 kB
- Xet hash:
- 11b6dcdb369a7785a47eae69b5f0646fc3c08e22d2fdd8faae4ccbbab99927d8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.