Skip to content

Commit

Permalink
Release v0.1.0 - Dev PR
Browse files Browse the repository at this point in the history
Viper config
  • Loading branch information
seemywingz authored Apr 2, 2023
1 parent b852d06 commit 5977649
Show file tree
Hide file tree
Showing 16 changed files with 782 additions and 182 deletions.
19 changes: 10 additions & 9 deletions cmd/Discord_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/bwmarrin/discordgo"
"github.com/spf13/viper"
)

var discord *discordgo.Session
Expand Down Expand Up @@ -97,10 +98,10 @@ func handleMessages(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}

channelName := discordGetChannelName(m.ChannelID)
// channelName := discordGetChannelName(m.ChannelID)

// Respond to messages in the #ponder channel
if channelName == "ponder" || m.GuildID == "" {
if m.GuildID == "" {
discordOpenAIResponse(s, m, false)
return
}
Expand All @@ -120,10 +121,10 @@ func discordOpenAIResponse(s *discordgo.Session, m *discordgo.MessageCreate, men
discord.ChannelTyping(m.ChannelID)
openaiMessages := []OPENAI_Message{{
Role: "system",
Content: discord_SystemMessage,
Content: viper.GetString("discord_bot_systemMessage"),
}}

discordMessages, err := discord.ChannelMessages(m.ChannelID, 9, "", "", "")
discordMessages, err := discord.ChannelMessages(m.ChannelID, viper.GetInt("discord_message_context_count"), "", "", "")
catchErr(err)
discordMessages = discordReverseMessageOrder(discordMessages)

Expand All @@ -145,11 +146,11 @@ func discordOpenAIResponse(s *discordgo.Session, m *discordgo.MessageCreate, men
s.ChannelMessageSend(m.ChannelID, oaiResponse)
}

func discordGetChannelName(channelID string) string {
channel, err := discord.Channel(channelID)
catchErr(err)
return channel.Name
}
// func discordGetChannelName(channelID string) string {
// channel, err := discord.Channel(channelID)
// catchErr(err)
// return channel.Name
// }

func discordGetChannelID(s *discordgo.Session, guildID string, channelName string) string {
channels, err := s.GuildChannels(guildID)
Expand Down
52 changes: 25 additions & 27 deletions cmd/OpenAI_API.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"path/filepath"
"strconv"

"github.com/spf13/viper"
)

func openAI_UploadImage(requestJson, responseJson interface{}, endpoint, filePath string) {
Expand All @@ -18,7 +20,7 @@ func openAI_UploadImage(requestJson, responseJson interface{}, endpoint, filePat
fullPath, err := filepath.Abs(filePath)
catchErr(err)

// https://platform.openai.com/docs/api-reference/images/create-edit#images/create-edit-image
// https://platform.openAI_com/docs/api-reference/images/create-edit#images/create-edit-image
// The image to edit. Must be a valid PNG file, less than 4MB, and square.
// If mask is not provided, image must have transparency, which will be used as the mask.
//
Expand Down Expand Up @@ -105,85 +107,81 @@ func openAI_ImageGen(prompt, imageFile string, n int) OPENAI_ImageResponse {

// Create the JSON Request Body
oaiRequest = &OPENAI_ImageEditRequest{
Prompt: prompt,
N: n,
Size: "1024x1024",
ResponseFormat: "url",
Prompt: prompt,
User: openAIUser,
Size: viper.GetString("openAI_image_size"),
}
openAI_UploadImage(oaiRequest, &oaiResponse, openai_endpoint+"images/edits", imageFile)
openAI_UploadImage(oaiRequest, &oaiResponse, viper.GetString("openAI_endpoint")+"images/edits", imageFile)

} else { // Generate a new image

oaiRequest = &OPENAI_ImageRequest{
Prompt: prompt,
N: n,
Size: "1024x1024",
ResponseFormat: "url",
Prompt: prompt,
User: openAIUser,
Size: viper.GetString("openAI_image_size"),
}
openAI_PostJson(oaiRequest, &oaiResponse, openai_endpoint+"images/generations")
openAI_PostJson(oaiRequest, &oaiResponse, viper.GetString("openAI_endpoint")+"images/generations")
}
if verbose {
trace()
fmt.Println(oaiRequest)
}

return oaiResponse
}

func openai_ChatCompletion(messages []OPENAI_Message) string {
oaiResponse := OPENAI_ChatCompletionResponse{}
oaiRequest := OPENAI_ChatCompletionRequest{
Model: "gpt-3.5-turbo",
Messages: messages,
N: 1,
Temperature: 0,
TopP: 0.1,
FrequencyPenalty: 0.0,
PresencePenalty: 0.6,
MaxTokens: 999,
Messages: messages,
User: openAIUser,
TopP: viper.GetFloat64("openAI_chat_topP"),
Model: viper.GetString("openAI_chat_model"),
MaxTokens: viper.GetInt("openAI_chat_maxTokens"),
Temperature: viper.GetFloat64("openAI_chat_temperature"),
FrequencyPenalty: viper.GetFloat64("openAI_chat_frequencyPenalty"),
PresencePenalty: viper.GetFloat64("openAI_chat_presencePenalty"),
}
openAI_PostJson(oaiRequest, &oaiResponse, openai_endpoint+"chat/completions")
openAI_PostJson(oaiRequest, &oaiResponse, viper.GetString("openAI_endpoint")+"chat/completions")
return oaiResponse.Choices[0].Message.Content
}

func openAI_Completion(prompt string) OPENAI_ChatResponse {
func openAI_TextCompletion(prompt string) OPENAI_ChatResponse {
oaiResponse := OPENAI_ChatResponse{}
oaiRequest := &OPENAI_ChatRequest{
Model: "text-davinci-003",
Prompt: prompt,
MaxTokens: 999,
Temperature: 0,
TopP: 0.1,
FrequencyPenalty: 0.0,
PresencePenalty: 0.6,
User: openAIUser,
Model: viper.GetString("openAI_text_model"),
MaxTokens: viper.GetInt("openAI_text_maxTokens"),
Temperature: viper.GetFloat64("openAI_text_temperature"),
TopP: viper.GetFloat64("openAI_text_topP"),
FrequencyPenalty: viper.GetFloat64("openAI_text_frequencyPenalty"),
PresencePenalty: viper.GetFloat64("openAI_text_presencePenalty"),
}
if verbose {
trace()
fmt.Println(oaiRequest)
}
openAI_PostJson(oaiRequest, &oaiResponse, openai_endpoint+"completions")
openAI_PostJson(oaiRequest, &oaiResponse, viper.GetString("openAI_endpoint")+"completions")
return oaiResponse
}

func openAI_PostJson(requestJson, responseJson interface{}, endpoint string) {

// Marshal the JSON Request Body
requestBodyJson, err := json.Marshal(requestJson)
catchErr(err)
if verbose {
trace()
fmt.Println(string(requestBodyJson))
}

// Format HTTP Response and Set Headers
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(requestBodyJson))
catchErr(err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+OPENAI_API_KEY)

httpMakeRequest(req, responseJson)
}
108 changes: 0 additions & 108 deletions cmd/apiServer.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func chatCompletion(prompt string) string {

func textCompletion(prompt string) {

oaiResponse := openAI_Completion(prompt)
oaiResponse := openAI_TextCompletion(prompt)

for _, v := range oaiResponse.Choices {
text := v.Text
Expand Down
4 changes: 0 additions & 4 deletions cmd/consts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package cmd

const openai_endpoint = "https://api.openai.com/v1/"

const printify_endpoint = "https://api.printify.com/v1/"

const discord_SystemMessage = "You are Ponder. Ponder is here to help you with your Discord needs. Please be respectful and courteous when interacting with Ponder. Ponder will not tolerate any form of harassment, bullying, or discrimination. If you have any questions or concerns, please let us know. Thank you for using Ponder!"

const ponder_SystemMessage = "You are Ponder.Welcome to Ponder! I'm an advanced chat bot powered by GPT-3.5-Turbo, designed to assist you with your needs and provide helpful responses. Whether you have questions about a particular topic or need assistance with a task, I'm here to help. Please feel free to ask me anything, and I'll do my best to provide you with accurate and informative answers. Thank you for choosing Ponder!"
24 changes: 14 additions & 10 deletions cmd/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var filePath = "HOME"
var open, download bool
var file string
var n int
Expand All @@ -35,7 +36,7 @@ var imageCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(imageCmd)
imageCmd.Flags().BoolVarP(&download, "download", "d", false, "Download image(s) to local directory")
imageCmd.Flags().BoolVarP(&open, "open", "o", false, "Open image in browser")
imageCmd.Flags().BoolVarP(&open, "open", "o", false, "Open image in system default viewer")
imageCmd.Flags().IntVarP(&n, "n", "n", 1, "Number of images to generate")
imageCmd.Flags().StringVarP(&file, "file", "f", "", "Image file to edit")
}
Expand All @@ -49,18 +50,21 @@ func createImage(prompt, imageFile string) {
fmt.Println("🌐 Image URL: " + url)

if download { // Download image to local directory if download flag is set
promptPath := formatPrompt(prompt)
if filePath == "HOME" { // If no path is specified, use the user's home directory
currentUser, err := user.Current()
catchErr(err)
filePath = currentUser.HomeDir + "/Ponder/Images/" + promptPath
promptFormatted := formatPrompt(prompt)
filePath := viper.GetString("openAI_image_downloadPath")
currentUser, err := user.Current()
homeDir := currentUser.HomeDir
catchErr(err)
if filePath == `~` || strings.HasPrefix(filePath, "~") { // Replace ~ with home directory
filePath = strings.Replace(filePath, "~", homeDir, 1)
}
fileName := strconv.Itoa(imgNum) + ".jpg"

fileName := promptFormatted + strconv.Itoa(imgNum) + ".jpg"
fullFilePath := filepath.Join(filePath, fileName)
// Create the directory (if it doesn't exist)
err := os.MkdirAll(filePath, os.ModePerm)
err = os.MkdirAll(filePath, os.ModePerm)
catchErr(err)
fmt.Printf("💾 Downloading Image...")
fmt.Printf("💾 Downloading Image:")
url = httpDownloadFile(url, fullFilePath)
fmt.Printf(" \"%s\"\n", url)
}
Expand Down
Loading

0 comments on commit 5977649

Please sign in to comment.