apitest

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintModelMenu

func PrintModelMenu(p *util.Printer, title string, models []string, defaultModels []string)

PrintModelMenu prints the model selection menu

Types

type APITester

type APITester interface {
	TestChannel(context.Context, *TestConfig) TestResult
	TestAllChannels(context.Context, []*TestConfig) []TestResult
	TestAllApis([]*Channel) []TestResult
	PrintResults([]TestResult) error
}

APITester defines the main interface for API testing

func NewApiTest

func NewApiTest(maxConcurrency int, opts ...ChannelTestOption) APITester

NewApiTest creates a new API test instance with options

type Channel

type Channel struct {
	Key       string      `json:"key"`
	TestModel []string    `json:"test_model"`
	URL       string      `json:"url"`
	Type      ChannelType `json:"type"`
}

Channel represents an API channel configuration

type ChannelTest

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

ChannelTest represents a test for API channels

func NewChannelTest

func NewChannelTest(maxConcurrency int, w io.Writer) *ChannelTest

NewChannelTest creates a new ChannelTest instance

func (*ChannelTest) PrintResults

func (ct *ChannelTest) PrintResults(results []TestResult) error

PrintResults prints the test results in a formatted way

func (*ChannelTest) TestAllApis

func (ct *ChannelTest) TestAllApis(channels []*Channel) []TestResult

TestAllApis is a compatibility method that calls TestAllChannels

func (*ChannelTest) TestAllChannels

func (ct *ChannelTest) TestAllChannels(ctx context.Context, configs []*TestConfig) []TestResult

TestAllChannels tests multiple channels concurrently

func (*ChannelTest) TestChannel

func (ct *ChannelTest) TestChannel(ctx context.Context, cfg *TestConfig) TestResult

TestChannel tests a single channel with the specified configuration

type ChannelTestConfig

type ChannelTestConfig struct {
	MaxConcurrency int
	Timeout        time.Duration
	ResultBuffer   int
}

func DefaultConfig

func DefaultConfig() *ChannelTestConfig

DefaultConfig returns the default configuration

type ChannelTestOption

type ChannelTestOption func(*ChannelTest)

ChannelTestOption defines a function type for configuring ChannelTest

func WithClient

func WithClient(client HTTPClient) ChannelTestOption

WithClient sets the HTTP client

func WithConfig

func WithConfig(config *ChannelTestConfig) ChannelTestOption

WithConfig sets the configuration

func WithPrinter

func WithPrinter(printer *util.Printer) ChannelTestOption

WithPrinter sets the printer

func WithRequestBuilder

func WithRequestBuilder(builder RequestBuilder) ChannelTestOption

WithRequestBuilder sets the request builder

func WithResultProcessor

func WithResultProcessor(processor ResultProcessor) ChannelTestOption

WithResultProcessor sets the result processor

type ChannelType

type ChannelType int

ChannelType represents the type of API channel

const (
	ChannelTypeGemini ChannelType = iota
	ChannelTypeOpenAI
)

type DefaultRequestBuilder

type DefaultRequestBuilder struct{}

DefaultRequestBuilder implements the RequestBuilder interface

func NewRequestBuilder

func NewRequestBuilder() *DefaultRequestBuilder

NewRequestBuilder creates a new DefaultRequestBuilder

func (*DefaultRequestBuilder) BuildRequest

func (b *DefaultRequestBuilder) BuildRequest(ctx context.Context, cfg *TestConfig) (*http.Request, error)

BuildRequest builds an HTTP request based on the test configuration

type DefaultResultProcessor

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

DefaultResultProcessor implements the ResultProcessor interface

func (*DefaultResultProcessor) ProcessResponse

func (p *DefaultResultProcessor) ProcessResponse(resp *http.Response) TestResult

ProcessResponse processes the HTTP response and returns a TestResult

type GeminiError

type GeminiError struct {
	Error struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Status  string `json:"status"`
		Details []struct {
			Type     string            `json:"@type"`
			Reason   string            `json:"reason,omitempty"`
			Domain   string            `json:"domain,omitempty"`
			Metadata map[string]string `json:"metadata,omitempty"`
			Message  string            `json:"message,omitempty"`
			Locale   string            `json:"locale,omitempty"`
		} `json:"details"`
	} `json:"error"`
}

GeminiError represents the error structure returned by Gemini API

type HTTPClient

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

HTTPClient abstracts the HTTP client for better testing

type Message

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

Message represents a message in the OpenAI request

type OpenAIError

type OpenAIError struct {
	Error struct {
		Message string `json:"message"`
		Type    string `json:"type"`
		Code    string `json:"code"`
	} `json:"error"`
}

OpenAIError represents the error structure returned by OpenAI API

type OpenAIRequest

type OpenAIRequest struct {
	Model               string    `json:"model"`
	Messages            []Message `json:"messages"`
	Stream              bool      `json:"stream"`
	MaxTokens           int       `json:"max_tokens,omitempty"`
	MaxCompletionTokens int       `json:"max_completion_tokens,omitempty"`
}

OpenAIRequest represents a request to the OpenAI API

type OpenAIResponse

type OpenAIResponse struct {
	Usage *Usage `json:"usage"`
}

Parse OpenAI response

type RequestBuilder

type RequestBuilder interface {
	BuildRequest(context.Context, *TestConfig) (*http.Request, error)
}

RequestBuilder builds HTTP requests for different API types

type RequestOptions

type RequestOptions struct {
	MaxTokens   int
	Temperature float64
	TopP        float64
	TopK        int
	Stream      bool
}

RequestOptions holds options for API requests

type ResultProcessor

type ResultProcessor interface {
	ProcessResponse(*http.Response) TestResult
}

ResultProcessor processes API responses

func NewResultProcessor

func NewResultProcessor(key, model string) ResultProcessor

NewResultProcessor creates a new DefaultResultProcessor

type TestConfig

type TestConfig struct {
	Channel     *Channel
	Model       string
	RequestOpts RequestOptions
	IsGemini    bool
}

TestConfig holds configuration for a single test

type TestResult

type TestResult struct {
	Channel  *Channel
	Model    string
	Success  bool
	Latency  float64
	Error    error
	Response interface{}
}

TestResult represents the result of an API test

type Usage

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

Usage represents the token usage information

Jump to

Keyboard shortcuts

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