goopenai

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 6 Imported by: 1

README

OpenAI Binding for Go

This is a super simple wrapper for OpenAI's GPT Completion, GPT Edits, and DALL-E Image generation APIs.

Getting Started

Installing

go get -u github.com/CasualCodersProjects/gopenai

Usage

Initializing the OpenAI object.

openAI := gopenai.NewOpenAI(&gopenai.OpenAIOpts{})

Passing in an API Key.

openAI := gopenai.NewOpenAI(&gopenai.OpenAIOpts{
    APIKey: "your-api-key-here",
})

Creating a Simple Completion.

// if passed an empty string for the model and zero for the tokens, 
// will default to "text-davinci-003" and 256
results, err := openAI.CreateCompletionSimple("Write me a story about a woman named Alice", "", 0)
if err != nil {
    panic(err)
}

Creating a completion with more options.

request := types.NewDefaultCompletionRequest("Your prompt here\n")
request.MaxTokens = 1024
request.Stop = []string{"\n"}
request.N = 3

results, err := openAI.CreateCompletion(request)
if err != nil {
    panic(err)
}

Creating an Edit.

request := types.NewDefaultEditRequest("Misspell this senstence", "Correct the spelling mistakes.")

results, err := openAI.CreateEdit(request)
if err != nil {
    panic(err)
}

Creating an Image.

request := types.NewDefaultImageRequest("A dog in a hat")

results, err := openAI.CreateImage(request)
if err != nil {
    panic(err)
}

FAQ

Will you support other OpenAI APIs?

Not unless we need them! This library is for another Go project of ours, we figured we'd open source it so that others can use it too! It fits our needs, so updates will only be made on an as-needed basis. However, anyone is welcome to submit a Pull Request and add anything else they would also find useful!

Are you affiliated with OpenAI?

No. This is a community project.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type OpenAI

type OpenAI struct {
	APIKey string
	Client *http.Client
}

func NewOpenAI

func NewOpenAI(options *OpenAIOpts) OpenAI

NewOpenAI creates a new instance of the OpenAI API

func (*OpenAI) CreateChat added in v0.2.0

func (o *OpenAI) CreateChat(chatRequest *types.ChatRequest) (types.ChatResponse, error)

CreateChat creates a chat

func (*OpenAI) CreateChatSimple added in v0.2.0

func (o *OpenAI) CreateChatSimple(prompt string, maxTokens int) (types.ChatResponse, error)

CreateChatSimple creates a chat with a simple interface

func (*OpenAI) CreateCompletion

func (o *OpenAI) CreateCompletion(completionRequest *types.CompletionRequest) (types.CompletionResponse, error)

CreateCompletion creates a completion

func (*OpenAI) CreateCompletionSimple

func (o *OpenAI) CreateCompletionSimple(prompt, model string, maxTokens int) (types.CompletionResponse, error)

CreateCompletionSimple creates a completion with a simple interface

func (*OpenAI) CreateEdit

func (o *OpenAI) CreateEdit(editRequest *types.EditRequest) (types.EditResponse, error)

CreateEdit creates an edit

func (*OpenAI) CreateImage

func (o *OpenAI) CreateImage(imageRequest *types.ImageRequest) (types.ImageResponse, error)

CreateImage creates an image

func (*OpenAI) CreateTranscription added in v0.3.0

func (o *OpenAI) CreateTranscription(file []byte, filename string) (types.AudioTranscriptionResponse, error)

func (*OpenAI) ListModels

func (o *OpenAI) ListModels() (types.ModelsResponse, error)

ListModels returns a list of models

type OpenAIOpts

type OpenAIOpts struct {
	APIKey string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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