handlers

package
v0.0.32 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package handlers provides HTTP handlers for the Hapax server. It implements request handling for completions, chat, and function calling using the gollm library and Hapax processing system.

The package follows these design principles: 1. Consistent error handling using the errors package 2. Structured logging with request IDs 3. Clear request validation and type conversion 4. Separation between request parsing and processing

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompletionHandler

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

CompletionHandler handles different types of completion requests. It supports: - Simple text completion (default) - Chat completion with message history - Function calling

func NewCompletionHandler

func NewCompletionHandler(processor *processing.Processor, logger *zap.Logger) *CompletionHandler

NewCompletionHandler creates a new completion handler with the given processor and logger. It requires both parameters to be non-nil.

func (*CompletionHandler) ServeHTTP

func (h *CompletionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface. It handles all completion requests by: 1. Determining the request type 2. Validating and parsing the request 3. Processing using the appropriate template 4. Formatting and returning the response

Error Handling: - ValidationError: Invalid request format or missing fields - ProcessingError: LLM or processing failures - InternalError: Unexpected system errors

Each error is logged with: - Request ID for tracking - Error type and message - Request context (type, path)

type CompletionRequest

type CompletionRequest struct {
	// Messages is the primary field for all requests. For simple completions,
	// a single user message is created from the Input field.
	Messages []gollm.PromptMessage `json:"messages,omitempty" validate:"omitempty,min=1"`

	// Input is maintained for backward compatibility with simple completions.
	// If present, it will be converted to a single user message.
	Input string `json:"input,omitempty" validate:"omitempty"`

	// FunctionDescription is used for function calling requests.
	// If present, it will be included in the system context.
	FunctionDescription string `json:"function_description,omitempty" validate:"omitempty"`
}

CompletionRequest represents a completion request with message history. This is the primary request type that supports both simple text and chat completions. All fields are validated before processing.

Jump to

Keyboard shortcuts

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