aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ChatBox/tools
diff options
context:
space:
mode:
authorA.J. Shulman <Shulman.aj@gmail.com>2024-07-16 15:50:39 -0400
committerA.J. Shulman <Shulman.aj@gmail.com>2024-07-16 15:50:39 -0400
commit74666884d0680745146f4e4ca24573637ee0a391 (patch)
treee81146c93cd5263d6cd0c683ca9aba93e35e9dbb /src/client/views/nodes/ChatBox/tools
parent65179e8b0519aa4ccf28afc4c429262ecf7a62f3 (diff)
working much better still working on adding images thouhg
Diffstat (limited to 'src/client/views/nodes/ChatBox/tools')
-rw-r--r--src/client/views/nodes/ChatBox/tools/RAGTool.ts43
1 files changed, 12 insertions, 31 deletions
diff --git a/src/client/views/nodes/ChatBox/tools/RAGTool.ts b/src/client/views/nodes/ChatBox/tools/RAGTool.ts
index 90f7bebfe..0a4529974 100644
--- a/src/client/views/nodes/ChatBox/tools/RAGTool.ts
+++ b/src/client/views/nodes/ChatBox/tools/RAGTool.ts
@@ -2,6 +2,7 @@ import { BaseTool } from './BaseTool';
import { Vectorstore } from '../vectorstore/VectorstoreUpload';
import { Chunk } from '../types';
import * as fs from 'fs';
+import { Networking } from '../../../../Network';
export class RAGTool extends BaseTool<{ hypothetical_document_chunk: string }> {
constructor(
@@ -52,42 +53,22 @@ export class RAGTool extends BaseTool<{ hypothetical_document_chunk: string }> {
async execute(args: { hypothetical_document_chunk: string }): Promise<any> {
const relevantChunks = await this.vectorstore.retrieve(args.hypothetical_document_chunk);
- return this.getFormattedChunks(relevantChunks);
+ const formatted_chunks = await this.getFormattedChunks(relevantChunks);
+ return formatted_chunks;
}
- private getFormattedChunks(relevantChunks: Chunk[]): { type: string; text?: string; image_url?: { url: string } }[] {
- const content: { type: string; text?: string; image_url?: { url: string } }[] = [{ type: 'text', text: '<chunks>' }];
+ async getFormattedChunks(relevantChunks: Chunk[]): Promise<{ type: string; text?: string; image_url?: { url: string } }[]> {
+ try {
+ const { formattedChunks } = await Networking.PostToServer('/formatChunks', { relevantChunks });
- for (const chunk of relevantChunks) {
- content.push({
- type: 'text',
- text: `<chunk chunk_id=${chunk.id} chunk_type=${chunk.metadata.type === 'image' || chunk.metadata.type === 'table' ? 'image' : 'text'}>`,
- });
-
- if (chunk.metadata.type === 'image' || chunk.metadata.type === 'table') {
- try {
- const imageBuffer = fs.readFileSync(chunk.metadata.file_path);
- const base64Image = imageBuffer.toString('base64');
- if (base64Image) {
- content.push({
- type: 'image_url',
- image_url: {
- url: `data:image/jpeg;base64,${base64Image}`,
- },
- });
- } else {
- console.log(`Failed to encode image for chunk ${chunk.id}`);
- }
- } catch (error) {
- console.error(`Error reading image file for chunk ${chunk.id}:`, error);
- }
+ if (!formattedChunks) {
+ throw new Error('Failed to format chunks');
}
- content.push({ type: 'text', text: `${chunk.metadata.text}\n</chunk>\n` });
+ return formattedChunks;
+ } catch (error) {
+ console.error('Error formatting chunks:', error);
+ throw error;
}
-
- content.push({ type: 'text', text: '</chunks>' });
-
- return content;
}
}