snakeying / flux-api-worker

Deployed on Cloudflare Worker, using the free Flux model and optimizing prompts with LLM. It can be called from any application compatible with the OpenAI API. ้ƒจ็ฝฒไบŽCloudflare Worker๏ผŒๅ…่ดนไฝฟ็”จFluxๆจกๅž‹๏ผŒๅนถ้€š่ฟ‡LLM่ฟ›่กŒๆ็คบ่ฏไผ˜ๅŒ–๏ผŒๆ”ฏๆŒๅœจไปปไฝ•ๅ…ผๅฎนOpenAI API็š„ๅบ”็”จไธญ่ฐƒ็”จใ€‚
58 stars 3 forks source link
cloudflare-ai cloudflare-api cloudflare-workers flux openai-api

Flux-API-Worker - README ๐Ÿ“˜๐ŸŽจ๐Ÿค–

English | ็ฎ€ไฝ“ไธญๆ–‡ | ็น้ซ”ไธญๆ–‡ | ๆ—ฅๆœฌ่ชž | Espaรฑol | Franรงais | ะ ัƒััะบะธะน | Deutsch

Introduction ๐ŸŒŸ๐Ÿ’ก

Flux-API-Worker is an AI image generation service deployed on Cloudflare Workers. It utilizes Cloudflare's Flux model to generate images and provides an efficient API interface for handling requests. This service can be easily integrated into various applications, offering users powerful AI image generation capabilities. โœจ๐Ÿ–ผ๏ธ๐Ÿš€

Features ๐Ÿš€๐ŸŒˆ

Quick Start ๐Ÿƒโ€โ™‚๏ธ๐Ÿ’จ

Deploy in Cloudflare Dashboard ๐Ÿ–ฅ๏ธ๐Ÿ› ๏ธ

  1. Log in to your Cloudflare account and navigate to the Workers page. ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป
  2. Click the "Create a Service" button. ๐Ÿ†•
  3. Name your Worker, e.g., "flux-api". โœ๏ธ
  4. Paste the provided Worker code into the editor. ๐Ÿ“‹
  5. Click the "Save and Deploy" button. ๐Ÿš€

Set Up Environment Variables โš™๏ธ๐Ÿ”ง

In the Worker's settings page, find the "Environment Variables" section and add the following variables:

Environment Variables List ๐Ÿ“‹๐Ÿ”‘

Variable Name Description Type Example Default
API_KEY API authentication key ๐Ÿ” String "your-complex-api-key-here" -
CF_ACCOUNT_ID Cloudflare Account ID ๐Ÿ†” String "1a2b3c4d5e6f7g8h9i0j" -
CF_API_TOKEN Cloudflare API Token ๐ŸŽŸ๏ธ String "your-cloudflare-api-token" -
PROMPT_OPTIMIZATION Enable Prompt Optimization ๐ŸŒ String "true" or "false" -
EXTERNAL_API_BASE External API base URL ๐Ÿ”— String "https://api.external-service.com" -
EXTERNAL_MODEL External optimization model name ๐Ÿค– String "gpt-3.5-turbo" -
EXTERNAL_API_KEY External API access key ๐Ÿ—๏ธ String "your-external-api-key" -
FLUX_NUM_STEPS Number of steps for Flux model ๐Ÿšถ Integer "4" 4
IMAGE_EXPIRATION Image expiration time in KV (seconds) โณ Integer "1800" 1800

Ensure these variables are correctly configured in your Cloudflare Worker's environment variables settings. For variables with default values, you can keep the default if no change is needed. ๐Ÿ”งโœ…

Note: For security, set a complex string for API_KEY. This will be used to validate the legitimacy of API calls. ๐Ÿ”’๐Ÿ›ก๏ธ

Create KV Namespace ๐Ÿ—„๏ธ๐Ÿ“ฆ

  1. In the Cloudflare Dashboard, go to the "Workers" page. ๐Ÿ–ฅ๏ธ
  2. Click on the "KV" tab. ๐Ÿ“‘
  3. Create a new namespace named "FLUX_CF_KV". ๐Ÿ†•
  4. In the Worker's settings, bind this KV namespace to the FLUX_CF_KV variable. ๐Ÿ”—

API Endpoints and Functionality ๐ŸŒ๐Ÿ› ๏ธ

1. Welcome Page ๐Ÿ‘‹

Accessing the Worker's root path (https://<your_worker_name>.<your_subdomain>.workers.dev/) will display a welcome page confirming the API service is running. โœ…๐Ÿ 

2. Chat Completions Endpoint ๐Ÿ’ฌ

Main image generation endpoint:

https://<your_worker_name>.<your_subdomain>.workers.dev/v1/chat/completions

๐ŸŽจโœจ

