http_request.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package real
  2. import (
  3. "fmt"
  4. "github.com/langgenius/dify-plugin-daemon/internal/core/dify_invocation"
  5. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/model_entities"
  6. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/tool_entities"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/validators"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/http_requests"
  9. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  10. )
  11. // Send a request to dify inner api and validate the response
  12. func Request[T any](i *RealBackwardsInvocation, method string, path string, options ...http_requests.HttpOptions) (*T, error) {
  13. options = append(options,
  14. http_requests.HttpHeader(map[string]string{
  15. "X-Inner-Api-Key": i.dify_inner_api_key,
  16. }),
  17. http_requests.HttpWriteTimeout(5000),
  18. http_requests.HttpReadTimeout(240000),
  19. )
  20. req, err := http_requests.RequestAndParse[T](i.client, i.difyPath(path), method, options...)
  21. if err != nil {
  22. return nil, err
  23. }
  24. if err := validators.GlobalEntitiesValidator.Struct(req); err != nil {
  25. return nil, fmt.Errorf("validate request failed: %s", err.Error())
  26. }
  27. return req, nil
  28. }
  29. func StreamResponse[T any](i *RealBackwardsInvocation, method string, path string, options ...http_requests.HttpOptions) (*stream.Stream[T], error) {
  30. options = append(
  31. options, http_requests.HttpHeader(map[string]string{
  32. "X-Inner-Api-Key": i.dify_inner_api_key,
  33. }),
  34. http_requests.HttpWriteTimeout(5000),
  35. http_requests.HttpReadTimeout(240000),
  36. )
  37. return http_requests.RequestAndParseStream[T](i.client, i.difyPath(path), method, options...)
  38. }
  39. func (i *RealBackwardsInvocation) InvokeLLM(payload *dify_invocation.InvokeLLMRequest) (*stream.Stream[model_entities.LLMResultChunk], error) {
  40. return StreamResponse[model_entities.LLMResultChunk](i, "POST", "invoke/llm", http_requests.HttpPayloadJson(payload))
  41. }
  42. func (i *RealBackwardsInvocation) InvokeTextEmbedding(payload *dify_invocation.InvokeTextEmbeddingRequest) (*model_entities.TextEmbeddingResult, error) {
  43. return Request[model_entities.TextEmbeddingResult](i, "POST", "invoke/text-embedding", http_requests.HttpPayloadJson(payload))
  44. }
  45. func (i *RealBackwardsInvocation) InvokeRerank(payload *dify_invocation.InvokeRerankRequest) (*model_entities.RerankResult, error) {
  46. return Request[model_entities.RerankResult](i, "POST", "invoke/rerank", http_requests.HttpPayloadJson(payload))
  47. }
  48. func (i *RealBackwardsInvocation) InvokeTTS(payload *dify_invocation.InvokeTTSRequest) (*stream.Stream[model_entities.TTSResult], error) {
  49. return StreamResponse[model_entities.TTSResult](i, "POST", "invoke/tts", http_requests.HttpPayloadJson(payload))
  50. }
  51. func (i *RealBackwardsInvocation) InvokeSpeech2Text(payload *dify_invocation.InvokeSpeech2TextRequest) (*model_entities.Speech2TextResult, error) {
  52. return Request[model_entities.Speech2TextResult](i, "POST", "invoke/speech2text", http_requests.HttpPayloadJson(payload))
  53. }
  54. func (i *RealBackwardsInvocation) InvokeModeration(payload *dify_invocation.InvokeModerationRequest) (*model_entities.ModerationResult, error) {
  55. return Request[model_entities.ModerationResult](i, "POST", "invoke/moderation", http_requests.HttpPayloadJson(payload))
  56. }
  57. func (i *RealBackwardsInvocation) InvokeTool(payload *dify_invocation.InvokeToolRequest) (*stream.Stream[tool_entities.ToolResponseChunk], error) {
  58. return StreamResponse[tool_entities.ToolResponseChunk](i, "POST", "invoke/tool", http_requests.HttpPayloadJson(payload))
  59. }
  60. func (i *RealBackwardsInvocation) InvokeApp(payload *dify_invocation.InvokeAppRequest) (*stream.Stream[map[string]any], error) {
  61. return StreamResponse[map[string]any](i, "POST", "invoke/app", http_requests.HttpPayloadJson(payload))
  62. }
  63. func (i *RealBackwardsInvocation) InvokeParameterExtractor(payload *dify_invocation.InvokeParameterExtractorRequest) (*dify_invocation.InvokeNodeResponse, error) {
  64. return Request[dify_invocation.InvokeNodeResponse](i, "POST", "invoke/parameter-extractor", http_requests.HttpPayloadJson(payload))
  65. }
  66. func (i *RealBackwardsInvocation) InvokeQuestionClassifier(payload *dify_invocation.InvokeQuestionClassifierRequest) (*dify_invocation.InvokeNodeResponse, error) {
  67. return Request[dify_invocation.InvokeNodeResponse](i, "POST", "invoke/question-classifier", http_requests.HttpPayloadJson(payload))
  68. }
  69. func (i *RealBackwardsInvocation) InvokeEncrypt(payload *dify_invocation.InvokeEncryptRequest) (map[string]any, error) {
  70. if !payload.EncryptRequired(payload.Data) {
  71. return payload.Data, nil
  72. }
  73. data, err := Request[dify_invocation.InvokeEncryptionResponse](i, "POST", "invoke/encrypt", http_requests.HttpPayloadJson(payload))
  74. if err != nil {
  75. return nil, err
  76. }
  77. return data.Data, nil
  78. }