goopenai

package module
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 11 Imported by: 0

README

This Fork

The original library from Francisco Escher has the OpenAI endpoints hard-coded as constants. This fork will add an alternate completion call that allows you to specify any server endpoint that mimics that the OpenAI API, e.g. the llama.cpp server interface.

Go OpenAI

This is a Go client library for the OpenAI API.

It implements the methods described in the docs: https://platform.openai.com/docs/api-reference/introduction

Implemented methods can be found in the Interface.go file.

Installation

go get github.com/franciscoescher/goopenai/v3

Usage

First, you need to create a client with the api key and organization id.

client := goopenai.NewClient(apiKey, organization)

Then, you can use the client to call the api.

Example:

package main

import (
	"context"
	"fmt"

	"github.com/franciscoescher/goopenai"
)

func main() {
	apiKey := os.Getenv("API_KEY")
	organization := os.Getenv("API_ORG")

	client := goopenai.NewClient(apiKey, organization)

	r := &goopenai.CreateChatCompletionsRequest{
		Model: "gpt-3.5-turbo",
		Messages: []goopenai.Message{
			{
				Role:    "user",
				Content: "Say this is a test!",
			},
		},
		Temperature: 0.7,
	}

	completions, err := client.CreateChatCompletions(context.Background(), r)
	if err != nil {
		panic(err)
	}

	fmt.Println(completions)
}

Run this code using:

API_KEY=<your-api-key> API_ORG=<your-org-id> go run .

Note

This library is not complete and not fully tested.

Feel free to contribute.

Documentation

Overview

Package goopenai is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var ErrStrArrayUnsupportedType = errors.New("unsupported type, must be string or []string")

Functions

This section is empty.

Types

type Client

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

func NewClient

func NewClient(apiKey string, organization string) *Client

NewClient creates a new client

func (*Client) Call

func (c *Client) Call(ctx context.Context, method string, url string, body io.Reader) (response *http.Response, err error)

Call makes a request

func (*Client) CreateChatCompletions

func (c *Client) CreateChatCompletions(ctx context.Context, r *CreateChatCompletionsRequest, url string) (response *CreateChatCompletionsResponse, err error)

func (*Client) CreateChatCompletionsRaw

func (c *Client) CreateChatCompletionsRaw(ctx context.Context, r *CreateChatCompletionsRequest, url string) ([]byte, error)

CreateChatCompletionsRaw creates a new completion. To use the OpenAI Chat API, pass "" as the url. Otherwise pass a valide endpoint for any server that mimics the API.

func (*Client) CreateEdits

func (c *Client) CreateEdits(ctx context.Context, r *CreateEditsRequest) (response *CreateEditsResponse, err error)

func (*Client) CreateEditsRaw

func (c *Client) CreateEditsRaw(ctx context.Context, r *CreateEditsRequest) ([]byte, error)

func (*Client) CreateEmbeddings

func (c *Client) CreateEmbeddings(ctx context.Context, r *CreateEmbeddingsRequest) (response *CreateEmbeddingsResponse, err error)

func (*Client) CreateEmbeddingsRaw

func (c *Client) CreateEmbeddingsRaw(ctx context.Context, r *CreateEmbeddingsRequest) ([]byte, error)

func (*Client) CreateImages

func (c *Client) CreateImages(ctx context.Context, r *CreateImagesRequest) (response *CreateImagesResponse, err error)

func (*Client) CreateImagesEdits

func (c *Client) CreateImagesEdits(ctx context.Context, r *CreateImagesEditsRequest) (response *CreateImagesEditsResponse, err error)

func (*Client) CreateImagesEditsRaw

func (c *Client) CreateImagesEditsRaw(ctx context.Context, r *CreateImagesEditsRequest) ([]byte, error)

func (*Client) CreateImagesRaw

func (c *Client) CreateImagesRaw(ctx context.Context, r *CreateImagesRequest) ([]byte, error)

func (*Client) CreateImagesVariations

func (c *Client) CreateImagesVariations(ctx context.Context, r *CreateImagesVariationsRequest) (response *CreateImagesVariationsResponse, err error)

func (*Client) CreateImagesVariationsRaw

func (c *Client) CreateImagesVariationsRaw(ctx context.Context, r *CreateImagesVariationsRequest) ([]byte, error)

