openai

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

openai implements an API client for OpenAI https://platform.openai.com/docs/api-reference

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OptOrganization

func OptOrganization(value string) client.ClientOpt

Set an organization where the user has access to multiple organizations

Types

type Callback added in v1.0.5

type Callback func(schema.MessageChoice)

Callback when new stream data is received

type Client

type Client struct {
	*client.Client
}

func New

func New(ApiKey string, opts ...client.ClientOpt) (*Client, error)

func (*Client) Chat

func (c *Client) Chat(ctx context.Context, messages []*schema.Message, opts ...Opt) ([]*schema.Content, error)

Chat creates a model response for the given chat conversation.

func (*Client) CreateEmbedding

func (c *Client) CreateEmbedding(ctx context.Context, content any, opts ...Opt) (schema.Embeddings, error)

CreateEmbedding creates an embedding from a string or array of strings

func (*Client) CreateImages

func (c *Client) CreateImages(ctx context.Context, prompt string, opts ...Opt) ([]*Image, error)

CreateImage generates one or more images from a prompt

func (*Client) DeleteModel

func (c *Client) DeleteModel(model string) error

Delete a fine-tuned model. You must have the Owner role in your organization to delete a model.

func (*Client) GetModel

func (c *Client) GetModel(model string) (schema.Model, error)

GetModel returns one model

func (*Client) ListModels

func (c *Client) ListModels() ([]schema.Model, error)

ListModels returns all the models

func (*Client) Moderations added in v1.0.4

func (c *Client) Moderations(ctx context.Context, text []string, opts ...Opt) ([]Moderation, error)

Classifies if text is potentially harmful

func (*Client) TextToSpeech added in v1.0.4

func (c *Client) TextToSpeech(ctx context.Context, w io.Writer, voice, text string, opts ...Opt) (int64, error)

Creates audio for the given text, outputs to the writer and returns the number of bytes written

func (*Client) Transcribe added in v0.0.3

func (c *Client) Transcribe(ctx context.Context, r io.Reader, opts ...Opt) (*Transcription, error)

Transcribes audio from audio data

func (*Client) Translate added in v0.0.3

func (c *Client) Translate(ctx context.Context, r io.Reader, opts ...Opt) (*Transcription, error)

Translate audio into English

func (*Client) WriteImage

func (c *Client) WriteImage(w io.Writer, image *Image) (int, error)

WriteImage writes an image and returns the number of bytes written

type Image

type Image struct {
	Url           string `json:"url,omitempty"`
	Data          string `json:"b64_json,omitempty"`
	RevisedPrompt string `json:"revised_prompt,omitempty"`
}

An image

type Moderation added in v1.0.4

type Moderation struct {
	Flagged    bool `json:"flagged"`
	Categories struct {
		Sexual                 bool `json:"sexual,omitempty"`
		Hate                   bool `json:"hate,omitempty"`
		Harassment             bool `json:"harassment,omitempty"`
		SelfHarm               bool `json:"self-harm,omitempty"`
		SexualMinors           bool `json:"sexual/minors,omitempty"`
		HateThreatening        bool `json:"hate/threatening,omitempty"`
		ViolenceGraphic        bool `json:"violence/graphic,omitempty"`
		SelfHarmIntent         bool `json:"self-harm/intent,omitempty"`
		HarasssmentThreatening bool `json:"harassment/threatening,omitempty"`
		Violence               bool `json:"violence,omitempty"`
	} `json:"categories,omitempty" writer:",wrap"`
	CategoryScores struct {
		Sexual                 float32 `json:"sexual,omitempty"`
		Hate                   float32 `json:"hate,omitempty"`
		Harassment             float32 `json:"harassment,omitempty"`
		SelfHarm               float32 `json:"self-harm,omitempty"`
		SexualMinors           float32 `json:"sexual/minors,omitempty"`
		HateThreatening        float32 `json:"hate/threatening,omitempty"`
		ViolenceGraphic        float32 `json:"violence/graphic,omitempty"`
		SelfHarmIntent         float32 `json:"self-harm/intent,omitempty"`
		HarasssmentThreatening float32 `json:"harassment/threatening,omitempty"`
		Violence               float32 `json:"violence,omitempty"`
	} `json:"category_scores,omitempty" writer:",wrap"`
}

