Skip to content

honins/dify-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dify Chat Agent with JWT Authentication

This is a Python application that provides JWT authentication and integrates with the Dify API for chat functionality.

Features

  • JWT-based authentication
  • Integration with Dify API for chat
  • FastAPI backend
  • Secure password hashing
  • Environment variable configuration

Setup

  1. Install the required dependencies:
pip install -r requirements.txt
  1. Configure the application:

    • Copy .env.example to .env:
      cp .env.example .env
    • Update the values in .env file with your settings:
      # JWT Settings
      SECRET_KEY=your-secret-key-here
      ALGORITHM=HS256
      ACCESS_TOKEN_EXPIRE_MINUTES=30
      
      # Dify API Settings
      DIFY_API_URL=http://localhost/v1
      DIFY_API_KEY=your-dify-api-key
      
      # Server Settings
      HOST=0.0.0.0
      PORT=8000
      
  2. Set up Python virtual environment (recommended):

    # Create virtual environment
    python -m venv venv
    
    # Activate virtual environment
    # On Windows:
    .\venv\Scripts\activate
    # On Unix or MacOS:
    source venv/bin/activate
    
    # Install dependencies
    pip install -r requirements.txt

Usage

  1. Start the server:
python main.py
  1. The server will run on http://localhost:8000

Authentication

  1. Get a JWT token:
curl -X POST "http://localhost:8000/token" -H "Content-Type: application/x-www-form-urlencoded" -d "username=testuser&password=testpassword"
  1. Use the token for authenticated requests:
curl -X POST "http://localhost:8000/chat" -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{"query": "Hello", "response_mode": "blocking"}'

Default Credentials

  • Username: testuser
  • Password: testpassword

API Endpoints

  • POST /token: Get JWT token
  • POST /chat: Send message to Dify API

Environment Variables

The application uses the following environment variables:

  • SECRET_KEY: JWT secret key for token generation
  • ALGORITHM: JWT algorithm (default: HS256)
  • ACCESS_TOKEN_EXPIRE_MINUTES: Token expiration time in minutes
  • DIFY_API_URL: Dify API endpoint URL
  • DIFY_API_KEY: Dify API key
  • HOST: Server host address
  • PORT: Server port number

Environment Variable Priority

The application loads environment variables in the following order:

  1. System environment variables
  2. .env file
  3. Default values in the code

Security Notes

  • Never commit the .env file to version control
  • Use strong, unique values for SECRET_KEY in production
  • Implement proper user management in production
  • Keep your API keys secure

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages