dalle

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2025 License: MIT Imports: 7 Imported by: 0

README ¶

Go Report Card GoDoc

Dalle API Client

Overview

This dependency provides a simple Go client for interacting with the Dalle API. It allows you to generate images from a prompt by sending HTTP requests to the image generation endpoint. The client is designed to be configurable with options for setting a custom HTTP client and request timeout.

Features

  • Create a new client instance using your API key.
  • Optionally specify a custom domain (defaults to https://api.openai.com).
  • Configure HTTP client settings such as timeout or a custom HTTP client.
  • Generate images by providing a prompt, the number of images to generate, and the desired image size.
  • Handles request marshalling and HTTP POST operations internally.

Installation

  1. Include the package in your Go project by importing it (for example, yourmodule/dalle).
  2. Ensure you have a valid API key from the image generation service.

Usage Example

package main

import (
    "fmt"
    "time"
    "github.com/junkd0g/dalle-e/dalle"
)

func main() {
    // Create a new Dalle client with your API key.
    client, err := dalle.NewClient("your_api_key", "")
    if err != nil {
        fmt.Println("Error creating client:", err)
        return
    }

    // Optionally, set a custom timeout.
    client, err = dalle.NewClient("your_api_key", "", dalle.WithTimeout(30*time.Second))
    if err != nil {
        fmt.Println("Error creating client:", err)
        return
    }

    // Generate an image using a prompt.
    response, err := client.GenerateImageV1("A futuristic city skyline", 1, "1024x1024")
    if err != nil {
        fmt.Println("Error generating image:", err)
        return
    }

    // Process the response (response is a byte slice).
    fmt.Println("Response:", string(response))
}

Configuration Options

  • APIKey (string): Your API authentication key. This is required.
  • Domain (string): The API domain to use. If left empty, the default value https://api.openai.com is used.
  • WithHTTPClient: An option to provide a custom HTTP client.
  • WithTimeout: An option to set a specific timeout for API requests.

Error Handling

  • An error is returned if the API key is not provided during client creation.
  • Errors during JSON marshalling or the HTTP request are propagated back to the caller.

Additional Notes

  • The GenerateImageV1 function constructs the request using the provided prompt, number of images (n), and size.
  • The client’s post method constructs the URL using the domain, API version (/v1), and endpoint (/images/generations).
  • Response data is returned as a byte slice. You may need to further process or decode this data according to your application’s requirements.
  • The printed output of the API key in the post function is for debugging purposes; consider removing or securing it in production.

📝 License

This project is licensed under the MIT License. See the LICENSE file for details.

Authors

Documentation ¶

Index ¶

Constants ¶

View Source
const (
	// DefaultDomain is the default API endpoint used if no custom domain is provided.
	DefaultDomain = "https://api.openai.com"
)

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type Client ¶

type Client struct {
	// The API key to use for authentication.
	APIKey string
	// The Domain to use for the API.
	Domain string
	// The HTTP client to use for requests.
	Client http.Client
}

Client is the main struct for the Dalle API client.

func NewClient ¶

func NewClient(apiKey string, domain string, opts ...ClientOption) (*Client, error)

NewClient initializes and returns a new instance of the Dalle API client. It requires a non-empty API key and optionally accepts a custom domain. Additional configuration can be applied using functional options.

func (*Client) GenerateImageV1 ¶

func (c *Client) GenerateImageV1(prompt string, n int, size string) ([]byte, error)

GenerateImageV1 sends a request to generate an image from a provided prompt. It takes a prompt string, the number of images (n), and the size as parameters. The function returns the response as a byte slice or an error if any.

type ClientOption ¶

type ClientOption func(*Client)

ClientOption defines a function type for configuring the Client. It allows for modular and flexible client configuration.

func WithHTTPClient ¶

func WithHTTPClient(httpClient *http.Client) ClientOption

WithHTTPClient returns a ClientOption that sets a custom HTTP client for making API requests.

func WithTimeout ¶

func WithTimeout(timeout time.Duration) ClientOption

WithTimeout returns a ClientOption that configures the timeout duration for API requests.

type ImageGenerationRequest ¶

type ImageGenerationRequest struct {
	Prompt string `json:"prompt"`
	N      int    `json:"n"`
	Size   string `json:"size"`
}

ImageGenerationRequest is the request body for the image generation endpoint.

Directories ¶

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL