Skip to content

A high-performance Go-based API proxy service for the Akash Network's image generation capabilities.

License

Notifications You must be signed in to change notification settings

006lp/akashgen-api-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AkashGen API Go

Go Version License API

A high-performance Go-based API proxy service for the Akash Network's image generation capabilities. This service provides a clean REST API interface to interact with Akash Network's decentralized AI image generation infrastructure.

δΈ­ζ–‡ζ–‡ζ‘£ | English

Features

  • πŸš€ High Performance: Built with Go and Gin framework for optimal performance
  • πŸ”„ Asynchronous Processing: Non-blocking job submission with status polling
  • πŸ›‘οΈ Concurrency Control: Built-in rate limiting to prevent resource exhaustion
  • πŸ“Š Structured Logging: Comprehensive logging with Zap for production monitoring
  • πŸ—οΈ Clean Architecture: Modular design following Go best practices
  • 🌐 REST API: Simple and intuitive HTTP endpoints
  • ⚑ Graceful Shutdown: Proper cleanup and connection handling
  • 🎯 GPU Preference: Automatic GPU selection from available options

Quick Start

Prerequisites

  • Go 1.21 or higher
  • Internet connection to access Akash Network

Installation

  1. Clone the repository

    git clone https://github.com/006lp/akashgen-api-go.git
    cd akashgen-api-go
  2. Initialize Go modules

    go mod init akashgen-api-go
    go mod tidy
  3. Build and run

    go build -o akashgen-api .
    ./akashgen-api

Or run directly:

go run main.go

The server will start on port 6571 by default.

API Documentation

Generate Image

Generate an AI image using Akash Network's decentralized infrastructure.

Endpoint: POST /api/generate

Request Body:

{
    "prompt": "A beautiful sunset over mountains",
    "negative": "blurry, low quality",
    "sampler": "DPM++ 2M Karras",
    "scheduler": "karras"
}

Parameters:

  • prompt (required): Text description of the desired image
  • negative (optional): Text describing what to avoid in the image
  • sampler (required): The sampling method to use
  • scheduler (required): The scheduling algorithm

Supported Samplers:

euler, euler_cfg_pp, euler_ancestral, euler_ancestral_cfg_pp, heun, heunpp2, 
dpm_2, dpm_2_ancestral, lms, dpm_fast, dpm_adaptive, dpmpp_2s_ancestral, 
dpmpp_2s_ancestral_cfg_pp, dpmpp_sde, dpmpp_sde_gpu, dpmpp_2m, dpmpp_2m_cfg_pp, 
dpmpp_2m_sde, dpmpp_2m_sde_gpu, dpmpp_3m_sde, dpmpp_3m_sde_gpu, ddpm, lcm, 
ipndm, ipndm_v, deis, ddim, uni_pc, uni_pc_bh2

Supported Schedulers:

normal, karras, exponential, sgm_uniform, simple, ddim_uniform, beta, linear_quadratic

Response:

  • Success (200): Returns the generated image as binary data
  • Error (400): Invalid request format
  • Error (500): Generation failed or timeout

Example using curl:

curl -X POST http://localhost:6571/api/generate \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "A serene lake with mountains in the background",
    "negative": "ugly, blurry, low resolution",
    "sampler": "DPM++ 2M Karras",
    "scheduler": "karras"
  }' \
  --output generated_image.png

Health Check

Check if the service is running properly.

Endpoint: GET /health

Response:

{
    "status": "ok"
}

Configuration

The service can be configured by modifying the constants in config/config.go:

const (
    // Timeout configurations
    GenerateTimeout    = 30 * time.Second  // Max time for generation request
    StatusTimeout      = 10 * time.Second  // Max time for status check
    ImageFetchTimeout  = 30 * time.Second  // Max time for image download
    PollingInterval    = 1 * time.Second   // How often to check job status
    MaxPollingDuration = 5 * time.Minute   // Max time to wait for completion
    
    // Concurrency
    MaxConcurrentRequests = 10  // Max simultaneous requests
    
    // Server
    ServerPort = ":6571"  // Port to listen on
)

