|
| 1 | +""" |
| 2 | +GitHub Copilot Embedding API Configuration. |
| 3 | +
|
| 4 | +This module provides the configuration for GitHub Copilot's Embedding API. |
| 5 | +
|
| 6 | +Implementation based on analysis of the copilot-api project by caozhiyuan: |
| 7 | +https://github.com/caozhiyuan/copilot-api |
| 8 | +""" |
| 9 | +from typing import TYPE_CHECKING, Any, Optional |
| 10 | + |
| 11 | +import httpx |
| 12 | + |
| 13 | +from litellm._logging import verbose_logger |
| 14 | +from litellm.exceptions import AuthenticationError |
| 15 | +from litellm.llms.base_llm.embedding.transformation import BaseEmbeddingConfig |
| 16 | +from litellm.types.llms.openai import AllEmbeddingInputValues |
| 17 | +from litellm.types.utils import EmbeddingResponse |
| 18 | +from litellm.utils import convert_to_model_response_object |
| 19 | + |
| 20 | +from ..authenticator import Authenticator |
| 21 | +from ..common_utils import ( |
| 22 | + GetAPIKeyError, |
| 23 | + GITHUB_COPILOT_API_BASE, |
| 24 | + get_copilot_default_headers, |
| 25 | +) |
| 26 | + |
| 27 | +if TYPE_CHECKING: |
| 28 | + from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj |
| 29 | + |
| 30 | + LiteLLMLoggingObj = _LiteLLMLoggingObj |
| 31 | +else: |
| 32 | + LiteLLMLoggingObj = Any |
| 33 | + |
| 34 | + |
| 35 | +class GithubCopilotEmbeddingConfig(BaseEmbeddingConfig): |
| 36 | + """ |
| 37 | + Configuration for GitHub Copilot's Embedding API. |
| 38 | +
|
| 39 | + Reference: https://api.githubcopilot.com/embeddings |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__(self) -> None: |
| 43 | + super().__init__() |
| 44 | + self.authenticator = Authenticator() |
| 45 | + |
| 46 | + def validate_environment( |
| 47 | + self, |
| 48 | + headers: dict, |
| 49 | + model: str, |
| 50 | + messages: list, |
| 51 | + optional_params: dict, |
| 52 | + litellm_params: dict, |
| 53 | + api_key: Optional[str] = None, |
| 54 | + api_base: Optional[str] = None, |
| 55 | + ) -> dict: |
| 56 | + """ |
| 57 | + Validate environment and set up headers for GitHub Copilot API. |
| 58 | + """ |
| 59 | + try: |
| 60 | + # Get GitHub Copilot API key via OAuth |
| 61 | + api_key = self.authenticator.get_api_key() |
| 62 | + |
| 63 | + if not api_key: |
| 64 | + raise AuthenticationError( |
| 65 | + model=model, |
| 66 | + llm_provider="github_copilot", |
| 67 | + message="GitHub Copilot API key is required. Please authenticate via OAuth Device Flow.", |
| 68 | + ) |
| 69 | + |
| 70 | + # Get default headers |
| 71 | + default_headers = get_copilot_default_headers(api_key) |
| 72 | + |
| 73 | + # Merge with existing headers (user's extra_headers take priority) |
| 74 | + merged_headers = {**default_headers, **headers} |
| 75 | + |
| 76 | + verbose_logger.debug( |
| 77 | + f"GitHub Copilot Embedding API: Successfully configured headers for model {model}" |
| 78 | + ) |
| 79 | + |
| 80 | + return merged_headers |
| 81 | + |
| 82 | + except GetAPIKeyError as e: |
| 83 | + raise AuthenticationError( |
| 84 | + model=model, |
| 85 | + llm_provider="github_copilot", |
| 86 | + message=str(e), |
| 87 | + ) |
| 88 | + |
| 89 | + def get_complete_url( |
| 90 | + self, |
| 91 | + api_base: Optional[str], |
| 92 | + api_key: Optional[str], |
| 93 | + model: str, |
| 94 | + optional_params: dict, |
| 95 | + litellm_params: dict, |
| 96 | + stream: Optional[bool] = None, |
| 97 | + ) -> str: |
| 98 | + """ |
| 99 | + Get the complete URL for GitHub Copilot Embedding API endpoint. |
| 100 | + """ |
| 101 | + # Use provided api_base or fall back to authenticator's base or default |
| 102 | + api_base = ( |
| 103 | + self.authenticator.get_api_base() |
| 104 | + or api_base |
| 105 | + or GITHUB_COPILOT_API_BASE |
| 106 | + ) |
| 107 | + |
| 108 | + # Remove trailing slashes |
| 109 | + api_base = api_base.rstrip("/") |
| 110 | + |
| 111 | + # Return the embeddings endpoint |
| 112 | + return f"{api_base}/embeddings" |
| 113 | + |
| 114 | + def transform_embedding_request( |
| 115 | + self, |
| 116 | + model: str, |
| 117 | + input: AllEmbeddingInputValues, |
| 118 | + optional_params: dict, |
| 119 | + headers: dict, |
| 120 | + ) -> dict: |
| 121 | + """ |
| 122 | + Transform embedding request to GitHub Copilot format. |
| 123 | + """ |
| 124 | + |
| 125 | + # Ensure input is a list |
| 126 | + if isinstance(input, str): |
| 127 | + input = [input] |
| 128 | + |
| 129 | + return { |
| 130 | + "model": model, |
| 131 | + "input": input, |
| 132 | + **optional_params, |
| 133 | + } |
| 134 | + |
| 135 | + def transform_embedding_response( |
| 136 | + self, |
| 137 | + model: str, |
| 138 | + raw_response: httpx.Response, |
| 139 | + model_response: EmbeddingResponse, |
| 140 | + logging_obj: LiteLLMLoggingObj, |
| 141 | + api_key: Optional[str], |
| 142 | + request_data: dict, |
| 143 | + optional_params: dict, |
| 144 | + litellm_params: dict, |
| 145 | + ) -> EmbeddingResponse: |
| 146 | + """ |
| 147 | + Transform embedding response from GitHub Copilot format. |
| 148 | + """ |
| 149 | + logging_obj.post_call(original_response=raw_response.text) |
| 150 | + |
| 151 | + # GitHub Copilot returns standard OpenAI-compatible embedding response |
| 152 | + response_json = raw_response.json() |
| 153 | + |
| 154 | + return convert_to_model_response_object( |
| 155 | + response_object=response_json, |
| 156 | + model_response_object=model_response, |
| 157 | + response_type="embedding", |
| 158 | + ) |
| 159 | + |
| 160 | + def get_supported_openai_params(self, model: str) -> list: |
| 161 | + return [ |
| 162 | + "timeout", |
| 163 | + "dimensions", |
| 164 | + "encoding_format", |
| 165 | + "user", |
| 166 | + ] |
| 167 | + |
| 168 | + def map_openai_params( |
| 169 | + self, |
| 170 | + non_default_params: dict, |
| 171 | + optional_params: dict, |
| 172 | + model: str, |
| 173 | + drop_params: bool, |
| 174 | + ) -> dict: |
| 175 | + for param, value in non_default_params.items(): |
| 176 | + if param in self.get_supported_openai_params(model): |
| 177 | + optional_params[param] = value |
| 178 | + return optional_params |
| 179 | + |
| 180 | + def get_error_class( |
| 181 | + self, error_message: str, status_code: int, headers: Any |
| 182 | + ) -> Any: |
| 183 | + from litellm.llms.openai.openai import OpenAIConfig |
| 184 | + |
| 185 | + return OpenAIConfig().get_error_class( |
| 186 | + error_message=error_message, status_code=status_code, headers=headers |
| 187 | + ) |
| 188 | + |
0 commit comments