mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-11-25 22:48:13 +00:00
feat(routes): update routes to handle new llm types
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
import searchImages from '@/lib/agents/media/image';
|
import searchImages from '@/lib/agents/media/image';
|
||||||
import ModelRegistry from '@/lib/models/registry';
|
import ModelRegistry from '@/lib/models/registry';
|
||||||
import { ModelWithProvider } from '@/lib/models/types';
|
import { ModelWithProvider } from '@/lib/models/types';
|
||||||
import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
|
|
||||||
|
|
||||||
interface ImageSearchBody {
|
interface ImageSearchBody {
|
||||||
query: string;
|
query: string;
|
||||||
@@ -20,19 +19,9 @@ export const POST = async (req: Request) => {
|
|||||||
body.chatModel.key,
|
body.chatModel.key,
|
||||||
);
|
);
|
||||||
|
|
||||||
const chatHistory = body.chatHistory
|
|
||||||
.map((msg: any) => {
|
|
||||||
if (msg.role === 'user') {
|
|
||||||
return new HumanMessage(msg.content);
|
|
||||||
} else if (msg.role === 'assistant') {
|
|
||||||
return new AIMessage(msg.content);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.filter((msg) => msg !== undefined) as BaseMessage[];
|
|
||||||
|
|
||||||
const images = await searchImages(
|
const images = await searchImages(
|
||||||
{
|
{
|
||||||
chatHistory: chatHistory,
|
chatHistory: body.chatHistory,
|
||||||
query: body.query,
|
query: body.query,
|
||||||
},
|
},
|
||||||
llm,
|
llm,
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
|
|
||||||
import { MetaSearchAgentType } from '@/lib/search/metaSearchAgent';
|
|
||||||
import { searchHandlers } from '@/lib/search';
|
|
||||||
import ModelRegistry from '@/lib/models/registry';
|
import ModelRegistry from '@/lib/models/registry';
|
||||||
import { ModelWithProvider } from '@/lib/models/types';
|
import { ModelWithProvider } from '@/lib/models/types';
|
||||||
|
import SessionManager from '@/lib/session';
|
||||||
|
import SearchAgent from '@/lib/agents/search';
|
||||||
|
import { ChatTurnMessage } from '@/lib/types';
|
||||||
|
|
||||||
interface ChatRequestBody {
|
interface ChatRequestBody {
|
||||||
optimizationMode: 'speed' | 'balanced';
|
optimizationMode: 'speed' | 'balanced';
|
||||||
@@ -40,27 +40,26 @@ export const POST = async (req: Request) => {
|
|||||||
),
|
),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const history: BaseMessage[] = body.history.map((msg) => {
|
const history: ChatTurnMessage[] = body.history.map((msg) => {
|
||||||
return msg[0] === 'human'
|
return msg[0] === 'human'
|
||||||
? new HumanMessage({ content: msg[1] })
|
? { role: 'user', content: msg[1] }
|
||||||
: new AIMessage({ content: msg[1] });
|
: { role: 'assistant', content: msg[1] };
|
||||||
});
|
});
|
||||||
|
|
||||||
const searchHandler: MetaSearchAgentType = searchHandlers[body.focusMode];
|
const session = SessionManager.createSession();
|
||||||
|
|
||||||
if (!searchHandler) {
|
const agent = new SearchAgent();
|
||||||
return Response.json({ message: 'Invalid focus mode' }, { status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const emitter = await searchHandler.searchAndAnswer(
|
agent.searchAsync(session, {
|
||||||
body.query,
|
chatHistory: history,
|
||||||
history,
|
config: {
|
||||||
llm,
|
embedding: embeddings,
|
||||||
embeddings,
|
llm: llm,
|
||||||
body.optimizationMode,
|
sources: ['web', 'discussions', 'academic'],
|
||||||
[],
|
mode: 'balanced',
|
||||||
body.systemInstructions || '',
|
},
|
||||||
);
|
followUp: body.query,
|
||||||
|
});
|
||||||
|
|
||||||
if (!body.stream) {
|
if (!body.stream) {
|
||||||
return new Promise(
|
return new Promise(
|
||||||
@@ -71,7 +70,7 @@ export const POST = async (req: Request) => {
|
|||||||
let message = '';
|
let message = '';
|
||||||
let sources: any[] = [];
|
let sources: any[] = [];
|
||||||
|
|
||||||
emitter.on('data', (data: string) => {
|
session.addListener('data', (data: string) => {
|
||||||
try {
|
try {
|
||||||
const parsedData = JSON.parse(data);
|
const parsedData = JSON.parse(data);
|
||||||
if (parsedData.type === 'response') {
|
if (parsedData.type === 'response') {
|
||||||
@@ -89,11 +88,11 @@ export const POST = async (req: Request) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
emitter.on('end', () => {
|
session.addListener('end', () => {
|
||||||
resolve(Response.json({ message, sources }, { status: 200 }));
|
resolve(Response.json({ message, sources }, { status: 200 }));
|
||||||
});
|
});
|
||||||
|
|
||||||
emitter.on('error', (error: any) => {
|
session.addListener('error', (error: any) => {
|
||||||
reject(
|
reject(
|
||||||
Response.json(
|
Response.json(
|
||||||
{ message: 'Search error', error },
|
{ message: 'Search error', error },
|
||||||
@@ -124,14 +123,14 @@ export const POST = async (req: Request) => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
signal.addEventListener('abort', () => {
|
signal.addEventListener('abort', () => {
|
||||||
emitter.removeAllListeners();
|
session.removeAllListeners();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
controller.close();
|
controller.close();
|
||||||
} catch (error) { }
|
} catch (error) {}
|
||||||
});
|
});
|
||||||
|
|
||||||
emitter.on('data', (data: string) => {
|
session.addListener('data', (data: string) => {
|
||||||
if (signal.aborted) return;
|
if (signal.aborted) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -162,7 +161,7 @@ export const POST = async (req: Request) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
emitter.on('end', () => {
|
session.addListener('end', () => {
|
||||||
if (signal.aborted) return;
|
if (signal.aborted) return;
|
||||||
|
|
||||||
controller.enqueue(
|
controller.enqueue(
|
||||||
@@ -175,7 +174,7 @@ export const POST = async (req: Request) => {
|
|||||||
controller.close();
|
controller.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
emitter.on('error', (error: any) => {
|
session.addListener('error', (error: any) => {
|
||||||
if (signal.aborted) return;
|
if (signal.aborted) return;
|
||||||
|
|
||||||
controller.error(error);
|
controller.error(error);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { DocxLoader } from '@langchain/community/document_loaders/fs/docx';
|
|||||||
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
||||||
import { Document } from '@langchain/core/documents';
|
import { Document } from '@langchain/core/documents';
|
||||||
import ModelRegistry from '@/lib/models/registry';
|
import ModelRegistry from '@/lib/models/registry';
|
||||||
|
import { Chunk } from '@/lib/types';
|
||||||
|
|
||||||
interface FileRes {
|
interface FileRes {
|
||||||
fileName: string;
|
fileName: string;
|
||||||
@@ -87,9 +88,17 @@ export async function POST(req: Request) {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
const embeddings = await model.embedDocuments(
|
const chunks: Chunk[] = splitted.map((doc) => {
|
||||||
splitted.map((doc) => doc.pageContent),
|
return {
|
||||||
|
content: doc.pageContent,
|
||||||
|
metadata: doc.metadata,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const embeddings = await model.embedChunks(
|
||||||
|
chunks
|
||||||
);
|
);
|
||||||
|
|
||||||
const embeddingsDataPath = filePath.replace(
|
const embeddingsDataPath = filePath.replace(
|
||||||
/\.\w+$/,
|
/\.\w+$/,
|
||||||
'-embeddings.json',
|
'-embeddings.json',
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import handleVideoSearch from '@/lib/agents/media/video';
|
import handleVideoSearch from '@/lib/agents/media/video';
|
||||||
import ModelRegistry from '@/lib/models/registry';
|
import ModelRegistry from '@/lib/models/registry';
|
||||||
import { ModelWithProvider } from '@/lib/models/types';
|
import { ModelWithProvider } from '@/lib/models/types';
|
||||||
import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
|
|
||||||
|
|
||||||
interface VideoSearchBody {
|
interface VideoSearchBody {
|
||||||
query: string;
|
query: string;
|
||||||
@@ -20,19 +19,9 @@ export const POST = async (req: Request) => {
|
|||||||
body.chatModel.key,
|
body.chatModel.key,
|
||||||
);
|
);
|
||||||
|
|
||||||
const chatHistory = body.chatHistory
|
|
||||||
.map((msg: any) => {
|
|
||||||
if (msg.role === 'user') {
|
|
||||||
return new HumanMessage(msg.content);
|
|
||||||
} else if (msg.role === 'assistant') {
|
|
||||||
return new AIMessage(msg.content);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.filter((msg) => msg !== undefined) as BaseMessage[];
|
|
||||||
|
|
||||||
const videos = await handleVideoSearch(
|
const videos = await handleVideoSearch(
|
||||||
{
|
{
|
||||||
chatHistory: chatHistory,
|
chatHistory: body.chatHistory,
|
||||||
query: body.query,
|
query: body.query,
|
||||||
},
|
},
|
||||||
llm,
|
llm,
|
||||||
|
|||||||
Reference in New Issue
Block a user