mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-10 22:41:41 +00:00
Implement CompactifAI chat completion provider
- Create CompactifAIChatConfig extending OpenAIGPTConfig for compatibility - Handle authentication via COMPACTIFAI_API_KEY environment variable - Set default API base to https://api.compactif.ai/v1 - Support OpenAI-compatible request/response transformation - Implement JSON mode handling for tool calls - Add proper model name prefixing with 'compactifai/' provider - Leverage existing OpenAI infrastructure for minimal code complexity
This commit is contained in:
parent
e1329b03c6
commit
6925c113af
3 changed files with 87 additions and 0 deletions
1
litellm/llms/compactifai/__init__.py
Normal file
1
litellm/llms/compactifai/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# CompactifAI provider for LiteLLM
|
||||
1
litellm/llms/compactifai/chat/__init__.py
Normal file
1
litellm/llms/compactifai/chat/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
# CompactifAI chat completions
|
||||
85
litellm/llms/compactifai/chat/transformation.py
Normal file
85
litellm/llms/compactifai/chat/transformation.py
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
"""
|
||||
CompactifAI chat completion transformation
|
||||
"""
|
||||
|
||||
from typing import TYPE_CHECKING, Any, List, Optional, Tuple
|
||||
|
||||
import httpx
|
||||
|
||||
from litellm.secret_managers.main import get_secret_str
|
||||
from litellm.types.utils import ModelResponse
|
||||
|
||||
from ...openai.chat.gpt_transformation import OpenAIGPTConfig
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
|
||||
|
||||
LiteLLMLoggingObj = _LiteLLMLoggingObj
|
||||
else:
|
||||
LiteLLMLoggingObj = Any
|
||||
|
||||
|
||||
class CompactifAIChatConfig(OpenAIGPTConfig):
|
||||
"""
|
||||
Configuration class for CompactifAI chat completions.
|
||||
Since CompactifAI is OpenAI-compatible, we extend OpenAIGPTConfig.
|
||||
"""
|
||||
|
||||
def _get_openai_compatible_provider_info(
|
||||
self,
|
||||
api_base: Optional[str],
|
||||
api_key: Optional[str],
|
||||
) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""
|
||||
Get API base and key for CompactifAI provider.
|
||||
"""
|
||||
api_base = api_base or "https://api.compactif.ai/v1"
|
||||
dynamic_api_key = api_key or get_secret_str("COMPACTIFAI_API_KEY") or ""
|
||||
return api_base, dynamic_api_key
|
||||
|
||||
def transform_response(
|
||||
self,
|
||||
model: str,
|
||||
raw_response: httpx.Response,
|
||||
model_response: ModelResponse,
|
||||
logging_obj: LiteLLMLoggingObj,
|
||||
request_data: dict,
|
||||
messages: List,
|
||||
optional_params: dict,
|
||||
litellm_params: dict,
|
||||
encoding: Any,
|
||||
api_key: Optional[str] = None,
|
||||
json_mode: Optional[bool] = None,
|
||||
) -> ModelResponse:
|
||||
"""
|
||||
Transform CompactifAI response to LiteLLM format.
|
||||
Since CompactifAI is OpenAI-compatible, we can use the standard OpenAI transformation.
|
||||
"""
|
||||
## LOGGING
|
||||
logging_obj.post_call(
|
||||
input=messages,
|
||||
api_key=api_key,
|
||||
original_response=raw_response.text,
|
||||
additional_args={"complete_input_dict": request_data},
|
||||
)
|
||||
|
||||
## RESPONSE OBJECT
|
||||
response_json = raw_response.json()
|
||||
|
||||
# Handle JSON mode if needed
|
||||
if json_mode:
|
||||
for choice in response_json["choices"]:
|
||||
message = choice.get("message")
|
||||
if message and message.get("tool_calls"):
|
||||
# Convert tool calls to content for JSON mode
|
||||
tool_calls = message.get("tool_calls", [])
|
||||
if len(tool_calls) == 1:
|
||||
message["content"] = tool_calls[0]["function"].get("arguments", "")
|
||||
message["tool_calls"] = None
|
||||
|
||||
returned_response = ModelResponse(**response_json)
|
||||
|
||||
# Set model name with provider prefix
|
||||
returned_response.model = f"compactifai/{model}"
|
||||
|
||||
return returned_response
|
||||
Loading…
Add table
Reference in a new issue