mirror of
https://github.com/langgenius/dify.git
synced 2026-05-03 08:58:09 +08:00
Merge main
This commit is contained in:
@ -17,14 +17,15 @@ from models.model import App, AppMode, EndUser
|
||||
class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
@classmethod
|
||||
def invoke_app(
|
||||
cls, app_id: str,
|
||||
user_id: str,
|
||||
cls,
|
||||
app_id: str,
|
||||
user_id: str,
|
||||
tenant_id: str,
|
||||
conversation_id: Optional[str],
|
||||
query: Optional[str],
|
||||
stream: bool,
|
||||
inputs: Mapping,
|
||||
files: list[dict],
|
||||
files: list[dict],
|
||||
) -> Generator[dict | str, None, None] | dict:
|
||||
"""
|
||||
invoke app
|
||||
@ -37,28 +38,28 @@ class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
|
||||
conversation_id = conversation_id or ""
|
||||
|
||||
if app.mode in [AppMode.ADVANCED_CHAT.value, AppMode.AGENT_CHAT.value, AppMode.CHAT.value]:
|
||||
if app.mode in {AppMode.ADVANCED_CHAT.value, AppMode.AGENT_CHAT.value, AppMode.CHAT.value}:
|
||||
if not query:
|
||||
raise ValueError("missing query")
|
||||
|
||||
|
||||
return cls.invoke_chat_app(app, user, conversation_id, query, stream, inputs, files)
|
||||
elif app.mode in [AppMode.WORKFLOW.value]:
|
||||
elif app.mode == AppMode.WORKFLOW.value:
|
||||
return cls.invoke_workflow_app(app, user, stream, inputs, files)
|
||||
elif app.mode in [AppMode.COMPLETION]:
|
||||
elif app.mode == AppMode.COMPLETION:
|
||||
return cls.invoke_completion_app(app, user, stream, inputs, files)
|
||||
|
||||
raise ValueError("unexpected app type")
|
||||
|
||||
@classmethod
|
||||
def invoke_chat_app(
|
||||
cls,
|
||||
cls,
|
||||
app: App,
|
||||
user: Account | EndUser,
|
||||
user: Account | EndUser,
|
||||
conversation_id: str,
|
||||
query: str,
|
||||
stream: bool,
|
||||
inputs: Mapping,
|
||||
files: list[dict],
|
||||
files: list[dict],
|
||||
) -> Generator[dict | str, None, None] | dict:
|
||||
"""
|
||||
invoke chat app
|
||||
@ -67,11 +68,11 @@ class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
workflow = app.workflow
|
||||
if not workflow:
|
||||
raise ValueError("unexpected app type")
|
||||
|
||||
|
||||
return AdvancedChatAppGenerator().generate(
|
||||
app_model=app,
|
||||
workflow=workflow,
|
||||
user=user,
|
||||
app_model=app,
|
||||
workflow=workflow,
|
||||
user=user,
|
||||
args={
|
||||
"inputs": inputs,
|
||||
"query": query,
|
||||
@ -79,12 +80,12 @@ class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
"conversation_id": conversation_id,
|
||||
},
|
||||
invoke_from=InvokeFrom.SERVICE_API,
|
||||
stream=stream
|
||||
stream=stream,
|
||||
)
|
||||
elif app.mode == AppMode.AGENT_CHAT.value:
|
||||
return AgentChatAppGenerator().generate(
|
||||
app_model=app,
|
||||
user=user,
|
||||
app_model=app,
|
||||
user=user,
|
||||
args={
|
||||
"inputs": inputs,
|
||||
"query": query,
|
||||
@ -92,12 +93,12 @@ class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
"conversation_id": conversation_id,
|
||||
},
|
||||
invoke_from=InvokeFrom.SERVICE_API,
|
||||
stream=stream
|
||||
stream=stream,
|
||||
)
|
||||
elif app.mode == AppMode.CHAT.value:
|
||||
return ChatAppGenerator().generate(
|
||||
app_model=app,
|
||||
user=user,
|
||||
app_model=app,
|
||||
user=user,
|
||||
args={
|
||||
"inputs": inputs,
|
||||
"query": query,
|
||||
@ -105,19 +106,19 @@ class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
"conversation_id": conversation_id,
|
||||
},
|
||||
invoke_from=InvokeFrom.SERVICE_API,
|
||||
stream=stream
|
||||
stream=stream,
|
||||
)
|
||||
else:
|
||||
raise ValueError("unexpected app type")
|
||||
|
||||
@classmethod
|
||||
def invoke_workflow_app(
|
||||
cls,
|
||||
cls,
|
||||
app: App,
|
||||
user: EndUser | Account,
|
||||
user: EndUser | Account,
|
||||
stream: bool,
|
||||
inputs: Mapping,
|
||||
files: list[dict],
|
||||
files: list[dict],
|
||||
):
|
||||
"""
|
||||
invoke workflow app
|
||||
@ -127,13 +128,10 @@ class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
raise ValueError("")
|
||||
|
||||
return WorkflowAppGenerator().generate(
|
||||
app_model=app,
|
||||
workflow=workflow,
|
||||
user=user,
|
||||
args={
|
||||
'inputs': inputs,
|
||||
'files': files
|
||||
},
|
||||
app_model=app,
|
||||
workflow=workflow,
|
||||
user=user,
|
||||
args={"inputs": inputs, "files": files},
|
||||
invoke_from=InvokeFrom.SERVICE_API,
|
||||
stream=stream,
|
||||
call_depth=1,
|
||||
@ -141,23 +139,20 @@ class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
|
||||
@classmethod
|
||||
def invoke_completion_app(
|
||||
cls,
|
||||
cls,
|
||||
app: App,
|
||||
user: EndUser | Account,
|
||||
user: EndUser | Account,
|
||||
stream: bool,
|
||||
inputs: Mapping,
|
||||
files: list[dict],
|
||||
files: list[dict],
|
||||
):
|
||||
"""
|
||||
invoke completion app
|
||||
"""
|
||||
return CompletionAppGenerator().generate(
|
||||
app_model=app,
|
||||
user=user,
|
||||
args={
|
||||
'inputs': inputs,
|
||||
'files': files
|
||||
},
|
||||
app_model=app,
|
||||
user=user,
|
||||
args={"inputs": inputs, "files": files},
|
||||
invoke_from=InvokeFrom.SERVICE_API,
|
||||
stream=stream,
|
||||
)
|
||||
@ -173,24 +168,21 @@ class PluginAppBackwardsInvocation(BaseBackwardsInvocation):
|
||||
user = db.session.query(Account).filter(Account.id == user_id).first()
|
||||
|
||||
if not user:
|
||||
raise ValueError('user not found')
|
||||
raise ValueError("user not found")
|
||||
|
||||
return user
|
||||
|
||||
|
||||
@classmethod
|
||||
def _get_app(cls, app_id: str, tenant_id: str) -> App:
|
||||
"""
|
||||
get app
|
||||
"""
|
||||
try:
|
||||
app = db.session.query(App). \
|
||||
filter(App.id == app_id). \
|
||||
filter(App.tenant_id == tenant_id). \
|
||||
first()
|
||||
app = db.session.query(App).filter(App.id == app_id).filter(App.tenant_id == tenant_id).first()
|
||||
except Exception:
|
||||
raise ValueError("app not found")
|
||||
|
||||
|
||||
if not app:
|
||||
raise ValueError("app not found")
|
||||
|
||||
return app
|
||||
|
||||
return app
|
||||
|
||||
@ -32,6 +32,7 @@ class RequestInvokeLLM(BaseRequestInvokeModel):
|
||||
"""
|
||||
Request to invoke LLM
|
||||
"""
|
||||
|
||||
model_type: ModelType = ModelType.LLM
|
||||
mode: str
|
||||
model_parameters: dict[str, Any] = Field(default_factory=dict)
|
||||
@ -40,19 +41,19 @@ class RequestInvokeLLM(BaseRequestInvokeModel):
|
||||
stop: Optional[list[str]] = Field(default_factory=list)
|
||||
stream: Optional[bool] = False
|
||||
|
||||
@field_validator('prompt_messages', mode='before')
|
||||
def convert_prompt_messages(cls, v):
|
||||
@field_validator("prompt_messages", mode="before")
|
||||
def convert_prompt_messages(self, v):
|
||||
if not isinstance(v, list):
|
||||
raise ValueError('prompt_messages must be a list')
|
||||
raise ValueError("prompt_messages must be a list")
|
||||
|
||||
for i in range(len(v)):
|
||||
if v[i]['role'] == PromptMessageRole.USER.value:
|
||||
if v[i]["role"] == PromptMessageRole.USER.value:
|
||||
v[i] = UserPromptMessage(**v[i])
|
||||
elif v[i]['role'] == PromptMessageRole.ASSISTANT.value:
|
||||
elif v[i]["role"] == PromptMessageRole.ASSISTANT.value:
|
||||
v[i] = AssistantPromptMessage(**v[i])
|
||||
elif v[i]['role'] == PromptMessageRole.SYSTEM.value:
|
||||
elif v[i]["role"] == PromptMessageRole.SYSTEM.value:
|
||||
v[i] = SystemPromptMessage(**v[i])
|
||||
elif v[i]['role'] == PromptMessageRole.TOOL.value:
|
||||
elif v[i]["role"] == PromptMessageRole.TOOL.value:
|
||||
v[i] = ToolPromptMessage(**v[i])
|
||||
else:
|
||||
v[i] = PromptMessage(**v[i])
|
||||
@ -95,10 +96,12 @@ class RequestInvokeNode(BaseModel):
|
||||
Request to invoke node
|
||||
"""
|
||||
|
||||
|
||||
class RequestInvokeApp(BaseModel):
|
||||
"""
|
||||
Request to invoke app
|
||||
"""
|
||||
|
||||
app_id: str
|
||||
inputs: dict[str, Any]
|
||||
query: Optional[str] = None
|
||||
@ -107,12 +110,14 @@ class RequestInvokeApp(BaseModel):
|
||||
user: Optional[str] = None
|
||||
files: list[dict] = Field(default_factory=list)
|
||||
|
||||
|
||||
class RequestInvokeEncrypt(BaseModel):
|
||||
"""
|
||||
Request to encryption
|
||||
"""
|
||||
|
||||
opt: Literal["encrypt", "decrypt"]
|
||||
namespace: Literal["endpoint"]
|
||||
identity: str
|
||||
data: dict = Field(default_factory=dict)
|
||||
config: Mapping[str, BasicProviderConfig] = Field(default_factory=Mapping)
|
||||
config: Mapping[str, BasicProviderConfig] = Field(default_factory=Mapping)
|
||||
|
||||
Reference in New Issue
Block a user