Skip to content

Enable provider model updating when updating provider itself #1014

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

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions src/codegate/providers/crud/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,32 @@ async def update_endpoint(

dbendpoint = await self._db_writer.update_provider_endpoint(endpoint.to_db_model())

# If the auth type has not changed or no authentication is needed,
# we can update the models
if (
founddbe.auth_type == endpoint.auth_type
or endpoint.auth_type == apimodelsv1.ProviderAuthType.none
):
try:
authm = await self._db_reader.get_auth_material_by_provider_id(str(endpoint.id))

models = await self._find_models_for_provider(
endpoint, authm.auth_type, authm.auth_blob, prov
)

await self._update_models_for_provider(dbendpoint, endpoint, prov, models)

# a model might have been deleted, let's repopulate the cache
await self._ws_crud.repopulate_mux_cache()
except Exception as err:
# This is a non-fatal error. The endpoint might have changed
# And the user will need to push a new API key anyway.
logger.error(
"Unable to update models for provider",
provider=endpoint.name,
err=str(err),
)

return apimodelsv1.ProviderEndpoint.from_db_model(dbendpoint)

async def configure_auth_material(
Expand All @@ -164,12 +190,9 @@ async def configure_auth_material(
provider_registry = get_provider_registry()
prov = endpoint.get_from_registry(provider_registry)

models = []
if config.auth_type != apimodelsv1.ProviderAuthType.passthrough:
try:
models = prov.models(endpoint=endpoint.endpoint, api_key=config.api_key)
except Exception as err:
raise ProviderModelsNotFoundError(f"Unable to get models from provider: {err}")
models = await self._find_models_for_provider(
endpoint, config.auth_type, config.api_key, prov
)

await self._db_writer.push_provider_auth_material(
dbmodels.ProviderAuthMaterial(
Expand All @@ -179,7 +202,32 @@ async def configure_auth_material(
)
)

models_set = set(models)
await self._update_models_for_provider(dbendpoint, endpoint, models)

# a model might have been deleted, let's repopulate the cache
await self._ws_crud.repopulate_mux_cache()

async def _find_models_for_provider(
self,
endpoint: apimodelsv1.ProviderEndpoint,
auth_type: apimodelsv1.ProviderAuthType,
api_key: str,
prov: BaseProvider,
) -> List[str]:
if auth_type != apimodelsv1.ProviderAuthType.passthrough:
try:
return prov.models(endpoint=endpoint.endpoint, api_key=api_key)
except Exception as err:
raise ProviderModelsNotFoundError(f"Unable to get models from provider: {err}")
return []

async def _update_models_for_provider(
self,
dbendpoint: dbmodels.ProviderEndpoint,
endpoint: apimodelsv1.ProviderEndpoint,
found_models: List[str],
) -> None:
models_set = set(found_models)

# Get the models from the provider
models_in_db = await self._db_reader.get_provider_models_by_provider_id(str(endpoint.id))
Expand All @@ -202,9 +250,6 @@ async def configure_auth_material(
model,
)

# a model might have been deleted, let's repopulate the cache
await self._ws_crud.repopulate_mux_cache()

async def delete_endpoint(self, provider_id: UUID):
"""Delete an endpoint."""

Expand Down
2 changes: 1 addition & 1 deletion src/codegate/providers/ollama/provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import List, Optional
from typing import List

import httpx
import structlog
Expand Down