feat: add lazy id generation for providers (#543)

This commit is contained in:
cthomas
2025-01-10 11:04:24 -08:00
committed by GitHub
parent 995539d047
commit 7ffaeaa3be
2 changed files with 8 additions and 1 deletions

View File

@@ -16,10 +16,15 @@ class ProviderBase(LettaBase):
class Provider(ProviderBase):
id: Optional[str] = Field(None, description="The id of the provider, lazily created by the database manager.")
name: str = Field(..., description="The name of the provider")
api_key: Optional[str] = Field(None, description="API key used for requests to the provider.")
organization_id: Optional[str] = Field(OrganizationManager.DEFAULT_ORG_ID, description="The organization id of the user")
def resolve_identifier(self):
if not self.id:
self.id = ProviderBase._generate_id(prefix=ProviderBase.__id_prefix__)
def list_llm_models(self) -> List[LLMConfig]:
return []
@@ -40,7 +45,7 @@ class Provider(ProviderBase):
class ProviderCreate(ProviderBase):
name: str = Field(..., description="The name of the provider.")
api_key: str = Field(..., description="API key used for requests to the provider.")
organization_id: str = Field(..., description="The organization id that this provider information pertains to.")
organization_id: Optional[str] = Field(OrganizationManager.DEFAULT_ORG_ID, description="The organization id of the user")
class ProviderUpdate(ProviderBase):

View File

@@ -17,6 +17,8 @@ class ProviderManager:
def create_provider(self, provider: PydanticProvider) -> PydanticProvider:
"""Create a new provider if it doesn't already exist."""
with self.session_maker() as session:
# Lazily create the provider id prior to persistence
provider.resolve_identifier()
new_provider = ProviderModel(**provider.model_dump())
new_provider.create(session)
return new_provider.to_pydantic()