Added Web search DuckDuckGo

This commit is contained in:
n4ze3m
2024-03-31 15:57:56 +05:30
parent 317011a6d2
commit 9ced6469ce
14 changed files with 455 additions and 112 deletions

View File

@@ -1,42 +1,61 @@
import { getWebSearchPrompt } from "~/services/ollama"
import { webSearch } from "./local-google"
import { webGoogleSearch } from "./local-google"
import { webDuckDuckGoSearch } from "./local-duckduckgo"
import { getSearchProvider } from "@/services/search"
const getHostName = (url: string) => {
try {
const hostname = new URL(url).hostname
return hostname
} catch (e) {
return ""
}
try {
const hostname = new URL(url).hostname
return hostname
} catch (e) {
return ""
}
}
const searchWeb = (provider: string, query: string) => {
switch (provider) {
case "duckduckgo":
return webDuckDuckGoSearch(query)
default:
return webGoogleSearch(query)
}
}
export const getSystemPromptForWeb = async (query: string) => {
try {
const search = await webSearch(query)
try {
const searchProvider = await getSearchProvider()
const search = await searchWeb(searchProvider, query)
const search_results = search.map((result, idx) => `<result source="${result.url}" id="${idx}">${result.content}</result>`).join("\n")
const search_results = search
.map(
(result, idx) =>
`<result source="${result.url}" id="${idx}">${result.content}</result>`
)
.join("\n")
const current_date_time = new Date().toLocaleString()
const current_date_time = new Date().toLocaleString()
const system = await getWebSearchPrompt();
const system = await getWebSearchPrompt()
const prompt = system.replace("{current_date_time}", current_date_time).replace("{search_results}", search_results)
const prompt = system
.replace("{current_date_time}", current_date_time)
.replace("{search_results}", search_results)
return {
prompt,
source: search.map((result) => {
return {
prompt,
source: search.map((result) => {
return {
url: result.url,
name: getHostName(result.url),
type: "url",
}
})
}
} catch (e) {
console.error(e)
return {
prompt: "",
source: [],
url: result.url,
name: getHostName(result.url),
type: "url"
}
})
}
}
} catch (e) {
console.error(e)
return {
prompt: "",
source: []
}
}
}