feat: add support for Firecrawl
This commit is contained in:
@ -1,9 +0,0 @@
|
||||
import { tavily } from '@tavily/core'
|
||||
|
||||
export const useTavily = () => {
|
||||
const config = useConfigStore()
|
||||
const tvly = tavily({
|
||||
apiKey: config.config.webSearch.apiKey,
|
||||
})
|
||||
return tvly
|
||||
}
|
60
composables/useWebSearch.ts
Normal file
60
composables/useWebSearch.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { tavily } from '@tavily/core'
|
||||
import Firecrawl from '@mendable/firecrawl-js'
|
||||
|
||||
type WebSearchOptions = {
|
||||
maxResults?: number
|
||||
/** The search language, e.g. `en`. Only works for Firecrawl. */
|
||||
lang?: string
|
||||
}
|
||||
|
||||
export type WebSearchResult = {
|
||||
content: string
|
||||
url: string
|
||||
title?: string
|
||||
}
|
||||
|
||||
type WebSearchFunction = (
|
||||
query: string,
|
||||
options: WebSearchOptions,
|
||||
) => Promise<WebSearchResult[]>
|
||||
|
||||
export const useWebSearch = (): WebSearchFunction => {
|
||||
const { config } = useConfigStore()
|
||||
|
||||
switch (config.webSearch.provider) {
|
||||
case 'firecrawl': {
|
||||
const fc = new Firecrawl({
|
||||
apiKey: config.webSearch.apiKey,
|
||||
})
|
||||
return async (q: string, o: WebSearchOptions) => {
|
||||
const results = await fc.search(q, o)
|
||||
if (results.error) {
|
||||
throw new Error(results.error)
|
||||
}
|
||||
return results.data
|
||||
.filter((x) => !!x?.markdown && !!x.url)
|
||||
.map((r) => ({
|
||||
content: r.markdown!,
|
||||
url: r.url!,
|
||||
title: r.title,
|
||||
}))
|
||||
}
|
||||
}
|
||||
case 'tavily':
|
||||
default: {
|
||||
const tvly = tavily({
|
||||
apiKey: config.webSearch.apiKey,
|
||||
})
|
||||
return async (q: string, o: WebSearchOptions) => {
|
||||
const results = await tvly.search(q, o)
|
||||
return results.results
|
||||
.filter((x) => !!x?.content && !!x.url)
|
||||
.map((r) => ({
|
||||
content: r.content,
|
||||
url: r.url,
|
||||
title: r.title,
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user