3. Model Information Endpoint โ„น๏ธ

Get available model information:

https://<your_worker_name>.<your_subdomain>.workers.dev/v1/models

This endpoint returns information about the currently used Flux model. ๐Ÿค–๐Ÿ“Š

4. Image Retrieval Endpoint ๐Ÿ–ผ๏ธ

Retrieve generated images:

https://<your_worker_name>.<your_subdomain>.workers.dev/image/{image_key}

๐Ÿ“ฅ๐ŸŽญ

Usage Guide ๐Ÿ“–๐Ÿงญ

Generate Images ๐Ÿ–ผ๏ธ๐ŸŽจ

Send a POST request to the chat completions endpoint in the following format:

{
  "messages": [
    {
      "role": "user",
      "content": "A cute cat 3:2"
    }
  ],
  "stream": false
}

The request headers must include:

Authorization: Bearer YOUR_API_KEY
Content-Type: application/json

Important: Replace YOUR_API_KEY with the API_KEY value you set in the environment variables. ๐Ÿ”‘๐Ÿ”„

Streaming Response ๐ŸŒŠ๐Ÿ“ก

If you want to receive a streaming response, set the stream parameter to true:

{
  "messages": [
    {
      "role": "user",
      "content": "A cute cat 3:2"
    }
  ],
  "stream": true
}

Streaming responses will be returned in Server-Sent Events (SSE) format, allowing real-time generation progress updates. โšก๐Ÿ”„

Supported Image Sizes ๐Ÿ“๐Ÿ–ผ๏ธ

Flux-API-Worker supports the following preset image sizes and aspect ratios:

To specify a particular size, simply add the corresponding ratio after the prompt, for example:

"A cute cat 16:9"

If no size is specified, the system will default to generating a 1:1 (1024x1024) image. ๐ŸŽ›๏ธ๐Ÿ”ง

Cross-Origin Resource Sharing (CORS) Support ๐ŸŒ๐Ÿ”“

Flux-API-Worker supports CORS, allowing access to the API from web applications on different domains. This means you can call the API directly from frontend JavaScript applications without encountering cross-origin issues. ๐Ÿ”—๐Ÿšซ๐Ÿšง

Using in Third-Party Applications ๐Ÿ”—๐Ÿ”Œ

Flux-API-Worker can be easily integrated into various applications such as NextWeb, ChatBox, etc. When configuring in these applications:

  1. Set the API address to your Worker URL (chat completions endpoint). ๐Ÿ”—
  2. Enter the API KEY you set. ๐Ÿ”‘
  3. No need to consider the System Message settings provided by the application, as the Flux-API-Worker uses a built-in System Message. ๐Ÿ’ฌ๐Ÿšซ

Note: Flux-API-Worker has removed the context functionality, generating a new unique image with each call. ๐Ÿ†•๐Ÿ–ผ๏ธ

Response Format ๐Ÿ“ค๐Ÿ“Š

Example of a non-streaming response:

{
  "id": "chatcmpl-1234567890",
  "created": 1677649420,
  "model": "@cf/black-forest-labs/flux-1-schnell",
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "๐ŸŽจ Original prompt: A cute cat 3:2\n๐Ÿ’ฌ Prompt generation model: Original Prompt\n๐ŸŒ Optimized prompt: A cute cat\n๐Ÿ“ Image specifications: 768x512\n๐ŸŒŸ Image generation successful!\nHere's the result:\n\n![Generated Image](https://your-worker-url.workers.dev/image/12345)"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 20,
    "completion_tokens": 100,
    "total_tokens": 120
  }
}

Considerations โš ๏ธ๐Ÿšจ

Troubleshooting ๐Ÿ”ง๐Ÿš‘

  1. For unauthorized errors, check if the API key is correctly set and used. ๐Ÿ”‘โ“
  2. If image generation fails, verify that the Cloudflare API Token has the correct permissions. ๐ŸŽŸ๏ธ๐Ÿ”
  3. If prompt optimization is not working, please ensure PROMPT_OPTIMIZATION is set to 'true' and the external API is properly configured. ๐ŸŒ๐Ÿ”ง
  4. For 404 errors, ensure you're accessing the correct endpoint path. ๐Ÿ”๐Ÿšท
  5. For other errors, check the Worker's logs for more detailed error information. ๐Ÿ“‹๐Ÿ”ฌ

Further Customization ๐Ÿ› ๏ธ๐ŸŽจ

You can further optimize the API's functionality by modifying the Worker code, for example:

I hope this README helps you quickly deploy and use Flux-API-Worker. If you have any questions or need further assistance, please feel free to contact me. ๐Ÿ’Œ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

If you find this repo helpful, please give it a star. โญโญโญ Thank you!