mirror of
https://github.com/langgenius/dify.git
synced 2026-04-15 09:57:03 +08:00
refactor: replace bare dict with typed annotations in llm_generator and prompt (#35100)
This commit is contained in:
parent
ed401728eb
commit
974d2f1627
@ -2,7 +2,7 @@ import json
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import Sequence
|
||||
from typing import Protocol, TypedDict, cast
|
||||
from typing import Any, Protocol, TypedDict, cast
|
||||
|
||||
import json_repair
|
||||
from graphon.enums import WorkflowNodeExecutionMetadataKey
|
||||
@ -533,7 +533,7 @@ class LLMGenerator:
|
||||
def __instruction_modify_common(
|
||||
tenant_id: str,
|
||||
model_config: ModelConfig,
|
||||
last_run: dict | None,
|
||||
last_run: dict[str, Any] | None,
|
||||
current: str | None,
|
||||
error_message: str | None,
|
||||
instruction: str,
|
||||
|
||||
@ -202,7 +202,7 @@ def _handle_native_json_schema(
|
||||
structured_output_schema: Mapping,
|
||||
model_parameters: dict[str, Any],
|
||||
rules: list[ParameterRule],
|
||||
):
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
Handle structured output for models with native JSON schema support.
|
||||
|
||||
@ -224,7 +224,7 @@ def _handle_native_json_schema(
|
||||
return model_parameters
|
||||
|
||||
|
||||
def _set_response_format(model_parameters: dict[str, Any], rules: list[ParameterRule]):
|
||||
def _set_response_format(model_parameters: dict[str, Any], rules: list[ParameterRule]) -> None:
|
||||
"""
|
||||
Set the appropriate response format parameter based on model rules.
|
||||
|
||||
@ -326,7 +326,7 @@ def _prepare_schema_for_model(provider: str, model_schema: AIModelEntity, schema
|
||||
return {"schema": processed_schema, "name": "llm_response"}
|
||||
|
||||
|
||||
def remove_additional_properties(schema: dict[str, Any]):
|
||||
def remove_additional_properties(schema: dict[str, Any]) -> None:
|
||||
"""
|
||||
Remove additionalProperties fields from JSON schema.
|
||||
Used for models like Gemini that don't support this property.
|
||||
@ -349,7 +349,7 @@ def remove_additional_properties(schema: dict[str, Any]):
|
||||
remove_additional_properties(item)
|
||||
|
||||
|
||||
def convert_boolean_to_string(schema: dict):
|
||||
def convert_boolean_to_string(schema: dict[str, Any]) -> None:
|
||||
"""
|
||||
Convert boolean type specifications to string in JSON schema.
|
||||
|
||||
|
||||
@ -313,7 +313,7 @@ class SimplePromptTransform(PromptTransform):
|
||||
|
||||
return prompt_message
|
||||
|
||||
def _get_prompt_rule(self, app_mode: AppMode, provider: str, model: str):
|
||||
def _get_prompt_rule(self, app_mode: AppMode, provider: str, model: str) -> dict[str, Any]:
|
||||
"""
|
||||
Get simple prompt rule.
|
||||
:param app_mode: app mode
|
||||
@ -325,7 +325,7 @@ class SimplePromptTransform(PromptTransform):
|
||||
|
||||
# Check if the prompt file is already loaded
|
||||
if prompt_file_name in prompt_file_contents:
|
||||
return cast(dict, prompt_file_contents[prompt_file_name])
|
||||
return cast(dict[str, Any], prompt_file_contents[prompt_file_name])
|
||||
|
||||
# Get the absolute path of the subdirectory
|
||||
prompt_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "prompt_templates")
|
||||
@ -338,7 +338,7 @@ class SimplePromptTransform(PromptTransform):
|
||||
# Store the content of the prompt file
|
||||
prompt_file_contents[prompt_file_name] = content
|
||||
|
||||
return cast(dict, content)
|
||||
return cast(dict[str, Any], content)
|
||||
|
||||
def _prompt_file_name(self, app_mode: AppMode, provider: str, model: str) -> str:
|
||||
# baichuan
|
||||
|
||||
Loading…
Reference in New Issue
Block a user