mirror of
https://github.com/langgenius/dify.git
synced 2026-04-05 04:59:23 +08:00
feat: optimize override app model config convert (#874)
This commit is contained in:
@@ -63,26 +63,23 @@ class CompletionService:
|
||||
raise ConversationCompletedError()
|
||||
|
||||
if not conversation.override_model_configs:
|
||||
app_model_config = db.session.query(AppModelConfig).get(conversation.app_model_config_id)
|
||||
app_model_config = db.session.query(AppModelConfig).filter(
|
||||
AppModelConfig.id == conversation.app_model_config_id,
|
||||
AppModelConfig.app_id == app_model.id
|
||||
).first()
|
||||
|
||||
if not app_model_config:
|
||||
raise AppModelConfigBrokenError()
|
||||
else:
|
||||
conversation_override_model_configs = json.loads(conversation.override_model_configs)
|
||||
|
||||
app_model_config = AppModelConfig(
|
||||
id=conversation.app_model_config_id,
|
||||
app_id=app_model.id,
|
||||
provider="",
|
||||
model_id="",
|
||||
configs="",
|
||||
opening_statement=conversation_override_model_configs['opening_statement'],
|
||||
suggested_questions=json.dumps(conversation_override_model_configs['suggested_questions']),
|
||||
model=json.dumps(conversation_override_model_configs['model']),
|
||||
user_input_form=json.dumps(conversation_override_model_configs['user_input_form']),
|
||||
pre_prompt=conversation_override_model_configs['pre_prompt'],
|
||||
agent_mode=json.dumps(conversation_override_model_configs['agent_mode']),
|
||||
)
|
||||
|
||||
app_model_config = app_model_config.from_model_config_dict(conversation_override_model_configs)
|
||||
|
||||
if is_model_config_override:
|
||||
# build new app model config
|
||||
if 'model' not in args['model_config']:
|
||||
@@ -99,19 +96,8 @@ class CompletionService:
|
||||
app_model_config_model = app_model_config.model_dict
|
||||
app_model_config_model['completion_params'] = completion_params
|
||||
|
||||
app_model_config = AppModelConfig(
|
||||
id=app_model_config.id,
|
||||
app_id=app_model.id,
|
||||
provider="",
|
||||
model_id="",
|
||||
configs="",
|
||||
opening_statement=app_model_config.opening_statement,
|
||||
suggested_questions=app_model_config.suggested_questions,
|
||||
model=json.dumps(app_model_config_model),
|
||||
user_input_form=app_model_config.user_input_form,
|
||||
pre_prompt=app_model_config.pre_prompt,
|
||||
agent_mode=app_model_config.agent_mode,
|
||||
)
|
||||
app_model_config = app_model_config.copy()
|
||||
app_model_config.model = json.dumps(app_model_config_model)
|
||||
else:
|
||||
if app_model.app_model_config_id is None:
|
||||
raise AppModelConfigBrokenError()
|
||||
@@ -135,20 +121,10 @@ class CompletionService:
|
||||
app_model_config = AppModelConfig(
|
||||
id=app_model_config.id,
|
||||
app_id=app_model.id,
|
||||
provider="",
|
||||
model_id="",
|
||||
configs="",
|
||||
opening_statement=model_config['opening_statement'],
|
||||
suggested_questions=json.dumps(model_config['suggested_questions']),
|
||||
suggested_questions_after_answer=json.dumps(model_config['suggested_questions_after_answer']),
|
||||
more_like_this=json.dumps(model_config['more_like_this']),
|
||||
sensitive_word_avoidance=json.dumps(model_config['sensitive_word_avoidance']),
|
||||
model=json.dumps(model_config['model']),
|
||||
user_input_form=json.dumps(model_config['user_input_form']),
|
||||
pre_prompt=model_config['pre_prompt'],
|
||||
agent_mode=json.dumps(model_config['agent_mode']),
|
||||
)
|
||||
|
||||
app_model_config = app_model_config.from_model_config_dict(model_config)
|
||||
|
||||
# clean input by app_model_config form rules
|
||||
inputs = cls.get_cleaned_inputs(inputs, app_model_config)
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
from typing import Optional, Union, List
|
||||
|
||||
from core.completion import Completion
|
||||
@@ -5,8 +6,10 @@ from core.generator.llm_generator import LLMGenerator
|
||||
from libs.infinite_scroll_pagination import InfiniteScrollPagination
|
||||
from extensions.ext_database import db
|
||||
from models.account import Account
|
||||
from models.model import App, EndUser, Message, MessageFeedback
|
||||
from models.model import App, EndUser, Message, MessageFeedback, AppModelConfig
|
||||
from services.conversation_service import ConversationService
|
||||
from services.errors.app_model_config import AppModelConfigBrokenError
|
||||
from services.errors.conversation import ConversationNotExistsError, ConversationCompletedError
|
||||
from services.errors.message import FirstMessageNotExistsError, MessageNotExistsError, LastMessageNotExistsError, \
|
||||
SuggestedQuestionsAfterAnswerDisabledError
|
||||
|
||||
@@ -172,12 +175,6 @@ class MessageService:
|
||||
if not user:
|
||||
raise ValueError('user cannot be None')
|
||||
|
||||
app_model_config = app_model.app_model_config
|
||||
suggested_questions_after_answer = app_model_config.suggested_questions_after_answer_dict
|
||||
|
||||
if check_enabled and suggested_questions_after_answer.get("enabled", False) is False:
|
||||
raise SuggestedQuestionsAfterAnswerDisabledError()
|
||||
|
||||
message = cls.get_message(
|
||||
app_model=app_model,
|
||||
user=user,
|
||||
@@ -190,10 +187,38 @@ class MessageService:
|
||||
user=user
|
||||
)
|
||||
|
||||
if not conversation:
|
||||
raise ConversationNotExistsError()
|
||||
|
||||
if conversation.status != 'normal':
|
||||
raise ConversationCompletedError()
|
||||
|
||||
if not conversation.override_model_configs:
|
||||
app_model_config = db.session.query(AppModelConfig).filter(
|
||||
AppModelConfig.id == conversation.app_model_config_id,
|
||||
AppModelConfig.app_id == app_model.id
|
||||
).first()
|
||||
|
||||
if not app_model_config:
|
||||
raise AppModelConfigBrokenError()
|
||||
else:
|
||||
conversation_override_model_configs = json.loads(conversation.override_model_configs)
|
||||
app_model_config = AppModelConfig(
|
||||
id=conversation.app_model_config_id,
|
||||
app_id=app_model.id,
|
||||
)
|
||||
|
||||
app_model_config = app_model_config.from_model_config_dict(conversation_override_model_configs)
|
||||
|
||||
suggested_questions_after_answer = app_model_config.suggested_questions_after_answer_dict
|
||||
|
||||
if check_enabled and suggested_questions_after_answer.get("enabled", False) is False:
|
||||
raise SuggestedQuestionsAfterAnswerDisabledError()
|
||||
|
||||
# get memory of conversation (read-only)
|
||||
memory = Completion.get_memory_from_conversation(
|
||||
tenant_id=app_model.tenant_id,
|
||||
app_model_config=app_model.app_model_config,
|
||||
app_model_config=app_model_config,
|
||||
conversation=conversation,
|
||||
max_token_limit=3000,
|
||||
message_limit=3,
|
||||
@@ -209,4 +234,3 @@ class MessageService:
|
||||
)
|
||||
|
||||
return questions
|
||||
|
||||
|
||||
Reference in New Issue
Block a user