func (*Client) CreateModerations

func (c *Client) CreateModerations(ctx context.Context, r *CreateModerationsRequest) (response *CreateModerationsResponse, err error)

func (*Client) CreateModerationsRaw

func (c *Client) CreateModerationsRaw(ctx context.Context, r *CreateModerationsRequest) ([]byte, error)

func (*Client) CreateTranscriptions

func (c *Client) CreateTranscriptions(ctx context.Context, r *CreateTranscriptionsRequest) (response *CreateTranscriptionsResponse, err error)

func (*Client) CreateTranscriptionsRaw

func (c *Client) CreateTranscriptionsRaw(ctx context.Context, r *CreateTranscriptionsRequest) ([]byte, error)

func (*Client) CreateTranslations

func (c *Client) CreateTranslations(ctx context.Context, r *CreateTranslationsRequest) (response *CreateTranslationsResponse, err error)

func (*Client) CreateTranslationsRaw

func (c *Client) CreateTranslationsRaw(ctx context.Context, r *CreateTranslationsRequest) ([]byte, error)

func (*Client) Get

func (c *Client) Get(ctx context.Context, url string, input any) (response []byte, err error)

Get makes a get request

func (*Client) ListModels

func (c *Client) ListModels(ctx context.Context) (response *ListModelsResponse, err error)

func (*Client) ListModelsRaw

func (c *Client) ListModelsRaw(ctx context.Context) ([]byte, error)

func (*Client) Post

func (c *Client) Post(ctx context.Context, url string, input any) (response []byte, err error)

Post makes a post request

func (*Client) RetrieveModel

func (c *Client) RetrieveModel(ctx context.Context, id string) (response *RetrieveModelResponse, err error)

func (*Client) RetrieveModelRaw

func (c *Client) RetrieveModelRaw(ctx context.Context, id string) ([]byte, error)

type ClientInterface

type ClientInterface interface {
	// ListModels Lists the currently available models, and provides basic information about each one such as the owner and availability.
	ListModels(ctx context.Context) (response *ListModelsResponse, err error)

	// RetrieveModel Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
	RetrieveModel(ctx context.Context, id string) (response *RetrieveModelResponse, err error)

	// CreateChatCompletions Creates a completion for the provided prompt and parameters
	CreateChatCompletions(ctx context.Context, r *CreateChatCompletionsRequest, url string) (response *CreateChatCompletionsResponse, err error)

	// CreateEdits Creates a new edit for the provided input, instruction, and parameters.
	CreateEdits(ctx context.Context, r *CreateEditsRequest) (response *CreateEditsResponse, err error)

	// CreateImages Creates an image given a prompt.
	CreateImages(ctx context.Context, r *CreateImagesRequest) (response *CreateImagesResponse, err error)

	// CreateImagesEdits Creates an edited or extended image given an original image and a prompt.
	CreateImagesEdits(ctx context.Context, r *CreateImagesEditsRequest) (response *CreateImagesEditsResponse, err error)

	// CreateImagesVariations Creates a variation of a given image.
	CreateImagesVariations(ctx context.Context, r *CreateImagesVariationsRequest) (response *CreateImagesVariationsResponse, err error)

	// CreateEmbeddings Creates an embedding vector representing the input text.
	CreateEmbeddings(ctx context.Context, r *CreateEmbeddingsRequest) (response *CreateEmbeddingsResponse, err error)

	// CreateTranscriptions Transcribes audio into the input language.
	CreateTranscriptions(ctx context.Context, r *CreateTranscriptionsRequest) (response *CreateTranscriptionsResponse, err error)

	// CreateTranslations Translates audio into into English.
	CreateTranslations(ctx context.Context, r *CreateTranslationsRequest) (response *CreateTranslationsResponse, err error)

	// CreateModerations Classifies if text violates OpenAI's Content Policy
	CreateModerations(ctx context.Context, r *CreateModerationsRequest) (response *CreateModerationsResponse, err error)
}

ClientInterface is the interface for the OpenAI API client. Documentation at https://platform.openai.com/docs/api-reference/introduction

type CompletionFunciton

type CompletionFunciton struct {
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
	Parameters  any    `json:"parameters,omitempty"`
}

type CreateChatCompletionsChoice

