openai

package module
v0.0.0-...-2575a75 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2023 License: MIT Imports: 5 Imported by: 1

README

OpenAI

Go Reference

OpenAI is a package that wraps around the OpenAI HTTP APIs. To use this package properly you will need to have an OpenAI account (currently in beta), which will give you the API key that you need. You will also be assigned to an organization, which you can optionally add to the API request.

Important to note that each application developed using the OpenAI APIs need to be approved by OpenAI.

Completion

OpenAI models can be used for multiple text related tasks. Given a prompt and some parameters, the model can return one or more predicted completions and other information.

To start using the API, create a client and set the API key and organization.

client := NewClient(apiKey, organization)

Next, we need to create a CompletionRequest, which is a map[string]any with some attached methods. The 2 mandatory fields in the request are model and prompt, which sets the model to use, as well as the prompt to send for completion. You should also send the ID for the end-user even though it is optional, because OpenAI will review your application and this is a stated criteria in the review.

request := make(CompletionRequest)
request.SetUser("test-user")
request.SetModel(TEXT_DAVINCI_002)
request.SetPrompt("Suggest three names for a horse that is a superhero.")

You can also set other parameters, which you can get from the API documentation, by treating the request as a map.

request["temperature"] = 0.75
request["max_tokens"] = 50

To send the completion to OpenAI, use the Complete method in the client, passing it the request.

cr, err := client.Complete(request)
if err != nil {
    // resolve the error
}
fmt.Println(cr.Text())

Once OpenAI responds, it will provide a CompletionResponse which is also a map[string]any that has a few methods. You will most likely just use the Text method to extract the text response, but you can also get the other response data by using the methods or treating it like a map.

Documentation

Index

Constants

View Source
const (
	COMPLETIONS_URL = "https://api.openai.com/v1/completions"
	IMAGE_URL       = "https://api.openai.com/v1/images"
	EDITS_URL       = "https://api.openai.com/v1/edits"
	MODELS_URL      = "https://api.openai.com/v1/models"
	EMBEDDINGS_URL  = "https://api.openai.com/v1/embeddings"
	FILES_URL       = "https://api.openai.com/v1/files"
	FINETUNES_URL   = "https://api.openai.com/v1/finetunes"
	MODERATIONS_URL = "https://api.openai.com/v1/moderations"

	TEXT_GPT_35_TURBO = "gpt-3.5-turbo"
	TEXT_DAVINCI_003  = "text-davinci-003"
	TEXT_DAVINCI_002  = "text-davinci-002"
	TEXT_CURIE_001    = "text-curie-001"
	TEXT_BABBAGE_001  = "text-babbage-001"
	TEXT_ADA_001      = "text-ada-001"

	CODE_DAVINCI_002 = "code-davinci-002"
	CODE_CUSHMAN_001 = "code-cushman-001"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Authorization string
	Organization  string
	Request       map[string]any
}

func NewClient

func NewClient(auth, org string) *Client

func (*Client) Complete

func (c *Client) Complete(request CompletionRequest) (response CompletionResponse, err error)

func (*Client) GenerateImage

func (c *Client) GenerateImage(request ImageRequest) (response ImageResponse, err error)

type CompletionRequest

type CompletionRequest map[string]any

CompletionRequest is the request to send to the OpenAI API for completion The 2 required fields are prompt and model You should also set the user to the actual end-user that is sending the request

func (CompletionRequest) SetModel

func (c CompletionRequest) SetModel(model string)

func (CompletionRequest) SetPrompt

func (c CompletionRequest) SetPrompt(prompt string)

func (CompletionRequest) SetUser

func (c CompletionRequest) SetUser(user string)

User is unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. You should set the user to the actual end-user that is sending the request OpenAI has since changed their policy to not require a user

type CompletionResponse

type CompletionResponse map[string]any

func (CompletionResponse) Choices

func (cr CompletionResponse) Choices() map[string]any

func (CompletionResponse) Created

func (cr CompletionResponse) Created() time.Time

func (CompletionResponse) Id

func (cr CompletionResponse) Id() string

func (CompletionResponse) Model

func (cr CompletionResponse) Model() string

func (CompletionResponse) Object

func (cr CompletionResponse) Object() string

func (CompletionResponse) Text

func (cr CompletionResponse) Text() string

func (CompletionResponse) Usage

func (cr CompletionResponse) Usage() map[string]any

type ImageRequest

type ImageRequest map[string]any

ImageRequest is the request to send to the OpenAI API for image generation The only required field is the prompt You should also set the user to the actual end-user that is sending the request

func (ImageRequest) SetFormat

func (c ImageRequest) SetFormat(format string)

func (ImageRequest) SetN

func (c ImageRequest) SetN(n string)

func (ImageRequest) SetPrompt

func (c ImageRequest) SetPrompt(prompt string)

func (ImageRequest) SetSize

func (c ImageRequest) SetSize(size string)

func (ImageRequest) SetUser

func (c ImageRequest) SetUser(user string)

User is unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse. You should set the user to the actual end-user that is sending the request

type ImageResponse

type ImageResponse map[string]any

func (ImageResponse) Created

func (cr ImageResponse) Created() time.Time

func (ImageResponse) ImageBase64

func (cr ImageResponse) ImageBase64() string

func (ImageResponse) URL

func (cr ImageResponse) URL() string

Jump to

Keyboard shortcuts

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