langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
43.37k stars 6.01k forks source link

Self-Hosted Guide for Azure #4849

Closed rachaelsingleton closed 2 months ago

rachaelsingleton commented 2 months ago

Self Checks

Provide a description of requested docs changes

Looking for a guide to self-host in Azure.

dosubot[bot] commented 2 months ago

To self-host the Dify application in Azure, you need to configure various environment variables and settings. Here are the key steps and configurations you need to set up:

  1. Common Variables: Set the URLs for different services.

    export CONSOLE_API_URL="https://api.console.dify.ai"
    export CONSOLE_WEB_URL="https://console.dify.ai"
    export SERVICE_API_URL="https://api.dify.ai"
    export APP_API_URL="https://app.dify.ai"
    export APP_WEB_URL="https://api.app.dify.ai"
    export FILES_URL="your-files-url"
  2. Server Configuration: Set the server mode, debug options, and other server-related settings.

    export MODE="api"  # or "worker"
    export DEBUG=false
    export FLASK_DEBUG=false
    export SECRET_KEY=$(openssl rand -base64 42)
    export DEPLOY_ENV="PRODUCTION"
    export LOG_LEVEL="ERROR"
    export MIGRATION_ENABLED=true
    export CHECK_UPDATE_URL=false
    export OPENAI_API_BASE="https://api.openai.com/v1"
  3. Container Startup Configuration: If using Docker, set the following:

    export DIFY_BIND_ADDRESS="0.0.0.0"
    export DIFY_PORT=5001
    export SERVER_WORKER_AMOUNT=3  # Example: number of CPU cores x 2 + 1
    export SERVER_WORKER_CLASS="gevent"
    export GUNICORN_TIMEOUT=360
    export CELERY_WORKER_CLASS="gevent"
    export CELERY_WORKER_AMOUNT=1
  4. Database Configuration: Configure PostgreSQL settings.

    export DB_USERNAME="your-db-username"
    export DB_PASSWORD="your-db-password"
    export DB_HOST="your-db-host"
    export DB_PORT=5432
    export DB_DATABASE="your-db-name"
    export SQLALCHEMY_POOL_SIZE=30
    export SQLALCHEMY_POOL_RECYCLE=3600
    export SQLALCHEMY_ECHO=false
  5. Redis Configuration: Set up Redis for caching and pub/sub.

    export REDIS_HOST="your-redis-host"
    export REDIS_PORT=6379
    export REDIS_DB=0
    export REDIS_USERNAME=""
    export REDIS_PASSWORD="your-redis-password"
    export REDIS_USE_SSL=false
  6. Celery Configuration: Configure Celery for task queues.

    export CELERY_BROKER_URL="redis://:your-redis-password@your-redis-host:6379/1"
    export BROKER_USE_SSL=false
  7. CORS Configuration: Set CORS policies.

    export CONSOLE_CORS_ALLOW_ORIGINS="*"
    export WEB_API_CORS_ALLOW_ORIGINS="*"
  8. File Storage Configuration: Configure storage type and paths.

    export STORAGE_TYPE="azure-blob"
    export AZURE_BLOB_ACCOUNT_NAME="your-account-name"
    export AZURE_BLOB_ACCOUNT_KEY="your-account-key"
    export AZURE_BLOB_CONTAINER_NAME="your-container-name"
    export AZURE_BLOB_ACCOUNT_URL="https://your_account_name.blob.core.windows.net"
  9. Vector Database Configuration: Set up the vector database.

    export VECTOR_STORE="weaviate"
    export WEAVIATE_ENDPOINT="http://weaviate:8080"
    export WEAVIATE_API_KEY="your-weaviate-api-key"
    export WEAVIATE_BATCH_SIZE=100
    export WEAVIATE_GRPC_ENABLED=true
  10. Knowledge Configuration: Configure file upload limits and ETL type.

    export UPLOAD_FILE_SIZE_LIMIT=15M
    export UPLOAD_FILE_BATCH_LIMIT=5
    export ETL_TYPE="dify"
  11. Multi-modal Configuration: Set multi-modal settings.

    export MULTIMODAL_SEND_IMAGE_FORMAT="base64"
    export UPLOAD_IMAGE_FILE_SIZE_LIMIT=10M
  12. Sentry Configuration: Set up Sentry for monitoring.

    export SENTRY_DSN="your-sentry-dsn"
    export SENTRY_TRACES_SAMPLE_RATE=0.01
    export SENTRY_PROFILES_SAMPLE_RATE=0.01
  13. Notion Integration Configuration: Configure Notion integration.

    export NOTION_INTEGRATION_TYPE="internal"
    export NOTION_CLIENT_SECRET="your-notion-client-secret"
    export NOTION_CLIENT_ID="your-notion-client-id"
    export NOTION_INTERNAL_SECRET="your-notion-internal-secret"
  14. Mail Configuration: Set up mail settings.

    export MAIL_TYPE="smtp"
    export SMTP_SERVER="your-smtp-server"
    export SMTP_PORT=587
    export SMTP_USERNAME="your-smtp-username"
    export SMTP_PASSWORD="your-smtp-password"
    export SMTP_USE_TLS=true
    export MAIL_DEFAULT_SEND_FROM="no-reply@dify.ai"
  15. Other Configurations: Set additional configurations.

    export INVITE_EXPIRY_HOURS=72

