providers

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 12, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package providers contains implementations of different LLM providers for text summarization.

Index

Constants

View Source
const (
	// Provider constants
	ProviderAnthropic = "anthropic"
	ProviderOpenAI    = "openai"
	ProviderGoogle    = "google"
	ProviderXAI       = "xai"

	// Default settings
	DefaultTimeout        = 30 * time.Second
	DefaultMaxInputLength = 8000
)

Variables

This section is empty.

Functions

func MockServer

func MockServer(t *testing.T, config MockResponseConfig) *httptest.Server

MockServer creates a test server that returns the configured response

Types

type AnthropicMessage

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

AnthropicMessage represents the request structure for Anthropic's API

type AnthropicProvider

type AnthropicProvider struct {
	Config
	// contains filtered or unexported fields
}

AnthropicProvider implements the LLMProvider interface for Anthropic's Claude

func NewAnthropicProvider

func NewAnthropicProvider(config Config) *AnthropicProvider

NewAnthropicProvider creates a new instance of the Anthropic provider

func (*AnthropicProvider) Name

func (p *AnthropicProvider) Name() string

Name returns the provider name

func (*AnthropicProvider) Summarize

func (p *AnthropicProvider) Summarize(ctx context.Context, text string, maxLength int) (string, error)

Summarize implements the LLMProvider interface for Anthropic

type AnthropicRequest

type AnthropicRequest struct {
	Model     string             `json:"model"`
	Messages  []AnthropicMessage `json:"messages"`
	MaxTokens int                `json:"max_tokens"`
}

AnthropicRequest represents a request to Anthropic's API

type AnthropicResponse

type AnthropicResponse struct {
	Content []struct {
		Text string `json:"text"`
	} `json:"content"`
	Error *struct {
		Type    string `json:"type"`
		Message string `json:"message"`
	} `json:"error,omitempty"`
}

AnthropicResponse represents a response from Anthropic's API

type CapturingProvider

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

CapturingProvider is a provider that captures the inputs for testing

func NewCapturingProvider

func NewCapturingProvider(name, returnString string, returnError error) *CapturingProvider

NewCapturingProvider creates a new CapturingProvider

func (*CapturingProvider) GetCapturedMaxLength

func (p *CapturingProvider) GetCapturedMaxLength() int

GetCapturedMaxLength returns the maxLength that was passed to Summarize

func (*CapturingProvider) GetCapturedText

func (p *CapturingProvider) GetCapturedText() string

GetCapturedText returns the text that was passed to Summarize

func (*CapturingProvider) Name

func (p *CapturingProvider) Name() string

Name returns the provider name

func (*CapturingProvider) Summarize

func (p *CapturingProvider) Summarize(_ context.Context, text string, maxLength int) (string, error)

Summarize captures inputs and returns configured response

type Config

type Config struct {
	APIKey  string
	ModelID string
}

Config holds common configuration for LLM providers

type GoogleContent

type GoogleContent struct {
	Parts []struct {
		Text string `json:"text"`
	} `json:"parts"`
}

GoogleContent represents content in Google's Gemini API format

type GoogleProvider

type GoogleProvider struct {
	Config
	// contains filtered or unexported fields
}

GoogleProvider implements the LLMProvider interface for Google's Gemini models

func NewGoogleProvider

func NewGoogleProvider(config Config) *GoogleProvider

NewGoogleProvider creates a new instance of the Google provider

func (*GoogleProvider) Name

func (p *GoogleProvider) Name() string

Name returns the provider name

func (*GoogleProvider) Summarize

func (p *GoogleProvider) Summarize(ctx context.Context, text string, maxLength int) (string, error)

Summarize implements the LLMProvider interface for Google

type GoogleRequest

type GoogleRequest struct {
	Contents []struct {
		Parts []struct {
			Text string `json:"text"`
		} `json:"parts"`
		Role string `json:"role,omitempty"`
	} `json:"contents"`
	GenerationConfig struct {
		MaxOutputTokens int `json:"maxOutputTokens"`
	} `json:"generationConfig"`
}

GoogleRequest represents a request to Google's Gemini API

type GoogleResponse

type GoogleResponse struct {
	Candidates []struct {
		Content struct {
			Parts []struct {
				Text string `json:"text"`
			} `json:"parts"`
		} `json:"content"`
	} `json:"candidates"`
	Error *struct {
		Code    int    `json:"code"`
		Message string `json:"message"`
		Status  string `json:"status"`
	} `json:"error,omitempty"`
}

GoogleResponse represents a response from Google's Gemini API

type LLMProvider

