|
| 1 | +import { cleanUrl } from "@/libs/clean-url" |
| 2 | +import { PageAssistHtmlLoader } from "@/loader/html" |
| 3 | +import { pageAssistEmbeddingModel } from "@/models/embedding" |
| 4 | +import { |
| 5 | + defaultEmbeddingModelForRag, |
| 6 | + getOllamaURL |
| 7 | +} from "@/services/ollama" |
| 8 | +import { |
| 9 | + getIsSimpleInternetSearch, |
| 10 | + totalSearchResults |
| 11 | +} from "@/services/search" |
| 12 | +import { getPageAssistTextSplitter } from "@/utils/text-splitter" |
| 13 | +import type { Document } from "@langchain/core/documents" |
| 14 | +import { MemoryVectorStore } from "langchain/vectorstores/memory" |
| 15 | + |
| 16 | +export const localBaiduSearch = async (query: string) => { |
| 17 | + const TOTAL_SEARCH_RESULTS = await totalSearchResults() |
| 18 | + |
| 19 | + const abortController = new AbortController() |
| 20 | + setTimeout(() => abortController.abort(), 10000) |
| 21 | + |
| 22 | + const jsonRes = await fetch( |
| 23 | + "https://www.baidu.com/s?wd=" + encodeURIComponent(query) + "&tn=json&rn=" + TOTAL_SEARCH_RESULTS, |
| 24 | + { |
| 25 | + signal: abortController.signal |
| 26 | + } |
| 27 | + ) |
| 28 | + .then((response) => response.json()) |
| 29 | + .catch((e) => { |
| 30 | + console.log(e) |
| 31 | + return { |
| 32 | + feed: { |
| 33 | + entry: [] |
| 34 | + } |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + const data = jsonRes?.feed?.entry || [] |
| 39 | + |
| 40 | + const searchResults = data.map((result: any) => { |
| 41 | + const title = result?.title || "" |
| 42 | + const link = result?.url |
| 43 | + const content = result?.abs || "" |
| 44 | + return { title, link, content } |
| 45 | + }) |
| 46 | + |
| 47 | + |
| 48 | + return searchResults.filter((result) => result?.link) |
| 49 | +} |
| 50 | + |
| 51 | +export const webBaiduSearch = async (query: string) => { |
| 52 | + const searchResults = await localBaiduSearch(query) |
| 53 | + |
| 54 | + const isSimpleMode = await getIsSimpleInternetSearch() |
| 55 | + |
| 56 | + if (isSimpleMode) { |
| 57 | + await getOllamaURL() |
| 58 | + return searchResults.map((result) => { |
| 59 | + return { |
| 60 | + url: result.link, |
| 61 | + content: result.content |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + const docs: Document<Record<string, any>>[] = [] |
| 67 | + for (const result of searchResults) { |
| 68 | + const loader = new PageAssistHtmlLoader({ |
| 69 | + html: "", |
| 70 | + url: result.link |
| 71 | + }) |
| 72 | + |
| 73 | + const documents = await loader.loadByURL() |
| 74 | + |
| 75 | + documents.forEach((doc) => { |
| 76 | + docs.push(doc) |
| 77 | + }) |
| 78 | + } |
| 79 | + const ollamaUrl = await getOllamaURL() |
| 80 | + |
| 81 | + const embeddingModle = await defaultEmbeddingModelForRag() |
| 82 | + const ollamaEmbedding = await pageAssistEmbeddingModel({ |
| 83 | + model: embeddingModle || "", |
| 84 | + baseUrl: cleanUrl(ollamaUrl) |
| 85 | + }) |
| 86 | + |
| 87 | + const textSplitter = await getPageAssistTextSplitter() |
| 88 | + |
| 89 | + const chunks = await textSplitter.splitDocuments(docs) |
| 90 | + |
| 91 | + const store = new MemoryVectorStore(ollamaEmbedding) |
| 92 | + |
| 93 | + await store.addDocuments(chunks) |
| 94 | + |
| 95 | + const resultsWithEmbeddings = await store.similaritySearch(query, 3) |
| 96 | + |
| 97 | + const searchResult = resultsWithEmbeddings.map((result) => { |
| 98 | + return { |
| 99 | + url: result.metadata.url, |
| 100 | + content: result.pageContent |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + return searchResult |
| 105 | +} |
0 commit comments