Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search): add Bing as search engine #431

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/features/internet-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Page Assist supports internet search which can be used with your LLM. It works s
- Searxng
- Brave
- Brave Search API
- Bing

## How to use Internet Search

Expand Down
4 changes: 4 additions & 0 deletions src/utils/search-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ export const SUPPORTED_SERACH_PROVIDERS = [
{
label: "Brave Search API",
value: "brave-api"
},
{
label: "Bing",
value: "bing"
}
]
119 changes: 119 additions & 0 deletions src/web/search-engines/bing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { cleanUrl } from "@/libs/clean-url"
import { urlRewriteRuntime } from "@/libs/runtime"
import { PageAssistHtmlLoader } from "@/loader/html"
import { pageAssistEmbeddingModel } from "@/models/embedding"
import {
defaultEmbeddingModelForRag,
getOllamaURL
} from "@/services/ollama"
import {
getIsSimpleInternetSearch,
totalSearchResults
} from "@/services/search"
import { getPageAssistTextSplitter } from "@/utils/text-splitter"
import type { Document } from "@langchain/core/documents"
import * as cheerio from "cheerio"
import { MemoryVectorStore } from "langchain/vectorstores/memory"

const BING_SEARCH_URL = "https://www.bing.com/search?q="

export const localBingSearch = async (query: string) => {
await urlRewriteRuntime(
cleanUrl(BING_SEARCH_URL + query),
"bing"
)

const abortController = new AbortController()
setTimeout(() => abortController.abort(), 10000)

const htmlString = await fetch(BING_SEARCH_URL + query, {
signal: abortController.signal
})
.then((response) => response.text())
.catch()

const $ = cheerio.load(htmlString)
const $results = $("#b_content #b_results")
const $snippets = $results.find(".b_algo")
const searchResults = Array.from($snippets).map((result) => {
const link = $(result).find(".tilk").attr("href")
const title = $(result).find("h2").find("a").text()
const content = $(result).find(".b_caption").find("p").text()
return { title, link, content }
}).filter((result) => result.link && result.title && result.content)

const $newsSnippets = $results.find(".b_nwsAns")
if ($newsSnippets.length > 0) {
const newsResults = Array.from($newsSnippets).map((result) => {
const link = $(result).find("a.itm_link").attr("href")
const title = $(result).find(".na_t_news_caption").text()
const content = $(result).find(".itm_spt_news_caption").text()
const _source = 'Bing News'
return { title, link, content, _source }
}).filter((result) => result.link && result.title && result.content)
searchResults.push(...newsResults)
}

return searchResults
}

export const webBingSearch = async (query: string) => {
const results = await localBingSearch(query)
const TOTAL_SEARCH_RESULTS = await totalSearchResults()
const searchResults = results.slice(0, TOTAL_SEARCH_RESULTS)

const isSimpleMode = await getIsSimpleInternetSearch()

if (isSimpleMode) {
await getOllamaURL()
return searchResults.map((result) => {
return {
url: result.link,
content: result.content
}
})
}

const docs: Document<Record<string, any>>[] = []
for (const result of searchResults) {
const loader = new PageAssistHtmlLoader({
html: "",
url: result.link
})

const documents = await loader.loadByURL()

documents.forEach((doc) => {
docs.push(doc)
})
}

const ollamaUrl = await getOllamaURL()

const embeddingModle = await defaultEmbeddingModelForRag()
const ollamaEmbedding = await pageAssistEmbeddingModel({
model: embeddingModle || "",
baseUrl: cleanUrl(ollamaUrl)
})


const textSplitter = await getPageAssistTextSplitter();

const chunks = await textSplitter.splitDocuments(docs)

const store = new MemoryVectorStore(ollamaEmbedding)

await store.addDocuments(chunks)

const resultsWithEmbeddings = await store.similaritySearch(query, 3)

const searchResult = resultsWithEmbeddings.map((result) => {
return {
url: result.metadata.url,
content: result.pageContent
}
})

return searchResult

}
3 changes: 3 additions & 0 deletions src/web/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getWebsiteFromQuery, processSingleWebsite } from "./website"
import { searxngSearch } from "./search-engines/searxng"
import { braveAPISearch } from "./search-engines/brave-api"
import { webBaiduSearch } from "./search-engines/baidu"
import { webBingSearch } from "./search-engines/bing"

const getHostName = (url: string) => {
try {
Expand All @@ -32,6 +33,8 @@ const searchWeb = (provider: string, query: string) => {
return braveAPISearch(query)
case "baidu":
return webBaiduSearch(query)
case "bing":
return webBingSearch(query)
default:
return webGoogleSearch(query)
}
Expand Down