internal

package
v0.0.0-...-f19fa8f Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequestDataNil      = errors.New("requestData is nil")
	ErrRequestDataMessages = errors.New("requestData.Messages is nil")
)

Define static errors.

View Source
var (
	ErrInvalidFileSettings = fmt.Errorf("invalid file settings for log output")
	ErrInvalidLogLevel     = fmt.Errorf("failed to parse log level")
	ErrInvalidLogOutput    = fmt.Errorf("invalid log output setting")
)
View Source
var (
	ErrJSONUnmarshalFailed  = fmt.Errorf("json.Unmarshal failed")
	ErrJSONMarshalFailed    = fmt.Errorf("json.MarshalIndent failed")
	ErrLoggerNotFound       = fmt.Errorf("logger not found in context")
	ErrInvalidRequestFormat = fmt.Errorf("invalid request format")
)

Define static errors.

Functions

func CreateAzureChatCompletionStream

func CreateAzureChatCompletionStream(
	cfg *Config,
	logger *log.Logger,
	apiKey string,
	azureURL string,
	messages []openai.ChatCompletionMessage,
	maxTokens int,
) <-chan string

CreateAzureChatCompletionStream creates a chat completion stream using Azure.

func CreateAzureOpenAICompletion

func CreateAzureOpenAICompletion(
	cfg *Config,
	logger *log.Logger,
	apiKey string,
	azureURL string,
	prompt string,
	maxTokens int,
) <-chan string

CreateAzureOpenAICompletionStream creates an OpenAI completion stream using Azure.

func CreateChatCompletionStream

func CreateChatCompletionStream(
	cfg *Config,
	logger *log.Logger,
	upstreams map[string]Upstream,
	messages []openai.ChatCompletionMessage,
	maxTokens int,
) (<-chan string, string)

CreateChatCompletionStream creates a chat completion stream based on the given upstreams and messages, by Default it will use the upstream with the lowest "priority number" and send requests to that one.

func CreateOpenAIChatCompletionStream

func CreateOpenAIChatCompletionStream(
	cfg *Config,
	logger *log.Logger,
	apiKey string,
	messages []openai.ChatCompletionMessage,
	maxTokens int,
) <-chan string

CreateOpenAIChatCompletionStream creates a chat completion stream using OpenAI.

func CreateOpenAICompletion

func CreateOpenAICompletion(
	cfg *Config,
	logger *log.Logger,
	apiKey string,
	prompt string,
	maxTokens int,
) <-chan string

CreateOpenAICompletion creates a completion stream using OpenAI (non-Azure).

func CreateOpenAIRequest

func CreateOpenAIRequest(
	cfg *Config,
	logger *log.Logger,
	requestType string,
	messages []openai.ChatCompletionMessage,
	prompt string,
	maxTokens int,
) (<-chan string, string)

New version of CreateChatCompletionStream

func GoogleSearchInterceptor

func GoogleSearchInterceptor(cfg *Config, logger *log.Logger, requestData *RequestData) error

Interceptor function.

func HandleOptionsRequest

func HandleOptionsRequest(w http.ResponseWriter)

func InitializeLogger

func InitializeLogger(cfg *LogConfig) (*log.Logger, error)

InitializeLogger initializes the logger with the given log level and other settings.

func PerformGoogleSearch

func PerformGoogleSearch(cfg *Config, logger *log.Logger, query string) (string, error)

Function to perform a Google search using the rocketlaunchr/google-search package.

func Response

func Response(
	cfg *Config,
	logger *log.Logger,
	w http.ResponseWriter,
	r *http.Request,
)

func SetCommonHeaders

func SetCommonHeaders(w http.ResponseWriter, contentType string)

Prevent Content-Security-Policy Errors when used with webapps served from a different domain.

Types

type Choice

type Choice struct {
	Index        int               `json:"index"`
	FinishReason string            `json:"finish_reason"`
	Text         string            `json:"text"`
	Message      map[string]string `json:"message"`
	Delta        map[string]string `json:"delta"`
}

type ClosingResponse

type ClosingResponse struct {
	ID      string                 `json:"id"`
	Object  string                 `json:"object"`
	Created int64                  `json:"created"`
	Model   string                 `json:"model"`
	Choices []Choice               `json:"choices"`
	Usage   map[string]interface{} `json:"usage"`
}

type Config

type Config struct {
	Upstreams map[string]Upstream `yaml:"upstreams"`
	Listeners []Listener          `yaml:"listeners"`
	CertFile  string              `yaml:"certFile"`
	KeyFile   string              `yaml:"keyFile"`
	UseTLS    bool                `yaml:"useTLS"`
	LogConfig LogConfig           `yaml:"logConfig"`
}

func LoadConfig

func LoadConfig(filename string) (*Config, error)

type JSONResponse

type JSONResponse struct {
	ID      string                 `json:"id"`
	Object  string                 `json:"object"`
	Created int64                  `json:"created"`
	Model   string                 `json:"model"`
	Usage   map[string]interface{} `json:"usage"`
	Choices []Choice               `json:"choices"`
}

type Listener

type Listener struct {
	Interface string `yaml:"interface"`
	Port      string `yaml:"port"`
}

type LogConfig

type LogConfig struct {
	LogOutput string `yaml:"logOutput"` // Can be "stdout" or "file"
	Filepath  string `yaml:"filepath"`
	Filesize  int64  `yaml:"filesize"`
	FileCount int    `yaml:"fileCount"`
	LogLevel  string `yaml:"logLevel"` // Log level like "info", "debug", "warn", etc.
}

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type RequestData

type RequestData struct {
	RequestType string                         `json:"requestType"`
	Prompt      string                         `json:"prompt,omitempty"`
	Model       string                         `json:"model"`
	Temperature float64                        `json:"temperature"`
	MaxTokens   int                            `json:"maxTokens"`
	Messages    []openai.ChatCompletionMessage `json:"messages"`
}

func ReadAndUnmarshalBody

func ReadAndUnmarshalBody(cfg *Config, logger *log.Logger, resp http.ResponseWriter, req *http.Request) (RequestData, error)

Reads Data from the Client.

type RequestInterceptor

type RequestInterceptor func(cfg *Config, logger *log.Logger, requestData *RequestData) error

type Upstream

type Upstream struct {
	Type     string `yaml:"type"`
	URL      string `yaml:"url,omitempty"`
	Model    string `yaml:"model"`
	Priority int    `yaml:"priority"`
	APIKey   string `yaml:"apiKey"`
}

Jump to

Keyboard shortcuts

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