Skip to content

Commit

Permalink
feat(chat): better file attachement handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Dec 26, 2024
1 parent d88dad4 commit 53ef70e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
26 changes: 25 additions & 1 deletion apps/backend/src/modules/messages/messages.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { taskEither as TE } from 'fp-ts';
import { pipe } from 'fp-ts/lib/function';
import { delay, inject, injectable } from 'tsyringe';

import { findItemIndexById, mapAsyncIterator, tryOrThrowTE } from '@llm/commons';
import { findItemIndexById, mapAsyncIterator, pluckTyped, tryOrThrowTE } from '@llm/commons';
import {
groupSdkAIMessagesByRepeats,
type SdkCreateMessageInputT,
Expand Down Expand Up @@ -85,7 +85,31 @@ export class MessagesService implements WithAuthFirewall<MessagesFirewall> {
messageId: id,
...file,
})),
TE.chainW(files => this.projectsFilesService.search({
sort: 'createdAt:desc',
limit: files.length,
projectId: project.id,
offset: 0,
ids: pipe([...files], pluckTyped('id')),
})),
)),
TE.chainW(attachedFiles => this.repo.create({
value: {
chatId: chat.id,
metadata: {},
aiModelId: null,
creatorUserId: creator.id,
role: 'system',
content: [
'User attached to chat these files:',
...attachedFiles.items.map(({ description, resource }) =>
`- ${resource.name} - ${description}`,
),
'---',
'If there is application attached to this chat, process these files with it.',
].join('\n'),
},
})),
);
}),
TE.tap(({ id }) => this.esIndexRepo.findAndIndexDocumentById(id)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type { EsMatchingProjectEmbedding } from '../elasticsearch';

import { groupEmbeddingsByFile } from './group-embeddings';

export function createRelevantEmbeddingsPrompt(message: string, embeddings: EsMatchingProjectEmbedding[]): string {
export function createRelevantEmbeddingsPrompt(
message: string,
embeddings: EsMatchingProjectEmbedding[],
): string {
const groupedEmbeddings = groupEmbeddingsByFile(embeddings);

const fragmentsText = Object
Expand Down Expand Up @@ -32,10 +35,15 @@ export function createRelevantEmbeddingsPrompt(message: string, embeddings: EsMa
'',
'Core Instructions:',
'1. Respond in the same language as the user\'s message',
'2. Consider and analyze ALL provided file fragments',
'3. Search through ALL file types, including source code, documents, images (.png, .jpg, etc), presentations, and any other files',
'4. MANDATORY: Never mention filenames directly in text - always use #embedding:<id> instead',
'5. MANDATORY: Always consider image files in your search - they may contain crucial diagrams, screenshots or visual documentation',
'2. If message contains #app mention: MAINTAIN THE APP\'S PERSONALITY AND TONE throughout the response',
'3. If responding as an app: DO NOT describe files, instead keep acting as the app would',
'4. Consider and analyze ALL provided file fragments',
'5. Search through ALL file types, including source code, documents, images (.png, .jpg, etc), presentations, and any other files',
'6. MANDATORY: Never mention filenames directly in text - always use #embedding:<id> instead',
'7. MANDATORY: Always consider image files in your search - they may contain crucial diagrams, screenshots or visual documentation',
'8. MANDATORY: If files were attached to the chat or previous messages - treat them with highest priority',
'9. MANDATORY: Always analyze attached files first before other context',
'10. MANDATORY: For attached files, provide more detailed analysis unless user specifies otherwise',
'',
'Text Format Rules:',
'- ❌ WRONG: "In config.ts we see..."',
Expand Down Expand Up @@ -66,6 +74,9 @@ export function createRelevantEmbeddingsPrompt(message: string, embeddings: EsMa
'- For file queries: Provide file name and one-sentence description unless more details requested',
'- If relevant file found: Suggest its potential usefulness',
'- If context not relevant: Provide general response',
'- If user uploads a file without asking about it: DO NOT comment on or describe the file',
'- If files were attached: Give them priority in analysis and responses',
'- For attached files: Provide more detailed insights unless explicitly asked not to',
'- When mentioning files, add RELEVANT action buttons based on context:',
' CRITICAL: Button labels and actions:',
' - MUST be in user\'s language',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function ChatInputToolbar(

<FilesCardsControlledList
{...bind.path('files')}
className="mt-3 px-3"
className="mt-3 mb-2 px-3"
/>

<ChatSelectApp
Expand Down

0 comments on commit 53ef70e

Please sign in to comment.