Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor all 'or []' and 'or {}' logic to make code more clear #10883

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions api/core/agent/base_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,9 @@ def __init__(
# check if model supports stream tool call
llm_model = cast(LargeLanguageModel, model_instance.model_type_instance)
model_schema = llm_model.get_model_schema(model_instance.model, model_instance.credentials)
if model_schema and ModelFeature.STREAM_TOOL_CALL in (model_schema.features or []):
self.stream_tool_call = True
else:
self.stream_tool_call = False

# check if model supports vision
if model_schema and ModelFeature.VISION in (model_schema.features or []):
self.files = application_generate_entity.files
else:
self.files = []
features = model_schema.features if model_schema and model_schema.features else []
self.stream_tool_call = ModelFeature.STREAM_TOOL_CALL in features
self.files = application_generate_entity.files if ModelFeature.VISION in features else []
self.query = None
self._current_thoughts: list[PromptMessage] = []

Expand Down Expand Up @@ -250,7 +243,7 @@ def update_prompt_message_tool(self, tool: Tool, prompt_tool: PromptMessageTool)
update prompt message tool
"""
# try to get tool runtime parameters
tool_runtime_parameters = tool.get_runtime_parameters() or []
tool_runtime_parameters = tool.get_runtime_parameters()

for parameter in tool_runtime_parameters:
if parameter.form != ToolParameter.ToolParameterForm.LLM:
Expand Down
4 changes: 2 additions & 2 deletions api/core/app/task_pipeline/workflow_cycle_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def _workflow_start_to_stream_response(
id=workflow_run.id,
workflow_id=workflow_run.workflow_id,
sequence_number=workflow_run.sequence_number,
inputs=workflow_run.inputs_dict or {},
inputs=workflow_run.inputs_dict,
created_at=int(workflow_run.created_at.timestamp()),
),
)
Expand Down Expand Up @@ -428,7 +428,7 @@ def _workflow_finish_to_stream_response(
created_by=created_by,
created_at=int(workflow_run.created_at.timestamp()),
finished_at=int(workflow_run.finished_at.timestamp()),
files=self._fetch_files_from_node_outputs(workflow_run.outputs_dict or {}),
files=self._fetch_files_from_node_outputs(workflow_run.outputs_dict),
),
)

Expand Down
4 changes: 2 additions & 2 deletions api/core/model_runtime/model_providers/cohere/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,8 +691,8 @@ def get_customizable_model_schema(self, model: str, credentials: dict) -> AIMode
base_model_schema = cast(AIModelEntity, base_model_schema)

base_model_schema_features = base_model_schema.features or []
base_model_schema_model_properties = base_model_schema.model_properties or {}
base_model_schema_parameters_rules = base_model_schema.parameter_rules or []
base_model_schema_model_properties = base_model_schema.model_properties
base_model_schema_parameters_rules = base_model_schema.parameter_rules

entity = AIModelEntity(
model=model,
Expand Down
4 changes: 2 additions & 2 deletions api/core/model_runtime/model_providers/openai/llm/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,8 +1178,8 @@ def get_customizable_model_schema(self, model: str, credentials: dict) -> AIMode
base_model_schema = model_map[base_model]

base_model_schema_features = base_model_schema.features or []
base_model_schema_model_properties = base_model_schema.model_properties or {}
base_model_schema_parameters_rules = base_model_schema.parameter_rules or []
base_model_schema_model_properties = base_model_schema.model_properties
base_model_schema_parameters_rules = base_model_schema.parameter_rules

entity = AIModelEntity(
model=model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ def __init__(self, content: str, role: str = "user") -> None:
class OpenLLMGenerate:
def generate(
self,
*,
server_url: str,
model_name: str,
stream: bool,
model_parameters: dict[str, Any],
stop: list[str],
stop: list[str] | None = None,
prompt_messages: list[OpenLLMGenerateMessage],
user: str,
user: str | None = None,
) -> Union[Generator[OpenLLMGenerateMessage, None, None], OpenLLMGenerateMessage]:
if not server_url:
raise InvalidAuthenticationError("Invalid server URL")
Expand Down
2 changes: 1 addition & 1 deletion api/core/tools/tool/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def get_all_runtime_parameters(self) -> list[ToolParameter]:
"""
parameters = self.parameters or []
parameters = parameters.copy()
user_parameters = self.get_runtime_parameters() or []
user_parameters = self.get_runtime_parameters()
user_parameters = user_parameters.copy()

# override parameters
Expand Down
2 changes: 1 addition & 1 deletion api/core/tools/tool_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def agent_invoke(
# check if this tool has only one parameter
parameters = [
parameter
for parameter in tool.get_runtime_parameters() or []
for parameter in tool.get_runtime_parameters()
if parameter.form == ToolParameter.ToolParameterForm.LLM
]
if parameters and len(parameters) == 1:
Expand Down
2 changes: 1 addition & 1 deletion api/core/tools/utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _merge_parameters(self) -> list[ToolParameter]:
# get tool parameters
tool_parameters = self.tool_runtime.parameters or []
# get tool runtime parameters
runtime_parameters = self.tool_runtime.get_runtime_parameters() or []
runtime_parameters = self.tool_runtime.get_runtime_parameters()
# override parameters
current_parameters = tool_parameters.copy()
for runtime_parameter in runtime_parameters:
Expand Down
2 changes: 1 addition & 1 deletion api/services/app_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def get_app_meta(self, app_model: App) -> dict:
if not app_model_config:
return meta

agent_config = app_model_config.agent_mode_dict or {}
agent_config = app_model_config.agent_mode_dict

# get all tools
tools = agent_config.get("tools", [])
Expand Down
2 changes: 1 addition & 1 deletion api/services/tools/tools_transform_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def tool_to_user_tool(
# get tool parameters
parameters = tool.parameters or []
# get tool runtime parameters
runtime_parameters = tool.get_runtime_parameters() or []
runtime_parameters = tool.get_runtime_parameters()
# override parameters
current_parameters = parameters.copy()
for runtime_parameter in runtime_parameters:
Expand Down
4 changes: 2 additions & 2 deletions api/services/website_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def crawl_url(cls, args: dict) -> dict:
excludes = options.get("excludes").split(",") if options.get("excludes") else []
params = {
"crawlerOptions": {
"includes": includes or [],
"excludes": excludes or [],
"includes": includes,
"excludes": excludes,
"generateImgAltText": True,
"limit": options.get("limit", 1),
"returnOnlyUrls": False,
Expand Down