mirror of
https://github.com/langgenius/dify.git
synced 2026-11-14 07:11:06 +08:00
Merge branch 'main' into feat/dify-builder
This commit is contained in:
4715 files changed
+573354
-179240
No files matched your search
@@ -34,6 +34,7 @@ from graphon.model_runtime.model_providers.base.large_language_model import norm
|
||||
from graphon.model_runtime.protocols.runtime import ModelRuntime
|
||||
from graphon.model_runtime.protocols.tts_runtime import TTSChunk, TTSModelVoice
|
||||
from models.provider_ids import ModelProviderID
|
||||
from services.plugin.dispatch_identity import PluginDispatchIdentity
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -108,6 +109,8 @@ class PluginModelRuntime(ModelRuntime):
|
||||
|
||||
Provider discovery goes through ``PluginService`` so the plugin lifecycle
|
||||
methods and provider reads share one tenant-scoped cache owner.
|
||||
Dispatch reuses package identities bound to prepared declarations rather
|
||||
than discovering a new version after credentials have been resolved.
|
||||
"""
|
||||
|
||||
tenant_id: str
|
||||
@@ -130,10 +133,23 @@ class PluginModelRuntime(ModelRuntime):
|
||||
self.user_id = user_id
|
||||
self.client = client
|
||||
self._plugin_service = plugin_service
|
||||
self._provider_identifiers: dict[str, str] = {}
|
||||
|
||||
@override
|
||||
def fetch_model_providers(self) -> Sequence[PluginModelProviderDeclaration]:
|
||||
return self._plugin_service.fetch_plugin_model_providers(tenant_id=self.tenant_id, client=self.client)
|
||||
providers = self._plugin_service.fetch_plugin_model_providers(tenant_id=self.tenant_id, client=self.client)
|
||||
for provider in providers:
|
||||
self.bind_provider_schema(provider)
|
||||
return providers
|
||||
|
||||
def bind_provider_schema(self, provider: ProviderEntity) -> None:
|
||||
"""Retain the package identity associated with a prepared declaration."""
|
||||
if isinstance(provider, PluginModelProviderDeclaration):
|
||||
self._provider_identifiers[provider.provider] = provider.plugin_unique_identifier
|
||||
|
||||
def _dispatch_identity(self, provider: str) -> PluginDispatchIdentity:
|
||||
identifier = self._provider_identifiers.get(str(ModelProviderID(provider)))
|
||||
return {"plugin_unique_identifier": identifier} if identifier is not None else {}
|
||||
|
||||
@override
|
||||
def get_provider_icon(self, *, provider: str, icon_type: str, lang: str) -> tuple[bytes, str]:
|
||||
@@ -185,6 +201,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
credentials=credentials,
|
||||
)
|
||||
@@ -203,6 +220,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model_type=model_type.value,
|
||||
model=model,
|
||||
@@ -259,6 +277,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model_type=model_type.value,
|
||||
model=model,
|
||||
@@ -331,6 +350,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -345,6 +365,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -451,6 +472,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model_type=model_type.value,
|
||||
model=model,
|
||||
@@ -477,6 +499,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -501,6 +524,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -524,6 +548,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -548,6 +573,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -569,6 +595,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -594,6 +621,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -622,6 +650,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -648,6 +677,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -671,6 +701,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -693,6 +724,7 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
@@ -715,13 +747,14 @@ class PluginModelRuntime(ModelRuntime):
|
||||
tenant_id=self.tenant_id,
|
||||
user_id=self.user_id,
|
||||
plugin_id=plugin_id,
|
||||
**self._dispatch_identity(provider),
|
||||
provider=provider_name,
|
||||
model=model,
|
||||
credentials=credentials,
|
||||
text=text,
|
||||
)
|
||||
|
||||
def _get_provider_schema(self, provider: str) -> ProviderEntity:
|
||||
def _get_provider_schema(self, provider: str) -> PluginModelProviderDeclaration:
|
||||
providers = self.fetch_model_providers()
|
||||
provider_entity = next((item for item in providers if item.provider == provider), None)
|
||||
if provider_entity is None:
|
||||
@@ -742,6 +775,9 @@ class PluginModelRuntime(ModelRuntime):
|
||||
# caller id, so the cache must only collapse ``None`` into tenant scope.
|
||||
cache_user_id = TENANT_SCOPE_SCHEMA_CACHE_USER_ID if self.user_id is None else self.user_id
|
||||
cache_key = f"{self.tenant_id}:{provider}:{model_type.value}:{model}:{cache_user_id}"
|
||||
identifier = self._provider_identifiers.get(str(ModelProviderID(provider)))
|
||||
if identifier is not None:
|
||||
cache_key = f"{cache_key}:plugin:{identifier}"
|
||||
sorted_credentials = sorted(credentials.items()) if credentials else []
|
||||
if not sorted_credentials:
|
||||
return cache_key
|
||||
|
||||
Reference in new issue
Block a user