openai

package
v5.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const OpenAIBackend = "https://api.openai.com/v1"

OpenAIBackend is the default URI endpoint for the OpenAI API

Variables

This section is empty.

Functions

This section is empty.

Types

type Conversation

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

Conversation is a struct used to converse with an OpenAI chat model. It maintains all messages sent/received in order to maintain context just like using ChatGPT.

func (*Conversation) AddHeader added in v5.2.0

func (conv *Conversation) AddHeader(key, val string)

AddHeader adds an extra HTTP header that will be added to every HTTP request issued as part of this conversation. Any headers added will be in addition to any extra headers defined for the backend itself, and will take precedence over them.

func (*Conversation) Messages added in v5.1.0

func (conv *Conversation) Messages() []types.Message

Messages returns all the messages that have been exchanged between the user and the assistant up to this point.

func (*Conversation) Send

func (conv *Conversation) Send(ctx context.Context, prompt string) (
	res types.Response,
	err error,
)

Send sends the provided message to the API and returns a Response object. To maintain context, all previous messages (whether from you to the API or vice-versa) are sent as well, allowing you to ask the API to modify the code it already generated.

type OpenAI

type OpenAI struct {
	*requests.HTTPClient
	// contains filtered or unexported fields
}

OpenAI is a structure used to continuously generate IaC code via OpenAPI

func New

func New(opts *Options) (*OpenAI, error)

New creates a new instance of the OpenAI struct, with the provided input options. The OpenAI API backend is not yet contacted at this point.

func (*OpenAI) Chat

func (backend *OpenAI) Chat(model string, msgs ...types.Message) types.Conversation

Chat initiates a conversation with an OpenAI chat model. A conversation maintains context, allowing to send further instructions to modify the output from previous requests, just like using the ChatGPT website. The name of the model to use must be provided. Users can also supply zero or more "previous messages" that may have been exchanged in the past. This practically allows "loading" previous conversations and continuing them.

func (*OpenAI) ListModels

func (backend *OpenAI) ListModels(ctx context.Context) (
	models []string,
	err error,
)

ListModels returns a list of all the models supported by this backend.

type Options

type Options struct {
	// APIKey is the OpenAI API key to use for requests. Optional, but most
	// OpenAI-compatible deployments will require it.
	ApiKey string

	// URL is the OpenAI API URL to userequests. Optional, defaults to OpenAIBackend.
	URL string

	// APIVersion is the version of the OpenAI API to use. Optional.
	APIVersion string

	// AuthHeader allows modifying the header where the API key is sent. This
	// defaults to Authorization. If it is "Authorization" or
	// "Proxy-Authorization", the API key is sent with a "Bearer " prefix. If
	// it's anything else, the API key is sent alone.
	AuthHeader string

	// ExtraHeaders are extra HTTP headers to send with every request to the
	// provider.
	ExtraHeaders map[string]string
}

Options is a struct containing all the parameters accepted by the New constructor.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