type CreateChatCompletionsChoice struct {
	Index        int      `json:"index,omitempty"`
	Message      *Message `json:"message,omitempty"`
	Delta        *Message `json:"delta,omitempty"`
	FinishReason string   `json:"finish_reason,omitempty"`
}

type CreateChatCompletionsRequest

type CreateChatCompletionsRequest struct {
	Model            string            `json:"model,omitempty"`
	Messages         []Message         `json:"messages,omitempty"`
	Temperature      float64           `json:"temperature,omitempty"`
	TopP             *float64          `json:"top_p,omitempty"`
	N                *int              `json:"n,omitempty"`
	Stream           *bool             `json:"stream,omitempty"`
	Stop             StrArray          `json:"stop,omitempty"`
	MaxTokens        *int              `json:"max_tokens,omitempty"`
	PresencePenalty  *float64          `json:"presence_penalty,omitempty"`
	FrequencyPenalty *float64          `json:"frequency_penalty,omitempty"`
	LogitBias        map[string]string `json:"logit_bias,omitempty"`
	ResponseFormat   *ResponseFormat   `json:"response_format,omitempty"`
	Seed             *int              `json:"seed,omitempty"`
	Tools            []Tools           `json:"tools,omitempty"`
	ToolChoice       *ToolChoice       `json:"tool_choice,omitempty"`
	User             *string           `json:"user,omitempty"`
	// The following struct is specific to the llama.cpp API and should be left empty for the OpenAI Chat API.
	CachePrompt *bool   `json:"cache_prompt,omitempty"` // default: false, pass true to request caching.
	SlotId      *int    `json:"slot_id,omitempty"`      // default: 0, pass a specific slot id to use a cached prompt.
	Prompt      *string `json:"prompt,omitempty"`       // used when asking for simple text completions

	// FunctionCall is deprecated in favor of Tools
	FunctionCall *string `json:"function_call,omitempty"`
	// Functions is deprecated in favor of Tools
	Functions []CompletionFunciton `json:"functions,omitempty"`
}

type CreateChatCompletionsResponse

type CreateChatCompletionsResponse struct {
	ID                string                        `json:"id,omitempty"`
	Choices           []CreateChatCompletionsChoice `json:"choices,omitempty"`
	Created           int                           `json:"created,omitempty"`
	Model             string                        `json:"model,omitempty"`
	SystemFingerprint string                        `json:"system_fingerprint,omitempty"`
	Object            string                        `json:"object,omitempty"`
	Usage             CreateChatCompletionsUsave    `json:"usage,omitempty"`
}

type CreateChatCompletionsUsave

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

type CreateEditsChoice

type CreateEditsChoice struct {
	Text  string `json:"text,omitempty"`
	Index int    `json:"index,omitempty"`
}

type CreateEditsRequest

type CreateEditsRequest struct {
	Model       string  `json:"model,omitempty"`
	Input       string  `json:"input,omitempty"`
	Instruction string  `json:"instruction,omitempty"`
	N           int     `json:"n,omitempty"`
	Temperature float64 `json:"temperature,omitempty"`
	TopP        float64 `json:"top_p,omitempty"`
}

type CreateEditsResponse

type CreateEditsResponse struct {
	Object  string              `json:"object,omitempty"`
	Created int                 `json:"created,omitempty"`
	Choices []CreateEditsChoice `json:"choices,omitempty"`
	Usage   CreateEditsUsage    `json:"usage,omitempty"`
}

type CreateEditsUsage

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

type CreateEmbeddingsData

type CreateEmbeddingsData struct {
	Object    string    `json:"object,omitempty"`
	Embedding []float64 `json:"embedding,omitempty"`
	Index     int       `json:"index,omitempty"`
}

type CreateEmbeddingsRequest

type CreateEmbeddingsRequest struct {
	Model string   `json:"model,omitempty"`
	Input StrArray `json:"input,omitempty"`
	User  string   `json:"user,omitempty"`
}

type CreateEmbeddingsResponse

type CreateEmbeddingsResponse struct {
	Object string                 `json:"object,omitempty"`
	Data   []CreateEmbeddingsData `json:"data,omitempty"`
	Model  string                 `json:"model,omitempty"`
	Usage  CreateEmbeddingsUsage  `json:"usage,omitempty"`
	Error  *Error                 `json:"error"`
}

type CreateEmbeddingsUsage

