Skip to content

Commit

Permalink
Feat: conversation variable & variable assigner node (langgenius#7222)
Browse files Browse the repository at this point in the history
Signed-off-by: -LAN- <[email protected]>
Co-authored-by: Joel <[email protected]>
Co-authored-by: -LAN- <[email protected]>
  • Loading branch information
3 people authored and cuiks committed Sep 2, 2024
1 parent 8d99e9e commit f28f3bf
Show file tree
Hide file tree
Showing 128 changed files with 3,355 additions and 684 deletions.
8 changes: 2 additions & 6 deletions api/configs/app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@
class DifyConfig(
# Packaging info
PackagingInfo,

# Deployment configs
DeploymentConfig,

# Feature configs
FeatureConfig,

# Middleware configs
MiddlewareConfig,

# Extra service configs
ExtraServiceConfig,

# Enterprise feature configs
# **Before using, please contact [email protected] by email to inquire about licensing matters.**
EnterpriseFeatureConfig,
Expand All @@ -36,7 +31,6 @@ class DifyConfig(
env_file='.env',
env_file_encoding='utf-8',
frozen=True,

# ignore extra attributes
extra='ignore',
)
Expand Down Expand Up @@ -67,3 +61,5 @@ def HTTP_REQUEST_NODE_READABLE_MAX_TEXT_SIZE(self) -> str:
SSRF_PROXY_HTTPS_URL: str | None = None

MODERATION_BUFFER_SIZE: int = Field(default=300, description='The buffer size for moderation.')

MAX_VARIABLE_SIZE: int = Field(default=5 * 1024, description='The maximum size of a variable. default is 5KB.')
1 change: 1 addition & 0 deletions api/controllers/console/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
audio,
completion,
conversation,
conversation_variables,
generator,
message,
model_config,
Expand Down
61 changes: 61 additions & 0 deletions api/controllers/console/app/conversation_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from flask_restful import Resource, marshal_with, reqparse
from sqlalchemy import select
from sqlalchemy.orm import Session

from controllers.console import api
from controllers.console.app.wraps import get_app_model
from controllers.console.setup import setup_required
from controllers.console.wraps import account_initialization_required
from extensions.ext_database import db
from fields.conversation_variable_fields import paginated_conversation_variable_fields
from libs.login import login_required
from models import ConversationVariable
from models.model import AppMode


class ConversationVariablesApi(Resource):
@setup_required
@login_required
@account_initialization_required
@get_app_model(mode=AppMode.ADVANCED_CHAT)
@marshal_with(paginated_conversation_variable_fields)
def get(self, app_model):
parser = reqparse.RequestParser()
parser.add_argument('conversation_id', type=str, location='args')
args = parser.parse_args()

stmt = (
select(ConversationVariable)
.where(ConversationVariable.app_id == app_model.id)
.order_by(ConversationVariable.created_at)
)
if args['conversation_id']:
stmt = stmt.where(ConversationVariable.conversation_id == args['conversation_id'])
else:
raise ValueError('conversation_id is required')

# NOTE: This is a temporary solution to avoid performance issues.
page = 1
page_size = 100
stmt = stmt.limit(page_size).offset((page - 1) * page_size)

with Session(db.engine) as session:
rows = session.scalars(stmt).all()

return {
'page': page,
'limit': page_size,
'total': len(rows),
'has_more': False,
'data': [
{
'created_at': row.created_at,
'updated_at': row.updated_at,
**row.to_variable().model_dump(),
}
for row in rows
],
}


api.add_resource(ConversationVariablesApi, '/apps/<uuid:app_id>/conversation-variables')
7 changes: 6 additions & 1 deletion api/controllers/console/app/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def post(self, app_model: App):
parser.add_argument('hash', type=str, required=False, location='json')
# TODO: set this to required=True after frontend is updated
parser.add_argument('environment_variables', type=list, required=False, location='json')
parser.add_argument('conversation_variables', type=list, required=False, location='json')
args = parser.parse_args()
elif 'text/plain' in content_type:
try:
Expand All @@ -88,7 +89,8 @@ def post(self, app_model: App):
'graph': data.get('graph'),
'features': data.get('features'),
'hash': data.get('hash'),
'environment_variables': data.get('environment_variables')
'environment_variables': data.get('environment_variables'),
'conversation_variables': data.get('conversation_variables'),
}
except json.JSONDecodeError:
return {'message': 'Invalid JSON data'}, 400
Expand All @@ -100,13 +102,16 @@ def post(self, app_model: App):
try:
environment_variables_list = args.get('environment_variables') or []
environment_variables = [factory.build_variable_from_mapping(obj) for obj in environment_variables_list]
conversation_variables_list = args.get('conversation_variables') or []
conversation_variables = [factory.build_variable_from_mapping(obj) for obj in conversation_variables_list]
workflow = workflow_service.sync_draft_workflow(
app_model=app_model,
graph=args['graph'],
features=args['features'],
unique_hash=args.get('hash'),
account=current_user,
environment_variables=environment_variables,
conversation_variables=conversation_variables,
)
except WorkflowHashNotEqualError:
raise DraftWorkflowNotSync()
Expand Down
8 changes: 2 additions & 6 deletions api/core/app/app_config/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

from pydantic import BaseModel

from core.file.file_obj import FileExtraConfig
from core.model_runtime.entities.message_entities import PromptMessageRole
from models.model import AppMode
from models import AppMode


class ModelConfigEntity(BaseModel):
Expand Down Expand Up @@ -200,11 +201,6 @@ class TracingConfigEntity(BaseModel):
tracing_provider: str


class FileExtraConfig(BaseModel):
"""
File Upload Entity.
"""
image_config: Optional[dict[str, Any]] = None


class AppAdditionalFeatures(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion api/core/app/app_config/features/file_upload/manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections.abc import Mapping
from typing import Any, Optional

from core.app.app_config.entities import FileExtraConfig
from core.file.file_obj import FileExtraConfig


class FileUploadConfigManager:
Expand Down
6 changes: 2 additions & 4 deletions api/core/app/apps/advanced_chat/app_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def generate(
contexts.tenant_id.set(application_generate_entity.app_config.tenant_id)

return self._generate(
app_model=app_model,
workflow=workflow,
user=user,
invoke_from=invoke_from,
Expand Down Expand Up @@ -180,7 +179,6 @@ def single_iteration_generate(self, app_model: App,
contexts.tenant_id.set(application_generate_entity.app_config.tenant_id)

return self._generate(
app_model=app_model,
workflow=workflow,
user=user,
invoke_from=InvokeFrom.DEBUGGER,
Expand All @@ -189,12 +187,12 @@ def single_iteration_generate(self, app_model: App,
stream=stream
)

def _generate(self, app_model: App,
def _generate(self, *,
workflow: Workflow,
user: Union[Account, EndUser],
invoke_from: InvokeFrom,
application_generate_entity: AdvancedChatAppGenerateEntity,
conversation: Conversation = None,
conversation: Conversation | None = None,
stream: bool = True) \
-> Union[dict, Generator[dict, None, None]]:
is_first_conversation = False
Expand Down
Loading

0 comments on commit f28f3bf

Please sign in to comment.