openai

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 15 Imported by: 1

Documentation

Overview

Package openai implements the OpenAI provider.

Index

Constants

View Source
const Name = "openai"

Name of the provider.

Variables

This section is empty.

Functions

func Get

func Get() provider.IProvider

Get returns a setup MongoDB, or set it up.

func ProcessResponse

func ProcessResponse(resp ResponseBody) (string, error)

ProcessResponse processes the response from the API.

func Set

func Set(s provider.IProvider)

Set sets the provider, primarily used for testing.

Types

type Choice

type Choice struct {
	FinishReason string          `json:"finish_reason"`
	Index        int             `json:"index"`
	Message      message.Message `json:"message"`
}

Choice OpenAI API definition.

type OpenAI

type OpenAI struct {
	*provider.Provider

	// Endpoint of the LLM provider.
	Endpoint string `json:"url" validate:"required"`

	// Token of the LLM provider.
	Token string `json:"-" validate:"required"`
	// contains filtered or unexported fields
}

OpenAI provider definition.

func New

func New(
	options ...provider.ClientFunc,
) (*OpenAI, error)

New creates a new OpenAI provider.

func NewDefault

func NewDefault(options ...provider.ClientFunc) (*OpenAI, error)

NewDefault creates a new OpenAI provider with default values.

func (*OpenAI) Completion

func (p *OpenAI) Completion(ctx context.Context, options ...provider.Func) (string, error)

Completion generates a completion using the provider API. Optionally pass WithResponseBody to unmarshal the response body. It will always return the original, unparsed response body, if no error.

NOTE: Not all options are available for all providers.

func (*OpenAI) GetClient

func (p *OpenAI) GetClient() any

GetClient returns the client.

type RequestBody

type RequestBody struct {
	Messages []message.Message `json:"messages"`
	Model    string            `json:"model"`
	Stream   bool              `json:"stream"`

	MaxTokens   int     `json:"max_completion_tokens,omitempty"`
	Seed        int     `json:"seed,omitempty"`
	Temperature float64 `json:"temperature,omitempty"`
	TopP        float64 `json:"top_p,omitempty"`
}

RequestBody represents the request body for the OpenAI API.

type ResponseBody

type ResponseBody struct {
	Choices []Choice `json:"choices" validate:"gt=0"`
	Created int      `json:"created"`
	ID      string   `json:"id"`
	Model   string   `json:"model"`
	Object  string   `json:"object"`
	Usage   Usage    `json:"usage"`
}

ResponseBody represents the response body from the OpenAI API.

type Usage

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

Usage OpenAI API definition.

Jump to

Keyboard shortcuts

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