Skip to content

Commit

Permalink
Merge pull request #8 from atomy/main
Browse files Browse the repository at this point in the history
soft and hard char limit for !gpt to 121 chars, dystopia example bat
  • Loading branch information
algo7 authored Mar 20, 2023
2 parents 63f5134 + 67236b5 commit 5e4d543
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ main
sample.txt

run2.bat
run2.sh
run2.sh
runDystopia2.bat
runDystopia2.sh
8 changes: 4 additions & 4 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var SelfCommandMap = map[string]func(args string){
// Ask gpt API and print reponse
"!gpt": func(args string) {
fmt.Println(args)
gpt.Ask("In 1 sentence:" + args)
gpt.Ask(args)
},
// Just a test command
"!test": func(args string) {
Expand All @@ -38,7 +38,7 @@ var OtherUsersCommandMap = map[string]func(args string){
// Stuff follows the : are only function pointers not function calls
// Ask gpt API and print reponse
"!gpt": func(args string) {
gpt.Ask("In 1 sentence:" + args)
gpt.Ask(args)
},
"!nice": func(args string) {
time.Sleep(1000 * time.Millisecond)
Expand Down Expand Up @@ -74,7 +74,7 @@ func RunCommands(text string, isSelf bool) {
}

// Command is not configured
fmt.Printf("\nCommand '%s' unconfigured!\n", strings.TrimSuffix(strings.TrimSuffix(command, "\n"), "\r"))
fmt.Printf("\nSelfCommand '%s' unconfigured!\n", strings.TrimSuffix(strings.TrimSuffix(command, "\n"), "\r"))

case false:

Expand All @@ -90,7 +90,7 @@ func RunCommands(text string, isSelf bool) {
}

// Command is not configured
fmt.Printf("\nCommand '%s' unconfigured!\n", strings.TrimSuffix(strings.TrimSuffix(command, "\n"), "\r"))
fmt.Printf("\nOthersCommand '%s' unconfigured!\n", strings.TrimSuffix(strings.TrimSuffix(command, "\n"), "\r"))
}
}
}
53 changes: 24 additions & 29 deletions gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"tf2-rcon/network"
"tf2-rcon/utils"
"time"
"strings"

openai "github.com/sashabaranov/go-openai"
)
Expand Down Expand Up @@ -55,9 +56,10 @@ func Ask(question string) {
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleUser,
Content: question,
Content: "Always limit your response to maximum of 121 characters, try to formulate a short answer that always fits that criteria. The question is: " + question,
},
},
MaxTokens: int(121),
},
)

Expand All @@ -67,33 +69,26 @@ func Ask(question string) {
}

// Return Content node, remove empty lines from it beforehand
responses := utils.RemoveEmptyLines(resp.Choices[0].Message.Content)

fmt.Println("!gpt - requesting:", question, "- Response:", responses)

// Split the original string into chunks of 121 characters, the overall chat-say limit is 126, subtract any chars needed for prefix
// Have at max 2 interations cause we dont want to spam chat
chunk := ""
for i := 0; i < len(responses); i += 121 {
end := i + 121

if end > len(responses) {
end = len(responses)
}

chunk = responses[i:end]

// If no the 1st try, delay 1000 ms cause else we may get supressed
if i != 0 {
time.Sleep(1000 * time.Millisecond)

network.RconExecute("say \"GPT> " + chunk + "\"")
break // only execute this once, we dont want to spam

}

// on first run only delay 500 ms
time.Sleep(500 * time.Millisecond)
network.RconExecute("say \"GPT> " + chunk + "\"")
responseText := utils.RemoveEmptyLines(resp.Choices[0].Message.Content)

fmt.Println("!gpt - requesting:", question, "- Response:", responseText)

// Remove any newlines and limit response to 121 characters
responseText = strings.Replace(responseText, "\n", " ", -1)
if len(responseText) > 121 {
responseText = strings.TrimSpace(responseText)[:121]
lastSpace := strings.LastIndex(responseText, " ")

if lastSpace != -1 {
responseText = responseText[:lastSpace] + "..."
} else {
responseText += "..."
}
} else {
responseText = strings.TrimSpace(responseText)
}

// on first run only delay 500 ms
time.Sleep(500 * time.Millisecond)
network.RconExecute("say \"GPT> " + responseText + "\"")
}
8 changes: 8 additions & 0 deletions runDystopia.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@echo off

set "MONGODB_URI=mongodb+srv://mongo:[email protected]/?retryWrites=true&w=majority"
set TF2_LOGPATH=C:\\Steam\\steamapps\\common\\Dystopia\\dystopia\\console.log
set MONGODB_NAME=TeamFortress2
set OPENAI_APIKEY=xxx

.\build\main-windows-amd64.exe

0 comments on commit 5e4d543

Please sign in to comment.