Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /romiluz13_Hybrid-Search-RAG /src /hybridrag /engine /llm /zhipu.py
| import json | |
| import re | |
| import numpy as np | |
| import pipmaster as pm # Pipmaster for dynamic library install | |
| from openai import ( | |
| APIConnectionError, | |
| APITimeoutError, | |
| RateLimitError, | |
| ) | |
| from tenacity import ( | |
| retry, | |
| retry_if_exception_type, | |
| stop_after_attempt, | |
| wait_exponential, | |
| ) | |
| from ..utils import ( | |
| logger, | |
| verbose_debug, | |
| wrap_embedding_func_with_attrs, | |
| ) | |
| from .types import GPTKeywordExtractionFormat | |
| # install specific modules | |
| if not pm.is_installed("zhipuai"): | |
| pm.install("zhipuai") | |
| async def zhipu_complete_if_cache( | |
| prompt: str | list[dict[str, str]], | |
| model: str = "glm-4-flashx", # The most cost/performance balance model in glm-4 series | |
| api_key: str | None = None, | |
| system_prompt: str | None = None, | |
| history_messages: list[dict[str, str]] = None, | |
| enable_cot: bool = False, | |
| **kwargs, | |
| ) -> str: | |
| if history_messages is None: | |
| history_messages = [] | |
| if enable_cot: | |
| logger.debug( | |
| "enable_cot=True is not supported for ZhipuAI and will be ignored." | |
| ) | |
| # dynamically load ZhipuAI | |
| try: | |
| from zhipuai import ZhipuAI | |
| except ImportError as e: | |
| raise ImportError( | |
| "Please install zhipuai before initialize zhipuai backend." | |
| ) from e | |
| if api_key: | |
| client = ZhipuAI(api_key=api_key) | |
| else: | |
| # please set ZHIPUAI_API_KEY in your environment | |
| # os.environ["ZHIPUAI_API_KEY"] | |
| client = ZhipuAI() | |
| messages = [] | |
| if not system_prompt: | |
| system_prompt = "You are a helpful assistant. Note that sensitive words in the content should be replaced with ***" | |
| # Add system prompt if provided | |
| if system_prompt: | |
| messages.append({"role": "system", "content": system_prompt}) | |
| messages.extend(history_messages) | |
| messages.append({"role": "user", "content": prompt}) | |
| # Add debug logging | |
| logger.debug("===== Query Input to LLM =====") | |
| logger.debug(f"Query: {prompt}") | |
| verbose_debug(f"System prompt: {system_prompt}") | |
| # Remove unsupported kwargs | |
| kwargs = { | |
| k: v for k, v in kwargs.items() if k not in ["hashing_kv", "keyword_extraction"] | |
| } | |
| response = client.chat.completions.create(model=model, messages=messages, **kwargs) | |
| return response.choices[0].message.content | |
| async def zhipu_complete( | |
| prompt, | |
| system_prompt=None, | |
| history_messages=None, | |
| keyword_extraction=False, | |
| enable_cot: bool = False, | |
| **kwargs, | |
| ): | |
| # Pop keyword_extraction from kwargs to avoid passing it to zhipu_complete_if_cache | |
| if history_messages is None: | |
| history_messages = [] | |
| keyword_extraction = kwargs.pop("keyword_extraction", None) | |
| if keyword_extraction: | |
| # Add a system prompt to guide the model to return JSON format | |
| extraction_prompt = """You are a helpful assistant that extracts keywords from text. | |
| Please analyze the content and extract two types of keywords: | |
| 1. High-level keywords: Important concepts and main themes | |
| 2. Low-level keywords: Specific details and supporting elements | |
| Return your response in this exact JSON format: | |
| { | |
| "high_level_keywords": ["keyword1", "keyword2"], | |
| "low_level_keywords": ["keyword1", "keyword2", "keyword3"] | |
| } | |
| Only return the JSON, no other text.""" | |
| # Combine with existing system prompt if any | |
| if system_prompt: | |
| system_prompt = f"{system_prompt}\n\n{extraction_prompt}" | |
| else: | |
| system_prompt = extraction_prompt | |
| try: | |
| response = await zhipu_complete_if_cache( | |
| prompt=prompt, | |
| system_prompt=system_prompt, | |
| history_messages=history_messages, | |
| enable_cot=enable_cot, | |
| **kwargs, | |
| ) | |
| # Try to parse as JSON | |
| try: | |
| data = json.loads(response) | |
| return GPTKeywordExtractionFormat( | |
| high_level_keywords=data.get("high_level_keywords", []), | |
| low_level_keywords=data.get("low_level_keywords", []), | |
| ) | |
| except json.JSONDecodeError: | |
| # If direct JSON parsing fails, try to extract JSON from text | |
| match = re.search(r"\{[\s\S]*\}", response) | |
| if match: | |
| try: | |
| data = json.loads(match.group()) | |
| return GPTKeywordExtractionFormat( | |
| high_level_keywords=data.get("high_level_keywords", []), | |
| low_level_keywords=data.get("low_level_keywords", []), | |
| ) | |
| except json.JSONDecodeError: | |
| pass | |
| # If all parsing fails, log warning and return empty format | |
| logger.warning( | |
| f"Failed to parse keyword extraction response: {response}" | |
| ) | |
| return GPTKeywordExtractionFormat( | |
| high_level_keywords=[], low_level_keywords=[] | |
| ) | |
| except Exception as e: | |
| logger.error(f"Error during keyword extraction: {str(e)}") | |
| return GPTKeywordExtractionFormat( | |
| high_level_keywords=[], low_level_keywords=[] | |
| ) | |
| else: | |
| # For non-keyword-extraction, just return the raw response string | |
| return await zhipu_complete_if_cache( | |
| prompt=prompt, | |
| system_prompt=system_prompt, | |
| history_messages=history_messages, | |
| enable_cot=enable_cot, | |
| **kwargs, | |
| ) | |
| async def zhipu_embedding( | |
| texts: list[str], model: str = "embedding-3", api_key: str = None, **kwargs | |
| ) -> np.ndarray: | |
| # dynamically load ZhipuAI | |
| try: | |
| from zhipuai import ZhipuAI | |
| except ImportError as e: | |
| raise ImportError( | |
| "Please install zhipuai before initialize zhipuai backend." | |
| ) from e | |
| if api_key: | |
| client = ZhipuAI(api_key=api_key) | |
| else: | |
| # please set ZHIPUAI_API_KEY in your environment | |
| # os.environ["ZHIPUAI_API_KEY"] | |
| client = ZhipuAI() | |
| # Convert single text to list if needed | |
| if isinstance(texts, str): | |
| texts = [texts] | |
| embeddings = [] | |
| for text in texts: | |
| try: | |
| response = client.embeddings.create(model=model, input=[text], **kwargs) | |
| embeddings.append(response.data[0].embedding) | |
| except Exception as e: | |
| raise Exception(f"Error calling ChatGLM Embedding API: {str(e)}") from e | |
| return np.array(embeddings) | |
Xet Storage Details
- Size:
- 7.11 kB
- Xet hash:
- aef2dbb7b703e1490b5fd3495e498576ba7e4ac754970019359fbc6c316e13d1
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.