Add playground repl golang program

main
Cameron Murphy Reikes 2 years ago
parent 6f05ae350b
commit 12a155e681

@ -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)
}
}
}
Loading…
Cancel
Save