openai

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: Apache-2.0 Imports: 11 Imported by: 8

README

openai-go Go Reference Go

Go client libraries for OpenAI APIs. Supported APIs:

✅ completions
✅ chat
✅ edits
🚧 images
✅ moderations
✅ audio
✅ embeddings

Set your API key as the OPENAI_API_KEY environmental variable before running the examples.

Documentation

Overview

Package openai contains Go client libraries for OpenAI libraries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	Payload    []byte
}

APIError is returned from API requests if the API responds with an error.

func (*APIError) Error

func (e *APIError) Error() string

type Session

type Session struct {
	// OrganizationID is the ID optionally to be included as
	// a header to requests made from this session.
	// This field must be set before session is used.
	OrganizationID string

	// HTTPClient providing a custom HTTP client.
	// This field must be set before session is used.
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

Session is a session created to communicate with OpenAI.

func NewSession

func NewSession(apiKey string) *Session

NewSession creates a new session. Organization IDs are optional, use an empty string when you don't want to set one.

func (*Session) MakeRequest

func (s *Session) MakeRequest(ctx context.Context, endpoint string, input, output any) error

MakeRequest make HTTP requests and authenticates them with session's API key. MakeRequest marshals input as the request body, and unmarshals the response as output.

func (*Session) MakeStreamingRequest added in v1.0.3

func (s *Session) MakeStreamingRequest(ctx context.Context, endpoint string, input any, output any, fn func(any)) error

func (*Session) Upload added in v1.0.1

func (s *Session) Upload(ctx context.Context, endpoint string, file io.Reader, fileExt string, params url.Values, output any) error

Upload makes a multi-part form data upload them with session's API key. Upload combines the file with the given params and unmarshals the response as output.

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens,omitempty"`
	CompletionTokens int `json:"completion_tokens,omitempty"`
	TotalTokens      int `json:"total_tokens,omitempty"`
}

Usage reports the API usage.

Directories

Path Synopsis
Package audio implements a client for OpenAI's Whisper audio transcriber.
Package audio implements a client for OpenAI's Whisper audio transcriber.
Package chat contains a client for Open AI's ChatGPT APIs.
Package chat contains a client for Open AI's ChatGPT APIs.
Package completion contains a client for OpenAI's completion API.
Package completion contains a client for OpenAI's completion API.
Package edit contains a client for OpenAI's edits API.
Package edit contains a client for OpenAI's edits API.
Package embedding contains a client for Open AI's Embeddings APIs.
Package embedding contains a client for Open AI's Embeddings APIs.
examples
Package image contains a client for OpenAI's images API.
Package image contains a client for OpenAI's images API.
Package moderation contains a client for OpenAI's moderations API.
Package moderation contains a client for OpenAI's moderations API.

Jump to

Keyboard shortcuts

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