optimize conversation / message insert
Some checks failed
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/amd64, build-api-amd64) (push) Has been cancelled
Build and Push API & Web / build (api, DIFY_API_IMAGE_NAME, linux/arm64, build-api-arm64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/amd64, build-web-amd64) (push) Has been cancelled
Build and Push API & Web / build (web, DIFY_WEB_IMAGE_NAME, linux/arm64, build-web-arm64) (push) Has been cancelled
Build and Push API & Web / create-manifest (api, DIFY_API_IMAGE_NAME, merge-api-images) (push) Has been cancelled
Build and Push API & Web / create-manifest (web, DIFY_WEB_IMAGE_NAME, merge-web-images) (push) Has been cancelled

This commit is contained in:
takatost 2024-10-25 00:31:15 -07:00
parent 40fb522f56
commit 0c6ad1df64
3 changed files with 65 additions and 60 deletions

View File

@ -34,6 +34,11 @@ class RedisConfig(BaseSettings):
default=0, default=0,
) )
REDIS_MAX_CONNECTIONS: PositiveInt = Field(
description="Maximum number of connections to Redis",
default=200,
)
REDIS_USE_SSL: bool = Field( REDIS_USE_SSL: bool = Field(
description="Enable SSL/TLS for the Redis connection", description="Enable SSL/TLS for the Redis connection",
default=False, default=False,

View File

@ -226,13 +226,11 @@ class AdvancedChatAppGenerator(MessageBasedAppGenerator):
is_first_conversation = True is_first_conversation = True
# init generate records # init generate records
(conversation, message) = self._init_generate_records(application_generate_entity, conversation) (conversation, message) = self._init_generate_records(
application_generate_entity=application_generate_entity,
if is_first_conversation: conversation=conversation,
# update conversation features override_model_configs=workflow.features_dict if is_first_conversation else None,
conversation.override_model_configs = workflow.features )
db.session.commit()
db.session.refresh(conversation)
# init queue manager # init queue manager
queue_manager = MessageBasedAppQueueManager( queue_manager = MessageBasedAppQueueManager(

View File

@ -1,8 +1,9 @@
import json import json
import logging import logging
from collections.abc import Generator import uuid
from collections.abc import Generator, Mapping
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Optional, Union from typing import Any, Optional, Union
from sqlalchemy import and_ from sqlalchemy import and_
@ -137,6 +138,7 @@ class MessageBasedAppGenerator(BaseAppGenerator):
AdvancedChatAppGenerateEntity, AdvancedChatAppGenerateEntity,
], ],
conversation: Optional[Conversation] = None, conversation: Optional[Conversation] = None,
override_model_configs: Optional[Mapping[str, Any]] = None,
) -> tuple[Conversation, Message]: ) -> tuple[Conversation, Message]:
""" """
Initialize generate records Initialize generate records
@ -158,14 +160,12 @@ class MessageBasedAppGenerator(BaseAppGenerator):
if isinstance(application_generate_entity, AdvancedChatAppGenerateEntity): if isinstance(application_generate_entity, AdvancedChatAppGenerateEntity):
app_model_config_id = None app_model_config_id = None
override_model_configs = None
model_provider = None model_provider = None
model_id = None model_id = None
else: else:
app_model_config_id = app_config.app_model_config_id app_model_config_id = app_config.app_model_config_id
model_provider = application_generate_entity.model_conf.provider model_provider = application_generate_entity.model_conf.provider
model_id = application_generate_entity.model_conf.model model_id = application_generate_entity.model_conf.model
override_model_configs = None
if app_config.app_model_config_from == EasyUIBasedAppModelConfigFrom.ARGS and app_config.app_mode in { if app_config.app_model_config_from == EasyUIBasedAppModelConfigFrom.ARGS and app_config.app_mode in {
AppMode.AGENT_CHAT, AppMode.AGENT_CHAT,
AppMode.CHAT, AppMode.CHAT,
@ -177,61 +177,63 @@ class MessageBasedAppGenerator(BaseAppGenerator):
introduction = self._get_conversation_introduction(application_generate_entity) introduction = self._get_conversation_introduction(application_generate_entity)
if not conversation: if not conversation:
conversation = Conversation( with db.Session(bind=db.engine, expire_on_commit=False) as session:
app_id=app_config.app_id, conversation = Conversation()
app_model_config_id=app_model_config_id, conversation.id = str(uuid.uuid4())
model_provider=model_provider, conversation.app_id = app_config.app_id
model_id=model_id, conversation.app_model_config_id = app_model_config_id
override_model_configs=json.dumps(override_model_configs) if override_model_configs else None, conversation.model_provider = model_provider
mode=app_config.app_mode.value, conversation.model_id = model_id
name="New conversation", conversation.override_model_configs = (
inputs=application_generate_entity.inputs, json.dumps(override_model_configs) if override_model_configs else None
introduction=introduction, )
system_instruction="", conversation.mode = app_config.app_mode.value
system_instruction_tokens=0, conversation.name = "New conversation"
status="normal", conversation.inputs = application_generate_entity.inputs
invoke_from=application_generate_entity.invoke_from.value, conversation.introduction = introduction
from_source=from_source, conversation.system_instruction = ""
from_end_user_id=end_user_id, conversation.system_instruction_tokens = 0
from_account_id=account_id, conversation.status = "normal"
) conversation.invoke_from = application_generate_entity.invoke_from.value
conversation.from_source = from_source
conversation.from_end_user_id = end_user_id
conversation.from_account_id = account_id
db.session.add(conversation) session.add(conversation)
db.session.commit() session.commit()
db.session.refresh(conversation) session.refresh(conversation)
else: else:
conversation.updated_at = datetime.now(timezone.utc).replace(tzinfo=None) conversation.updated_at = datetime.now(timezone.utc).replace(tzinfo=None)
db.session.commit() db.session.commit()
message = Message( with db.Session(bind=db.engine, expire_on_commit=False) as session:
app_id=app_config.app_id, message = Message()
model_provider=model_provider, message.app_id = app_config.app_id
model_id=model_id, message.model_provider = model_provider
override_model_configs=json.dumps(override_model_configs) if override_model_configs else None, message.model_id = model_id
conversation_id=conversation.id, message.override_model_configs = json.dumps(override_model_configs) if override_model_configs else None
inputs=application_generate_entity.inputs, message.conversation_id = conversation.id
query=application_generate_entity.query or "", message.inputs = application_generate_entity.inputs
message="", message.query = application_generate_entity.query or ""
message_tokens=0, message.message = ""
message_unit_price=0, message.message_tokens = 0
message_price_unit=0, message.message_unit_price = 0
answer="", message.answer = ""
answer_tokens=0, message.answer_tokens = 0
answer_unit_price=0, message.answer_unit_price = 0
answer_price_unit=0, message.answer_price_unit = 0
parent_message_id=getattr(application_generate_entity, "parent_message_id", None), message.parent_message_id = getattr(application_generate_entity, "parent_message_id", None)
provider_response_latency=0, message.provider_response_latency = 0
total_price=0, message.total_price = 0
currency="USD", message.currency = "USD"
invoke_from=application_generate_entity.invoke_from.value, message.invoke_from = application_generate_entity.invoke_from.value
from_source=from_source, message.from_source = from_source
from_end_user_id=end_user_id, message.from_end_user_id = end_user_id
from_account_id=account_id, message.from_account_id = account_id
)
db.session.add(message) session.add(message)
db.session.commit() session.commit()
db.session.refresh(message) session.refresh(message)
for file in application_generate_entity.files: for file in application_generate_entity.files:
message_file = MessageFile( message_file = MessageFile(