Compare commits

...

10 Commits

11 changed files with 164 additions and 36 deletions

View File

@ -32,7 +32,8 @@ The API accepts a JSON object in the request body, where you define the focus mo
"history": [ "history": [
["human", "Hi, how are you?"], ["human", "Hi, how are you?"],
["assistant", "I am doing well, how can I help you today?"] ["assistant", "I am doing well, how can I help you today?"]
] ],
"stream": false
} }
``` ```
@ -71,11 +72,13 @@ The API accepts a JSON object in the request body, where you define the focus mo
] ]
``` ```
- **`stream`** (boolean, optional): When set to `true`, enables streaming responses. Default is `false`.
### Response ### Response
The response from the API includes both the final message and the sources used to generate that message. The response from the API includes both the final message and the sources used to generate that message.
#### Example Response #### Standard Response (stream: false)
```json ```json
{ {
@ -100,6 +103,28 @@ The response from the API includes both the final message and the sources used t
} }
``` ```
#### Streaming Response (stream: true)
When streaming is enabled, the API returns a stream of newline-delimited JSON objects. Each line contains a complete, valid JSON object. The response has Content-Type: application/json.
Example of streamed response objects:
```
{"type":"init","data":"Stream connected"}
{"type":"sources","data":[{"pageContent":"...","metadata":{"title":"...","url":"..."}},...]}
{"type":"response","data":"Perplexica is an "}
{"type":"response","data":"innovative, open-source "}
{"type":"response","data":"AI-powered search engine..."}
{"type":"done"}
```
Clients should process each line as a separate JSON object. The different message types include:
- **`init`**: Initial connection message
- **`sources`**: All sources used for the response
- **`response`**: Chunks of the generated answer text
- **`done`**: Indicates the stream is complete
### Fields in the Response ### Fields in the Response
- **`message`** (string): The search result, generated based on the query and focus mode. - **`message`** (string): The search result, generated based on the query and focus mode.

View File

@ -1,6 +1,6 @@
{ {
"name": "perplexica-frontend", "name": "perplexica-frontend",
"version": "1.10.0", "version": "1.10.1",
"license": "MIT", "license": "MIT",
"author": "ItzCrazyKns", "author": "ItzCrazyKns",
"scripts": { "scripts": {

View File

@ -295,9 +295,9 @@ export const POST = async (req: Request) => {
}, },
}); });
} catch (err) { } catch (err) {
console.error('An error ocurred while processing chat request:', err); console.error('An error occurred while processing chat request:', err);
return Response.json( return Response.json(
{ message: 'An error ocurred while processing chat request' }, { message: 'An error occurred while processing chat request' },
{ status: 500 }, { status: 500 },
); );
} }

View File

@ -59,9 +59,9 @@ export const GET = async (req: Request) => {
return Response.json({ ...config }, { status: 200 }); return Response.json({ ...config }, { status: 200 });
} catch (err) { } catch (err) {
console.error('An error ocurred while getting config:', err); console.error('An error occurred while getting config:', err);
return Response.json( return Response.json(
{ message: 'An error ocurred while getting config' }, { message: 'An error occurred while getting config' },
{ status: 500 }, { status: 500 },
); );
} }
@ -100,9 +100,9 @@ export const POST = async (req: Request) => {
return Response.json({ message: 'Config updated' }, { status: 200 }); return Response.json({ message: 'Config updated' }, { status: 200 });
} catch (err) { } catch (err) {
console.error('An error ocurred while updating config:', err); console.error('An error occurred while updating config:', err);
return Response.json( return Response.json(
{ message: 'An error ocurred while updating config' }, { message: 'An error occurred while updating config' },
{ status: 500 }, { status: 500 },
); );
} }

View File

