-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
233 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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,27 @@ | ||
from enum import Enum | ||
from typing import Optional | ||
|
||
import pydantic | ||
|
||
|
||
class MuxMatcherType(str, Enum): | ||
""" | ||
Represents the different types of matchers we support. | ||
""" | ||
|
||
# Always match this prompt | ||
catch_all = "catch_all" | ||
|
||
|
||
class MuxRule(pydantic.BaseModel): | ||
""" | ||
Represents a mux rule for a provider. | ||
""" | ||
|
||
provider_id: str | ||
model: str | ||
# The type of matcher to use | ||
matcher_type: MuxMatcherType | ||
# The actual matcher to use. Note that | ||
# this depends on the matcher type. | ||
matcher: Optional[str] = None |
This file contains 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 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 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 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,3 @@ | ||
from codegate.providers.openai.provider import OpenAIProvider | ||
|
||
__all__ = ["OpenAIProvider"] |
This file contains 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,47 @@ | ||
import json | ||
|
||
from fastapi import Header, HTTPException, Request | ||
|
||
from codegate.clients.detector import DetectClient | ||
from codegate.pipeline.factory import PipelineFactory | ||
from codegate.providers.openai import OpenAIProvider | ||
|
||
|
||
class OpenRouterProvider(OpenAIProvider): | ||
def __init__(self, pipeline_factory: PipelineFactory): | ||
super().__init__(pipeline_factory) | ||
|
||
@property | ||
def provider_route_name(self) -> str: | ||
return "openrouter" | ||
|
||
def _setup_routes(self): | ||
@self.router.post(f"/{self.provider_route_name}/api/v1/chat/completions") | ||
@self.router.post(f"/{self.provider_route_name}/chat/completions") | ||
@DetectClient() | ||
async def create_completion( | ||
request: Request, | ||
authorization: str = Header(..., description="Bearer token"), | ||
): | ||
if not authorization.startswith("Bearer "): | ||
raise HTTPException(status_code=401, detail="Invalid authorization header") | ||
|
||
api_key = authorization.split(" ")[1] | ||
body = await request.body() | ||
data = json.loads(body) | ||
|
||
base_url = self._get_base_url() | ||
data["base_url"] = base_url | ||
|
||
# litellm workaround - add openrouter/ prefix to model name to make it openai-compatible | ||
# once we get rid of litellm, this can simply be removed | ||
original_model = data.get("model", "") | ||
if not original_model.startswith("openrouter/"): | ||
data["model"] = f"openrouter/{original_model}" | ||
|
||
return await self.process_request( | ||
data, | ||
api_key, | ||
request.url.path, | ||
request.state.detected_client, | ||
) |
This file contains 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 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
Oops, something went wrong.