Skip to content

Commit

Permalink
Merge pull request #34 from freemocap/jon/clean-up
Browse files Browse the repository at this point in the history
Jon/clean up
  • Loading branch information
jonmatthis authored May 16, 2024
2 parents 279c801 + 1ed52a6 commit 66bf599
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ lerna-debug.log*

.env.slack
.env.discord
.env.analysis
/.env.openai
/src/interfaces/discord/commands/server-config-command/server-config-examples/capstone/2024-capstone-category-per-student-config.yaml
/src/interfaces/discord/commands/server-config-command/server-config-examples/capstone/2024-capstone-category-per-student-config-CHANGE_NICKNAMES.yaml
/src/interfaces/discord/commands/server-config-command/server-config-examples/capstone/student-identifiers.ts

venv/
.venv/
2 changes: 2 additions & 0 deletions src/core/ai/openai/openai-chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface OpenAiChatConfig {
//https://platform.openai.com/docs/api-reference/chat
messages: any[];
model:
| 'gpt-4o'
| 'gpt-4-1106-preview'
| 'gpt-4'
| 'gpt-4-vision-preview'
Expand Down Expand Up @@ -136,6 +137,7 @@ export class OpenaiChatService implements OnModuleInit {
max_tokens: 4096,
} as OpenAiChatConfig;
}

private _reloadMessageHistoryFromAiChatDocument(aiChat: AiChatDocument) {
const chatConfig = this._getConfigOrThrow(aiChat.aiChatId);

Expand Down
21 changes: 17 additions & 4 deletions src/interfaces/discord/services/discord-context-prompt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TextChannel,
ThreadChannel,
} from 'discord.js';
import { DiscordMessageService } from './discord-message.service';

@Injectable()
export class DiscordContextPromptService {
Expand All @@ -19,6 +20,8 @@ export class DiscordContextPromptService {
instructionsChannelPattern = new RegExp('.*?prompt-?settings.*', 'i');
botPromptEmoji = '🤖';

constructor(private readonly _messageService: DiscordMessageService) {}

async getContextPromptFromMessage(message: Message) {
try {
if (message.channel instanceof DMChannel) {
Expand All @@ -33,13 +36,17 @@ export class DiscordContextPromptService {
const server = await channel.client.guilds.fetch(channel.guildId);
const channelTopic =
`CHANNEL ${channel.name} TOPIC:\n\n${channel.topic}` || '';

const channelPinnedInstructions =
await this._getPinnedInstructions(channel);

const categoryInstructions = await this.getCategoryInstructions(
server,
channel.parent as CategoryChannel,
);

const serverInstructions = await this.getServerInstructions(server);

return [
serverInstructions,
categoryInstructions,
Expand Down Expand Up @@ -180,7 +187,9 @@ export class DiscordContextPromptService {
);

return instructionMessages
.map((message: Message) => message.content)
.map((message: Message) =>
this._messageService.extractMessageContentAsString(message),
)
.join('\n');
}

Expand Down Expand Up @@ -208,10 +217,14 @@ export class DiscordContextPromptService {

let pinnedMessageCount = 0;
for (const message of pinnedMessages.values()) {
pinnedMessagesContent += `Pinned message ${pinnedMessageCount++}:\n`;
pinnedMessagesContent += message.content + '\n';
pinnedMessagesContent += `BEGIN PINNED MESSAGE ${pinnedMessageCount++}:\n\n`;
const content =
await this._messageService.extractMessageContentAsString(message);
pinnedMessagesContent += content;
pinnedMessagesContent += `END PINNED MESSAGE ${pinnedMessageCount++}:\n\n`;

pinnedMessagesContent += '\nEND PINNED MESSAGES';
}
pinnedMessagesContent += '\nEND PINNED MESSAGES';
return pinnedMessagesContent;
}
}
17 changes: 17 additions & 0 deletions src/interfaces/discord/services/discord-message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { OpenaiChatService } from '../../../core/ai/openai/openai-chat.service';
export class DiscordMessageService {
private readonly maxMessageLength = 2000 * 0.85; // discord max message length is 2000 characters (and * 0.85 to be safe)
private readonly logger = new Logger(DiscordMessageService.name);

constructor(
private readonly _persistenceService: DiscordPersistenceService,
private readonly _contextService: DiscordContextRouteService,
Expand Down Expand Up @@ -191,6 +192,21 @@ export class DiscordMessageService {
}
}

public async extractMessageContentAsString(discordMessage: Message<boolean>) {
const { humanInputText, attachmentText, imageURLs } =
await this.extractMessageContent(discordMessage);
let fullText = `BEGIN MESSAGE CONTENT:\n\n ${humanInputText}\n\n END MESSAGE CONTENT\n\n`;
if (attachmentText) {
fullText += attachmentText;
}
if (imageURLs.length > 0) {
fullText += '\n\nBEGIN IMAGE URLS:\n\n';
fullText += imageURLs.join('\n');
fullText += '\n\nEND IMAGE URLS\n\n';
}
return fullText;
}

public async extractMessageContent(
discordMessage: Message<boolean>,
respondToChannelOrMessage?: Message<boolean> | TextBasedChannel,
Expand Down Expand Up @@ -246,6 +262,7 @@ export class DiscordMessageService {
attachmentText += 'END TEXT FROM ATTACHMENTS';
}
}

return { humanInputText, attachmentText, imageURLs };
}

Expand Down
10 changes: 3 additions & 7 deletions src/interfaces/discord/services/discord-on-message.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@ export class DiscordOnMessageService {

const chatConfig = {
messages: [],
model: 'gpt-4-vision-preview',
model: 'gpt-4o',
temperature: 0.7,
stream: true,
max_tokens: 4096,
} as OpenAiChatConfig;

this._openaiChatService.createChat(aiChatId, contextPrompt, chatConfig);
const aiChatDocument = await this._aiChatsService.createAiChat({
aiChatId,
ownerUser,
contextRoute,
contextInstructions: contextPrompt,
couplets: [],
modelName: 'gpt-4-vision-preview',
modelName: 'gpt-4o',
});

this.logger.debug(`Adding threadId ${aiChatId} to active listeners`);
Expand All @@ -78,11 +79,6 @@ export class DiscordOnMessageService {
}
const botId = message.client.user.id;

// Respond to messages that mention the bot
if (message.mentions.has(botId)) {
return true;
}

// Respond to messages in threads the bot created/owns
if (
message.channel instanceof ThreadChannel &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TextChannel,
} from 'discord.js';
import { AiChatDocument } from '../../../core/database/collections/ai-chats/ai-chat.schema';
import { CoupletDocument } from '../../../core/database/collections/couplets/couplet.schema';

@Injectable()
export class DiscordPersistenceService {
Expand Down Expand Up @@ -73,20 +74,22 @@ export class DiscordPersistenceService {
});

await this._aiChatsService.addCouplets(aiChatId, [couplet]);
await this.attachAiChatToOldestMessage(
await this._updateInChatPersistence(
aiChatId,
await this._aiChatsService.findOne(aiChatId),
discordMessage.channel as TextChannel,
couplet,
);
} catch (error) {
this.logger.error(`Error persisting interaction: ${error}`);
}
}

public async attachAiChatToOldestMessage(
private async _updateInChatPersistence(
aiChatId: string,
aiChatDocument: AiChatDocument,
channel: TextChannel,
couplet: CoupletDocument,
) {
this.logger.debug(
`Attaching chat document to oldest message in thread ${aiChatId}`,
Expand Down

0 comments on commit 66bf599

Please sign in to comment.