Skip to content
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
1 change: 1 addition & 0 deletions internal/core/dify_invocation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type InvokeLLMSchema struct {
CompletionParams map[string]any `json:"completion_params" validate:"omitempty"`
PromptMessages []model_entities.PromptMessage `json:"prompt_messages" validate:"omitempty"`
Tools []model_entities.PromptMessageTool `json:"tools" validate:"omitempty,dive"`
ToolChoice *model_entities.ToolChoice `json:"tool_choice,omitempty" validate:"omitempty"`
Stop []string `json:"stop" validate:"omitempty"`
Stream bool `json:"stream"`
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/entities/model_entities/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"strings"

"github.com/go-playground/validator/v10"
"github.com/langgenius/dify-plugin-daemon/pkg/validators"
Expand Down Expand Up @@ -127,6 +128,18 @@ func init() {
validators.GlobalEntitiesValidator.RegisterValidation("prompt_message_role", isPromptMessageRole)
validators.GlobalEntitiesValidator.RegisterValidation("prompt_message_content", isPromptMessageContent)
validators.GlobalEntitiesValidator.RegisterValidation("prompt_message_content_type", isPromptMessageContentType)
validators.GlobalEntitiesValidator.RegisterStructValidation(validateToolChoice, ToolChoice{})
}

func validateToolChoice(sl validator.StructLevel) {
choice, ok := sl.Current().Interface().(ToolChoice)
if !ok {
return
}

if choice.Mode == TOOL_CHOICE_MODE_TOOL && strings.TrimSpace(choice.Name) == "" {
sl.ReportError(choice.Name, "name", "Name", "required_for_tool", "")
}
}

func unmarshalPromptMessageContent(data json.RawMessage) (any, error) {
Expand Down Expand Up @@ -199,6 +212,20 @@ type PromptMessageTool struct {
Parameters map[string]any `json:"parameters"`
}

type ToolChoiceMode string

const (
TOOL_CHOICE_MODE_AUTO ToolChoiceMode = "auto"
TOOL_CHOICE_MODE_NONE ToolChoiceMode = "none"
TOOL_CHOICE_MODE_REQUIRED ToolChoiceMode = "required"
TOOL_CHOICE_MODE_TOOL ToolChoiceMode = "tool"
)

type ToolChoice struct {
Mode ToolChoiceMode `json:"mode" validate:"required,oneof=auto none required tool"`
Name string `json:"name,omitempty" validate:"omitempty"`
}

type LLMResultChunk struct {
Model LLMModel `json:"model" validate:"required"`
SystemFingerprint string `json:"system_fingerprint" validate:"omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/entities/requests/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type InvokeLLMSchema struct {
ModelParameters map[string]any `json:"model_parameters" validate:"omitempty"`
PromptMessages []model_entities.PromptMessage `json:"prompt_messages" validate:"omitempty"`
Tools []model_entities.PromptMessageTool `json:"tools" validate:"omitempty,dive"`
ToolChoice *model_entities.ToolChoice `json:"tool_choice,omitempty" validate:"omitempty"`
Stop []string `json:"stop" validate:"omitempty"`
JSONSchema map[string]any `json:"json_schema" validate:"omitempty"`
Stream bool `json:"stream"`
Expand Down
Loading