Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/dify_plugin/entities/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def to_prompt_message(self) -> PromptMessageTool:
for name, parameter in self.tool_parameters.items():
tool.parameters[name] = {
"type": parameter.type.value,
"description": parameter.description,
"description": parameter.description or "",
}

if parameter.required:
Expand Down
2 changes: 1 addition & 1 deletion src/dify_plugin/interfaces/agent/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def _convert_tool_to_prompt_message_tool(
"""Convert tool to prompt message tool"""
message_tool = PromptMessageTool(
name=tool.identity.name,
description=tool.description.llm if tool.description else "",
description=(tool.description.llm or "") if tool.description else "",
parameters={
"type": "object",
"properties": {},
Expand Down
4 changes: 2 additions & 2 deletions src/dify_plugin/interfaces/model/openai_compatible/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ def _generate(
data["functions"] = [
{
"name": tool.name,
"description": tool.description,
"description": tool.description or "",
"parameters": tool.parameters,
}
for tool in tools
Expand Down Expand Up @@ -1127,7 +1127,7 @@ def _num_tokens_for_tools(self, tools: list[PromptMessageTool]) -> int:
num_tokens += self._get_num_tokens_by_gpt2(tool.name)
num_tokens += self._get_num_tokens_by_gpt2("description")
if hasattr(tool, "description"):
num_tokens += self._get_num_tokens_by_gpt2(tool.description)
num_tokens += self._get_num_tokens_by_gpt2(tool.description or "")
Comment on lines 1129 to +1130
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since description is a required field in the PromptMessageTool Pydantic model, the hasattr(tool, "description") check is redundant. An instance of PromptMessageTool will always have this attribute.

You can simplify this by removing the conditional check. This would also make the code more consistent with the _generate method (line 601), which accesses tool.description directly.

            num_tokens += self._get_num_tokens_by_gpt2(tool.description or "")

if hasattr(tool, "parameters"):
parameters = tool.parameters
num_tokens += self._get_num_tokens_by_gpt2("parameters")
Expand Down