Architecture

akashgen-api-go/
β”œβ”€β”€ main.go              # Application entry point
β”œβ”€β”€ config/
β”‚   └── config.go       # Configuration constants
β”œβ”€β”€ handlers/
β”‚   └── generate.go     # HTTP request handlers
β”œβ”€β”€ models/
β”‚   └── types.go        # Data structures and types
β”œβ”€β”€ services/
β”‚   β”œβ”€β”€ akash.go        # Akash Network API client
β”‚   └── image.go        # Image fetching service
β”œβ”€β”€ middleware/
β”‚   └── logger.go       # HTTP logging middleware
└── utils/
    └── http.go         # HTTP utilities

Components

  • Handlers: Process HTTP requests and responses
  • Services: Business logic for interacting with external APIs
  • Models: Data structures for requests and responses
  • Middleware: Cross-cutting concerns like logging
  • Config: Centralized configuration management
  • Utils: Reusable utility functions

Development

Project Structure

This project follows Go best practices with clear separation of concerns:

  • Clean architecture with dependency injection
  • Modular design for easy testing and maintenance
  • Proper error handling and logging
  • Context-based timeout management

Building for Production

# Build optimized binary
go build -ldflags="-w -s" -o akashgen-api .

# Or build for different platforms
GOOS=linux GOARCH=amd64 go build -o akashgen-api-linux .

Docker Support

Create a Dockerfile:

FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN go build -o akashgen-api .

FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/akashgen-api .
EXPOSE 6571
CMD ["./akashgen-api"]

Build and run:

docker build -t akashgen-api .
docker run -p 6571:6571 akashgen-api

GPU Support

The service automatically selects from preferred GPU types in order:

  1. RTX4090
  2. A10
  3. A100
  4. V100-32Gi
  5. H100

Error Handling

The API provides detailed error responses:

{
    "error": "Failed to generate image",
    "details": "specific error description"
}

Common error scenarios:

  • Invalid JSON format
  • Missing required fields
  • Network timeouts
  • Upstream service failures
  • Job execution failures

Performance

  • Concurrency: Configurable request limiting prevents overload
  • Timeouts: Multiple timeout layers prevent hanging requests
  • Connection Pooling: HTTP client reuse for efficiency
  • Memory Management: Proper cleanup of resources
  • Graceful Shutdown: Clean termination handling

Monitoring

The service provides structured JSON logs suitable for aggregation:

{
    "level": "info",
    "ts": 1640995200.123456,
    "msg": "HTTP Request",
    "method": "POST",
    "path": "/api/generate",
    "status": 200,
    "duration": "2.5s",
    "client_ip": "192.168.1.100"
}

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow Go conventions and best practices
  • Add tests for new functionality
  • Update documentation for API changes
  • Use meaningful commit messages
  • Ensure code passes go fmt and go vet

Testing

# Run tests
go test ./...

# Run tests with coverage
go test -cover ./...

# Run race condition detection
go test -race ./...

Troubleshooting

Common Issues

  1. Port already in use: Change the port in config/config.go
  2. Timeout errors: Increase timeout values for slower networks
  3. Memory issues: Reduce MaxConcurrentRequests if experiencing memory pressure
  4. Connection refused: Ensure Akash Network endpoints are accessible

Debug Mode

For development, you can enable debug logging by modifying the logger initialization in main.go:

// Replace zap.NewProduction() with:
logger, err = zap.NewDevelopment()

License

This project is licensed under the AGPL v3 License - see the LICENSE file for details.

Support

Acknowledgments


Note: This is an unofficial proxy service for Akash Network's image generation API. Please refer to Akash Network's official documentation for the most up-to-date information about their services.

About

A high-performance Go-based API proxy service for the Akash Network's image generation capabilities.

Resources

License

Stars

Watchers

Forks

Languages