type CreateEmbeddingsUsage struct {
	PromptTokens int `json:"prompt_tokens,omitempty"`
	TotalTokens  int `json:"total_tokens,omitempty"`
}

type CreateImagesData

type CreateImagesData struct {
	URL string `json:"url,omitempty"`
}

type CreateImagesEditsData

type CreateImagesEditsData struct {
	URL string `json:"url,omitempty"`
}

type CreateImagesEditsRequest

type CreateImagesEditsRequest struct {
	Image          string `json:"image,omitempty"`
	Mask           string `json:"mask,omitempty"`
	Prompt         string `json:"prompt,omitempty"`
	N              int    `json:"n,omitempty"`
	Size           string `json:"size,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	User           string `json:"user,omitempty"`
}

type CreateImagesEditsResponse

type CreateImagesEditsResponse struct {
	Created int                     `json:"created,omitempty"`
	Data    []CreateImagesEditsData `json:"data,omitempty"`
}

type CreateImagesRequest

type CreateImagesRequest struct {
	Prompt         string `json:"prompt,omitempty"`
	N              int    `json:"n,omitempty"`
	Size           string `json:"size,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	User           string `json:"user,omitempty"`
}

type CreateImagesResponse

type CreateImagesResponse struct {
	Created int                `json:"created,omitempty"`
	Data    []CreateImagesData `json:"data,omitempty"`
}

type CreateImagesVariationsData

type CreateImagesVariationsData struct {
	URL string `json:"url,omitempty"`
}

type CreateImagesVariationsRequest

type CreateImagesVariationsRequest struct {
	Image          string `json:"image,omitempty"`
	N              int    `json:"n,omitempty"`
	Size           string `json:"size,omitempty"`
	ResponseFormat string `json:"response_format,omitempty"`
	User           string `json:"user,omitempty"`
}

type CreateImagesVariationsResponse

type CreateImagesVariationsResponse struct {
	Created int                          `json:"created,omitempty"`
	Data    []CreateImagesVariationsData `json:"data,omitempty"`
}

type CreateModerationsCategories

type CreateModerationsCategories struct {
	Hate            bool `json:"hate,omitempty"`
	HateThreatening bool `json:"hate/threatening,omitempty"`
	SelfHarm        bool `json:"self-harm,omitempty"`
	Sexual          bool `json:"sexual,omitempty"`
	SexualMinors    bool `json:"sexual/minors,omitempty"`
	Violence        bool `json:"violence,omitempty"`
	ViolenceGraphic bool `json:"violence/graphic,omitempty"`
}

type CreateModerationsCategoriesScores

type CreateModerationsCategoriesScores struct {
	Hate            float64 `json:"hate,omitempty"`
	HateThreatening float64 `json:"hate/threatening,omitempty"`
	SelfHarm        float64 `json:"self-harm,omitempty"`
	Sexual          float64 `json:"sexual,omitempty"`
	SexualMinors    float64 `json:"sexual/minors,omitempty"`
	Violence        float64 `json:"violence,omitempty"`
	ViolenceGraphic float64 `json:"violence/graphic,omitempty"`
}

type CreateModerationsRequest

type CreateModerationsRequest struct {
	Input StrArray `json:"input,omitempty"`
	Model string   `json:"model,omitempty"`
}

type CreateModerationsResponse

type CreateModerationsResponse struct {
	ID      string                    `json:"id,omitempty"`
	Model   string                    `json:"model,omitempty"`
	Results []CreateModerationsResult `json:"results,omitempty"`
}

type CreateModerationsResult

type CreateModerationsResult struct {
	Categories     CreateModerationsCategories       `json:"categories,omitempty"`
	CategoryScores CreateModerationsCategoriesScores `json:"category_scores,omitempty"`
	Flagged        bool                              `json:"flagged,omitempty"`
}

type CreateTranscriptionsRequest

type CreateTranscriptionsRequest struct {
	File           string  `json:"file,omitempty"`
	Model          string  `json:"model,omitempty"`
	Prompt         string  `json:"prompt,omitempty"`
	ResponseFormat string  `json:"response_format,omitempty"`
	Temperature    float64 `json:"temperature,omitempty"`
	Language       string  `json:"language,omitempty"`
}

type CreateTranscriptionsResponse

type CreateTranscriptionsResponse struct {
	Text string `json:"text,omitempty"`
}

