Compare commits

...

4 Commits

Author SHA1 Message Date
97e64aa65e Merge branch 'pr/703' 2025-03-30 21:12:27 +05:30
90e303f737 feat(search): lint & beautify, update content type 2025-03-30 21:12:04 +05:30
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
b285cb4323 Update Gemini chat models 2025-03-28 17:07:11 +01:00
2 changed files with 56 additions and 40 deletions

View File

@ -146,7 +146,10 @@ export const POST = async (req: Request) => {
}
} catch (error) {
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) => {
reject(
Response.json({ message: 'Search error', error }, { status: 500 }),
Response.json(
{ message: 'Search error', error },
{ status: 500 },
),
);
});
},
@ -166,7 +172,6 @@ export const POST = async (req: Request) => {
const encoder = new TextEncoder();
// Create an AbortController to handle cancellation
const abortController = new AbortController();
const { signal } = abortController;
@ -174,43 +179,48 @@ export const POST = async (req: Request) => {
start(controller) {
let sources: any[] = [];
// Send an initial message to keep the connection alive
controller.enqueue(encoder.encode(JSON.stringify({
type: 'init',
data: 'Stream connected'
}) + '\n'));
controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'init',
data: 'Stream connected',
}) + '\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
}
} catch (error) {}
});
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(JSON.stringify({
type: 'response',
data: parsedData.data
}) + '\n'));
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'));
controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'sources',
data: sources,
}) + '\n',
),
);
}
} catch (error) {
controller.error(error);
@ -218,17 +228,19 @@ 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(JSON.stringify({
type: 'done'
}) + '\n'));
controller.enqueue(
encoder.encode(
JSON.stringify({
type: 'done',
}) + '\n',
),
);
controller.close();
});
emitter.on('error', (error: any) => {
// Check if request has been cancelled before processing
if (signal.aborted) return;
controller.error(error);
@ -236,14 +248,14 @@ export const POST = async (req: Request) => {
},
cancel() {
abortController.abort();
}
},
});
return new Response(stream, {
headers: {
'Content-Type': 'application/json',
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-transform',
'Connection': 'keep-alive',
Connection: 'keep-alive',
},
});
} catch (err: any) {

View File

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