Skip to content

Commit

Permalink
Merge pull request #494 from innogames/md_openai_config
Browse files Browse the repository at this point in the history
openai: add more config flags
  • Loading branch information
brainexe authored Nov 8, 2023
2 parents 5391a0e + 14e55db commit 2e5c7ff
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
25 changes: 12 additions & 13 deletions command/openai/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ const (
roleAssistant = "assistant"
)

// https://platform.openai.com/docs/guides/chat/chat-completions-beta
// https://platform.openai.com/docs/api-reference/chat
type ChatRequest struct {
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
N int `json:"n,omitempty"`
Stop []string `json:"stop,omitempty"`
Stream bool `json:"stream,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
PresencePenalty float32 `json:"presence_penalty,omitempty"`
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
LogitBias map[string]int `json:"logit_bias,omitempty"`
User string `json:"user,omitempty"`
Model string `json:"model"`
Messages []ChatMessage `json:"messages"`
Temperature float32 `json:"temperature,omitempty"`
TopP float32 `json:"top_p,omitempty"`
N int `json:"n,omitempty"`
Stop []string `json:"stop,omitempty"`
Stream bool `json:"stream,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
PresencePenalty float32 `json:"presence_penalty,omitempty"`
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
User string `json:"user,omitempty"`
Seed string `json:"seed,omitempty"`
}

type ChatMessage struct {
Expand Down
2 changes: 2 additions & 0 deletions command/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func CallChatGPT(cfg Config, inputMessages []ChatMessage, stream bool) (<-chan s
jsonData, _ := json.Marshal(ChatRequest{
Model: cfg.Model,
Temperature: cfg.Temperature,
Seed: cfg.Seed,
MaxTokens: cfg.MaxTokens,
Stream: stream,
Messages: inputMessages,
})
Expand Down
15 changes: 11 additions & 4 deletions command/openai/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ func (c *chatGPTCommand) startConversation(message msg.Ref, text string) bool {
})
}
storageIdentifier = getIdentifier(message.GetChannel(), message.GetThread())
log.Infof("openai thread context: %s", messageHistory)
if c.cfg.LogTexts {
log.Infof("openai thread context: %s", messageHistory)
}
} else if linkRe.MatchString(text) {
// a link to another thread was posted -> use this messages as context
link := linkRe.FindStringSubmatch(text)
Expand Down Expand Up @@ -242,14 +244,19 @@ func (c *chatGPTCommand) callAndStore(messages []ChatMessage, storageIdentifier
stats.Increase("openai_output_tokens", estimateTokensForMessage(responseText.String()))

log.Infof(
"Openai %s call took %s with %d sub messages (%d tokens). Message: '%s'. Response: '%s'",
"Openai %s call took %s with %d sub messages (%d tokens).",
c.cfg.Model,
util.FormatDuration(time.Since(startTime)),
len(messages),
inputTokens,
inputText,
responseText.String(),
)
if c.cfg.LogTexts {
log.Infof(
"Openai texts. Input: '%s'. Response: '%s'",
inputText,
responseText.String(),
)
}
}()
}

Expand Down
5 changes: 5 additions & 0 deletions command/openai/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Config struct {
InitialSystemMessage string `mapstructure:"initial_system_message"`
Model string `mapstructure:"model"`
Temperature float32 `mapstructure:"temperature"`
Seed string `mapstructure:"seed"`
MaxTokens int `mapstructure:"max_tokens"`

// number of thread messages stored which are used as a context for further requests
HistorySize int `mapstructure:"history_size"`
Expand All @@ -22,6 +24,9 @@ type Config struct {

// maximum update frequency of slack messages when "stream" is active
UpdateInterval time.Duration `mapstructure:"update_interval"`

// log all input+output text to the logger. This could include personal information, therefore disabled by default!
LogTexts bool `mapstructure:"log_texts"`
}

// IsEnabled checks if token is set
Expand Down
14 changes: 8 additions & 6 deletions command/openai/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (
var maxTokens = map[string]int{
"gpt-4": 8192,
"gpt-4-32k": 32768,
"gpt-3.5-turbo-16k": 16385,
"gpt-3.5-turbo": 4096,
"gpt-4-1106-preview": 128000,
"gpt-4-vision-preview": 128000,
"gpt-3.5-turbo-16k": 16385,
"gpt-3.5-turbo": 4096,
"dummy-test": 100, // just for testing
}

var modelDateRe = regexp.MustCompile(`-\d{4}`)

// truncateMessages will truncate the messages to fit into the max tokens limit of the model
// we always try to keep the last message, so we will truncate the first messages
func truncateMessages(model string, inputMessages []ChatMessage) ([]ChatMessage, int, int) {
outputMessages := make([]ChatMessage, 0, len(inputMessages))

Expand Down Expand Up @@ -47,12 +49,12 @@ func getMaxTokensForModel(model string) int {
return getMaxTokensForModel(modelDateRe.ReplaceAllString(model, ""))
}

// we need some default
return 4000
// we need some default, keep it high, as new models will most likely support more tokens
return 128000
}

// to lower the dependency to heavy external libs we use the rule of thumbs which is totally fine here
// https://platform.openai.com/tokenizer
func estimateTokensForMessage(message string) int {
// to lower the dependency to heavy external libs we use the rule of thumbs which is totally fine here
// https://platform.openai.com/tokenizer
return len(message) / 4
}
4 changes: 2 additions & 2 deletions command/openai/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func TestModels(t *testing.T) {
input string
expected int
}{
{"", 4000},
{"jolo", 4000},
{"", 128000},
{"jolo", 128000},
{"gpt-4", 8192},
{"gpt-4-0613", 8192},
{"gpt-4-32k-0613", 32768},
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ oauth_config:
- reactions:read
- reactions:write
- users:read
- files:write
- files:read
settings:
event_subscriptions:
bot_events:
Expand Down Expand Up @@ -302,6 +302,7 @@ openai:
update_interval: '3s' # fewer Slack messages update during generation
model: gpt-3.5-turbo
temperature: 0.8
log_texts: true # opt in: log all input/output text to the log
```

When using the "openai XXX" command within a existing thread, the previous messages are used as context for further calls.
Expand Down

0 comments on commit 2e5c7ff

Please sign in to comment.