mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-06-19 08:18:48 +00:00
Compare commits
3 Commits
191d1dc25f
...
5d60ab1139
Author | SHA1 | Date | |
---|---|---|---|
5d60ab1139 | |||
9095996356 | |||
310c8a75fd |
@ -32,7 +32,8 @@ The API accepts a JSON object in the request body, where you define the focus mo
|
||||
"history": [
|
||||
["human", "Hi, how are you?"],
|
||||
["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
|
||||
|
||||
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
|
||||
{
|
||||
@ -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
|
||||
|
||||
- **`message`** (string): The search result, generated based on the query and focus mode.
|
||||
|
@ -295,9 +295,9 @@ export const POST = async (req: Request) => {
|
||||
},
|
||||
});
|
||||
} 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(
|
||||
{ message: 'An error ocurred while processing chat request' },
|
||||
{ message: 'An error occurred while processing chat request' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
@ -59,9 +59,9 @@ export const GET = async (req: Request) => {
|
||||
|
||||
return Response.json({ ...config }, { status: 200 });
|
||||
} catch (err) {
|
||||
console.error('An error ocurred while getting config:', err);
|
||||
console.error('An error occurred while getting config:', err);
|
||||
return Response.json(
|
||||
{ message: 'An error ocurred while getting config' },
|
||||
{ message: 'An error occurred while getting config' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
@ -100,9 +100,9 @@ export const POST = async (req: Request) => {
|
||||
|
||||
return Response.json({ message: 'Config updated' }, { status: 200 });
|
||||
} catch (err) {
|
||||
console.error('An error ocurred while updating config:', err);
|
||||
console.error('An error occurred while updating config:', err);
|
||||
return Response.json(
|
||||
{ message: 'An error ocurred while updating config' },
|
||||
{ message: 'An error occurred while updating config' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ export const GET = async (req: Request) => {
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(`An error ocurred in discover route: ${err}`);
|
||||
console.error(`An error occurred in discover route: ${err}`);
|
||||
return Response.json(
|
||||
{
|
||||
message: 'An error has occurred',
|
||||
|
@ -74,9 +74,9 @@ export const POST = async (req: Request) => {
|
||||
|
||||
return Response.json({ images }, { status: 200 });
|
||||
} catch (err) {
|
||||
console.error(`An error ocurred while searching images: ${err}`);
|
||||
console.error(`An error occurred while searching images: ${err}`);
|
||||
return Response.json(
|
||||
{ message: 'An error ocurred while searching images' },
|
||||
{ message: 'An error occurred while searching images' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ export const GET = async (req: Request) => {
|
||||
},
|
||||
);
|
||||
} catch (err) {
|
||||
console.error('An error ocurred while fetching models', err);
|
||||
console.error('An error occurred while fetching models', err);
|
||||
return Response.json(
|
||||
{
|
||||
message: 'An error has occurred.',
|
||||
|
@ -166,6 +166,7 @@ export const POST = async (req: Request) => {
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
// Create an AbortController to handle cancellation
|
||||
const abortController = new AbortController();
|
||||
const { signal } = abortController;
|
||||
|
||||
@ -173,37 +174,43 @@ export const POST = async (req: Request) => {
|
||||
start(controller) {
|
||||
let sources: any[] = [];
|
||||
|
||||
controller.enqueue(encoder.encode("data: " + JSON.stringify({
|
||||
// Send an initial message to keep the connection alive
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
type: 'init',
|
||||
data: 'Stream connected'
|
||||
}) + "\n\n"));
|
||||
}) + '\n'));
|
||||
|
||||
// Set up cleanup function for when client disconnects
|
||||
signal.addEventListener('abort', () => {
|
||||
// Remove all listeners from emitter to prevent memory leaks
|
||||
emitter.removeAllListeners();
|
||||
|
||||
// Close the controller if it's still active
|
||||
try {
|
||||
controller.close();
|
||||
} catch (error) {
|
||||
// Controller might already be closed
|
||||
}
|
||||
});
|
||||
|
||||
emitter.on('data', (data: string) => {
|
||||
// Check if request has been cancelled before processing
|
||||
if (signal.aborted) return;
|
||||
|
||||
try {
|
||||
const parsedData = JSON.parse(data);
|
||||
|
||||
if (parsedData.type === 'response') {
|
||||
controller.enqueue(encoder.encode("data: " + JSON.stringify({
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
type: 'response',
|
||||
data: parsedData.data
|
||||
}) + "\n\n"));
|
||||
}) + '\n'));
|
||||
} else if (parsedData.type === 'sources') {
|
||||
sources = parsedData.data;
|
||||
controller.enqueue(encoder.encode("data: " + JSON.stringify({
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
type: 'sources',
|
||||
data: sources
|
||||
}) + "\n\n"));
|
||||
}) + '\n'));
|
||||
}
|
||||
} catch (error) {
|
||||
controller.error(error);
|
||||
@ -211,21 +218,22 @@ export const POST = async (req: Request) => {
|
||||
});
|
||||
|
||||
emitter.on('end', () => {
|
||||
// Check if request has been cancelled before processing
|
||||
if (signal.aborted) return;
|
||||
|
||||
controller.enqueue(encoder.encode("data: " + JSON.stringify({
|
||||
controller.enqueue(encoder.encode(JSON.stringify({
|
||||
type: 'done'
|
||||
}) + "\n\n"));
|
||||
}) + '\n'));
|
||||
controller.close();
|
||||
});
|
||||
|
||||
emitter.on('error', (error: any) => {
|
||||
// Check if request has been cancelled before processing
|
||||
if (signal.aborted) return;
|
||||
|
||||
controller.error(error);
|
||||
});
|
||||
},
|
||||
|
||||
cancel() {
|
||||
abortController.abort();
|
||||
}
|
||||
@ -233,7 +241,7 @@ export const POST = async (req: Request) => {
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-cache, no-transform',
|
||||
'Connection': 'keep-alive',
|
||||
},
|
||||
|
@ -72,9 +72,9 @@ export const POST = async (req: Request) => {
|
||||
|
||||
return Response.json({ suggestions }, { status: 200 });
|
||||
} catch (err) {
|
||||
console.error(`An error ocurred while generating suggestions: ${err}`);
|
||||
console.error(`An error occurred while generating suggestions: ${err}`);
|
||||
return Response.json(
|
||||
{ message: 'An error ocurred while generating suggestions' },
|
||||
{ message: 'An error occurred while generating suggestions' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
@ -74,9 +74,9 @@ export const POST = async (req: Request) => {
|
||||
|
||||
return Response.json({ videos }, { status: 200 });
|
||||
} catch (err) {
|
||||
console.error(`An error ocurred while searching videos: ${err}`);
|
||||
console.error(`An error occurred while searching videos: ${err}`);
|
||||
return Response.json(
|
||||
{ message: 'An error ocurred while searching videos' },
|
||||
{ message: 'An error occurred while searching videos' },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user