Skip to content

Commit

Permalink
#67: Fixed broken code after the first wave of refactoring/restructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Jul 5, 2024
1 parent d842fa0 commit c924140
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 263 deletions.
5 changes: 3 additions & 2 deletions config.dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ routers:
- id: default
models:
- id: openai
openai:
api_key: "${env:OPENAI_API_KEY}"
provider:
openai:
api_key: "${env:OPENAI_API_KEY}"
9 changes: 9 additions & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ telemetry:
#api:
# http:
# ...

routers:
language:
- id: default
models:
- id: openai
provider:
openai:
api_key: "${env:OPENAI_API_KEY}"
17 changes: 14 additions & 3 deletions pkg/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package models
import (
"fmt"

"github.com/EinStack/glide/pkg/providers"

"github.com/EinStack/glide/pkg/clients"
"github.com/EinStack/glide/pkg/resiliency/health"
"github.com/EinStack/glide/pkg/routers/latency"
"github.com/EinStack/glide/pkg/telemetry"
)

type Config[P any] struct {
// Config defines an extra configuration for a model wrapper around a provider
type Config[P providers.ProviderFactory] struct {
ID string `yaml:"id" json:"id" validate:"required"` // Model instance ID (unique in scope of the router)
Enabled bool `yaml:"enabled" json:"enabled" validate:"required"` // Is the model enabled?
ErrorBudget *health.ErrorBudget `yaml:"error_budget" json:"error_budget" swaggertype:"primitive,string"`
Expand All @@ -20,7 +23,15 @@ type Config[P any] struct {
Provider P `yaml:"provider" json:"provider"`
}

func DefaultConfig[P any]() Config[P] {
func NewConfig[P providers.ProviderFactory](ID string) *Config[P] {
config := DefaultConfig[P]()

config.ID = ID

return &config
}

func DefaultConfig[P providers.ProviderFactory]() Config[P] {
return Config[P]{
Enabled: true,
Client: clients.DefaultClientConfig(),
Expand All @@ -30,7 +41,7 @@ func DefaultConfig[P any]() Config[P] {
}
}

func (c *Config) ToModel(tel *telemetry.Telemetry) (*LanguageModel, error) {
func (c *Config[P]) ToModel(tel *telemetry.Telemetry) (*LanguageModel, error) {
client, err := c.Provider.ToClient(tel, c.Client)
if err != nil {
return nil, fmt.Errorf("error initializing client: %w", err)
Expand Down
21 changes: 15 additions & 6 deletions pkg/providers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ import (
"github.com/EinStack/glide/pkg/telemetry"
)

var ErrProviderNotFound = errors.New("provider not found")
// TODO: ProviderFactory should be more generic, not tied to LangProviders

var ErrNoProviderConfigured = errors.New("exactly one provider must be configured, none is configured")

type ProviderFactory interface {
ToClient(tel *telemetry.Telemetry, clientConfig *clients.ClientConfig) (LangProvider, error)
}

// TODO: LangProviders should be decoupled and
// represented as a registry where providers can add their factories dynamically

type LangProviders struct {
// Add other providers like
Expand All @@ -29,9 +38,11 @@ type LangProviders struct {
Ollama *ollama.Config `yaml:"ollama,omitempty" json:"ollama,omitempty"`
}

var _ ProviderFactory = (*LangProviders)(nil)

// ToClient initializes the language model client based on the provided configuration.
// It takes a telemetry object as input and returns a LangModelProvider and an error.
func (c *LangProviders) ToClient(tel *telemetry.Telemetry, clientConfig *clients.ClientConfig) (LangProvider, error) {
func (c LangProviders) ToClient(tel *telemetry.Telemetry, clientConfig *clients.ClientConfig) (LangProvider, error) {
switch {
case c.OpenAI != nil:
return openai.NewClient(c.OpenAI, clientConfig, tel)
Expand Down Expand Up @@ -83,7 +94,7 @@ func (c *LangProviders) validateOneProvider() error {

// check other providers here
if providersConfigured == 0 {
return fmt.Errorf("exactly one provider must be configured, none is configured")
return ErrNoProviderConfigured
}

if providersConfigured > 1 {
Expand All @@ -97,9 +108,7 @@ func (c *LangProviders) validateOneProvider() error {
}

func (c *LangProviders) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig()

type plain LangModelConfig // to avoid recursion
type plain LangProviders // to avoid recursion

if err := unmarshal((*plain)(c)); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/openai/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Embed sends an embedding request to the specified OpenAI model.
func (c *Client) Embed(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error) {
func (c *Client) Embed(_ context.Context, _ *schemas.ChatParams) (*schemas.ChatResponse, error) {
// TODO: implement
return nil, nil
}
3 changes: 3 additions & 0 deletions pkg/providers/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package providers

import (
"context"
"errors"

"github.com/EinStack/glide/pkg/api/schemas"
"github.com/EinStack/glide/pkg/clients"
)

var ErrProviderNotFound = errors.New("provider not found")

// ModelProvider exposes provider context
type ModelProvider interface {
Provider() string
Expand Down
6 changes: 2 additions & 4 deletions pkg/providers/testing/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"time"

"github.com/EinStack/glide/pkg/config/fields"

"github.com/EinStack/glide/pkg/models"
"github.com/EinStack/glide/pkg/routers/latency"

"github.com/EinStack/glide/pkg/providers"
)

// LangModelMock
Expand Down Expand Up @@ -55,6 +53,6 @@ func (m LangModelMock) Weight() int {
return m.weight
}

func ChatMockLatency(model providers.Model) *latency.MovingAverage {
func ChatMockLatency(model models.Model) *latency.MovingAverage {
return model.(LangModelMock).chatLatency
}
11 changes: 11 additions & 0 deletions pkg/routers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ import (
"github.com/EinStack/glide/pkg/routers/routing"
)

// TODO: how to specify other backoff strategies?
// TODO: Had to keep RoutingStrategy because of https://github.com/swaggo/swag/issues/1738

type RouterConfig struct {
ID string `yaml:"id" json:"routers" validate:"required"` // Unique router ID
Enabled bool `yaml:"enabled" json:"enabled" validate:"required"` // Is router enabled?
Retry *retry.ExpRetryConfig `yaml:"retry" json:"retry" validate:"required"` // retry when no healthy model is available to router
RoutingStrategy routing.Strategy `yaml:"strategy" json:"strategy" swaggertype:"primitive,string" validate:"required"` // strategy on picking the next model to serve the request
}

func DefaultConfig() RouterConfig {
return RouterConfig{
Enabled: true,
RoutingStrategy: routing.Priority,
Retry: retry.DefaultExpRetryConfig(),
}
}
3 changes: 1 addition & 2 deletions pkg/routers/embed/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package embed

import (
"github.com/EinStack/glide/pkg/providers"
"github.com/EinStack/glide/pkg/routers"
)

type EmbeddingRouterConfig struct {
routers.RouterConfig
Models []providers.LangModelConfig `yaml:"models" json:"models" validate:"required,min=1,dive"` // the list of models that could handle requests
// Models []providers.LangModelConfig `yaml:"models" json:"models" validate:"required,min=1,dive"` // the list of models that could handle requests
}
24 changes: 7 additions & 17 deletions pkg/routers/embed/router.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
package embed

import (
"context"

"github.com/EinStack/glide/pkg/api/schemas"
"github.com/EinStack/glide/pkg/resiliency/retry"
"github.com/EinStack/glide/pkg/routers/lang"
"github.com/EinStack/glide/pkg/telemetry"
"go.uber.org/zap"
)

type EmbeddingRouter struct {
routerID lang.RouterID
Config *LangRouterConfig
retry *retry.ExpRetry
tel *telemetry.Telemetry
logger *zap.Logger
// routerID lang.RouterID
// Config *LangRouterConfig
// retry *retry.ExpRetry
// tel *telemetry.Telemetry
// logger *zap.Logger
}

func (r *EmbeddingRouter) Embed(ctx context.Context, req *schemas.EmbedRequest) (*schemas.EmbedResponse, error) {
}
//func (r *EmbeddingRouter) Embed(ctx context.Context, req *schemas.EmbedRequest) (*schemas.EmbedResponse, error) {
//}
37 changes: 29 additions & 8 deletions pkg/routers/lang/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,37 @@ import (
"go.uber.org/zap"
)

// TODO: how to specify other backoff strategies?
// TODO: Had to keep RoutingStrategy because of https://github.com/swaggo/swag/issues/1738
type (
ModelConfig = models.Config[providers.LangProviders]
ModelPoolConfig = []ModelConfig
)

// RouterConfig
type RouterConfig struct {
routers.RouterConfig
Models []providers.LangModelConfig `yaml:"models" json:"models" validate:"required,min=1,dive"` // the list of models that could handle requests
Models ModelPoolConfig `yaml:"models" json:"models" validate:"required,min=1,dive"` // the list of models that could handle requests
}

type RouterConfigOption = func(*RouterConfig)

func WithModels(models ModelPoolConfig) RouterConfigOption {
return func(c *RouterConfig) {
c.Models = models
}
}

func NewRouterConfig(RouterID string, opt ...RouterConfigOption) *RouterConfig {
config := &RouterConfig{
RouterConfig: routers.DefaultConfig(),
}

config.ID = RouterID

for _, o := range opt {
o(config)
}

return config
}

// BuildModels creates LanguageModel slice out of the given config
Expand Down Expand Up @@ -165,11 +190,7 @@ func (c *RouterConfig) BuildRouting(

func DefaultRouterConfig() *RouterConfig {
return &RouterConfig{
RouterConfig: routers.RouterConfig{
Enabled: true,
RoutingStrategy: routing.Priority,
Retry: retry.DefaultExpRetryConfig(),
},
RouterConfig: routers.DefaultConfig(),
}
}

Expand Down
Loading

0 comments on commit c924140

Please sign in to comment.