A Dify plugin that integrates with Alibaba Cloud AI Gateway, enabling access to AI models through the gateway's protocol conversion, security protection, traffic management, and observability features.
This plugin allows Dify to connect with Alibaba Cloud AI Gateway, which serves as a central hub between AI applications and model services. AI Gateway provides:
- Protocol Conversion: Standardized interfaces for accessing various AI models
- Security Protection: Content moderation, prompt injection detection, and sensitive data filtering
- Traffic Management: Rate limiting, load balancing, and intelligent routing
- Unified Observability: Monitoring, logging, and token usage tracking
- Cost Optimization: Request caching and token-based billing
Alibaba Cloud AI Gateway is a gateway service for AI application integration. It provides:
- Multi-Model Support: Unified interface for accessing models from different providers (OpenAI, Anthropic, Alibaba Qwen, etc.)
- Multi-Modal Support: Handles text, image, audio, and video processing with various transmission protocols
- Security & Compliance: Built-in AI security features including:
- Content moderation and compliance checking
- Prompt injection attack prevention
- Sensitive data detection
- Malicious file detection
- Digital watermarking
- Traffic Governance:
- Consumer-based rate limiting
- Token consumption limits
- Concurrent request controls
- Intelligent load balancing
- Observability & Analytics:
- Real-time request/response monitoring
- Token consumption statistics
- Cache hit rate tracking
- Model performance metrics
- ✅ Large Language Models (LLM): Chat and completion models
- ✅ Rerank: Document reranking models
- ✅ Speech-to-Text: Audio transcription models
- ✅ Text Embedding: Vector embedding models
- ✅ Text-to-Speech: Speech synthesis models
This plugin supports three authentication methods for AI Gateway:
- API Key: Simple bearer token authentication
- JWT (JSON Web Token): Standards-based authentication with JWKS support
- HMAC: Request signing with access key and secret key
- Structured Output: JSON schema validation for LLM responses
- Thinking Mode: Support for models with reasoning capabilities (o1, DeepSeek R1, etc.)
- Custom Model Configuration: Per-model display names and parameters
- Streaming Support: Real-time streaming responses for all compatible models
- Dify instance (self-hosted or cloud)
- Alibaba Cloud account with AI Gateway enabled
- Python 3.11+ (for development)
-
From Dify Marketplace (Recommended):
- Navigate to your Dify instance
- Go to Plugins → Marketplace
- Search for "AI Gateway"
- Click Install
-
Manual Installation (Development):
# Clone the repository git clone <repository-url> cd dify-plugin-ai-gateway # Install dependencies pip install -r requirements.txt # Package the plugin dify-plugin plugin package . # Upload the .difypkg file to your Dify instance
Before using this plugin, you need to create a consumer in Alibaba Cloud AI Gateway. Follow the official guide to create a consumer.
A consumer represents an identity that can access your AI Gateway APIs and is used for authentication, authorization, and billing.
When adding a model in Dify, you'll need to provide the following credentials:
- Endpoint URL: Your AI Gateway endpoint (e.g.,
https://your-gateway-cn-hangzhou.alicloudapi.com/v1) - Model Name: The model identifier in AI Gateway (e.g.,
qwen-turbo,qwen-max) - Display Name: (Optional) Custom name shown in Dify UI
Authentication Method: api_key
API Key: your-api-key-from-ai-gatewayWhen to use: Simple scenarios where you control the API key distribution.
Authentication Method: jwt
JWT JWKS: {"k":"your-base64-key","kty":"oct","alg":"HS256"}
JWT Algorithm: HS256 (or RS256, ES256, etc.)
JWT Consumer ID: your-consumer-id
JWT Expiration: 7200 (seconds)
JWT Issuer: (optional) your-issuer
JWT Header Name: Authorization
JWT Header Prefix: BearerWhen to use: Enterprise scenarios requiring standard token-based authentication with automatic expiration.
JWKS Format: The JWKS (JSON Web Key Set) should contain your signing key. For HMAC algorithms (HS256/384/512), use:
{
"k": "base64-encoded-secret",
"kty": "oct",
"alg": "HS256"
}Authentication Method: hmac
HMAC Access Key: your-access-key
HMAC Secret Key: your-secret-keyWhen to use: Scenarios where request integrity verification is required. Each request is signed with a timestamp and content hash.
How HMAC works:
- Plugin constructs a canonical string from the request (method, path, headers, body)
- Signs the string with your secret key using SHA256
- Includes the signature, access key, and timestamp in request headers
- AI Gateway verifies the signature to authenticate the request
Depending on the model type, you can configure additional parameters:
For LLM Models:
- Temperature, Top P, Top K
- Max tokens
- Response format (text, json_object, json_schema)
- Thinking mode (for reasoning models)
- Stop sequences
For Embedding Models:
- Document prefix
- Query prefix
- Embedding dimensions
For Speech Models:
- Language
- Voice selection
- Audio format
- Create a new chatbot application in Dify
- In the model settings, select your AI Gateway model
- Configure the system prompt and parameters
- Test the conversation
- Create a workflow application
- Add an LLM node
- Select your AI Gateway model from the dropdown
- Configure input variables and output processing
- Run and test the workflow
{
"response_format": "json_schema",
"json_schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "number"},
"email": {"type": "string"}
},
"required": ["name", "email"]
}
}The plugin automatically injects the schema into the system prompt to guide the model's response.
For models that support reasoning (like o1 or DeepSeek R1):
- In model configuration, set
agent_though_supporttosupportedoronly_thinking_supported - In your application, enable the "Thinking Mode" parameter
- The model will output its reasoning process before the final answer
Dify Application
↓
[AI Gateway Plugin]
↓
[Authentication Module]
├── API Key → Bearer token in Authorization header
├── JWT → Generate signed token with claims
└── HMAC → Sign request with access key + secret
↓
[Alibaba Cloud AI Gateway]
↓
[AI Model Service]
-
"Authentication failed" error
- Verify your credentials are correct
- Check that your consumer is active in AI Gateway console
- Ensure the endpoint URL is correct and accessible
-
"Model not found" error
- Verify the model name matches exactly with AI Gateway configuration
- Check that the model is published in your API configuration
-
Rate limit errors
- Check your consumer's rate limit settings in AI Gateway
- Consider implementing request queuing in your application
- Review token consumption limits
-
Timeout errors
- Long responses may exceed timeout limits
- Consider using streaming mode for better user experience
- Check AI Gateway's timeout configuration
To enable debug logging:
# Set environment variable before running
export DIFY_PLUGIN_LOG_LEVEL=DEBUG
python -m main-
Credential Management:
- Never commit credentials to version control
- Use environment variables or secret management systems
- Rotate keys regularly
-
HMAC Authentication:
- Prefer HMAC for production environments
- Keep secret keys secure and never expose them
- Monitor for signature verification failures
-
AI Gateway Security Features:
- Enable content moderation to filter inappropriate content
- Use prompt injection detection to prevent attacks
- Configure rate limiting to prevent abuse
- Enable request logging for audit trails
You can pass custom headers to AI Gateway by setting the extra_headers parameter:
{
"extra_headers": {
"X-Custom-Header": "value"
}
}AI Gateway supports model routing. You can configure multiple models and let AI Gateway route requests based on:
- Token consumption quotas
- Model availability
- Response time requirements
- Cost optimization
Contributions are welcome! Please feel free to submit issues or pull requests.
# Clone the repository
git clone <repository-url>
cd dify-plugin-ai-gateway
# Install dependencies
pip install -r requirements.txt
# Install development dependencies
pip install pytest black flake8
# Run linter
flake8 models/ provider/
# Format code
black models/ provider/This plugin is licensed under the MIT License. See LICENSE file for details.
- Dify Documentation: https://docs.dify.ai
- AI Gateway Documentation: https://www.alibabacloud.com/help/en/api-gateway/ai-gateway