Files
page-assist/src/web/web.ts

62 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-03-23 14:44:05 +05:30
import { getWebSearchPrompt } from "~/services/ollama"
2024-03-31 15:57:56 +05:30
import { webGoogleSearch } from "./local-google"
import { webDuckDuckGoSearch } from "./local-duckduckgo"
import { getSearchProvider } from "@/services/search"
const getHostName = (url: string) => {
2024-03-31 15:57:56 +05:30
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) => {
2024-03-31 15:57:56 +05:30
try {
const searchProvider = await getSearchProvider()
const search = await searchWeb(searchProvider, query)
2024-03-31 15:57:56 +05:30
const search_results = search
.map(
(result, idx) =>
`<result source="${result.url}" id="${idx}">${result.content}</result>`
)
.join("\n")
2024-03-31 15:57:56 +05:30
const current_date_time = new Date().toLocaleString()
2024-03-31 15:57:56 +05:30
const system = await getWebSearchPrompt()
2024-03-31 15:57:56 +05:30
const prompt = system
.replace("{current_date_time}", current_date_time)
.replace("{search_results}", search_results)
2024-03-31 15:57:56 +05:30
return {
prompt,
source: search.map((result) => {
return {
2024-03-31 15:57:56 +05:30
url: result.url,
name: getHostName(result.url),
type: "url"
}
2024-03-31 15:57:56 +05:30
})
}
} catch (e) {
console.error(e)
return {
prompt: "",
source: []
}
2024-03-31 15:57:56 +05:30
}
}