Skip to content

Commit

Permalink
#67: Restructure router config, model & provider interfaces to incorp…
Browse files Browse the repository at this point in the history
…orate the new embedding router
  • Loading branch information
roma-glushko committed Jul 5, 2024
1 parent 27347ec commit d842fa0
Show file tree
Hide file tree
Showing 59 changed files with 343 additions and 273 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.idea
dist
.env
.env.bak
config.yaml
bin
glide
glide.exe
tmp
coverage.txt
precommit.txt
Expand Down
11 changes: 6 additions & 5 deletions pkg/api/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"sync"

"github.com/EinStack/glide/pkg/routers/manager"

"github.com/EinStack/glide/pkg/api/schemas"
"github.com/EinStack/glide/pkg/routers"
"github.com/EinStack/glide/pkg/telemetry"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
Expand All @@ -31,7 +32,7 @@ type Handler = func(c *fiber.Ctx) error
// @Failure 400 {object} schemas.Error
// @Failure 404 {object} schemas.Error
// @Router /v1/language/{router}/chat [POST]
func LangChatHandler(routerManager *routers.RouterManager) Handler {
func LangChatHandler(routerManager *manager.RouterManager) Handler {
return func(c *fiber.Ctx) error {
if !c.Is("json") {
return c.Status(fiber.StatusBadRequest).JSON(schemas.ErrUnsupportedMediaType)
Expand Down Expand Up @@ -72,7 +73,7 @@ func LangChatHandler(routerManager *routers.RouterManager) Handler {
}
}

func LangStreamRouterValidator(routerManager *routers.RouterManager) Handler {
func LangStreamRouterValidator(routerManager *manager.RouterManager) Handler {
return func(c *fiber.Ctx) error {
if websocket.IsWebSocketUpgrade(c) {
routerID := c.Params("router")
Expand Down Expand Up @@ -107,7 +108,7 @@ func LangStreamRouterValidator(routerManager *routers.RouterManager) Handler {
// @Failure 426
// @Failure 404 {object} schemas.Error
// @Router /v1/language/{router}/chatStream [GET]
func LangStreamChatHandler(tel *telemetry.Telemetry, routerManager *routers.RouterManager) Handler {
func LangStreamChatHandler(tel *telemetry.Telemetry, routerManager *manager.RouterManager) Handler {
// TODO: expose websocket connection configs https://github.com/gofiber/contrib/tree/main/websocket
return websocket.New(func(c *websocket.Conn) {
routerID := c.Params("router")
Expand Down Expand Up @@ -175,7 +176,7 @@ func LangStreamChatHandler(tel *telemetry.Telemetry, routerManager *routers.Rout
// @Produce json
// @Success 200 {object} schemas.RouterListSchema
// @Router /v1/language/ [GET]
func LangRoutersHandler(routerManager *routers.RouterManager) Handler {
func LangRoutersHandler(routerManager *manager.RouterManager) Handler {
return func(c *fiber.Ctx) error {
configuredRouters := routerManager.GetLangRouters()
cfgs := make([]interface{}, 0, len(configuredRouters)) // opaque by design
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"time"

"github.com/EinStack/glide/pkg/routers/manager"

"github.com/gofiber/contrib/otelfiber"

"github.com/gofiber/swagger"
Expand All @@ -17,19 +19,17 @@ import (

"github.com/gofiber/fiber/v2"

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

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

type Server struct {
config *ServerConfig
telemetry *telemetry.Telemetry
routerManager *routers.RouterManager
routerManager *manager.RouterManager
server *fiber.App
}

func NewServer(config *ServerConfig, tel *telemetry.Telemetry, routerManager *routers.RouterManager) (*Server, error) {
func NewServer(config *ServerConfig, tel *telemetry.Telemetry, routerManager *manager.RouterManager) (*Server, error) {
srv := config.ToServer()

return &Server{
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"sync"

"go.uber.org/zap"
"github.com/EinStack/glide/pkg/routers/manager"

"github.com/EinStack/glide/pkg/routers"
"go.uber.org/zap"

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

Expand All @@ -19,7 +19,7 @@ type ServerManager struct {
telemetry *telemetry.Telemetry
}

func NewServerManager(cfg *Config, tel *telemetry.Telemetry, router *routers.RouterManager) (*ServerManager, error) {
func NewServerManager(cfg *Config, tel *telemetry.Telemetry, router *manager.RouterManager) (*ServerManager, error) {
httpServer, err := http.NewServer(cfg.HTTP, tel, router)
if err != nil {
return nil, err
Expand Down
12 changes: 5 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package config

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

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

"github.com/EinStack/glide/pkg/api"
routerconfig "github.com/EinStack/glide/pkg/routers/manager"
"github.com/EinStack/glide/pkg/telemetry"
)

// Config is a general top-level Glide configuration
type Config struct {
Telemetry *telemetry.Config `yaml:"telemetry" validate:"required"`
API *api.Config `yaml:"api" validate:"required"`
Routers routers.Config `yaml:"routers" validate:"required"`
Telemetry *telemetry.Config `yaml:"telemetry" validate:"required"`
API *api.Config `yaml:"api" validate:"required"`
Routers routerconfig.Config `yaml:"routers" validate:"required"`
}

func DefaultConfig() *Config {
Expand Down
5 changes: 3 additions & 2 deletions pkg/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"os/signal"
"syscall"

"github.com/EinStack/glide/pkg/routers"
"github.com/EinStack/glide/pkg/routers/manager"

"github.com/EinStack/glide/pkg/version"
"go.opentelemetry.io/contrib/instrumentation/host"
"go.opentelemetry.io/contrib/instrumentation/runtime"
Expand Down Expand Up @@ -49,7 +50,7 @@ func NewGateway(configProvider *config.Provider) (*Gateway, error) {
tel.L().Info("🐦Glide is starting up", zap.String("version", version.FullVersion))
tel.L().Debug("✅ Config loaded successfully:\n" + configProvider.GetStr())

routerManager, err := routers.NewManager(&cfg.Routers, tel)
routerManager, err := manager.NewManager(&cfg.Routers, tel)
if err != nil {
return nil, err
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/models/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package models

import (
"fmt"

"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 {
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"`
Latency *latency.Config `yaml:"latency" json:"latency"`
Weight int `yaml:"weight" json:"weight"`
Client *clients.ClientConfig `yaml:"client" json:"client"`

Provider P `yaml:"provider" json:"provider"`
}

func DefaultConfig[P any]() Config[P] {
return Config[P]{
Enabled: true,
Client: clients.DefaultClientConfig(),
ErrorBudget: health.DefaultErrorBudget(),
Latency: latency.DefaultConfig(),
Weight: 1,
}
}

func (c *Config) ToModel(tel *telemetry.Telemetry) (*LanguageModel, error) {

Check failure on line 33 in pkg/models/config.go

View workflow job for this annotation

GitHub Actions / Static Checks

cannot use generic type Config[P any] without instantiation (typecheck)
client, err := c.Provider.ToClient(tel, c.Client)
if err != nil {
return nil, fmt.Errorf("error initializing client: %w", err)
}

return NewLangModel(c.ID, client, c.ErrorBudget, *c.Latency, c.Weight), nil
}
23 changes: 8 additions & 15 deletions pkg/providers/lang.go → pkg/models/lang.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package providers
package models

import (
"context"
"github.com/EinStack/glide/pkg/clients"
health2 "github.com/EinStack/glide/pkg/resiliency/health"
"io"
"time"

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

Check failure on line 8 in pkg/models/lang.go

View workflow job for this annotation

GitHub Actions / Static Checks

could not import github.com/EinStack/glide/pkg/providers (-: # github.com/EinStack/glide/pkg/providers

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

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

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

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

// LangProvider defines an interface a provider should fulfill to be able to serve language chat requests
type LangProvider interface {
ModelProvider

SupportChatStream() bool

Chat(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error)
ChatStream(ctx context.Context, params *schemas.ChatParams) (clients.ChatStream, error)
}

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

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

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

// Model represent a configured external modality-agnostic model with its routing properties and status
type Model interface {
ID() string
Healthy() bool
LatencyUpdateInterval() *fields.Duration
Weight() int
}
3 changes: 2 additions & 1 deletion pkg/providers/anthropic/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/EinStack/glide/pkg/clients"
"io"
"net/http"
"time"

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

"github.com/EinStack/glide/pkg/api/schemas"
"go.uber.org/zap"
)
Expand Down
1 change: 1 addition & 0 deletions pkg/providers/anthropic/chat_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package anthropic

import (
"context"

clients2 "github.com/EinStack/glide/pkg/clients"

"github.com/EinStack/glide/pkg/api/schemas"
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/anthropic/client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package anthropic

import (
"github.com/EinStack/glide/pkg/clients"
"net/http"
"net/url"
"time"

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

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

Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/anthropic/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package anthropic
import (
"context"
"encoding/json"
"github.com/EinStack/glide/pkg/clients"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"

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

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

"github.com/EinStack/glide/pkg/telemetry"
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/anthropic/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package anthropic

import (
"fmt"
"github.com/EinStack/glide/pkg/clients"
"io"
"net/http"
"time"

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

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

"go.uber.org/zap"
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/azureopenai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/EinStack/glide/pkg/clients"
"io"
"net/http"

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

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

"github.com/EinStack/glide/pkg/api/schemas"
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/azureopenai/chat_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"context"
"encoding/json"
"fmt"
clients2 "github.com/EinStack/glide/pkg/clients"
"io"
"net/http"

clients2 "github.com/EinStack/glide/pkg/clients"

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

"github.com/EinStack/glide/pkg/providers/openai"
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/azureopenai/chat_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package azureopenai
import (
"context"
"encoding/json"
clients2 "github.com/EinStack/glide/pkg/clients"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"

clients2 "github.com/EinStack/glide/pkg/clients"

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

"github.com/EinStack/glide/pkg/telemetry"
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/azureopenai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package azureopenai

import (
"fmt"
"github.com/EinStack/glide/pkg/clients"
"net/http"
"time"

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

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

"github.com/EinStack/glide/pkg/telemetry"
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/azureopenai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package azureopenai
import (
"context"
"encoding/json"
"github.com/EinStack/glide/pkg/clients"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"

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

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

"github.com/EinStack/glide/pkg/telemetry"
Expand Down
Loading

0 comments on commit d842fa0

Please sign in to comment.