diff --git a/glide b/glide new file mode 100755 index 00000000..39b0ef50 Binary files /dev/null and b/glide differ diff --git a/go.mod b/go.mod index f4cf2572..b97eba37 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/pkg/buildAPIRequest.go b/pkg/buildAPIRequest.go new file mode 100644 index 00000000..6b8af6ea --- /dev/null +++ b/pkg/buildAPIRequest.go @@ -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 + + +} \ No newline at end of file diff --git a/pkg/providers/openai/api.go b/pkg/providers/openai/api.go index c9466b9a..515e2cc1 100644 --- a/pkg/providers/openai/api.go +++ b/pkg/providers/openai/api.go @@ -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 diff --git a/pkg/providers/openai/chat.go b/pkg/providers/openai/chat.go index 0ec3a34f..bb9051d7 100644 --- a/pkg/providers/openai/chat.go +++ b/pkg/providers/openai/chat.go @@ -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", diff --git a/pkg/providers/openai/index.go b/pkg/providers/openai/index.go new file mode 100644 index 00000000..9527b777 --- /dev/null +++ b/pkg/providers/openai/index.go @@ -0,0 +1,8 @@ +package pkg + +type ProviderConfigs map[string]interface{} // TODO: import from types.go + +var OpenAIConfig = ProviderConfigs{ + "api": OpenAIAPIConfig, + "chat": OpenAiChatDefaultConfig, +} diff --git a/pkg/providers/types.go b/pkg/providers/types.go new file mode 100644 index 00000000..b27094d5 --- /dev/null +++ b/pkg/providers/types.go @@ -0,0 +1,3 @@ +package pkg + +type ProviderConfigs map[string]interface{}