mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-09-20 08:11:33 +00:00
feat(tavily): integrate Tavily search engine with configuration and UI support
This commit is contained in:
@@ -17,7 +17,9 @@ import LineListOutputParser from '../outputParsers/listLineOutputParser';
|
||||
import LineOutputParser from '../outputParsers/lineOutputParser';
|
||||
import { getDocumentsFromLinks } from '../utils/documents';
|
||||
import { Document } from 'langchain/document';
|
||||
import { searchTavily } from '../tavily';
|
||||
import { searchSearxng } from '../searxng';
|
||||
import { getSearchEngine } from '../config';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import computeSimilarity from '../utils/computeSimilarity';
|
||||
@@ -205,25 +207,47 @@ class MetaSearchAgent implements MetaSearchAgentType {
|
||||
} else {
|
||||
question = question.replace(/<think>.*?<\/think>/g, '');
|
||||
|
||||
const res = await searchSearxng(question, {
|
||||
language: 'en',
|
||||
engines: this.config.activeEngines,
|
||||
});
|
||||
const searchEngine = getSearchEngine();
|
||||
console.log(`Using search engine: ${searchEngine}`);
|
||||
|
||||
const documents = res.results.map(
|
||||
(result) =>
|
||||
new Document({
|
||||
pageContent:
|
||||
result.content ||
|
||||
(this.config.activeEngines.includes('youtube')
|
||||
? result.title
|
||||
: '') /* Todo: Implement transcript grabbing using Youtubei (source: https://www.npmjs.com/package/youtubei) */,
|
||||
metadata: {
|
||||
title: result.title,
|
||||
url: result.url,
|
||||
...(result.img_src && { img_src: result.img_src }),
|
||||
},
|
||||
}),
|
||||
let res;
|
||||
|
||||
if (searchEngine === 'tavily') {
|
||||
res = await searchTavily(question, {
|
||||
search_depth: 'basic',
|
||||
max_results: 15,
|
||||
include_images: true,
|
||||
});
|
||||
} else {
|
||||
// Default to SearxNG
|
||||
res = await searchSearxng(question, {
|
||||
language: 'en',
|
||||
engines: this.config.activeEngines,
|
||||
});
|
||||
}
|
||||
|
||||
// If we have an AI-generated answer from Tavily, create a document for it
|
||||
let documents: Document[] = [];
|
||||
|
||||
|
||||
|
||||
// Add the regular search results
|
||||
documents = documents.concat(
|
||||
res.results.map(
|
||||
(result) =>
|
||||
new Document({
|
||||
pageContent:
|
||||
result.content ||
|
||||
(this.config.activeEngines.includes('youtube')
|
||||
? result.title
|
||||
: ''),
|
||||
metadata: {
|
||||
title: result.title,
|
||||
url: result.url,
|
||||
...(result.img_src ? { img_src: result.img_src } : {}),
|
||||
},
|
||||
}),
|
||||
)
|
||||
);
|
||||
|
||||
return { query: question, docs: documents };
|
||||
|
Reference in New Issue
Block a user