From 12a155e68136accb92907f6f0ef3806b48596978 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Sat, 8 Apr 2023 17:43:19 -0700 Subject: [PATCH] Add playground repl golang program --- server/playground.go | 70 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 server/playground.go diff --git a/server/playground.go b/server/playground.go new file mode 100644 index 0000000..7024659 --- /dev/null +++ b/server/playground.go @@ -0,0 +1,70 @@ +package main + +import ( + openai "github.com/sashabaranov/go-openai" + "context" + "bufio" + "os" + "fmt" +) + +func main() { + api_key := os.Getenv("OPENAI_API_KEY") + if api_key == "" { + fmt.Printf("Must provide openai key\n") + return + } + c := openai.NewClient(api_key) + + + messages := make([]openai.ChatCompletionMessage, 0) + + messages = append(messages, openai.ChatCompletionMessage { + Role: "system", + Content: `You are a wise dungeonmaster who carefully crafts interesting dialog and actions for an NPC in an action-rpg video game. The NPC performs actions by prefixing their dialog with one of these actions: [ACT_none, ACT_fights_player, ACT_joins_player, ACT_strikes_air] + +An example interaction between the player and an NPC: +Player: ACT_walks_up +Player: "What's going on npc?" +Fredrick: ACT_none "Hello young warrior, how are you doing?" +Player: "I'll kill you" +Fredrick: ACT_strikes_air "Watch yourself!" +Player: ACT_hits_npc +Fredrick: ACT_fights_player "There will be blood!" + +The NPC you will be acting as, Fredrick, is a soldier in death's cohort. +`, + }) + + + reader := bufio.NewReader(os.Stdin) + for { + fmt.Printf("Say something with format [action] \"dialog\": ") + text, _ := reader.ReadString('\n') + messages = append(messages, openai.ChatCompletionMessage { + Role: "user", + Content: text + "\nFredrick: ", + }) + + //fmt.Printf("Generating with messages: `%s`\n", messages) + resp, err := c.CreateChatCompletion( + context.Background(), + openai.ChatCompletionRequest{ + Model: openai.GPT3Dot5Turbo, + Messages: messages, + Stop: []string{"\n"}, + }, + ) + if err != nil { + fmt.Printf("Failed to generate: %s\n", err) + return + } else { + //fmt.Printf("Response: `%s`\n", resp) + messages = append(messages, openai.ChatCompletionMessage { + Role: "assistant", + Content: resp.Choices[0].Message.Content, + }) + fmt.Printf("Response: `%s`\n", resp.Choices[0].Message.Content) + } + } +}