Skip to content

Commit

Permalink
DuckDuckGo web search agent skill support (#2584)
Browse files Browse the repository at this point in the history
* duckduckgo web search agent skill support

* move ddg to first option in dropdown menu

* lint

* add duckduckgo option stating no config required
  • Loading branch information
shatfield4 authored Nov 5, 2024
1 parent 04e2920 commit a2eced0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,13 @@ export function TavilySearchOptions({ settings }) {
</>
);
}

export function DuckDuckGoOptions() {
return (
<>
<p className="text-sm text-white/60 my-2">
DuckDuckGo is ready to use without any additional configuration.
</p>
</>
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions frontend/src/pages/Admin/Agents/WebSearchSelection/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import BingSearchIcon from "./icons/bing.png";
import SerplySearchIcon from "./icons/serply.png";
import SearXNGSearchIcon from "./icons/searxng.png";
import TavilySearchIcon from "./icons/tavily.svg";
import DuckDuckGoIcon from "./icons/duckduckgo.png";
import {
CaretUpDown,
MagnifyingGlass,
Expand All @@ -24,6 +25,7 @@ import {
SerplySearchOptions,
SearXNGOptions,
TavilySearchOptions,
DuckDuckGoOptions,
} from "./SearchProviderOptions";

const SEARCH_PROVIDERS = [
Expand All @@ -35,6 +37,14 @@ const SEARCH_PROVIDERS = [
description:
"Web search will be disabled until a provider and keys are provided.",
},
{
name: "DuckDuckGo",
value: "duckduckgo-engine",
logo: DuckDuckGoIcon,
options: () => <DuckDuckGoOptions />,
description:
"Free and privacy-focused web search using DuckDuckGo's HTML interface.",
},
{
name: "Google Search Engine",
value: "google-search-engine",
Expand Down
1 change: 1 addition & 0 deletions server/models/systemSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const SystemSettings = {
"serply-engine",
"searxng-engine",
"tavily-search",
"duckduckgo-engine",
].includes(update)
)
throw new Error("Invalid SERP provider.");
Expand Down
63 changes: 63 additions & 0 deletions server/utils/agents/aibitat/plugins/web-browsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ const webBrowsing = {
case "tavily-search":
engine = "_tavilySearch";
break;
case "duckduckgo-engine":
engine = "_duckDuckGoEngine";
break;
default:
engine = "_googleSearchEngine";
}
Expand Down Expand Up @@ -499,6 +502,66 @@ const webBrowsing = {
);
return JSON.stringify(data);
},
_duckDuckGoEngine: async function (query) {
this.super.introspect(
`${this.caller}: Using DuckDuckGo to search for "${
query.length > 100 ? `${query.slice(0, 100)}...` : query
}"`
);

const searchURL = new URL("https://html.duckduckgo.com/html");
searchURL.searchParams.append("q", query);

const response = await fetch(searchURL.toString());

if (!response.ok) {
return `There was an error searching DuckDuckGo. Status: ${response.status}`;
}

const html = await response.text();
const data = [];

const results = html.split('<div class="result results_links');

// Skip first element since it's before the first result
for (let i = 1; i < results.length; i++) {
const result = results[i];

// Extract title
const titleMatch = result.match(
/<a[^>]*class="result__a"[^>]*>(.*?)<\/a>/
);
const title = titleMatch ? titleMatch[1].trim() : "";

// Extract URL
const urlMatch = result.match(
/<a[^>]*class="result__a"[^>]*href="([^"]*)">/
);
const link = urlMatch ? urlMatch[1] : "";

// Extract snippet
const snippetMatch = result.match(
/<a[^>]*class="result__snippet"[^>]*>(.*?)<\/a>/
);
const snippet = snippetMatch
? snippetMatch[1].replace(/<\/?b>/g, "").trim()
: "";

if (title && link && snippet) {
data.push({ title, link, snippet });
}
}

if (data.length === 0) {
return `No information was found online for the search query.`;
}

this.super.introspect(
`${this.caller}: I found ${data.length} results - looking over them now.`
);

return JSON.stringify(data);
},
});
},
};
Expand Down

0 comments on commit a2eced0

Please sign in to comment.