openai

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 5 Imported by: 0

README

OpenAI Go Client

This is an OpenAI client for Go I wrote for personal use. Do not expect it to necessarily be up to date quickly.

Go Reference

Usage

Creating a client
import "github.com/jclem/openai-go"

client := openai.NewClient(openai.WithKey(yourAPIKey))

// Optionally, provide a custom HTTP "Do"-er.
client = openai.NewClient(
	openai.WithKey(yourApiKey),
	openai.WithDoer(http.DefaultClient),
)
Making a completion request

Use the client's chat service to create a completion call.

import "github.com/jclem/openai-go/pkg/chat"

comp, err := client.Chat.CreateCompletion(
	context.Background(),
	"gpt-4",
	[]chat.Message{chat.NewMessage("user", chat.WithMessageContent("Hello, world"))},
	chat.WithTemperature(0.6),
)

// Various methods exist to easily read the completion.
content, ok := comp.GetContentAt(0)
Making a streaming completion request

Use the client's chat service to create a streaming completion call.

import "github.com/jclem/openai-go/pkg/chat"

stream, err := client.Chat.CreateStreamingCompletion(
	context.Background(),
	"gpt-4",
	[]chat.Message{chat.NewMessage("user", chat.WithMessageContent("Hello, world"))},
	chat.WithTemperature(0.6),
)

// Call `stream.Next()` to get the next stream completion object. It'll return
// `nil, nil` when done.
for {
	chunk, err := stream.Next()
	if err != nil {
		// Handle error.
	}

	if chunk == nil {
		break
	}

	// Various methods exist to easily read the stream chunk.
	content, ok := chunk.GetContentAt(0)
	if ok {
		fmt.Printf(content)
	}
}

// The caller must close the stream.
if closeErr := stream.Close(); closeErr != nil {
	// Handle error.
}
Creating embeddings

Use CreateEmbeddings to create embeddings, and get back a parsed response.

resp, err := client.Embeddings.Create(
	context.Background(),
	"text-embedding-ada-002",
	[]string{"HEllo, world."},
)

var embedding []float64 = resp.Data[0].Embedding

Documentation

Overview

Package openai provides an OpenAI-compatible API client.

Index

Constants

This section is empty.

Variables

View Source
var DefaultBaseURL = &url.URL{
	Scheme: "https",
	Host:   "api.openai.com",
	Path:   "/v1",
}

DefaultBaseURL is the default base URL for the OpenAI API.

Functions

This section is empty.

Types

type Client added in v0.1.4

type Client struct {
	Chat       *chat.ChatService
	Embeddings *embeddings.EmbeddingsService
	// contains filtered or unexported fields
}

A Client is an OpenAI-compatible API client.

func NewClient added in v0.1.4

func NewClient(opts ...ClientOpt) *Client

NewClient creates a new Client.

type ClientOpt added in v0.1.4

type ClientOpt func(*Client)

A ClientOpt is a functional option for configuring a Client.

func WithBaseURL added in v0.1.4

func WithBaseURL(baseURL *url.URL) ClientOpt

WithBaseURL sets the base URL for the Client.

The default value is "https://api.openai.com/v1".

func WithDoer added in v0.1.4

func WithDoer(doer Doer) ClientOpt

WithDoer sets the HTTP client for the Client.

func WithKey

func WithKey(key string) ClientOpt

WithKey sets the API key for the Client.

If no key is provided, one must be provided for every request.

type Doer added in v0.1.4

type Doer interface {
	Do(*http.Request) (*http.Response, error)
}

A Doer is an interface for performing HTTP requests.

Directories

Path Synopsis
internal
httptesting
Package httptesting provides internal testing utilities.
Package httptesting provides internal testing utilities.
service
Package service provides common types used by services.
Package service provides common types used by services.
pkg
chat
Package chat provides a chat client for the OpenAI API.
Package chat provides a chat client for the OpenAI API.
embeddings
Package embeddings provides a embeddings client for the OpenAI API.
Package embeddings provides a embeddings client for the OpenAI API.

Jump to

Keyboard shortcuts

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