mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-10-19 05:48:15 +00:00
feat(uploads): use new model registry
This commit is contained in:
@@ -2,11 +2,11 @@ import { NextResponse } from 'next/server';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import { getAvailableEmbeddingModelProviders } from '@/lib/providers';
|
|
||||||
import { PDFLoader } from '@langchain/community/document_loaders/fs/pdf';
|
import { PDFLoader } from '@langchain/community/document_loaders/fs/pdf';
|
||||||
import { DocxLoader } from '@langchain/community/document_loaders/fs/docx';
|
import { DocxLoader } from '@langchain/community/document_loaders/fs/docx';
|
||||||
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
||||||
import { Document } from 'langchain/document';
|
import { Document } from 'langchain/document';
|
||||||
|
import ModelRegistry from '@/lib/models/registry';
|
||||||
|
|
||||||
interface FileRes {
|
interface FileRes {
|
||||||
fileName: string;
|
fileName: string;
|
||||||
@@ -30,8 +30,8 @@ export async function POST(req: Request) {
|
|||||||
const formData = await req.formData();
|
const formData = await req.formData();
|
||||||
|
|
||||||
const files = formData.getAll('files') as File[];
|
const files = formData.getAll('files') as File[];
|
||||||
const embedding_model = formData.get('embedding_model');
|
const embedding_model = formData.get('embedding_model_key') as string;
|
||||||
const embedding_model_provider = formData.get('embedding_model_provider');
|
const embedding_model_provider = formData.get('embedding_model_provider_id') as string;
|
||||||
|
|
||||||
if (!embedding_model || !embedding_model_provider) {
|
if (!embedding_model || !embedding_model_provider) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@@ -40,20 +40,9 @@ export async function POST(req: Request) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const embeddingModels = await getAvailableEmbeddingModelProviders();
|
const registry = new ModelRegistry();
|
||||||
const provider =
|
|
||||||
embedding_model_provider ?? Object.keys(embeddingModels)[0];
|
|
||||||
const embeddingModel =
|
|
||||||
embedding_model ?? Object.keys(embeddingModels[provider as string])[0];
|
|
||||||
|
|
||||||
let embeddingsModel =
|
const model = await registry.loadEmbeddingModel(embedding_model_provider, embedding_model);
|
||||||
embeddingModels[provider as string]?.[embeddingModel as string]?.model;
|
|
||||||
if (!embeddingsModel) {
|
|
||||||
return NextResponse.json(
|
|
||||||
{ message: 'Invalid embedding model selected' },
|
|
||||||
{ status: 400 },
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const processedFiles: FileRes[] = [];
|
const processedFiles: FileRes[] = [];
|
||||||
|
|
||||||
@@ -98,7 +87,7 @@ export async function POST(req: Request) {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const embeddings = await embeddingsModel.embedDocuments(
|
const embeddings = await model.embedDocuments(
|
||||||
splitted.map((doc) => doc.pageContent),
|
splitted.map((doc) => doc.pageContent),
|
||||||
);
|
);
|
||||||
const embeddingsDataPath = filePath.replace(
|
const embeddingsDataPath = filePath.replace(
|
||||||
|
@@ -5,7 +5,15 @@ import {
|
|||||||
PopoverPanel,
|
PopoverPanel,
|
||||||
Transition,
|
Transition,
|
||||||
} from '@headlessui/react';
|
} from '@headlessui/react';
|
||||||
import { CopyPlus, File, LoaderCircle, Plus, Trash } from 'lucide-react';
|
import {
|
||||||
|
CopyPlus,
|
||||||
|
File,
|
||||||
|
Link,
|
||||||
|
LoaderCircle,
|
||||||
|
Paperclip,
|
||||||
|
Plus,
|
||||||
|
Trash,
|
||||||
|
} from 'lucide-react';
|
||||||
import { Fragment, useRef, useState } from 'react';
|
import { Fragment, useRef, useState } from 'react';
|
||||||
import { useChat } from '@/lib/hooks/useChat';
|
import { useChat } from '@/lib/hooks/useChat';
|
||||||
|
|
||||||
@@ -24,12 +32,12 @@ const Attach = ({ showText }: { showText?: boolean }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const embeddingModelProvider = localStorage.getItem(
|
const embeddingModelProvider = localStorage.getItem(
|
||||||
'embeddingModelProvider',
|
'embeddingModelProviderId',
|
||||||
);
|
);
|
||||||
const embeddingModel = localStorage.getItem('embeddingModel');
|
const embeddingModel = localStorage.getItem('embeddingModelKey');
|
||||||
|
|
||||||
data.append('embedding_model_provider', embeddingModelProvider!);
|
data.append('embedding_model_provider_id', embeddingModelProvider!);
|
||||||
data.append('embedding_model', embeddingModel!);
|
data.append('embedding_model_key', embeddingModel!);
|
||||||
|
|
||||||
const res = await fetch(`/api/uploads`, {
|
const res = await fetch(`/api/uploads`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -169,7 +177,7 @@ const Attach = ({ showText }: { showText?: boolean }) => {
|
|||||||
multiple
|
multiple
|
||||||
hidden
|
hidden
|
||||||
/>
|
/>
|
||||||
<CopyPlus size={showText ? 18 : undefined} />
|
<Paperclip size={showText ? 18 : undefined} />
|
||||||
{showText && <p className="text-xs font-medium pl-[1px]">Attach</p>}
|
{showText && <p className="text-xs font-medium pl-[1px]">Attach</p>}
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
|
@@ -5,7 +5,7 @@ import {
|
|||||||
PopoverPanel,
|
PopoverPanel,
|
||||||
Transition,
|
Transition,
|
||||||
} from '@headlessui/react';
|
} from '@headlessui/react';
|
||||||
import { CopyPlus, File, LoaderCircle, Plus, Trash } from 'lucide-react';
|
import { CopyPlus, File, LoaderCircle, Paperclip, Plus, Trash } from 'lucide-react';
|
||||||
import { Fragment, useRef, useState } from 'react';
|
import { Fragment, useRef, useState } from 'react';
|
||||||
import { File as FileType } from '../ChatWindow';
|
import { File as FileType } from '../ChatWindow';
|
||||||
import { useChat } from '@/lib/hooks/useChat';
|
import { useChat } from '@/lib/hooks/useChat';
|
||||||
@@ -25,12 +25,12 @@ const AttachSmall = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const embeddingModelProvider = localStorage.getItem(
|
const embeddingModelProvider = localStorage.getItem(
|
||||||
'embeddingModelProvider',
|
'embeddingModelProviderId',
|
||||||
);
|
);
|
||||||
const embeddingModel = localStorage.getItem('embeddingModel');
|
const embeddingModel = localStorage.getItem('embeddingModelKey');
|
||||||
|
|
||||||
data.append('embedding_model_provider', embeddingModelProvider!);
|
data.append('embedding_model_provider_id', embeddingModelProvider!);
|
||||||
data.append('embedding_model', embeddingModel!);
|
data.append('embedding_model_key', embeddingModel!);
|
||||||
|
|
||||||
const res = await fetch(`/api/uploads`, {
|
const res = await fetch(`/api/uploads`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -141,7 +141,7 @@ const AttachSmall = () => {
|
|||||||
multiple
|
multiple
|
||||||
hidden
|
hidden
|
||||||
/>
|
/>
|
||||||
<CopyPlus size={20} />
|
<Paperclip size={20} />
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user