feat(server): replace existing search functionality with searxng library

This commit is contained in:
Zan 2024-07-31 11:23:39 +08:00
parent 96f67c7028
commit 28077018a6
11 changed files with 460 additions and 519 deletions

View file

@ -41,6 +41,7 @@
"html-to-text": "^9.0.5", "html-to-text": "^9.0.5",
"langchain": "^0.1.30", "langchain": "^0.1.30",
"pdf-parse": "^1.1.1", "pdf-parse": "^1.1.1",
"searxng": "^0.0.5",
"winston": "^3.13.0", "winston": "^3.13.0",
"ws": "^8.17.1", "ws": "^8.17.1",
"zod": "^3.22.4" "zod": "^3.22.4"

View file

@ -113,11 +113,11 @@ const createBasicAcademicSearchRetrieverChain = (llm: BaseChatModel) => {
} }
const res = await searchSearxng(input, { const res = await searchSearxng(input, {
language: 'en', lang: 'en',
engines: [ engines: [
'arxiv', 'arxiv',
'google scholar', 'google_scholar',
'internetarchivescholar', 'internet_archive_scholar',
'pubmed', 'pubmed',
], ],
}); });

View file

@ -53,7 +53,7 @@ const createImageSearchChain = (llm: BaseChatModel) => {
strParser, strParser,
RunnableLambda.from(async (input: string) => { RunnableLambda.from(async (input: string) => {
const res = await searchSearxng(input, { const res = await searchSearxng(input, {
engines: ['bing images', 'google images'], engines: ['bing_images', 'google_images'],
}); });
const images = []; const images = [];

View file

@ -113,7 +113,7 @@ const createBasicRedditSearchRetrieverChain = (llm: BaseChatModel) => {
} }
const res = await searchSearxng(input, { const res = await searchSearxng(input, {
language: 'en', lang: 'en',
engines: ['reddit'], engines: ['reddit'],
}); });

View file

@ -53,7 +53,7 @@ const createVideoSearchChain = (llm: BaseChatModel) => {
strParser, strParser,
RunnableLambda.from(async (input: string) => { RunnableLambda.from(async (input: string) => {
const res = await searchSearxng(input, { const res = await searchSearxng(input, {
engines: ['youtube'], engines: ['youtube_api'],
}); });
const videos = []; const videos = [];

View file

@ -218,7 +218,7 @@ const createBasicWebSearchRetrieverChain = (llm: BaseChatModel) => {
return { query: question, docs: docs }; return { query: question, docs: docs };
} else { } else {
const res = await searchSearxng(input, { const res = await searchSearxng(input, {
language: 'en', lang: 'en',
}); });
const documents = res.results.map( const documents = res.results.map(

View file

@ -112,8 +112,8 @@ const createBasicWolframAlphaSearchRetrieverChain = (llm: BaseChatModel) => {
} }
const res = await searchSearxng(input, { const res = await searchSearxng(input, {
language: 'en', lang: 'en',
engines: ['wolframalpha'], engines: ['wolframalpha_api'],
}); });
const documents = res.results.map( const documents = res.results.map(

View file

@ -113,8 +113,8 @@ const createBasicYoutubeSearchRetrieverChain = (llm: BaseChatModel) => {
} }
const res = await searchSearxng(input, { const res = await searchSearxng(input, {
language: 'en', lang: 'en',
engines: ['youtube'], engines: ['youtube_api'],
}); });
const documents = res.results.map( const documents = res.results.map(

View file

@ -1,47 +1,19 @@
import axios from 'axios';
import { getSearxngApiEndpoint } from '../config'; import { getSearxngApiEndpoint } from '../config';
interface SearxngSearchOptions { import { SearxngService, type SearxngSearchParameters } from 'searxng';
categories?: string[];
engines?: string[];
language?: string;
pageno?: number;
}
interface SearxngSearchResult { const searxng = new SearxngService({
title: string; baseURL: getSearxngApiEndpoint(),
url: string; defaultSearchParams: {
img_src?: string; format: 'json'
thumbnail_src?: string; }
thumbnail?: string; })
content?: string;
author?: string;
iframe_src?: string;
}
export const searchSearxng = async ( export const searchSearxng = async (
query: string, query: string,
opts?: SearxngSearchOptions, opts?: SearxngSearchParameters,
) => { ) => {
const searxngURL = getSearxngApiEndpoint();
const url = new URL(`${searxngURL}/search?format=json`);
url.searchParams.append('q', query);
if (opts) {
Object.keys(opts).forEach((key) => {
if (Array.isArray(opts[key])) {
url.searchParams.append(key, opts[key].join(','));
return;
}
url.searchParams.append(key, opts[key]);
});
}
const res = await axios.get(url.toString());
const results: SearxngSearchResult[] = res.data.results;
const suggestions: string[] = res.data.suggestions;
const { results, suggestions } = await searxng.search(query, opts);
return { results, suggestions }; return { results, suggestions };
}; };

View file

@ -1,8 +1,8 @@
{ {
"compilerOptions": { "compilerOptions": {
"lib": ["ESNext"], "lib": ["ESNext"],
"module": "Node16", "module": "ESNext",
"moduleResolution": "Node16", "moduleResolution": "Node",
"target": "ESNext", "target": "ESNext",
"outDir": "dist", "outDir": "dist",
"sourceMap": false, "sourceMap": false,

884
yarn.lock

File diff suppressed because it is too large Load diff