-
-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from n4ze3m/next
v1.4.5
- Loading branch information
Showing
7 changed files
with
122 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { cleanUrl } from "@/libs/clean-url" | ||
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 { MemoryVectorStore } from "langchain/vectorstores/memory" | ||
|
||
export const localBaiduSearch = async (query: string) => { | ||
const TOTAL_SEARCH_RESULTS = await totalSearchResults() | ||
|
||
const abortController = new AbortController() | ||
setTimeout(() => abortController.abort(), 10000) | ||
|
||
const jsonRes = await fetch( | ||
"https://www.baidu.com/s?wd=" + encodeURIComponent(query) + "&tn=json&rn=" + TOTAL_SEARCH_RESULTS, | ||
{ | ||
signal: abortController.signal | ||
} | ||
) | ||
.then((response) => response.json()) | ||
.catch((e) => { | ||
console.log(e) | ||
return { | ||
feed: { | ||
entry: [] | ||
} | ||
} | ||
}) | ||
|
||
const data = jsonRes?.feed?.entry || [] | ||
|
||
const searchResults = data.map((result: any) => { | ||
const title = result?.title || "" | ||
const link = result?.url | ||
const content = result?.abs || "" | ||
return { title, link, content } | ||
}) | ||
|
||
|
||
return searchResults.filter((result) => result?.link) | ||
} | ||
|
||
export const webBaiduSearch = async (query: string) => { | ||
const searchResults = await localBaiduSearch(query) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters