Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openai: better calculation for output tokens #512

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build/cli: dep
run: dep
go run $(FLAGS) cmd/bot/main.go

run-cli:
run-cli: dep
@test -f config.yaml || (echo "please create a config.yaml first. Hint: check the config.example.yaml" && exit 1)
go run $(FLAGS) cmd/cli/main.go -config config.yaml

Expand Down
3 changes: 3 additions & 0 deletions bot/tester/fake_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func StartFakeSlack(cfg *config.Config, output io.Writer) *slacktest.Server {
c.Handle("/chat.postMessage", func(w http.ResponseWriter, r *http.Request) {
messageHandler(w, r, output)
})
c.Handle("/chat.update", func(w http.ResponseWriter, r *http.Request) {
messageHandler(w, r, output)
})
c.Handle("/chat.postEphemeral", func(w http.ResponseWriter, r *http.Request) {
messageHandler(w, r, output)
})
Expand Down
43 changes: 20 additions & 23 deletions command/openai/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,29 @@ func CallChatGPT(cfg Config, inputMessages []ChatMessage, stream bool) (<-chan s

if message := chatResponse.GetMessage().Content; message != "" {
messageUpdates <- message
return
}
} else {
// stream: each line contains a delta of the message, so one new token
fileScanner := bufio.NewScanner(resp.Body)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := fileScanner.Text()
if _, deltaJSON, found := strings.Cut(line, "data: "); found {
if deltaJSON == "[DONE]" {
// end of event stream
return
}

return
}

// each line contains a delta of the message, so one new token
fileScanner := bufio.NewScanner(resp.Body)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
line := fileScanner.Text()
if _, deltaJSON, found := strings.Cut(line, "data: "); found {
if deltaJSON == "[DONE]" {
// end of event stream
return
}

var delta ChatResponse
err = json.Unmarshal([]byte(deltaJSON), &delta)
if err != nil {
log.Warnf("openai error in json: %s (json: %s)", err, deltaJSON)
continue
}
var delta ChatResponse
err = json.Unmarshal([]byte(deltaJSON), &delta)
if err != nil {
log.Warnf("openai error in json: %s (json: %s)", err, deltaJSON)
continue
}

if deltaContent := delta.GetDelta().Content; deltaContent != "" {
messageUpdates <- deltaContent
if deltaContent := delta.GetDelta().Content; deltaContent != "" {
messageUpdates <- deltaContent
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion command/openai/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ func (c *openaiCommand) callAndStore(messages []ChatMessage, storageIdentifier s
responseText := strings.Builder{}
var lastUpdate time.Time
var dirty bool
var outputTokens int
for delta := range response {
responseText.WriteString(delta)
outputTokens++
dirty = true
if responseText.Len() > 0 && lastUpdate.Add(c.cfg.UpdateInterval).Before(time.Now()) {
lastUpdate = time.Now()
Expand Down Expand Up @@ -243,7 +245,6 @@ func (c *openaiCommand) callAndStore(messages []ChatMessage, storageIdentifier s
}

// log some stats in the end
outputTokens := estimateTokensForMessage(responseText.String())
stats.IncreaseOne("openai_calls")
stats.Increase("openai_input_tokens", inputTokens)
stats.Increase("openai_output_tokens", outputTokens)
Expand Down
4 changes: 2 additions & 2 deletions command/openai/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (c *Config) IsEnabled() bool {
var defaultConfig = Config{
APIHost: apiHost,
Model: "gpt-3.5-turbo", // aka model behind ChatGPT
UpdateInterval: time.Second,
HistorySize: 15,
UpdateInterval: time.Second * 1,
HistorySize: 25,
InitialSystemMessage: "You are a helpful Slack bot. By default, keep your answer short and truthful",

// default dall-e config
Expand Down
Loading