Skip to content

Commit

Permalink
3 provider openai (#17)
Browse files Browse the repository at this point in the history
* #3: Update dependencies, fix package names, add validators

* #3: Remove unused fields in ProviderConfig struct

* #3: Update OpenAI provider configuration validation

* #3: Refactor OpenAI provider config structure

* #3: Update go.mod and go.sum files

* #3: add comments

* #3: Refactor package name in OpenAI API and chat files

* #3: build OpenAI config

* #3: Build API Request

* #3: Refactor param checking in BuildAPIRequest function

* #3: build request based on client parameters

---------

Co-authored-by: Max <[email protected]>
  • Loading branch information
mkrueger12 and mkrueger12 authored Dec 12, 2023
1 parent 27f7118 commit bd1d4d3
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 39 deletions.
Binary file added glide
Binary file not shown.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module glide

go 1.21.5

require (
github.com/cloudwego/hertz v0.7.3
github.com/spf13/cobra v1.8.0
Expand Down
44 changes: 44 additions & 0 deletions pkg/buildAPIRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pkg

import (
"errors"
)

type ProviderConfigs map[string]interface{} // TODO: import from types.go

func BuildAPIRequest(provider string, params map[string]string, mode string, configList map[string]interface{}) (interface{}, error) {
// provider is the name of the provider, e.g. "openai", params is the map of parameters from the client,
// mode is the mode of the provider, e.g. "chat", configList is the list of provider configurations


var providerConfig map[string]interface{}
if config, ok := configList[provider].(ProviderConfigs); ok {
if modeConfig, ok := config[mode].(map[string]interface{}); ok {
providerConfig = modeConfig
}
}

// If the provider is not supported, return an error
if providerConfig == nil {
return nil, errors.New("unsupported provider")
}


// TODO: Next need to build the request based on the params from the client
// First for each param in param check if present. If yes then add it to the request.
// If not & the param is required, return a default value from the provider config

for key := range providerConfig {
if value, exists := params[key]; exists {
providerConfig[key] = value
}
}


}

// For now, return providerConfig and nil error to satisfy the function signature
return providerConfig, nil


}
4 changes: 3 additions & 1 deletion pkg/providers/openai/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package openai
package pkg

import (
"fmt"
"net/http"
)


// provides the base URL and headers for the OpenAI API
type ProviderAPIConfig struct {
BaseURL string
Headers func(string) http.Header
Expand Down
57 changes: 20 additions & 37 deletions pkg/providers/openai/chat.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,31 @@
package openai

type ProviderConfig struct {
Model ConfigItem `json:"model"`
Messages ConfigItem `json:"messages"`
Functions ConfigItem `json:"functions"`
FunctionCall ConfigItem `json:"function_call"`
MaxTokens NumericConfigItem `json:"max_tokens"`
Temperature NumericConfigItem `json:"temperature"`
TopP NumericConfigItem `json:"top_p"`
N NumericConfigItem `json:"n"`
Stream BoolConfigItem `json:"stream"`
Stop ConfigItem `json:"stop"`
PresencePenalty NumericConfigItem `json:"presence_penalty"`
FrequencyPenalty NumericConfigItem `json:"frequency_penalty"`
LogitBias ConfigItem `json:"logit_bias"`
User ConfigItem `json:"user"`
Seed ConfigItem `json:"seed"`
Tools ConfigItem `json:"tools"`
ToolChoice ConfigItem `json:"tool_choice"`
ResponseFormat ConfigItem `json:"response_format"`
Model ConfigItem `json:"model" validate:"required,lowercase"`
Messages ConfigItem `json:"messages" validate:"required"`
MaxTokens ConfigItem `json:"max_tokens" validate:"omitempty,gte=0"`
Temperature ConfigItem `json:"temperature" validate:"omitempty,gte=0,lte=2"`
TopP ConfigItem `json:"top_p" validate:"omitempty,gte=0,lte=1"`
N ConfigItem `json:"n" validate:"omitempty,gte=1"`
Stream ConfigItem `json:"stream" validate:"omitempty, boolean"`
Stop ConfigItem `json:"stop"`
PresencePenalty ConfigItem `json:"presence_penalty" validate:"omitempty,gte=-2,lte=2"`
FrequencyPenalty ConfigItem `json:"frequency_penalty" validate:"omitempty,gte=-2,lte=2"`
LogitBias ConfigItem `json:"logit_bias" validate:"omitempty"`
User ConfigItem `json:"user"`
Seed ConfigItem `json:"seed" validate:"omitempty,gte=0"`
Tools ConfigItem `json:"tools"`
ToolChoice ConfigItem `json:"tool_choice"`
ResponseFormat ConfigItem `json:"response_format"`
}

type ConfigItem struct {
Param string `json:"param"`
Required bool `json:"required,omitempty"`
Default interface{} `json:"default,omitempty"`
Min interface{} `json:"min,omitempty"`
Max interface{} `json:"max,omitempty"`
Param string `json:"param" validate:"required"`
Default interface{} `json:"default"`
}

type NumericConfigItem struct {
Param string `json:"param"`
Default float64 `json:"default,omitempty"`
Min float64 `json:"min,omitempty"`
Max float64 `json:"max,omitempty"`
}

type BoolConfigItem struct {
Param string `json:"param"`
Default bool `json:"default,omitempty"`
}

// DefaultProviderConfig returns an instance of ProviderConfig with default values.
func OpenAiDefaultConfig() ProviderConfig {
// Provide the request body for OpenAI's ChatCompletion API
func OpenAiChatDefaultConfig() ProviderConfig {
return ProviderConfig{
Model: ConfigItem{
Param: "model",
Expand Down
8 changes: 8 additions & 0 deletions pkg/providers/openai/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pkg

type ProviderConfigs map[string]interface{} // TODO: import from types.go

var OpenAIConfig = ProviderConfigs{
"api": OpenAIAPIConfig,
"chat": OpenAiChatDefaultConfig,
}
3 changes: 3 additions & 0 deletions pkg/providers/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package pkg

type ProviderConfigs map[string]interface{}

0 comments on commit bd1d4d3

Please sign in to comment.