Skip to content

Commit

Permalink
#29: chores
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger12 committed Dec 23, 2023
1 parent f5a8ecd commit e8fef9f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
39 changes: 19 additions & 20 deletions pkg/providers/openai/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"encoding/json"
"errors"
"fmt"
"glide/pkg/providers"
"io"
"log/slog"
"net/http"
"reflect"
"strings"

"glide/pkg/providers"
)

const (
Expand All @@ -25,11 +26,9 @@ type ProviderClient struct {
PoolName string `validate:"required"`
BaseURL string `validate:"required"`
Payload []byte `validate:"required"`
HttpClient *http.Client `validate:"required"`
HTTPClient *http.Client `validate:"required"`
}



// ChatRequest is a request to complete a chat completion..
type ChatRequest struct {
Model string `json:"model" validate:"required,lowercase"`
Expand Down Expand Up @@ -79,6 +78,20 @@ type ChatUsage struct {
TotalTokens int `json:"total_tokens"`
}

// ChatResponse is a response to a chat request.
type ChatResponse struct {
ID string `json:"id,omitempty"`
Created float64 `json:"created,omitempty"`
Choices []*ChatChoice `json:"choices,omitempty"`
Model string `json:"model,omitempty"`
Object string `json:"object,omitempty"`
Usage struct {
CompletionTokens float64 `json:"completion_tokens,omitempty"`
PromptTokens float64 `json:"prompt_tokens,omitempty"`
TotalTokens float64 `json:"total_tokens,omitempty"`
} `json:"usage,omitempty"`
}

// Chat sends a chat request to the specified OpenAI model.
//
// Parameters:
Expand Down Expand Up @@ -165,25 +178,11 @@ func (c *ProviderClient) CreateChatRequest(message []byte) *ChatRequest {
return chatRequest
}

// ChatResponse is a response to a chat request.
type ChatResponse struct {
ID string `json:"id,omitempty"`
Created float64 `json:"created,omitempty"`
Choices []*ChatChoice `json:"choices,omitempty"`
Model string `json:"model,omitempty"`
Object string `json:"object,omitempty"`
Usage struct {
CompletionTokens float64 `json:"completion_tokens,omitempty"`
PromptTokens float64 `json:"prompt_tokens,omitempty"`
TotalTokens float64 `json:"total_tokens,omitempty"`
} `json:"usage,omitempty"`
}

// CreateChatResponse creates chat Response.
func (c *ProviderClient) CreateChatResponse(ctx context.Context, r *ChatRequest) (*ChatResponse, error) {
_ = ctx // keep this for future use

resp, err := c.createChatHTTP(r) // netpoll -> hertz does not yet support tls
resp, err := c.createChatHTTP(r) // netpoll/hertz does not yet support tls
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -223,7 +222,7 @@ func (c *ProviderClient) createChatHTTP(payload *ChatRequest) (*ChatResponse, er
req.Header.Set("Authorization", "Bearer "+c.Provider.APIKey)
req.Header.Set("Content-Type", "application/json")

resp, err := c.HttpClient.Do(req)
resp, err := c.HTTPClient.Do(req)
if err != nil {
slog.Error(err.Error())
return nil, err
Expand Down
3 changes: 1 addition & 2 deletions pkg/providers/openai/openaiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var (
ErrEmptyResponse = errors.New("empty response")
)


// OpenAiClient creates a new client for the OpenAI API.
//
// Parameters:
Expand Down Expand Up @@ -68,7 +67,7 @@ func Client(poolName string, modelName string, payload []byte) (*ProviderClient,
PoolName: poolName,
BaseURL: defaultBaseURL,
Payload: payload,
HttpClient: providers.HTTPClient,
HTTPClient: providers.HTTPClient,
}

v := validator.New()
Expand Down
16 changes: 7 additions & 9 deletions pkg/providers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package providers
import (
"fmt"
"log/slog"
"net/http"
"os"
"path/filepath"
"net/http"
"time"

"gopkg.in/yaml.v2"

)

type GatewayConfig struct {
Expand Down Expand Up @@ -39,12 +38,12 @@ type ProviderVars struct {
}

type RequestBody struct {
Message []struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
MessageHistory []string `json:"messageHistory"`
}
Message []struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
MessageHistory []string `json:"messageHistory"`
}

// Variables

Expand Down Expand Up @@ -161,4 +160,3 @@ func ReadConfig(filePath string) (GatewayConfig, error) {

return config, nil
}

0 comments on commit e8fef9f

Please sign in to comment.