Additionally, you need to configure the hosting provider settings in your application. For Azure OpenAI, you can enable and configure it as follows:

class HostingConfiguration:
    provider_map: dict[str, HostingProvider] = {}
    moderation_config: HostedModerationConfig = None

    def init_app(self, app: Flask) -> None:
        config = app.config

        if config.get('EDITION') != 'CLOUD':
            return

        self.provider_map["azure_openai"] = self.init_azure_openai(config)
        self.provider_map["openai"] = self.init_openai(config)
        self.provider_map["anthropic"] = self.init_anthropic(config)
        self.provider_map["minimax"] = self.init_minimax(config)
        self.provider_map["spark"] = self.init_spark(config)
        self.provider_map["zhipuai"] = self.init_zhipuai(config)

        self.moderation_config = self.init_moderation_config(config)

    def init_azure_openai(self, app_config: Config) -> HostingProvider:
        quota_unit = QuotaUnit.TIMES
        if app_config.get("HOSTED_AZURE_OPENAI_ENABLED"):
            credentials = {
                "openai_api_key": app_config.get("HOSTED_AZURE_OPENAI_API_KEY"),
                "openai_api_base": app_config.get("HOSTED_AZURE_OPENAI_API_BASE"),
                "base_model_name": "gpt-35-turbo"
            }

            quotas = []
            hosted_quota_limit = int(app_config.get("HOSTED_AZURE_OPENAI_QUOTA_LIMIT", "1000"))
            trial_quota = TrialHostingQuota(
                quota_limit=hosted_quota_limit,
                restrict_models=[
                    RestrictModel(model="gpt-4", base_model_name="gpt-4", model_type=ModelType.LLM),
                    RestrictModel(model="gpt-4-32k", base_model_name="gpt-4-32k", model_type=ModelType.LLM),
                    RestrictModel(model="gpt-4-1106-preview", base_model_name="gpt-4-1106-preview", model_type=ModelType.LLM),
                    RestrictModel(model="gpt-4-vision-preview", base_model_name="gpt-4-vision-preview", model_type=ModelType.LLM),
                    RestrictModel(model="gpt-35-turbo", base_model_name="gpt-35-turbo", model_type=ModelType.LLM),
                    RestrictModel(model="gpt-35-turbo-1106", base_model_name="gpt-35-turbo-1106", model_type=ModelType.LLM),
                    RestrictModel(model="gpt-35-turbo-instruct", base_model_name="gpt-35-turbo-instruct", model_type=ModelType.LLM),
                    RestrictModel(model="gpt-35-turbo-16k", base_model_name="gpt-35-turbo-16k", model type=ModelType.LLM),
                    RestrictModel(model="text-davinci-003", base_model_name="text-davinci-003", model type=ModelType.LLM),
                    RestrictModel(model="text-embedding-ada-002", base_model_name="text-embedding-ada-002", model type=ModelType.TEXT_EMBEDDING),
                    RestrictModel(model="text-embedding-3-small", base_model_name="text-embedding-3-small", model type=ModelType.TEXT_EMBEDDING),
                    RestrictModel(model="text-embedding-3-large", base_model_name="text-embedding-3-large", model type=ModelType.TEXT_EMBEDDING),
                ]
            )
            quotas.append(trial_quota)

            return HostingProvider(
                enabled=True,
                credentials=credentials,
                quota_unit=quota_unit,
                quotas=quotas
            )

        return HostingProvider(
            enabled=False,
            quota_unit=quota_unit,
        )

After setting these environment variables and configuring the hosting provider, you can deploy your application to Azure using Docker or other deployment methods supported by Azure [1][2].

To continue talking to Dosu, mention @dosu.

crazywoola commented 2 months ago

https://docs.dify.ai/getting-started/install-self-hosted/docker-compose @rachaelsingleton

It can be easily started by docker. Just install the docker in azure machines, and follow the docs above.