Compare commits

..

6 Commits

Author SHA1 Message Date
ItzCrazyKns
97e64aa65e Merge branch 'pr/703' 2025-03-30 21:12:27 +05:30
ItzCrazyKns
90e303f737 feat(search): lint & beautify, update content type 2025-03-30 21:12:04 +05:30
ItzCrazyKns
7955d8e408 Merge pull request #705 from ottsch/add-gemini-2.5
feat(models): Update Gemini chat models
2025-03-29 21:53:02 +05:30
ottsch
b285cb4323 Update Gemini chat models 2025-03-28 17:07:11 +01:00
OTYAK
5d60ab1139 feat(api): Switch to newline-delimited JSON streaming instead of SSE 2025-03-27 13:04:09 +01:00
OTYAK
9095996356 Merge branch 'ItzCrazyKns:master' into master 2025-03-27 13:01:09 +01:00
3 changed files with 82 additions and 33 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

@@ -146,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 },
),
); );
} }
}); });
@@ -157,7 +160,10 @@ export const POST = async (req: Request) => {
emitter.on('error', (error: any) => { emitter.on('error', (error: any) => {
reject( reject(
Response.json({ message: 'Search error', error }, { status: 500 }), Response.json(
{ message: 'Search error', error },
{ status: 500 },
),
); );
}); });
}, },
@@ -173,18 +179,21 @@ export const POST = async (req: Request) => {
start(controller) { start(controller) {
let sources: any[] = []; let sources: any[] = [];
controller.enqueue(encoder.encode("data: " + JSON.stringify({ controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'init', type: 'init',
data: 'Stream connected' data: 'Stream connected',
}) + "\n\n")); }) + '\n',
),
);
signal.addEventListener('abort', () => { signal.addEventListener('abort', () => {
emitter.removeAllListeners(); emitter.removeAllListeners();
try { try {
controller.close(); controller.close();
} catch (error) { } catch (error) {}
}
}); });
emitter.on('data', (data: string) => { emitter.on('data', (data: string) => {
@@ -194,16 +203,24 @@ export const POST = async (req: Request) => {
const parsedData = JSON.parse(data); const parsedData = JSON.parse(data);
if (parsedData.type === 'response') { if (parsedData.type === 'response') {
controller.enqueue(encoder.encode("data: " + JSON.stringify({ controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'response', type: 'response',
data: parsedData.data data: parsedData.data,
}) + "\n\n")); }) + '\n',
),
);
} else if (parsedData.type === 'sources') { } else if (parsedData.type === 'sources') {
sources = parsedData.data; sources = parsedData.data;
controller.enqueue(encoder.encode("data: " + JSON.stringify({ controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'sources', type: 'sources',
data: sources data: sources,
}) + "\n\n")); }) + '\n',
),
);
} }
} catch (error) { } catch (error) {
controller.error(error); controller.error(error);
@@ -213,9 +230,13 @@ export const POST = async (req: Request) => {
emitter.on('end', () => { emitter.on('end', () => {
if (signal.aborted) return; if (signal.aborted) return;
controller.enqueue(encoder.encode("data: " + JSON.stringify({ controller.enqueue(
type: 'done' encoder.encode(
}) + "\n\n")); JSON.stringify({
type: 'done',
}) + '\n',
),
);
controller.close(); controller.close();
}); });
@@ -225,17 +246,16 @@ export const POST = async (req: Request) => {
controller.error(error); controller.error(error);
}); });
}, },
cancel() { cancel() {
abortController.abort(); abortController.abort();
} },
}); });
return new Response(stream, { return new Response(stream, {
headers: { headers: {
'Content-Type': 'text/event-stream', 'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-transform', 'Cache-Control': 'no-cache, no-transform',
'Connection': 'keep-alive', Connection: 'keep-alive',
}, },
}); });
} catch (err: any) { } catch (err: any) {

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',