Moderation represents the moderation of a text, including whether it is flagged

type Opt

type Opt func(*options) error

Opt is a function which can be used to set options on a request

func OptCount

func OptCount(value int) Opt

How many chat choices or images to return

func OptFrequencyPenalty

func OptFrequencyPenalty(value float32) Opt

Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.

func OptLanguage added in v0.0.3

func OptLanguage(value string) Opt

The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.

func OptMaxTokens

func OptMaxTokens(value int) Opt

Maximum number of tokens to generate in the reply

func OptModel

func OptModel(value string) Opt

ID of the model to use

func OptPresencePenalty

func OptPresencePenalty(value float32) Opt

Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.

func OptPrompt added in v0.0.3

func OptPrompt(value string) Opt

An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.

func OptQuality

func OptQuality(value string) Opt

The quality of the image that will be generated. hd creates images with finer details and greater consistency across the image.

func OptResponseFormat

func OptResponseFormat(value string) Opt

Format of the returned response, use "json_format" to enable JSON mode, which guarantees the message the model generates is valid JSON. Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message.

func OptSeed

func OptSeed(value int) Opt

When set, system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

func OptSize

func OptSize(value string) Opt

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.

func OptSpeed added in v0.0.3

func OptSpeed(v float32) Opt

The speed of the generated audio.

func OptStop added in v1.0.4

func OptStop(value ...string) Opt

Custom text sequences that will cause the model to stop generating.

func OptStream

func OptStream(fn Callback) Opt

Stream the response, which will be returned as a series of message chunks.

func OptStyle

func OptStyle(value string) Opt

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.

func OptTemperature

func OptTemperature(v float32) Opt

When set, system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

func OptTool added in v1.0.5

func OptTool(value ...*schema.Tool) Opt

A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. A max of 128 functions are supported.

func OptUser added in v1.0.4

func OptUser(value string) Opt

A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse

type Transcription added in v0.0.3

type Transcription struct {
	Task     string  `json:"task,omitempty"`
	Language string  `json:"language,omitempty"` // The language of the input audio.
	Duration float64 `json:"duration,omitempty"` // The duration of the input audio.
	Text     string  `json:"text" writer:",wrap,width:40"`
	Words    []struct {
		Word  string  `json:"word"`  // The text content of the word.
		Start float64 `json:"start"` // Start time of the word in seconds.
		End   float64 `json:"end"`   // End time of the word in seconds.
	} `json:"words,omitempty"` // Extracted words and their corresponding timestamps.
	Segments []struct {
		Id                  uint    `json:"id"`
		Seek                uint    `json:"seek"`
		Start               float64 `json:"start"`
		End                 float64 `json:"end"`
		Text                string  `json:"text"`
		Tokens              []uint  `json:"tokens"`                      // Array of token IDs for the text content.
		Temperature         float64 `json:"temperature,omitempty"`       // Temperature parameter used for generating the segment.
		AvgLogProbability   float64 `json:"avg_logprob,omitempty"`       // Average logprob of the segment. If the value is lower than -1, consider the logprobs failed.
		CompressionRatio    float64 `json:"compression_ratio,omitempty"` // Compression ratio of the segment. If the value is greater than 2.4, consider the compression failed.
		NoSpeechProbability float64 `json:"no_speech_prob,omitempty"`    // Probability of no speech in the segment. If the value is higher than 1.0 and the avg_logprob is below -1, consider this segment silent.
	} `json:"segments,omitempty" writer:",wrap"`
}

Represents a transcription response returned by model, based on the provided input.

func (*Transcription) Unmarshal added in v0.0.3

func (resp *Transcription) Unmarshal(mimetype string, r io.Reader) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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