Skip to content

Commit

Permalink
Feature/3 provider openai (#13)
Browse files Browse the repository at this point in the history
* init

* fix: remove package

* #3: project organization

* #3-feat: build chat api

* #3 rename func

* Remove unused dependencies from go.sum

* #3: Add .env to .gitignore

---------

Co-authored-by: Max <mkrueger190@gmail.com>
  • Loading branch information
mkrueger12 and mkrueger12 authored Dec 10, 2023
1 parent 1154fe8 commit 72e75e8
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
dist
.env
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module glide

go 1.21.5

Empty file added go.sum
Empty file.
28 changes: 28 additions & 0 deletions pkg/providers/openai/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package openai

import (
"fmt"
"net/http"
)

type ProviderAPIConfig struct {
BaseURL string
Headers func(string) http.Header
Complete string
Chat string
Embed string
}

func OpenAIAPIConfig(APIKey string) *ProviderAPIConfig {
return &ProviderAPIConfig{
BaseURL: "https://api.openai.com/v1",
Headers: func(APIKey string) http.Header {
headers := make(http.Header)
headers.Set("Authorization", fmt.Sprintf("Bearer %s", APIKey))
return headers
},
Complete: "/completions",
Chat: "/chat/completions",
Embed: "/embeddings",
}
}
119 changes: 119 additions & 0 deletions pkg/providers/openai/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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"`
}

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"`
}

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 {
return ProviderConfig{
Model: ConfigItem{
Param: "model",
Required: true,
Default: "gpt-3.5-turbo",
},
Messages: ConfigItem{
Param: "messages",
Default: "",
},
Functions: ConfigItem{
Param: "functions",
},
FunctionCall: ConfigItem{
Param: "function_call",
},
MaxTokens: NumericConfigItem{
Param: "max_tokens",
Default: 100,
Min: 0,
},
Temperature: NumericConfigItem{
Param: "temperature",
Default: 1,
Min: 0,
Max: 2,
},
TopP: NumericConfigItem{
Param: "top_p",
Default: 1,
Min: 0,
Max: 1,
},
N: NumericConfigItem{
Param: "n",
Default: 1,
},
Stream: BoolConfigItem{
Param: "stream",
Default: false,
},
Stop: ConfigItem{
Param: "stop",
},
PresencePenalty: NumericConfigItem{
Param: "presence_penalty",
Min: -2,
Max: 2,
},
FrequencyPenalty: NumericConfigItem{
Param: "frequency_penalty",
Min: -2,
Max: 2,
},
LogitBias: ConfigItem{
Param: "logit_bias",
},
User: ConfigItem{
Param: "user",
},
Seed: ConfigItem{
Param: "seed",
},
Tools: ConfigItem{
Param: "tools",
},
ToolChoice: ConfigItem{
Param: "tool_choice",
},
ResponseFormat: ConfigItem{
Param: "response_format",
},
}
}

0 comments on commit 72e75e8

Please sign in to comment.