-
Notifications
You must be signed in to change notification settings - Fork 7.1k
feat: add Novita AI as a native LLM provider #4939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Alex-wuhu
wants to merge
1
commit into
crewAIInc:main
Choose a base branch
from
Alex-wuhu:feat/add-novita-ai-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from crewai.llms.providers.openai.completion import OpenAICompletion | ||
|
|
||
| if TYPE_CHECKING: | ||
| import httpx | ||
|
|
||
| from crewai.llms.hooks.base import BaseInterceptor | ||
|
|
||
| NOVITA_BASE_URL = "https://api.novita.ai/openai" | ||
| NOVITA_DEFAULT_MODEL = "moonshotai/kimi-k2.5" | ||
|
|
||
|
|
||
| class NovitaCompletion(OpenAICompletion): | ||
| """Novita AI completion implementation. | ||
|
|
||
| Uses the OpenAI-compatible endpoint at https://api.novita.ai/openai. | ||
| Inherits all functionality from OpenAICompletion. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| model: str = NOVITA_DEFAULT_MODEL, | ||
| api_key: str | None = None, | ||
| base_url: str | None = None, | ||
| timeout: float | None = None, | ||
| max_retries: int = 2, | ||
| temperature: float | None = None, | ||
| provider: str | None = None, | ||
| interceptor: BaseInterceptor[httpx.Request, httpx.Response] | None = None, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| """Initialize Novita AI completion client. | ||
|
|
||
| Args: | ||
| model: Model ID, defaults to moonshotai/kimi-k2.5. | ||
| api_key: Novita API key. Falls back to NOVITA_API_KEY env var. | ||
| base_url: Override base URL. Defaults to https://api.novita.ai/openai. | ||
| **kwargs: Passed through to OpenAICompletion. | ||
| """ | ||
| resolved_api_key = api_key or os.getenv("NOVITA_API_KEY") | ||
| if not resolved_api_key: | ||
| raise ValueError( | ||
| "Novita AI API key is required. " | ||
| "Set the NOVITA_API_KEY environment variable or pass api_key." | ||
| ) | ||
|
|
||
| super().__init__( | ||
| model=model, | ||
| api_key=resolved_api_key, | ||
| base_url=base_url or NOVITA_BASE_URL, | ||
| timeout=timeout, | ||
| max_retries=max_retries, | ||
| temperature=temperature, | ||
| provider=provider or "novita", | ||
| interceptor=interceptor, | ||
| # Force completions API — Responses API is OpenAI-specific | ||
| api="completions", | ||
| **kwargs, | ||
| ) | ||
|
|
||
| def _get_client_params(self) -> dict[str, Any]: | ||
| """Get client parameters with Novita AI defaults.""" | ||
| if self.api_key is None: | ||
| self.api_key = os.getenv("NOVITA_API_KEY") | ||
| if self.api_key is None: | ||
| raise ValueError( | ||
| "Novita AI API key is required. " | ||
| "Set the NOVITA_API_KEY environment variable or pass api_key." | ||
| ) | ||
|
|
||
| base_params = { | ||
| "api_key": self.api_key, | ||
| "base_url": self.base_url or NOVITA_BASE_URL, | ||
| "timeout": self.timeout, | ||
| "max_retries": self.max_retries, | ||
| "default_headers": self.default_headers, | ||
| "default_query": self.default_query, | ||
| } | ||
|
|
||
| client_params = {k: v for k, v in base_params.items() if v is not None} | ||
|
|
||
| if self.client_params: | ||
| client_params.update(self.client_params) | ||
|
|
||
| return client_params | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Novita models get wrong context window size
Medium Severity
NovitaCompletioninheritsget_context_window_size()fromOpenAICompletion, which only checks OpenAI-specific model prefixes (e.g.gpt-4,o1). Since Novita models likemoonshotai/kimi-k2.5don't match any OpenAI prefix, the method returns the default ~6,963 tokens instead of the intended ~111,411 tokens (131072 × 0.85). The entries added toLLM_CONTEXT_WINDOW_SIZESare dead code here — that dict is only consumed by theLLM(litellm) class's override, not by native provider classes. Every other native provider (Anthropic, Gemini, Bedrock) overridesget_context_window_size()with its own model-specific windows;NovitaCompletionneeds the same.Additional Locations (1)
lib/crewai/src/crewai/llm.py#L314-L318