@ -48,7 +48,7 @@ export const GET = async (req: Request) => {
}, },
); );
} catch (err) { } catch (err) {
console.error(`An error ocurred in discover route: ${err}`); console.error(`An error occurred in discover route: ${err}`);
return Response.json( return Response.json(
{ {
message: 'An error has occurred', message: 'An error has occurred',

View File

@ -74,9 +74,9 @@ export const POST = async (req: Request) => {
return Response.json({ images }, { status: 200 }); return Response.json({ images }, { status: 200 });
} catch (err) { } catch (err) {
console.error(`An error ocurred while searching images: ${err}`); console.error(`An error occurred while searching images: ${err}`);
return Response.json( return Response.json(
{ message: 'An error ocurred while searching images' }, { message: 'An error occurred while searching images' },
{ status: 500 }, { status: 500 },
); );
} }

View File

@ -34,7 +34,7 @@ export const GET = async (req: Request) => {
}, },
); );
} catch (err) { } catch (err) {
console.error('An error ocurred while fetching models', err); console.error('An error occurred while fetching models', err);
return Response.json( return Response.json(
{ {
message: 'An error has occurred.', message: 'An error has occurred.',

View File

@ -33,6 +33,7 @@ interface ChatRequestBody {
embeddingModel?: embeddingModel; embeddingModel?: embeddingModel;
query: string; query: string;
history: Array<[string, string]>; history: Array<[string, string]>;
stream?: boolean;
} }
export const POST = async (req: Request) => { export const POST = async (req: Request) => {
@ -48,6 +49,7 @@ export const POST = async (req: Request) => {
body.history = body.history || []; body.history = body.history || [];
body.optimizationMode = body.optimizationMode || 'balanced'; body.optimizationMode = body.optimizationMode || 'balanced';
body.stream = body.stream || false;
const history: BaseMessage[] = body.history.map((msg) => { const history: BaseMessage[] = body.history.map((msg) => {
return msg[0] === 'human' return msg[0] === 'human'
@ -125,6 +127,7 @@ export const POST = async (req: Request) => {
[], [],
); );
if (!body.stream) {
return new Promise( return new Promise(
( (
resolve: (value: Response) => void, resolve: (value: Response) => void,
@ -133,7 +136,7 @@ export const POST = async (req: Request) => {
let message = ''; let message = '';
let sources: any[] = []; let sources: any[] = [];
emitter.on('data', (data) => { emitter.on('data', (data: string) => {
try { try {
const parsedData = JSON.parse(data); const parsedData = JSON.parse(data);
if (parsedData.type === 'response') { if (parsedData.type === 'response') {
@ -143,7 +146,10 @@ export const POST = async (req: Request) => {
} }
} catch (error) { } catch (error) {
reject( reject(
Response.json({ message: 'Error parsing data' }, { status: 500 }), Response.json(
{ message: 'Error parsing data' },
{ status: 500 },
),
); );
} }
}); });
@ -152,13 +158,106 @@ export const POST = async (req: Request) => {
resolve(Response.json({ message, sources }, { status: 200 })); resolve(Response.json({ message, sources }, { status: 200 }));
}); });
emitter.on('error', (error) => { emitter.on('error', (error: any) => {
reject( reject(
Response.json({ message: 'Search error', error }, { status: 500 }), Response.json(
{ message: 'Search error', error },
{ status: 500 },
),
); );
}); });
}, },
); );
}
const encoder = new TextEncoder();
const abortController = new AbortController();
const { signal } = abortController;
const stream = new ReadableStream({
start(controller) {
let sources: any[] = [];
controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'init',
data: 'Stream connected',
}) + '\n',
),
);
signal.addEventListener('abort', () => {
emitter.removeAllListeners();
try {
controller.close();
} catch (error) {}
});
emitter.on('data', (data: string) => {
if (signal.aborted) return;
try {
const parsedData = JSON.parse(data);
if (parsedData.type === 'response') {
controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'response',
data: parsedData.data,
}) + '\n',
),
);
} else if (parsedData.type === 'sources') {
sources = parsedData.data;
controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'sources',
data: sources,
}) + '\n',
),
);
}
} catch (error) {
controller.error(error);
}
});
emitter.on('end', () => {
if (signal.aborted) return;
controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'done',
}) + '\n',
),
);
controller.close();
});
emitter.on('error', (error: any) => {
if (signal.aborted) return;
controller.error(error);
});
},
cancel() {
abortController.abort();
},
});
return new Response(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-transform',
Connection: 'keep-alive',
},
});
} catch (err: any) { } catch (err: any) {
console.error(`Error in getting search results: ${err.message}`); console.error(`Error in getting search results: ${err.message}`);
return Response.json( return Response.json(

View File

@ -72,9 +72,9 @@ export const POST = async (req: Request) => {
return Response.json({ suggestions }, { status: 200 }); return Response.json({ suggestions }, { status: 200 });
} catch (err) { } catch (err) {
console.error(`An error ocurred while generating suggestions: ${err}`); console.error(`An error occurred while generating suggestions: ${err}`);
return Response.json( return Response.json(
{ message: 'An error ocurred while generating suggestions' }, { message: 'An error occurred while generating suggestions' },
{ status: 500 }, { status: 500 },
); );
} }

View File

@ -74,9 +74,9 @@ export const POST = async (req: Request) => {
return Response.json({ videos }, { status: 200 }); return Response.json({ videos }, { status: 200 });
} catch (err) { } catch (err) {
console.error(`An error ocurred while searching videos: ${err}`); console.error(`An error occurred while searching videos: ${err}`);
return Response.json( return Response.json(
{ message: 'An error ocurred while searching videos' }, { message: 'An error occurred while searching videos' },
{ status: 500 }, { status: 500 },
); );
} }

View File

@ -8,6 +8,10 @@ import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { Embeddings } from '@langchain/core/embeddings'; import { Embeddings } from '@langchain/core/embeddings';
const geminiChatModels: Record<string, string>[] = [ const geminiChatModels: Record<string, string>[] = [
{
displayName: 'Gemini 2.5 Pro Experimental',
key: 'gemini-2.5-pro-exp-03-25',
},
{ {
displayName: 'Gemini 2.0 Flash', displayName: 'Gemini 2.0 Flash',
key: 'gemini-2.0-flash', key: 'gemini-2.0-flash',
@ -17,8 +21,8 @@ const geminiChatModels: Record<string, string>[] = [
key: 'gemini-2.0-flash-lite', key: 'gemini-2.0-flash-lite',
}, },
{ {
displayName: 'Gemini 2.0 Pro Experimental', displayName: 'Gemini 2.0 Flash Thinking Experimental',
key: 'gemini-2.0-pro-exp-02-05', key: 'gemini-2.0-flash-thinking-exp-01-21',
}, },
{ {
displayName: 'Gemini 1.5 Flash', displayName: 'Gemini 1.5 Flash',