type LLMProvider interface {
	// Summarize takes a text input and returns a condensed summary
	Summarize(ctx context.Context, text string, maxLength int) (string, error)

	// Name returns the provider name
	Name() string
}

LLMProvider defines the interface for different LLM service providers

type MockResponseConfig

type MockResponseConfig struct {
	StatusCode   int
	ResponseBody interface{}
	Headers      map[string]string
}

MockResponseConfig holds configuration for mock API responses

type OpenAIMessage

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

OpenAIMessage represents a message in OpenAI's chat format

type OpenAIProvider

type OpenAIProvider struct {
	Config
	// contains filtered or unexported fields
}

OpenAIProvider implements the LLMProvider interface for OpenAI's models

func NewOpenAIProvider

func NewOpenAIProvider(config Config) *OpenAIProvider

NewOpenAIProvider creates a new instance of the OpenAI provider

func (*OpenAIProvider) Name

func (p *OpenAIProvider) Name() string

Name returns the provider name

func (*OpenAIProvider) Summarize

func (p *OpenAIProvider) Summarize(ctx context.Context, text string, maxLength int) (string, error)

Summarize implements the LLMProvider interface for OpenAI

type OpenAIRequest

type OpenAIRequest struct {
	Model     string          `json:"model"`
	Messages  []OpenAIMessage `json:"messages"`
	MaxTokens int             `json:"max_tokens"`
}

OpenAIRequest represents a request to OpenAI's API

type OpenAIResponse

type OpenAIResponse struct {
	Choices []struct {
		Message struct {
			Content string `json:"content"`
		} `json:"message"`
	} `json:"choices"`
	Error *struct {
		Message string `json:"message"`
		Type    string `json:"type"`
	} `json:"error,omitempty"`
}

OpenAIResponse represents a response from OpenAI's API

type ProviderFactory

type ProviderFactory struct {
	// ProviderConfigs stores configuration for each provider
	ProviderConfigs map[string]Config
}

ProviderFactory creates and returns appropriate LLM providers

func NewProviderFactory

func NewProviderFactory(configs map[string]Config) *ProviderFactory

NewProviderFactory creates a new provider factory

func (*ProviderFactory) GetAllProviders

func (f *ProviderFactory) GetAllProviders() []LLMProvider

GetAllProviders returns all available providers based on configured providers

func (*ProviderFactory) GetProvider

func (f *ProviderFactory) GetProvider(providerName string) (LLMProvider, error)

GetProvider returns an initialized provider instance for the specified provider name

func (*ProviderFactory) GetProviderChain

func (f *ProviderFactory) GetProviderChain(preferenceOrder []string) []LLMProvider

GetProviderChain returns an ordered list of providers to try in sequence The ordered list is created based on the given preference order

type TestProvider

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

TestProvider is a simple implementation of LLMProvider for testing

func NewTestProvider

func NewTestProvider(name string, returnString string, returnError error) *TestProvider

NewTestProvider creates a new TestProvider

func (*TestProvider) Name

func (p *TestProvider) Name() string

Name returns the provider name

func (*TestProvider) Summarize

func (p *TestProvider) Summarize(_ context.Context, _ string, _ int) (string, error)

Summarize returns the configured string or error

type XAIMessage

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

XAIMessage represents a message in X.AI's chat format (OpenAI compatible)

type XAIProvider

type XAIProvider struct {
	Config
	// contains filtered or unexported fields
}

XAIProvider implements the LLMProvider interface for X.AI's Grok

func NewXAIProvider

func NewXAIProvider(config Config) *XAIProvider

NewXAIProvider creates a new instance of the X.AI provider

func (*XAIProvider) Name

func (p *XAIProvider) Name() string

Name returns the provider name

func (*XAIProvider) Summarize

func (p *XAIProvider) Summarize(ctx context.Context, text string, maxLength int) (string, error)

Summarize implements the LLMProvider interface for X.AI

type XAIRequest

type XAIRequest struct {
	Model     string       `json:"model"`
	Messages  []XAIMessage `json:"messages"`
	MaxTokens int          `json:"max_tokens"`
}

XAIRequest represents a request to X.AI's API (OpenAI compatible)

type XAIResponse

type XAIResponse struct {
	Choices []struct {
		Message struct {
			Content string `json:"content"`
		} `json:"message"`
	} `json:"choices"`
	Error *struct {
		Message string `json:"message"`
		Type    string `json:"type"`
	} `json:"error,omitempty"`
}

XAIResponse represents a response from X.AI's API (OpenAI compatible)

Jump to

Keyboard shortcuts

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