Skip to content

Commit

Permalink
#67: Experimenting with dynamic lang provider config loading & valida…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
roma-glushko committed Jul 31, 2024
1 parent c924140 commit 003691c
Show file tree
Hide file tree
Showing 22 changed files with 360 additions and 112 deletions.
10 changes: 4 additions & 6 deletions pkg/models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package models

import (
"fmt"

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

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

// Config defines an extra configuration for a model wrapper around a provider
type Config[P providers.ProviderFactory] struct {
type Config[P provider.ProviderConfig] 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 @@ -23,15 +21,15 @@ type Config[P providers.ProviderFactory] struct {
Provider P `yaml:"provider" json:"provider"`
}

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

config.ID = ID

return &config
}

func DefaultConfig[P providers.ProviderFactory]() Config[P] {
func DefaultConfig[P provider.ProviderConfig]() Config[P] {
return Config[P]{
Enabled: true,
Client: clients.DefaultClientConfig(),
Expand Down
7 changes: 3 additions & 4 deletions pkg/models/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package models

import (
"context"
"github.com/EinStack/glide/pkg/provider"
"io"
"time"

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

"github.com/EinStack/glide/pkg/clients"
health2 "github.com/EinStack/glide/pkg/resiliency/health"

Expand All @@ -32,14 +31,14 @@ type LangModel interface {
type LanguageModel struct {
modelID string
weight int
client providers.LangProvider
client provider.LangProvider
healthTracker *health2.Tracker
chatLatency *latency.MovingAverage
chatStreamLatency *latency.MovingAverage
latencyUpdateInterval *fields.Duration
}

func NewLangModel(modelID string, client providers.LangProvider, budget *health2.ErrorBudget, latencyConfig latency.Config, weight int) *LanguageModel {
func NewLangModel(modelID string, client provider.LangProvider, budget *health2.ErrorBudget, latencyConfig latency.Config, weight int) *LanguageModel {
return &LanguageModel{
modelID: modelID,
client: client,
Expand Down
12 changes: 12 additions & 0 deletions pkg/provider/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package provider

import (
"github.com/EinStack/glide/pkg/clients"
"github.com/EinStack/glide/pkg/telemetry"
)

// TODO: ProviderConfig should be more generic, not tied to LangProviders
type ProviderConfig interface {
UnmarshalYAML(unmarshal func(interface{}) error) error
ToClient(tel *telemetry.Telemetry, clientConfig *clients.ClientConfig) (LangProvider, error)
}
15 changes: 13 additions & 2 deletions pkg/providers/provider.go → pkg/provider/provider.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package providers
package provider

import (
"context"
Expand All @@ -10,9 +10,11 @@ import (

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

type ProviderID = string

// ModelProvider exposes provider context
type ModelProvider interface {
Provider() string
Provider() ProviderID
ModelName() string
}

Expand All @@ -25,3 +27,12 @@ type LangProvider interface {
Chat(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error)
ChatStream(ctx context.Context, params *schemas.ChatParams) (clients.ChatStream, error)
}

// EmbeddingProvider defines an interface a provider should fulfill to be able to generate embeddings
type EmbeddingProvider interface {
ModelProvider

SupportEmbedding() bool

Embed(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error)
}
Loading

0 comments on commit 003691c

Please sign in to comment.