Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
integrated nvidia_mixins
Alishahryar1de6437326466
diff --git a/providers/nvidia_nim.py b/providers/nvidia_nim.pyindex 5ba40eca..67ca724b 100644--- a/providers/nvidia_nim.py+++ b/providers/nvidia_nim.py@@ -27,12 +27,24 @@ from .exceptions import (OverloadedError,APIError,)+from .nvidia_mixins import (+ RequestBuilderMixin,+ StreamProcessorMixin,+ ErrorMapperMixin,+ ResponseConverterMixin,+)from .rate_limit import GlobalRateLimiterlogger = logging.getLogger(__name__)-class NvidiaNimProvider(BaseProvider):+class NvidiaNimProvider(+ RequestBuilderMixin,+ StreamProcessorMixin,+ ErrorMapperMixin,+ ResponseConverterMixin,+ BaseProvider,+):"""NVIDIA NIM provider using direct httpx requests."""def __init__(self, config: ProviderConfig):@@ -49,64 +61,6 @@ class NvidiaNimProvider(BaseProvider):limits=httpx.Limits(max_connections=100, max_keepalive_connections=20),)- def _load_nim_params(self) -> Dict[str, Any]:- """Load NIM-specific parameters from environment."""- params: Dict[str, Any] = {}- if val := os.getenv("NVIDIA_NIM_TEMPERATURE"):- params["temperature"] = float(val)- if val := os.getenv("NVIDIA_NIM_TOP_P"):- params["top_p"] = float(val)- if val := os.getenv("NVIDIA_NIM_MAX_TOKENS"):- params["max_tokens"] = int(val)- return params-- def _build_request_body(self, request_data: Any, stream: bool = False) -> dict:- """Build OpenAI-format request body from Anthropic request."""- messages = AnthropicToOpenAIConverter.convert_messages(request_data.messages)-- # Add system prompt- if request_data.system:- system_msg = AnthropicToOpenAIConverter.convert_system_prompt(- request_data.system- )- if system_msg:- messages.insert(0, system_msg)-- body = {- "model": request_data.model,- "messages": messages,- "max_tokens": request_data.max_tokens,- "stream": stream,- }-- if request_data.temperature is not None:- body["temperature"] = request_data.temperature- if request_data.top_p is not None:- body["top_p"] = request_data.top_p- if request_data.stop_sequences:- body["stop"] = request_data.stop_sequences- if request_data.tools:- body["tools"] = AnthropicToOpenAIConverter.convert_tools(request_data.tools)-- # Handle thinking/reasoning mode- extra_body = request_data.extra_body.copy() if request_data.extra_body else {}- if request_data.thinking and getattr(request_data.thinking, "enabled", True):- extra_body.setdefault("thinking", {"type": "enabled"})- extra_body.setdefault("reasoning_split", True)-- # Handle DeepSeek-specific thinking mode- if "deepseek" in request_data.model.lower():- extra_body.setdefault("chat_template_kwargs", {"thinking": True})-- body.update(extra_body)-- # Apply NIM defaults- for key, val in self._nim_params.items():- if key not in body:- body[key] = val-- return body-async def stream_response(self, request: Any, input_tokens: int = 0) -> AsyncIterator[str]:@@ -294,29 +248,6 @@ class NvidiaNimProvider(BaseProvider):yield sse.message_stop()yield sse.done()- def _map_error(self, response_status: int, error_text: str) -> Exception:- """Map HTTP status and error body to specific ProviderError."""- try:- error_data = json.loads(error_text)- message = error_data.get("error", {}).get("message", error_text)- except Exception:- message = error_text-- if response_status == 401:- return AuthenticationError(message, raw_error=error_text)- if response_status == 429:- # Trigger global rate limit block- self._global_rate_limiter.set_blocked(60) # Default 60s cooldown- return RateLimitError(message, raw_error=error_text)- if response_status in (400, 422):- return InvalidRequestError(message, raw_error=error_text)- if response_status >= 500:- if "overloaded" in message.lower() or "capacity" in message.lower():- return OverloadedError(message, raw_error=error_text)- return APIError(message, status_code=response_status, raw_error=error_text)-- return APIError(message, status_code=response_status, raw_error=error_text)-async def _stream_chunks(self, headers: dict, body: dict):"""Generator that yields parsed SSE data chunks from the API."""async with self._client.stream(@@ -347,65 +278,6 @@ class NvidiaNimProvider(BaseProvider):if parsed is not None:yield parsed- def _parse_sse_event(self, event_data: str) -> Any:- """Parse a single SSE event, return None if invalid/done."""- if not event_data.strip():- return None-- for line in event_data.splitlines():- line = line.strip()- if line.startswith("data:"):- data_content = line[5:].lstrip()- if data_content == "[DONE]":- return None- try:- return json.loads(data_content)- except json.JSONDecodeError:- logger.debug(f"JSON decode failed for SSE data: {data_content}")- return None- return None-- def _process_tool_call(self, tc: dict, sse: SSEBuilder):- """Process a single tool call delta and yield SSE events."""- tc_index = tc.get("index", 0)- if tc_index < 0:- tc_index = len(sse.blocks.tool_indices)-- # Update accumulated name if present- fn_delta = tc.get("function", {})- if fn_delta.get("name") is not None:- sse.blocks.tool_names[tc_index] = (- sse.blocks.tool_names.get(tc_index, "") + fn_delta["name"]- )-- # Check if we should start the tool block- if tc_index not in sse.blocks.tool_indices:- name = sse.blocks.tool_names.get(tc_index, "")- # Only start if name is non-empty or we have an ID (start of tool call)- if name or tc.get("id"):- tool_id = tc.get("id") or f"tool_{uuid.uuid4()}"- yield sse.start_tool_block(tc_index, tool_id, name)- sse.blocks.tool_started[tc_index] = True- elif not sse.blocks.tool_started.get(tc_index) and sse.blocks.tool_names.get(- tc_index- ):- # Block index exists (due to ID in previous chunk) but not started due to empty name- tool_id = f"tool_{uuid.uuid4()}" # Should ideally reuse ID if we saved it- name = sse.blocks.tool_names[tc_index]- yield sse.start_tool_block(tc_index, tool_id, name)- sse.blocks.tool_started[tc_index] = True-- args = fn_delta.get("arguments", "")- if args:- # Ensure block is started before emitting args (with a fallback name if still empty)- if not sse.blocks.tool_started.get(tc_index):- tool_id = tc.get("id") or f"tool_{uuid.uuid4()}"- name = sse.blocks.tool_names.get(tc_index, "tool_call") or "tool_call"- yield sse.start_tool_block(tc_index, tool_id, name)- sse.blocks.tool_started[tc_index] = True-- yield sse.emit_tool_delta(tc_index, args)-async def complete(self, request: Any) -> dict:"""Make a non-streaming completion request."""# Wait if globally rate limited (proactive throttle + reactive block)@@ -439,78 +311,3 @@ class NvidiaNimProvider(BaseProvider):response_status=response.status_code, error_text=response.text)return response.json()-- def convert_response(self, response_json: dict, original_request: Any) -> dict:- """Convert OpenAI response to Anthropic format."""- choice = response_json["choices"][0]- message = choice["message"]- content = []-- # Extract reasoning from various sources- reasoning = message.get("reasoning_content")- if not reasoning:- reasoning_details = message.get("reasoning_details")- if reasoning_details and isinstance(reasoning_details, list):- reasoning = "\n".join(- item.get("text", "")- for item in reasoning_details- if isinstance(item, dict)- )-- if reasoning:- content.append({"type": "thinking", "thinking": reasoning})-- # Extract text content (with think tag handling)- if message.get("content"):- raw_content = message["content"]- if isinstance(raw_content, str):- if not reasoning:- think_content, raw_content = extract_think_content(raw_content)- if think_content:- content.append({"type": "thinking", "thinking": think_content})- if raw_content:- content.append({"type": "text", "text": raw_content})- elif isinstance(raw_content, list):- for item in raw_content:- if isinstance(item, dict) and item.get("type") == "text":- content.append(item)-- # Extract tool calls- if message.get("tool_calls"):- for tc in message["tool_calls"]:- try:- args = json.loads(tc["function"]["arguments"])- except Exception:- args = tc["function"].get("arguments", {})- content.append(- {- "type": "tool_use",- "id": tc["id"],- "name": tc["function"]["name"],- "input": args,- }- )-- if not content:- # NIM models (especially Mistral-based) often require non-empty content.- # Adding a single space satisfies this requirement while avoiding- # the "(no content)" display issue in Claude Code.- content.append({"type": "text", "text": " "})-- usage = response_json.get("usage", {})-- return {- "id": response_json.get("id", f"msg_{uuid.uuid4()}"),- "type": "message",- "role": "assistant",- "model": original_request.model,- "content": content,- "stop_reason": map_stop_reason(choice.get("finish_reason")),- "stop_sequence": None,- "usage": {- "input_tokens": usage.get("prompt_tokens", 0),- "output_tokens": usage.get("completion_tokens", 0),- "cache_creation_input_tokens": 0,- "cache_read_input_tokens": 0,- },- }