Skip to content

Commit

Permalink
feat: Add support for JSON file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
n4ze3m committed Sep 5, 2024
1 parent 1c3003e commit 6bc983b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/ui/src/components/Common/BotForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const BotForm = ({
}}
>
<Upload.Dragger
accept={`.pdf,.docx,.csv,.txt,.mp3,.mp4,.zip`}
accept={`.pdf,.docx,.csv,.txt,.mp3,.mp4,.zip,.json`}
multiple={true}
maxCount={botConfig?.fileUploadSizeLimit}
beforeUpload={(file) => {
Expand All @@ -132,6 +132,7 @@ export const BotForm = ({
"video/mpeg",
"application/zip",
"application/x-zip-compressed",
"application/json",
]
.map((type) => type.toLowerCase())
.join(", ");
Expand Down Expand Up @@ -163,8 +164,8 @@ export const BotForm = ({
<InboxIcon className="h-10 w-10 text-gray-400" />
</p>
<p className="ant-upload-text">
Click or drag PDF, Docx, CSV , TXT, MP3, MP4, Zip files to
this
Click or drag PDF, Docx, CSV, TXT, MP3, MP4, Zip, or JSON
files to this area
</p>
<p className="ant-upload-hint">
{`Support is available for a single or bulk upload of up to ${botConfig?.fileUploadSizeLimit}
Expand Down
55 changes: 55 additions & 0 deletions server/src/queue/controllers/json.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { QSource } from "../type";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { DialoqbaseVectorStore } from "../../utils/store";
import { embeddings } from "../../utils/embeddings";
import { PrismaClient } from "@prisma/client";
import { getModelInfo } from "../../utils/get-model-info";
import * as fs from "fs/promises"

export const jsonQueueController = async (
source: QSource,
prisma: PrismaClient
) => {
console.log("loading json");

const location = source.location!;

const json = await fs.readFile(location, "utf-8");

const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: source.chunkSize,
chunkOverlap: source.chunkOverlap,
});

const chunks = await textSplitter.splitDocuments([
{
pageContent: json,
metadata: {
source: location,
},
},
]);

const embeddingInfo = await getModelInfo({
model: source.embedding,
prisma,
type: "embedding",
});

if (!embeddingInfo) {
throw new Error("Embedding not found. Please verify the embedding id");
}

await DialoqbaseVectorStore.fromDocuments(
chunks,
embeddings(
embeddingInfo.model_provider!.toLowerCase(),
embeddingInfo.model_id,
embeddingInfo?.config
),
{
botId: source.botId,
sourceId: source.id,
}
);
};
2 changes: 2 additions & 0 deletions server/src/queue/controllers/zip.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function getMimeType(filename: string): string {
return "audio/mpeg";
case ".zip":
return "application/zip";
case ".json":
return "application/json";
default:
return "none";
}
Expand Down
4 changes: 4 additions & 0 deletions server/src/queue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { sitemapQueueController } from "./controllers/sitemap.controller";
import { SandboxedJob } from "bullmq";
import { getRagSettings } from "../utils/rag-settings";
import { zipQueueController } from "./controllers/zip.controller";
import { jsonQueueController } from "./controllers/json.controller";

const prisma = new PrismaClient();

Expand Down Expand Up @@ -84,6 +85,9 @@ export default async function queueHandler(job: SandboxedJob) {
case "zip":
await zipQueueController(source, prisma);
break;
case "json":
await jsonQueueController(source, prisma);
break;
default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions server/src/utils/fileType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export const fileTypeFinder = (mimeType: string) => {
return "zip";
case "application/x-zip-compressed":
return "zip";
case "application/json":
return "json";
default:
return "none";
}
Expand Down

0 comments on commit 6bc983b

Please sign in to comment.