model.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package requests
  2. import (
  3. "encoding/json"
  4. "github.com/langgenius/dify-plugin-daemon/pkg/entities/model_entities"
  5. )
  6. type Credentials struct {
  7. Credentials map[string]any `json:"credentials" validate:"omitempty"`
  8. }
  9. type BaseRequestInvokeModel struct {
  10. Provider string `json:"provider" validate:"required"`
  11. Model string `json:"model" validate:"required"`
  12. }
  13. type InvokeLLMSchema struct {
  14. ModelParameters map[string]any `json:"model_parameters" validate:"omitempty"`
  15. PromptMessages []model_entities.PromptMessage `json:"prompt_messages" validate:"omitempty"`
  16. Tools []model_entities.PromptMessageTool `json:"tools" validate:"omitempty,dive"`
  17. Stop []string `json:"stop" validate:"omitempty"`
  18. Stream bool `json:"stream"`
  19. }
  20. type RequestInvokeLLM struct {
  21. BaseRequestInvokeModel
  22. Credentials
  23. InvokeLLMSchema
  24. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=llm"`
  25. }
  26. type InvokeTextEmbeddingSchema struct {
  27. Texts []string `json:"texts" validate:"required,dive"`
  28. InputType string `json:"input_type" validate:"required"`
  29. }
  30. type RequestInvokeTextEmbedding struct {
  31. BaseRequestInvokeModel
  32. Credentials
  33. InvokeTextEmbeddingSchema
  34. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=text-embedding"`
  35. }
  36. type InvokeRerankSchema struct {
  37. Query string `json:"query" validate:"required"`
  38. Docs []string `json:"docs" validate:"required,dive"`
  39. ScoreThreshold float64 `json:"score_threshold" `
  40. TopN int `json:"top_n" `
  41. }
  42. type RequestInvokeRerank struct {
  43. BaseRequestInvokeModel
  44. Credentials
  45. InvokeRerankSchema
  46. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=rerank"`
  47. }
  48. type InvokeTTSSchema struct {
  49. ContentText string `json:"content_text" validate:"required"`
  50. Voice string `json:"voice" validate:"required"`
  51. TenantID string `json:"tenant_id" validate:"required"`
  52. }
  53. type RequestInvokeTTS struct {
  54. BaseRequestInvokeModel
  55. Credentials
  56. InvokeTTSSchema
  57. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=tts"`
  58. }
  59. type InvokeSpeech2TextSchema struct {
  60. File string `json:"file" validate:"required"` // hexing encoded voice file
  61. }
  62. type RequestInvokeSpeech2Text struct {
  63. BaseRequestInvokeModel
  64. Credentials
  65. InvokeSpeech2TextSchema
  66. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=speech2text"`
  67. }
  68. type InvokeModerationSchema struct {
  69. Text string `json:"text" validate:"required"`
  70. }
  71. type RequestInvokeModeration struct {
  72. BaseRequestInvokeModel
  73. Credentials
  74. InvokeModerationSchema
  75. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=moderation"`
  76. }
  77. type RequestValidateProviderCredentials struct {
  78. Credentials
  79. Provider string `json:"provider" validate:"required"`
  80. }
  81. type RequestValidateModelCredentials struct {
  82. BaseRequestInvokeModel
  83. Credentials
  84. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type"`
  85. }
  86. type RequestGetTTSModelVoices struct {
  87. BaseRequestInvokeModel
  88. Credentials
  89. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=tts"`
  90. Language string `json:"language" validate:"omitempty"`
  91. }
  92. type RequestGetTextEmbeddingNumTokens struct {
  93. BaseRequestInvokeModel
  94. Credentials
  95. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=text-embedding"`
  96. Texts []string `json:"texts" validate:"required,dive"`
  97. }
  98. type RequestGetLLMNumTokens struct {
  99. BaseRequestInvokeModel
  100. Credentials
  101. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type,eq=llm"`
  102. PromptMessages []model_entities.PromptMessage `json:"prompt_messages" validate:"omitempty,dive"`
  103. Tools []model_entities.PromptMessageTool `json:"tools" validate:"omitempty,dive"`
  104. }
  105. func (r RequestGetLLMNumTokens) MarshalJSON() ([]byte, error) {
  106. type alias RequestGetLLMNumTokens
  107. p := alias(r)
  108. if p.PromptMessages == nil {
  109. p.PromptMessages = []model_entities.PromptMessage{}
  110. }
  111. if p.Tools == nil {
  112. p.Tools = []model_entities.PromptMessageTool{}
  113. }
  114. return json.Marshal(p)
  115. }
  116. type RequestGetAIModelSchema struct {
  117. BaseRequestInvokeModel
  118. Credentials
  119. ModelType model_entities.ModelType `json:"model_type" validate:"required,model_type"`
  120. }