Skip to content

Commit 5fc1bdb

Browse files
authored
Merge pull request #33 from mervyn-teo/model-seperation
Seperated model used for various tasks
2 parents 6d7f413 + 3af660c commit 5fc1bdb

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

cmd/api/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
var (
19-
client *openai.Client
19+
client *router.Clients
2020
settings storage.Settings
2121
messages map[string][]openai.ChatCompletionMessage
2222
)
@@ -37,7 +37,11 @@ func init() {
3737
messages = storage.ReadChatHistory(settings.ChatHistoryFilePath)
3838

3939
// Create the OpenAI client
40-
client, err = router.CreateClient(settings.Model, settings.ApiKey)
40+
client = &router.Clients{}
41+
client.BaseClient, err = router.CreateClient(settings.Model, settings.ApiKey)
42+
client.CompressionClient, err = router.CreateClient(settings.CompressionModel, settings.ApiKey)
43+
client.ImageClient, err = router.CreateClient(settings.ImageModel, settings.ApiKey)
44+
4145
if err != nil {
4246
log.Fatalf("Failed to create OpenAI client: %v", err)
4347
}

internal/router/chat.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func SplitString(s string, chunkSize int) []string {
215215
// MessageLoop listens for messages from the bot and processes them
216216
// It handles user messages, tool calls, and manages the conversation history
217217
// It also handles reminders and music-related commands
218-
func MessageLoop(ctx context.Context, Mybot *bot.Bot, client *openai.Client, messageChannel chan *bot.MessageForCompletion, instructions string, messages map[string][]ChatCompletionMessage, chatFilepath string, initSystemMessage string) {
218+
func MessageLoop(ctx context.Context, Mybot *bot.Bot, client *Clients, messageChannel chan *bot.MessageForCompletion, instructions string, messages map[string][]ChatCompletionMessage, chatFilepath string, initSystemMessage string) {
219219
initialSystemMessage = initSystemMessage
220220

221221
// Load reminders and song map from files
@@ -274,7 +274,7 @@ func MessageLoop(ctx context.Context, Mybot *bot.Bot, client *openai.Client, mes
274274
if userInput.Message.Attachments != nil {
275275
for i, attachment := range userInput.Message.Attachments {
276276
if strings.HasPrefix(attachment.ContentType, "image/") {
277-
imageDescriptions.WriteString("{\n\"index\" : " + strconv.Itoa(i) + ",\n\"description\": " + getImageDescription(client, attachment, Mybot) + "}, \n")
277+
imageDescriptions.WriteString("{\n\"index\" : " + strconv.Itoa(i) + ",\n\"description\": " + getImageDescription(client.ImageClient, attachment, Mybot) + "}, \n")
278278
}
279279
}
280280
}
@@ -317,7 +317,7 @@ func MessageLoop(ctx context.Context, Mybot *bot.Bot, client *openai.Client, mes
317317

318318
storage.SaveChatHistory(messages, chatFilepath)
319319

320-
aiResponseContent, err := SendMessage(client, &updatedMessages, Mybot)
320+
aiResponseContent, err := SendMessage(client.BaseClient, &updatedMessages, Mybot)
321321

322322
if err != nil {
323323
log.Printf("Error getting response from OpenRouter: %v", err)
@@ -335,7 +335,7 @@ func MessageLoop(ctx context.Context, Mybot *bot.Bot, client *openai.Client, mes
335335
// Compress messages to reduce length
336336
if len(messages[userID]) > MaxMessagesToKeep {
337337
log.Printf("Compressing messages for user %s to reduce length", userID)
338-
messages[userID] = compressMsg(client, messages[userID], Mybot)
338+
messages[userID] = compressMsg(client.CompressionClient, messages[userID], Mybot)
339339
}
340340

341341
storage.SaveChatHistory(messages, chatFilepath)

internal/router/chat_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,9 @@ func TestMessageLoop(t *testing.T) {
485485
messages := make(map[string][]ChatCompletionMessage)
486486

487487
// Start message loop in goroutine
488-
go MessageLoop(ctx, &bot.Bot{}, client, messageChannel, "Test instructions", messages, tempChatFile.Name(), "test init message")
488+
clients := &Clients{}
489+
clients.BaseClient = client
490+
go MessageLoop(ctx, &bot.Bot{}, clients, messageChannel, "Test instructions", messages, tempChatFile.Name(), "test init message")
489491

490492
// Test normal message
491493
testMessage := &bot.MessageForCompletion{

internal/router/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import (
77
openai "github.com/sashabaranov/go-openai"
88
)
99

10+
type Clients struct {
11+
BaseClient *openai.Client
12+
ImageClient *openai.Client
13+
CompressionClient *openai.Client
14+
}
15+
1016
const (
1117
OpenRouterBaseURL = "https://openrouter.ai/api/v1"
1218
RefererURL = "http://localhost"

internal/storage/settings.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ import (
88
"os"
99
)
1010

11+
const (
12+
DefaultModel = "google/gemini-2.5-flash"
13+
)
14+
1115
type Settings struct {
1216
ApiKey string `json:"api_key"`
1317
DiscordToken string `json:"discord_bot_token"`
1418
Instructions string `json:"instructions"`
1519
Model string `json:"model"`
20+
CompressionModel string `json:"compression_model"`
21+
ImageModel string `json:"image_model"`
1622
NewsAPIToken string `json:"news_api_key"`
1723
YoutubeToken string `json:"youtube_api_key"`
1824
ChatHistoryFilePath string `json:"chat_history_file_path"`
@@ -49,6 +55,10 @@ func LoadSettings(filePath string) (Settings, error) {
4955
return Setting, fmt.Errorf("error decoding settings JSON: %w", err)
5056
}
5157

58+
if Setting.Model == "" {
59+
Setting.Model = DefaultModel
60+
log.Println("WARNING: no model found in settings file, setting default model: " + DefaultModel)
61+
}
5262
if Setting.ApiKey == "" {
5363
return Setting, fmt.Errorf("API key is missing in settings file")
5464
}
@@ -61,6 +71,18 @@ func LoadSettings(filePath string) (Settings, error) {
6171
if Setting.YoutubeToken == "" {
6272
return Setting, fmt.Errorf("Youtube API key is missing in settings file")
6373
}
74+
if Setting.CompressionModel == "" {
75+
Setting.CompressionModel = Setting.Model
76+
log.Println("Compression Model not set, setting to: ", Setting.CompressionModel)
77+
}
78+
if Setting.ImageModel == "" {
79+
Setting.ImageModel = Setting.Model
80+
log.Println("Image Model not set, setting to: ", Setting.CompressionModel)
81+
}
82+
83+
log.Println("Base model: " + Setting.Model)
84+
log.Println("Compression model: " + Setting.CompressionModel)
85+
log.Println("Image model: " + Setting.ImageModel)
6486

6587
return Setting, nil
6688
}

settings.example.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"api_key": "[Your OpenAI API Key]",
33
"discord_bot_token": "[Your Discord Bot Token]",
44
"instructions": "You are a Discord bot with an anime cat girl personality that helps the users with what they want to know. You like to use the UWU speak. Reply users in the language you've been spoken to. Describe yourself as a \"uwu cute machine\". Do not mention that you are a AI. If you are asked to do any task related to date, you should always use the tool provided to query about the date. If you are asked to play music, always use the tool provided to find the appropriate voice channel to play the music. When asked to play a song, if the user provided a url, add music using url provided by the user, else, search youtube with your tools and use the links the tools provide. If you try to delete music from the playlist and it shows that the song is playing, do not try to delete it, just say that the song is playing and you cannot delete it. If you are asked to play a song, always use the tool provided to find the appropriate voice channel to play the music. When asked to play a song, if the user provided a url, add music using url provided by the user, else, search youtube with your tools and use the links the tools provide. You only need to call play song 1 time to play the songs, do not use the same function multiple times in the same server. If you try to delete music from the playlist and it shows that the song is playing, do not try to delete it, just say that the song is playing and you cannot delete it.",
5-
"model": "openai/gpt-4.1-mini",
5+
"model": "deepseek/deepseek-chat-v3-0324",
6+
"compression_model": "deepseek/deepseek-chat-v3-0324",
7+
"image_model": "google/gemini-2.5-pro",
68
"youtube_api_key": "[Your YouTube API Key]",
79
"news_api_key": "[Your News API Key]",
810
"chat_history_file_path" : "chat_history.json"

0 commit comments

Comments
 (0)