Skip to content

Commit

Permalink
#67: Renamed schemas to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Aug 12, 2024
1 parent 24be91c commit 65d4fa0
Show file tree
Hide file tree
Showing 47 changed files with 362 additions and 359 deletions.
31 changes: 15 additions & 16 deletions pkg/api/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

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

"github.com/EinStack/glide/pkg/api/schemas"
"github.com/EinStack/glide/pkg/telemetry"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -35,35 +34,35 @@ type Handler = func(c *fiber.Ctx) error
func LangChatHandler(routerManager *router.Manager) Handler {
return func(c *fiber.Ctx) error {
if !c.Is("json") {
return c.Status(fiber.StatusBadRequest).JSON(schemas.ErrUnsupportedMediaType)
return c.Status(fiber.StatusBadRequest).JSON(schema.ErrUnsupportedMediaType)
}

// Unmarshal request body
req := schemas.GetChatRequest()
defer schemas.ReleaseChatRequest(req)
req := schema.GetChatRequest()
defer schema.ReleaseChatRequest(req)

err := c.BodyParser(&req)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(schemas.NewPayloadParseErr(err))
return c.Status(fiber.StatusBadRequest).JSON(schema.NewPayloadParseErr(err))
}

// Get router ID from path
routerID := c.Params("router")

r, err := routerManager.GetLangRouter(routerID)
if err != nil {
httpErr := schemas.FromErr(err)
httpErr := schema.FromErr(err)

return c.Status(httpErr.Status).JSON(httpErr)
}

// Chat with router
resp := schemas.GetChatResponse()
defer schemas.ReleaseChatResponse(resp)
resp := schema.GetChatResponse()
defer schema.ReleaseChatResponse(resp)

resp, err = r.Chat(c.Context(), req)
if err != nil {
httpErr := schemas.FromErr(err)
httpErr := schema.FromErr(err)

return c.Status(httpErr.Status).JSON(httpErr)
}
Expand All @@ -80,7 +79,7 @@ func LangStreamRouterValidator(routerManager *router.Manager) Handler {

_, err := routerManager.GetLangRouter(routerID)
if err != nil {
httpErr := schemas.FromErr(err)
httpErr := schema.FromErr(err)

return c.Status(httpErr.Status).JSON(httpErr)
}
Expand Down Expand Up @@ -119,7 +118,7 @@ func LangStreamChatHandler(tel *telemetry.Telemetry, routerManager *router.Manag
wg sync.WaitGroup
)

chatStreamC := make(chan *schemas.ChatStreamMessage)
chatStreamC := make(chan *schema.ChatStreamMessage)

r, _ := routerManager.GetLangRouter(routerID)

Expand All @@ -139,7 +138,7 @@ func LangStreamChatHandler(tel *telemetry.Telemetry, routerManager *router.Manag
}()

for {
var chatRequest schemas.ChatStreamRequest
var chatRequest schema.ChatStreamRequest

if err = c.ReadJSON(&chatRequest); err != nil {
// TODO: handle bad request schemas gracefully and return back validation errors
Expand All @@ -155,7 +154,7 @@ func LangStreamChatHandler(tel *telemetry.Telemetry, routerManager *router.Manag
// TODO: handle termination gracefully
wg.Add(1)

go func(chatRequest schemas.ChatStreamRequest) {
go func(chatRequest schema.ChatStreamRequest) {
defer wg.Done()

r.ChatStream(context.Background(), &chatRequest, chatStreamC)
Expand Down Expand Up @@ -185,7 +184,7 @@ func LangRoutersHandler(routerManager *router.Manager) Handler {
cfgs = append(cfgs, r.Config)
}

return c.Status(fiber.StatusOK).JSON(schemas.RouterListSchema{Routers: cfgs})
return c.Status(fiber.StatusOK).JSON(schema.RouterListSchema{Routers: cfgs})
}
}

Expand All @@ -200,9 +199,9 @@ func LangRoutersHandler(routerManager *router.Manager) Handler {
// @Success 200 {object} schemas.HealthSchema
// @Router /v1/health/ [get]
func HealthHandler(c *fiber.Ctx) error {
return c.Status(fiber.StatusOK).JSON(schemas.HealthSchema{Healthy: true})
return c.Status(fiber.StatusOK).JSON(schema.HealthSchema{Healthy: true})
}

func NotFoundHandler(c *fiber.Ctx) error {
return c.Status(fiber.StatusNotFound).JSON(schemas.ErrRouteNotFound)
return c.Status(fiber.StatusNotFound).JSON(schema.ErrRouteNotFound)
}
2 changes: 1 addition & 1 deletion pkg/api/schemas/chat.go → pkg/api/schema/chat.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schemas
package schema

// ChatRequest defines Glide's Chat Request Schema unified across all language models
type ChatRequest struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schemas
package schema

import "time"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schemas
package schema

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/schemas/embed.go → pkg/api/schema/embed.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schemas
package schema

type EmbedRequest struct {
// TODO: implement
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/schemas/errors.go → pkg/api/schema/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schemas
package schema

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schemas
package schema

type HealthSchema struct {
Healthy bool `json:"healthy"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/schemas/pool.go → pkg/api/schema/pool.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schemas
package schema

import (
"sync"
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/schemas/routers.go → pkg/api/schema/routers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schemas
package schema

// RouterListSchema returns list of active configured routers.
//
Expand Down
12 changes: 5 additions & 7 deletions pkg/clients/stream.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package clients

import (
"github.com/EinStack/glide/pkg/api/schemas"
)
import "github.com/EinStack/glide/pkg/api/schema"

type ChatStream interface {
Open() error
Recv() (*schemas.ChatStreamChunk, error)
Recv() (*schema.ChatStreamChunk, error)
Close() error
}

type ChatStreamResult struct {
chunk *schemas.ChatStreamChunk
chunk *schema.ChatStreamChunk
err error
}

func (r *ChatStreamResult) Chunk() *schemas.ChatStreamChunk {
func (r *ChatStreamResult) Chunk() *schema.ChatStreamChunk {
return r.chunk
}

func (r *ChatStreamResult) Error() error {
return r.err
}

func NewChatStreamResult(chunk *schemas.ChatStreamChunk, err error) *ChatStreamResult {
func NewChatStreamResult(chunk *schema.ChatStreamChunk, err error) *ChatStreamResult {
return &ChatStreamResult{
chunk: chunk,
err: err,
Expand Down
12 changes: 6 additions & 6 deletions pkg/extmodel/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"time"

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

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

"github.com/EinStack/glide/pkg/clients"
Expand All @@ -13,16 +15,14 @@ import (
"github.com/EinStack/glide/pkg/config/fields"

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

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

type LangModel interface {
Interface
Provider() string
ModelName() string
Chat(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error)
ChatStream(ctx context.Context, params *schemas.ChatParams) (<-chan *clients.ChatStreamResult, error)
Chat(ctx context.Context, params *schema.ChatParams) (*schema.ChatResponse, error)
ChatStream(ctx context.Context, params *schema.ChatParams) (<-chan *clients.ChatStreamResult, error)
}

// LanguageModel wraps provider client and expend it with health & latency tracking
Expand Down Expand Up @@ -79,7 +79,7 @@ func (m LanguageModel) ChatStreamLatency() *latency.MovingAverage {
return m.chatStreamLatency
}

func (m *LanguageModel) Chat(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error) {
func (m *LanguageModel) Chat(ctx context.Context, params *schema.ChatParams) (*schema.ChatResponse, error) {
startedAt := time.Now()

resp, err := m.client.Chat(ctx, params)
Expand All @@ -98,7 +98,7 @@ func (m *LanguageModel) Chat(ctx context.Context, params *schemas.ChatParams) (*
return resp, err
}

func (m *LanguageModel) ChatStream(ctx context.Context, params *schemas.ChatParams) (<-chan *clients.ChatStreamResult, error) {
func (m *LanguageModel) ChatStream(ctx context.Context, params *schema.ChatParams) (<-chan *clients.ChatStreamResult, error) {
stream, err := m.client.ChatStream(ctx, params)
if err != nil {
m.healthTracker.TrackErr(err)
Expand Down
37 changes: 19 additions & 18 deletions pkg/provider/anthropic/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@ import (
"net/http"
"time"

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

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

"github.com/EinStack/glide/pkg/api/schemas"
"go.uber.org/zap"
)

// ChatRequest is an Anthropic-specific request schema
type ChatRequest struct {
Model string `json:"model"`
Messages []schemas.ChatMessage `json:"messages"`
System string `json:"system,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"top_p,omitempty"`
TopK int `json:"top_k,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Stream bool `json:"stream,omitempty"`
Metadata *string `json:"metadata,omitempty"`
StopSequences []string `json:"stop_sequences,omitempty"`
Model string `json:"model"`
Messages []schema.ChatMessage `json:"messages"`
System string `json:"system,omitempty"`
Temperature float64 `json:"temperature,omitempty"`
TopP float64 `json:"top_p,omitempty"`
TopK int `json:"top_k,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Stream bool `json:"stream,omitempty"`
Metadata *string `json:"metadata,omitempty"`
StopSequences []string `json:"stop_sequences,omitempty"`
}

func (r *ChatRequest) ApplyParams(params *schemas.ChatParams) {
func (r *ChatRequest) ApplyParams(params *schema.ChatParams) {
r.Messages = params.Messages
}

Expand All @@ -51,7 +52,7 @@ func NewChatRequestFromConfig(cfg *Config) *ChatRequest {
// Chat sends a chat request to the specified anthropic model.
//
// Ref: https://docs.anthropic.com/claude/reference/messages_post
func (c *Client) Chat(ctx context.Context, params *schemas.ChatParams) (*schemas.ChatResponse, error) {
func (c *Client) Chat(ctx context.Context, params *schema.ChatParams) (*schema.ChatResponse, error) {
// Create a new chat request
// TODO: consider using objectpool to optimize memory allocation
chatReq := *c.chatRequestTemplate
Expand All @@ -67,7 +68,7 @@ func (c *Client) Chat(ctx context.Context, params *schemas.ChatParams) (*schemas
return chatResponse, nil
}

func (c *Client) doChatRequest(ctx context.Context, payload *ChatRequest) (*schemas.ChatResponse, error) {
func (c *Client) doChatRequest(ctx context.Context, payload *ChatRequest) (*schema.ChatResponse, error) {
// Build request payload
rawPayload, err := json.Marshal(payload)
if err != nil {
Expand Down Expand Up @@ -130,19 +131,19 @@ func (c *Client) doChatRequest(ctx context.Context, payload *ChatRequest) (*sche
usage := anthropicResponse.Usage

// Map response to ChatResponse schema
response := schemas.ChatResponse{
response := schema.ChatResponse{
ID: anthropicResponse.ID,
Created: int(time.Now().UTC().Unix()), // not provided by anthropic
Provider: ProviderID,
ModelName: anthropicResponse.Model,
Cached: false,
ModelResponse: schemas.ModelResponse{
ModelResponse: schema.ModelResponse{
Metadata: map[string]string{},
Message: schemas.ChatMessage{
Message: schema.ChatMessage{
Role: completion.Type,
Content: completion.Text,
},
TokenUsage: schemas.TokenUsage{
TokenUsage: schema.TokenUsage{
PromptTokens: usage.InputTokens,
ResponseTokens: usage.OutputTokens,
TotalTokens: usage.InputTokens + usage.OutputTokens,
Expand Down
8 changes: 4 additions & 4 deletions pkg/provider/anthropic/chat_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package anthropic
import (
"context"

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

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

func (c *Client) SupportChatStream() bool {
return false
}

func (c *Client) ChatStream(_ context.Context, _ *schemas.ChatParams) (clients2.ChatStream, error) {
return nil, clients2.ErrChatStreamNotImplemented
func (c *Client) ChatStream(_ context.Context, _ *schema.ChatParams) (clients.ChatStream, error) {
return nil, clients.ErrChatStreamNotImplemented
}
8 changes: 4 additions & 4 deletions pkg/provider/anthropic/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"path/filepath"
"testing"

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

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

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

Expand Down Expand Up @@ -56,7 +56,7 @@ func TestAnthropicClient_ChatRequest(t *testing.T) {
client, err := NewClient(providerCfg, clientCfg, telemetry.NewTelemetryMock())
require.NoError(t, err)

chatParams := schemas.ChatParams{Messages: []schemas.ChatMessage{{
chatParams := schema.ChatParams{Messages: []schema.ChatMessage{{
Role: "human",
Content: "What's the biggest animal?",
}}}
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestAnthropicClient_BadChatRequest(t *testing.T) {
client, err := NewClient(providerCfg, clientCfg, telemetry.NewTelemetryMock())
require.NoError(t, err)

chatParams := schemas.ChatParams{Messages: []schemas.ChatMessage{{
chatParams := schema.ChatParams{Messages: []schema.ChatMessage{{
Role: "human",
Content: "What's the biggest animal?",
}}}
Expand Down
Loading

0 comments on commit 65d4fa0

Please sign in to comment.