This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Kick off provider endpoint CRUD structure #775
Closed
Closed
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
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,3 @@ | ||
| from .crud import ProviderCrud | ||
|
|
||
| __all__ = ["ProviderCrud"] |
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,78 @@ | ||
| from typing import List, Optional | ||
| from urllib.parse import urlparse | ||
|
|
||
| import structlog | ||
| from pydantic import ValidationError | ||
|
|
||
| from codegate.api import v1_models as apimodelsv1 | ||
| from codegate.config import Config | ||
| from codegate.db.connection import DbReader, DbRecorder | ||
|
|
||
| logger = structlog.get_logger("codegate") | ||
|
|
||
|
|
||
| class ProviderCrud: | ||
| """The CRUD operations for the provider endpoint references within | ||
| Codegate. | ||
|
|
||
| This is meant to handle all the transformations in between the | ||
| database and the API, as well as other sources of information. All | ||
| operations should result in the API models being returned. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self._db_reader = DbReader() | ||
| self._db_writer = DbRecorder() | ||
| config = Config.get_config() | ||
| if config is None: | ||
| logger.warning("OZZ: No configuration found.") | ||
| provided_urls = {} | ||
| else: | ||
| logger.info("OZZ: Using configuration for provider URLs.") | ||
| provided_urls = config.provider_urls | ||
|
|
||
| self._provider_urls = provided_urls | ||
|
|
||
| def list_endpoints(self) -> List[apimodelsv1.ProviderEndpoint]: | ||
| """List all the endpoints.""" | ||
|
|
||
| endpoints = [] | ||
| for provider_name, provider_url in self._provider_urls.items(): | ||
| provend = self.__provider_endpoint_from_cfg(provider_name, provider_url) | ||
| if provend is not None: | ||
| endpoints.append(provend) | ||
|
|
||
| return endpoints | ||
|
|
||
| def __provider_endpoint_from_cfg( | ||
| self, provider_name: str, provider_url: str | ||
| ) -> Optional[apimodelsv1.ProviderEndpoint]: | ||
| """Create a provider endpoint from the config entry.""" | ||
|
|
||
| try: | ||
| _ = urlparse(provider_url) | ||
| except Exception: | ||
| logger.warning( | ||
| "Invalid provider URL", provider_name=provider_name, provider_url=provider_url | ||
| ) | ||
| return None | ||
|
|
||
| try: | ||
| return apimodelsv1.ProviderEndpoint( | ||
| name=provider_name, | ||
| endpoint=provider_url, | ||
| descrption=("Endpoint for the {} provided via the CodeGate configuration.").format( | ||
| provider_name | ||
| ), | ||
| provider_type=provider_name, | ||
| auth_type=apimodelsv1.ProviderAuthType.none, | ||
| source=apimodelsv1.ProviderEndpointSource.config, | ||
| ) | ||
| except ValidationError as err: | ||
| logger.warning( | ||
| "Invalid provider name", | ||
| provider_name=provider_name, | ||
| provider_url=provider_url, | ||
| err=str(err), | ||
| ) | ||
| return None |
Oops, something went wrong.
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.
Probably for a future PR but I think it's worth it to persist the provider information from config in DB. That way we can have a single source of truth
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.
this is an excellent point for discussion. Are we planning to keep the provider configs? what do we do when a user changes the API endpoint from the config?
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.
I think from now on for muxing we should keep the provider configs
I don't know how common it is to change the API endpoint of an LLM provider. Right now, a user would need to stop CodeGate and start it again with new config in order to change the API endpoint.
Keeping the provider configs in DB and be able to change them without stopping the server gives a better UX IMO