openai

package module
v0.0.0-...-2c08237 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

openai-go-sdk

Golang implementation for OpenAI API

Installation

To install this library, follow the instructions below.

go get -u github.com/egorkurito/openai-go-sdk

Usage Example

Here is an example of how to use the library. For more detailed information, see the documentation.

package main

import (
    "github.com/egorkurito/openai-go-sdk/openai"
    "context"
)

func main() {
    client := openai.NewClient("your_api_token")

    // Example of using a function from the library
    params := openai.AudioParams{
        FilePath: "path_to_audio_file.mp3",
        Model:    openai.Whisper1,
    }

    response, err := client.CreateTranscription(context.Background(), params)
    if err != nil {
        panic(err)
    }

    println(response.Text)
}

Contributing

We welcome and appreciate contributions to our project. If you want to help improve or expand this library, please first read our contribution guidelines.

Documentation

Index

Constants

View Source
const (
	GenerateImageModelDallE2 = "dall-e-2"
	GenerateImageModelDallE3 = "dall-e-3"

	GenerateImageQualityHD       = "hd"
	GenerateImageQualityStandard = "standard"

	GenerateImageResponseFormatURL     = "url"
	GenerateImageResponseFormatB64JSON = "b64_json"

	GenerateImageSize256x256   = "256x256"
	GenerateImageSize512x512   = "512x512"
	GenerateImageSize1024x1024 = "1024x1024"
	GenerateImageSize1792x1024 = "1792x1024"
	GenerateImageSize1024x1792 = "1024x1792"

	GenerateImageStyleVivid   = "vivid"
	GenerateImageStyleNatural = "natural"
)
View Source
const (
	Whisper1 = "whisper-1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Code           any     `json:"code,omitempty"`
	Message        string  `json:"message"`
	Param          *string `json:"param,omitempty"`
	Type           string  `json:"type"`
	HTTPStatusCode int     `json:"-"`
}

func (*APIError) Error

func (e *APIError) Error() string

type AudioParams

type AudioParams struct {
	// The audio file object (not file name) to transcribe, in one of these formats:
	// flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm.
	FilePath string

	// ID of the model to use. Only `whisper-1` is currently available.
	Model string

	// The language of the input audio.
	// Supplying the input language in [ISO-639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
	// format will improve accuracy and latency.
	Language string

	// An optional text to guide the model's style or continue a previous audio segment.
	// The [prompt](https://platform.openai.com/docs/guides/speech-to-text/prompting) should match the audio language.
	Prompt string

	// The format of the transcript output, in one of these options: `json`, `text`, `srt`, `verbose_json`, or `vtt`.
	ResponseFormat string

	// The sampling temperature, between 0 and 1.
	// Higher values like 0.8 will make the output more random, while lower values like
	// 0.2 will make it more focused and deterministic. If set to 0, the model will use
	// [log probability](https://en.wikipedia.org/wiki/Log_probability)
	// to automatically increase the temperature until certain thresholds are hit.
	Temperature float32
}

AudioParams - represents the request structure for API.

type AudioResponse

type AudioResponse struct {
	Text string `json:"text"`
}

AudioResponse represents a response structure for API.

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(apiAuthToken string) *Client

func NewClientWithConfig

func NewClientWithConfig(config ClientConfig) *Client

func (*Client) CreateTranscription

func (c *Client) CreateTranscription(ctx context.Context, params AudioParams) (*AudioResponse, error)

CreateTranscription — API call to create a transcription. Returns transcribed text.

func (*Client) CreateTranslation

func (c *Client) CreateTranslation(ctx context.Context, params AudioParams) (*AudioResponse, error)

CreateTranslation — API call to translate audio into English.

func (*Client) GenerateImage

func (c *Client) GenerateImage(ctx context.Context, params GenerateImageParams) (*GenerateImageResponse, error)

GenerateImage - API call to generate image.

type ClientConfig

type ClientConfig struct {
	BaseURL      string
	Organization string
	HTTPClient   *http.Client
	// contains filtered or unexported fields
}

func DefaultConfig

func DefaultConfig(apiAuthToken string) ClientConfig

type ErrorResponse

type ErrorResponse struct {
	Error *APIError `json:"error,omitempty"`
}

type GenerateImageParams

type GenerateImageParams struct {
	// A text description of the desired image(s).
	// The maximum length is 1000 characters for `dall-e-2` and 4000 characters for `dall-e-3`.
	Prompt string `json:"prompt,omitempty"`

	// The model to use for image generation.
	Model string `json:"model,omitempty"`

	// The number of images to generate.
	// Must be between 1 and 10. For `dall-e-3`, only `n=1` is supported.
	N int `json:"n,omitempty"`

	// The quality of the image that will be generated.
	// `hd` creates images with finer details and greater consistency across the image.
	// This param is only supported for `dall-e-3`.
	Quality string `json:"quality,omitempty"`

	// The format in which the generated images are returned.
	// Must be one of `url` or `b64_json`.
	ResponseFormat string `json:"response_format,omitempty"`

	// The size of the generated images.
	// Must be one of `256x256`, `512x512`, or `1024x1024` for `dall-e-2`. Must be one
	// of `1024x1024`, `1792x1024`, or `1024x1792` for `dall-e-3` models.
	Size string `json:"size,omitempty"`

	// The style of the generated images.
	// Must be one of `vivid` or `natural`. Vivid causes the model to lean towards
	// generating hyper-real and dramatic images. Natural causes the model to produce
	// more natural, less hyper-real looking images. This param is only supported for `dall-e-3`.
	Style string `json:"style,omitempty"`

	// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
	// [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
	User string `json:"user,omitempty"`
}

GenerateImageParams - represents the request structure for API.

type GenerateImageResponse

type GenerateImageResponse struct {
	Created int `json:"created"`
	Data    []struct {
		URL           string `json:"url,omitempty"`
		B64JSON       string `json:"b64_json,omitempty"`
		RevisedPrompt string `json:"revised_prompt,omitempty"`
	} `json:"data"`
}

GenerateImageResponse - represents a response structure for API.

type RequestError

type RequestError struct {
	HTTPStatusCode int
	Err            error
}

func (*RequestError) Error

func (e *RequestError) Error() string

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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