|
| 1 | +# Copyright 2025 Google LLC. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""MiniMax provider for LangExtract. |
| 16 | +
|
| 17 | +This provider uses MiniMax's OpenAI-compatible API to extract structured |
| 18 | +information from text. |
| 19 | +
|
| 20 | +Usage: |
| 21 | + # Using factory |
| 22 | + from langextract.factory import ModelConfig, create_model |
| 23 | + |
| 24 | + config = ModelConfig( |
| 25 | + model_id="MiniMax-M2.5", |
| 26 | + provider="MiniMaxLanguageModel", |
| 27 | + provider_kwargs={ |
| 28 | + "api_key": "your-minimax-api-key" |
| 29 | + } |
| 30 | + ) |
| 31 | + model = create_model(config) |
| 32 | + |
| 33 | + result = lx.extract( |
| 34 | + text_or_documents=text, |
| 35 | + prompt_description=instructions, |
| 36 | + model=model |
| 37 | + ) |
| 38 | +""" |
| 39 | + |
| 40 | +from __future__ import annotations |
| 41 | + |
| 42 | +import dataclasses |
| 43 | +from typing import Any |
| 44 | + |
| 45 | +from langextract.core import base_model |
| 46 | +from langextract.core import data |
| 47 | +from langextract.providers import patterns |
| 48 | +from langextract.providers import router |
| 49 | + |
| 50 | + |
| 51 | +_DEFAULT_MODEL_ID = "MiniMax-M2.5" |
| 52 | +_DEFAULT_BASE_URL = "https://api.minimax.io/v1" |
| 53 | + |
| 54 | + |
| 55 | +@router.register( |
| 56 | + *patterns.MINIMAX_PATTERNS, |
| 57 | + priority=patterns.MINIMAX_PRIORITY, |
| 58 | +) |
| 59 | +@dataclasses.dataclass(init=False) |
| 60 | +class MiniMaxLanguageModel(base_model.BaseLanguageModel): |
| 61 | + """Language model inference using MiniMax's OpenAI-compatible API.""" |
| 62 | + |
| 63 | + model_id: str = _DEFAULT_MODEL_ID |
| 64 | + api_key: str | None = None |
| 65 | + base_url: str = _DEFAULT_BASE_URL |
| 66 | + organization: str | None = None |
| 67 | + format_type: data.FormatType = data.FormatType.JSON |
| 68 | + temperature: float | None = None |
| 69 | + max_workers: int = 10 |
| 70 | + _client: Any = dataclasses.field(default=None, repr=False, compare=False) |
| 71 | + _extra_kwargs: dict[str, Any] = dataclasses.field( |
| 72 | + default_factory=dict, repr=False, compare=False |
| 73 | + ) |
| 74 | + |
| 75 | + @property |
| 76 | + def requires_fence_output(self) -> bool: |
| 77 | + """MiniMax returns raw JSON without fences.""" |
| 78 | + if self.format_type == data.FormatType.JSON: |
| 79 | + return False |
| 80 | + return super().requires_fence_output |
| 81 | + |
| 82 | + def __post_init__(self): |
| 83 | + """Initialize the OpenAI client with MiniMax configuration.""" |
| 84 | + try: |
| 85 | + from openai import AsyncOpenAI |
| 86 | + except ImportError as e: |
| 87 | + raise ImportError( |
| 88 | + "OpenAI package is required for MiniMax provider. " |
| 89 | + "Install with: pip install langextract[openai]" |
| 90 | + ) from e |
| 91 | + |
| 92 | + if self._client is None: |
| 93 | + self._client = AsyncOpenAI( |
| 94 | + api_key=self.api_key, |
| 95 | + base_url=self.base_url, |
| 96 | + organization=self.organization, |
| 97 | + **self._extra_kwargs, |
| 98 | + ) |
| 99 | + |
| 100 | + async def _generate( |
| 101 | + self, |
| 102 | + texts: list[str], |
| 103 | + prompt_description: str, |
| 104 | + extra_params: dict[str, Any] | None = None, |
| 105 | + ) -> list[list[base_model.ExtractionCandidate]]: |
| 106 | + """Generate extractions for the given texts.""" |
| 107 | + import asyncio |
| 108 | + |
| 109 | + extra_params = extra_params or {} |
| 110 | + |
| 111 | + async def process_single(text: str) -> list[base_model.ExtractionCandidate]: |
| 112 | + response = await self._client.chat.completions.create( |
| 113 | + model=self.model_id, |
| 114 | + messages=[ |
| 115 | + { |
| 116 | + "role": "system", |
| 117 | + "content": "You are a helpful assistant that extracts structured information from text.", |
| 118 | + }, |
| 119 | + { |
| 120 | + "role": "user", |
| 121 | + "content": f"{prompt_description}\n\nText: {text}", |
| 122 | + }, |
| 123 | + ], |
| 124 | + response_format={"type": "json_object"} |
| 125 | + if self.format_type == data.FormatType.JSON |
| 126 | + else None, |
| 127 | + temperature=self.temperature, |
| 128 | + **extra_params, |
| 129 | + ) |
| 130 | + |
| 131 | + content = response.choices[0].message.content |
| 132 | + if not content: |
| 133 | + return [] |
| 134 | + |
| 135 | + try: |
| 136 | + import json |
| 137 | + |
| 138 | + data = json.loads(content) |
| 139 | + # Wrap in ExtractionCandidate format |
| 140 | + if isinstance(data, list): |
| 141 | + return [ |
| 142 | + base_model.ExtractionCandidate( |
| 143 | + extraction_text=item.get("text", str(item)), |
| 144 | + extraction_class=item.get("class", "unknown"), |
| 145 | + extraction_index=i, |
| 146 | + ) |
| 147 | + for i, item in enumerate(data) |
| 148 | + ] |
| 149 | + elif isinstance(data, dict): |
| 150 | + # For single object extractions |
| 151 | + return [ |
| 152 | + base_model.ExtractionCandidate( |
| 153 | + extraction_text=str(v), |
| 154 | + extraction_class=k, |
| 155 | + extraction_index=i, |
| 156 | + ) |
| 157 | + for i, (k, v) in enumerate(data.items()) |
| 158 | + ] |
| 159 | + except (json.JSONDecodeError, AttributeError): |
| 160 | + pass |
| 161 | + |
| 162 | + return [ |
| 163 | + base_model.ExtractionCandidate( |
| 164 | + extraction_text=content, |
| 165 | + extraction_class="extracted", |
| 166 | + extraction_index=0, |
| 167 | + ) |
| 168 | + ] |
| 169 | + |
| 170 | + # Process texts in parallel |
| 171 | + tasks = [process_single(text) for text in texts] |
| 172 | + results = await asyncio.gather(*tasks) |
| 173 | + return results |
| 174 | + |
| 175 | + def _generate_sync( |
| 176 | + self, |
| 177 | + texts: list[str], |
| 178 | + prompt_description: str, |
| 179 | + extra_params: dict[str, Any] | None = None, |
| 180 | + ) -> list[list[base_model.ExtractionCandidate]]: |
| 181 | + """Synchronous wrapper for generation.""" |
| 182 | + import asyncio |
| 183 | + |
| 184 | + try: |
| 185 | + loop = asyncio.get_event_loop() |
| 186 | + if loop.is_running(): |
| 187 | + # If we're in an async context, we need to create a new loop |
| 188 | + # This is a simplified sync wrapper - for production use async directly |
| 189 | + import concurrent.futures |
| 190 | + |
| 191 | + def run_in_executor(): |
| 192 | + return asyncio.run( |
| 193 | + self._generate(texts, prompt_description, extra_params) |
| 194 | + ) |
| 195 | + |
| 196 | + with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: |
| 197 | + future = executor.submit(run_in_executor) |
| 198 | + return future.result() |
| 199 | + except RuntimeError: |
| 200 | + # No event loop, run directly |
| 201 | + return asyncio.run( |
| 202 | + self._generate(texts, prompt_description, extra_params) |
| 203 | + ) |
| 204 | + |
| 205 | + def __call__( |
| 206 | + self, |
| 207 | + texts: Sequence[str], |
| 208 | + prompt_description: str, |
| 209 | + extra_params: dict[str, Any] | None = None, |
| 210 | + ) -> list[list[base_model.ExtractionCandidate]]: |
| 211 | + """Synchronous interface for the model.""" |
| 212 | + return self._generate_sync(list(texts), prompt_description, extra_params) |
| 213 | + |
| 214 | + async def _call_async( |
| 215 | + self, |
| 216 | + texts: Sequence[str], |
| 217 | + prompt_description: str, |
| 218 | + extra_params: dict[str, Any] | None = None, |
| 219 | + ) -> list[list[base_model.ExtractionCandidate]]: |
| 220 | + """Asynchronous interface for the model.""" |
| 221 | + return await self._generate(list(texts), prompt_description, extra_params) |
| 222 | + |
| 223 | + def close(self): |
| 224 | + """Close the client connection.""" |
| 225 | + # AsyncOpenAI doesn't need explicit close |
| 226 | + pass |
0 commit comments