From e8fef9f3ef5211ca50114c27615c9b9c2c393c22 Mon Sep 17 00:00:00 2001 From: Max Date: Fri, 22 Dec 2023 18:32:01 -0700 Subject: [PATCH] #29: chores --- pkg/providers/openai/chat.go | 39 ++++++++++++++-------------- pkg/providers/openai/openaiclient.go | 3 +-- pkg/providers/types.go | 16 +++++------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/pkg/providers/openai/chat.go b/pkg/providers/openai/chat.go index 7bd832c5..da6a5aab 100644 --- a/pkg/providers/openai/chat.go +++ b/pkg/providers/openai/chat.go @@ -6,12 +6,13 @@ import ( "encoding/json" "errors" "fmt" - "glide/pkg/providers" "io" "log/slog" "net/http" "reflect" "strings" + + "glide/pkg/providers" ) const ( @@ -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"` @@ -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: @@ -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 } @@ -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 diff --git a/pkg/providers/openai/openaiclient.go b/pkg/providers/openai/openaiclient.go index 14620770..a22f9c07 100644 --- a/pkg/providers/openai/openaiclient.go +++ b/pkg/providers/openai/openaiclient.go @@ -24,7 +24,6 @@ var ( ErrEmptyResponse = errors.New("empty response") ) - // OpenAiClient creates a new client for the OpenAI API. // // Parameters: @@ -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() diff --git a/pkg/providers/types.go b/pkg/providers/types.go index 37fefc6b..48e024f3 100644 --- a/pkg/providers/types.go +++ b/pkg/providers/types.go @@ -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 { @@ -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 @@ -161,4 +160,3 @@ func ReadConfig(filePath string) (GatewayConfig, error) { return config, nil } -