From 732e8856b788b27b00afcab414a66ff13e0cde49 Mon Sep 17 00:00:00 2001 From: Kristian _server Date: Fri, 6 Oct 2023 17:17:26 +0200 Subject: [PATCH] pocketbase integration for persisting data --- bot/bot.go | 2 +- bot/chat.go | 14 +++++++------- pb/pb.go | 39 ++++++++++++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index d16ddb9..89731a8 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -19,6 +19,7 @@ var ( ) func Run() { + pb.Init() discord, err := discordgo.New("Bot " + BotToken) if err != nil { log.Fatal(err) @@ -28,7 +29,6 @@ func Run() { discord.Open() defer discord.Close() - pb.Run() log.Info("BitBot is running...") c := make(chan os.Signal, 1) diff --git a/bot/chat.go b/bot/chat.go index a909927..b041ba5 100644 --- a/bot/chat.go +++ b/bot/chat.go @@ -10,10 +10,10 @@ import ( ) const ( - maxTokens = 2000 - maxContextTokens = 16000 + maxTokens = 4000 + maxContextTokens = 2000 maxMessageTokens = 2000 - systemMessageText = "0. your name is bit you are a discord bot 1. Identify the key points or main ideas of the original answers.\n2. Summarize each answer using concise and informative language.\n3. Prioritize clarity and brevity, capturing the essence of the information provided.\n4. Trim down unnecessary details and avoid elaboration.\n5. Make sure the summarized answers still convey accurate and meaningful information." + systemMessageText = "0. your name is !bit you are a discord bot 1. Identify the key points or main ideas of the original answers.\n2. Summarize each answer using concise and informative language.\n3. Prioritize clarity and brevity, capturing the essence of the information provided.\n4. Trim down unnecessary details and avoid elaboration.\n5. Make sure the summarized answers still convey accurate and meaningful information." ) func populateConversationHistory(session *discordgo.Session, channelID string, conversationHistory []openai.ChatCompletionMessage) []openai.ChatCompletionMessage { @@ -94,19 +94,19 @@ func chatGPT(session *discordgo.Session, channelID string, message string, conve } messages = append(messages, userMessage) - // Perform GPT-3.5 Turbo completion - log.Info("Starting GPT-3.5 Turbo completion...") + // Perform GPT-4 completion + log.Info("Starting completion...") resp, err := client.CreateChatCompletion( context.Background(), openai.ChatCompletionRequest{ MaxTokens: maxTokens, FrequencyPenalty: 0.3, PresencePenalty: 0.6, - Model: openai.GPT3Dot5Turbo16K, + Model: openai.GPT3Dot5Turbo, Messages: messages, }, ) - log.Info("GPT-3.5 Turbo completion done.") + log.Info("completion done.") // Handle API errors if err != nil { diff --git a/pb/pb.go b/pb/pb.go index aec276d..1066a5f 100644 --- a/pb/pb.go +++ b/pb/pb.go @@ -1,17 +1,42 @@ +// pb.go package pb import ( + "sync" + "github.com/charmbracelet/log" "github.com/pocketbase/pocketbase" + "github.com/pocketbase/pocketbase/models" ) -func Run() { - app := pocketbase.New() +var ( + app *pocketbase.PocketBase + once sync.Once +) - func() { - if err := app.Start(); err != nil { - log.Fatal(err) - } - }() +// Init initializes the PocketBase app +func Init() { + once.Do(func() { + app = pocketbase.New() + // Add any necessary configuration or initialization here + + func() { + if err := app.Start(); err != nil { + log.Fatal(err) + } + }() + }) +} + +func GetRecordById(collectionID string, recordID string) (*models.Record, error) { + // Access the PocketBase DAO and perform database operations + // Example: Retrieve all records from the "articles" collection + record, err := app.Dao().FindRecordById(collectionID, recordID) + if err != nil { + log.Error(err) + return nil, err + } + + return record, nil }