type CreateTranslationsRequest

type CreateTranslationsRequest struct {
	File           string  `json:"file,omitempty"`
	Model          string  `json:"model,omitempty"`
	Prompt         string  `json:"prompt,omitempty"`
	ResponseFormat string  `json:"response_format,omitempty"`
	Temperature    float64 `json:"temperature,omitempty"`
}

type CreateTranslationsResponse

type CreateTranslationsResponse struct {
	Text string `json:"text,omitempty"`
}

type Error

type Error struct {
	Message string      `json:"message,omitempty"`
	Type    string      `json:"type,omitempty"`
	Param   interface{} `json:"param,omitempty"`
	Code    interface{} `json:"code,omitempty"`
}

Error is the error standard response from the API

func (*Error) Error

func (e *Error) Error() string

type ErrorResponse

type ErrorResponse struct {
	Error *Error `json:"error,omitempty"`
}

type Function

type Function struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Parameters  any    `json:"parameters"`
}

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

type HttpClient

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

type ListModelsData

type ListModelsData struct {
	ID          string   `json:"id,omitempty"`
	Object      string   `json:"object,omitempty"`
	OwnedBy     string   `json:"owned_by,omitempty"`
	Permissions []string `json:"permissions,omitempty"`
}

type ListModelsResponse

type ListModelsResponse struct {
	Data   []ListModelsData `json:"data,omitempty"`
	Object string           `json:"object,omitempty"`
}

type LlamaCppParams added in v1.2.3

type LlamaCppParams struct {
	CachePrompt *bool `json:"cache_prompt,omitempty"` // default: false, pass true to request caching.
	SlotId      *int  `json:"slot_id,omitempty"`      // default: 0, pass a specific slot id to use a cached prompt.
}

type Message

type Message struct {
	Role      string      `json:"role,omitempty"`
	Content   string      `json:"content,omitempty"`
	ToolCalls []ToolCalls `json:"tool_calls,omitempty"`
	// FunctionCall is deprecated in favor of ToolCalls
	FunctionCall *FunctionCall `json:"function_call,omitempty"`
}

Message is the message struct for the chat and completions endpoint

type MockHttpClient

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

MockHttpClient is a mock of HttpClient interface.

func NewMockHttpClient

func NewMockHttpClient(ctrl *gomock.Controller) *MockHttpClient

NewMockHttpClient creates a new mock instance.

func (*MockHttpClient) Do

func (m *MockHttpClient) Do(req *http.Request) (*http.Response, error)

Do mocks base method.

func (*MockHttpClient) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

type MockHttpClientMockRecorder

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

MockHttpClientMockRecorder is the mock recorder for MockHttpClient.

func (*MockHttpClientMockRecorder) Do

func (mr *MockHttpClientMockRecorder) Do(req interface{}) *gomock.Call

Do indicates an expected call of Do.

type ResponseFormat

type ResponseFormat struct {
	Type string `json:"type"`
}

type RetrieveModelResponse

type RetrieveModelResponse struct {
	ID          string   `json:"id,omitempty"`
	Object      string   `json:"object,omitempty"`
	OwnedBy     string   `json:"owned_by,omitempty"`
	Permissions []string `json:"permissions,omitempty"`
}

type StrArray

type StrArray []string

func (*StrArray) UnmarshalJSON

func (sa *StrArray) UnmarshalJSON(data []byte) error

type ToolCallFunction

type ToolCallFunction struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

type ToolCalls

type ToolCalls struct {
	ID       string           `json:"id,omitempty"`
	Type     string           `json:"type,omitempty"`
	Function ToolCallFunction `json:"function,omitempty"`
}

type ToolChoice

type ToolChoice struct {
	String *string           `json:"string,omitempty"`
	Object *ToolChoiceObject `json:"object,omitempty"`
}

func (*ToolChoice) MarshalJSON

func (c *ToolChoice) MarshalJSON() ([]byte, error)

type ToolChoiceObject

type ToolChoiceObject struct {
	Type     string        `json:"type"`
	Function ToolsFunction `json:"function"`
}

type Tools

type Tools struct {
	Type     string   `json:"type"`
	Function Function `json:"function"`
}

type ToolsFunction

type ToolsFunction struct {
	Name string `json:"name"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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