[FEAT] Bing Search API web search provider (#1519)

implement bing search engine for agents
This commit is contained in:
Sean Hatfield 2024-05-23 16:49:30 -07:00 committed by GitHub
parent 649d0d79eb
commit c24b79c9d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 140 additions and 3 deletions

View File

@ -233,3 +233,6 @@ GID='1000'
#------ Serper.dev ----------- https://serper.dev/
# AGENT_SERPER_DEV_KEY=
#------ Bing Search ----------- https://portal.azure.com/
# AGENT_BING_SEARCH_API_KEY=

View File

@ -6,6 +6,7 @@ export function GoogleSearchOptions({ settings }) {
<a
href="https://programmablesearchengine.google.com/controlpanel/create"
target="_blank"
rel="noreferrer"
className="text-blue-300 underline"
>
from Google here.
@ -57,6 +58,7 @@ export function SerperDotDevOptions({ settings }) {
<a
href="https://serper.dev"
target="_blank"
rel="noreferrer"
className="text-blue-300 underline"
>
from Serper.dev.
@ -82,3 +84,66 @@ export function SerperDotDevOptions({ settings }) {
</>
);
}
export function BingSearchOptions({ settings }) {
return (
<>
<p className="text-sm text-white/60 my-2">
You can get a Bing Web Search API subscription key{" "}
<a
href="https://portal.azure.com/"
target="_blank"
rel="noreferrer"
className="text-blue-300 underline"
>
from the Azure portal.
</a>
</p>
<div className="flex gap-x-4">
<div className="flex flex-col w-60">
<label className="text-white text-sm font-semibold block mb-4">
API Key
</label>
<input
type="password"
name="env::AgentBingSearchApiKey"
className="border-none bg-zinc-900 text-white placeholder:text-white/20 text-sm rounded-lg focus:border-white block w-full p-2.5"
placeholder="Bing Web Search API Key"
defaultValue={settings?.AgentBingSearchApiKey ? "*".repeat(20) : ""}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
</div>
<p className="text-sm text-white/60 my-2">
To set up a Bing Web Search API subscription:
</p>
<ol className="list-decimal text-sm text-white/60 ml-6">
<li>
Go to the Azure portal:{" "}
<a
href="https://portal.azure.com/"
target="_blank"
rel="noreferrer"
className="text-blue-300 underline"
>
https://portal.azure.com/
</a>
</li>
<li>Create a new Azure account or sign in with an existing one.</li>
<li>
Navigate to the "Create a resource" section and search for "Bing
Search v7".
</li>
<li>
Select the "Bing Search v7" resource and create a new subscription.
</li>
<li>
Choose the pricing tier that suits your needs (free tier available).
</li>
<li>Obtain the API key for your Bing Web Search subscription.</li>
</ol>
</>
);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

View File

@ -2,11 +2,13 @@ import React, { useEffect, useRef, useState } from "react";
import AnythingLLMIcon from "@/media/logo/anything-llm-icon.png";
import GoogleSearchIcon from "./icons/google.png";
import SerperDotDevIcon from "./icons/serper.png";
import BingSearchIcon from "./icons/bing.png";
import { CaretUpDown, MagnifyingGlass, X } from "@phosphor-icons/react";
import SearchProviderItem from "./SearchProviderItem";
import {
SerperDotDevOptions,
GoogleSearchOptions,
BingSearchOptions,
} from "./SearchProviderOptions";
const SEARCH_PROVIDERS = [
@ -34,6 +36,14 @@ const SEARCH_PROVIDERS = [
description:
"Serper.dev web-search. Free account with a 2,500 calls, but then paid.",
},
{
name: "Bing Search",
value: "bing-search",
logo: BingSearchIcon,
options: (settings) => <BingSearchOptions settings={settings} />,
description:
"Web search powered by the Bing Search API. Free for 1000 queries per month.",
},
];
export default function AgentWebSearchSelection({

View File

@ -229,3 +229,6 @@ TTS_PROVIDER="native"
#------ Serper.dev ----------- https://serper.dev/
# AGENT_SERPER_DEV_KEY=
#------ Bing Search ----------- https://portal.azure.com/
# AGENT_BING_SEARCH_API_KEY=

View File

@ -70,7 +70,11 @@ const SystemSettings = {
agent_search_provider: (update) => {
try {
if (update === "none") return null;
if (!["google-search-engine", "serper-dot-dev"].includes(update))
if (
!["google-search-engine", "serper-dot-dev", "bing-search"].includes(
update
)
)
throw new Error("Invalid SERP provider.");
return String(update);
} catch (e) {
@ -171,6 +175,7 @@ const SystemSettings = {
AgentGoogleSearchEngineId: process.env.AGENT_GSE_CTX || null,
AgentGoogleSearchEngineKey: process.env.AGENT_GSE_KEY || null,
AgentSerperApiKey: process.env.AGENT_SERPER_DEV_KEY || null,
AgentBingSearchApiKey: process.env.AGENT_BING_SEARCH_API_KEY || null,
};
},

View File

@ -65,6 +65,9 @@ const webBrowsing = {
case "serper-dot-dev":
engine = "_serperDotDev";
break;
case "bing-search":
engine = "_bingWebSearch";
break;
default:
engine = "_googleSearchEngine";
}
@ -172,6 +175,49 @@ const webBrowsing = {
return `No information was found online for the search query.`;
return JSON.stringify(data);
},
_bingWebSearch: async function (query) {
if (!process.env.AGENT_BING_SEARCH_API_KEY) {
this.super.introspect(
`${this.caller}: I can't use Bing Web Search because the user has not defined the required API key.\nVisit: https://portal.azure.com/ to create the API key.`
);
return `Search is disabled and no content was found. This functionality is disabled because the user has not set it up yet.`;
}
const searchURL = new URL(
"https://api.bing.microsoft.com/v7.0/search"
);
searchURL.searchParams.append("q", query);
this.super.introspect(
`${this.caller}: Using Bing Web Search to search for "${
query.length > 100 ? `${query.slice(0, 100)}...` : query
}"`
);
const searchResponse = await fetch(searchURL, {
headers: {
"Ocp-Apim-Subscription-Key":
process.env.AGENT_BING_SEARCH_API_KEY,
},
})
.then((res) => res.json())
.then((data) => {
const searchResults = data.webPages?.value || [];
return searchResults.map((result) => ({
title: result.name,
link: result.url,
snippet: result.snippet,
}));
})
.catch((e) => {
console.log(e);
return [];
});
if (searchResponse.length === 0)
return `No information was found online for the search query.`;
return JSON.stringify(searchResponse);
},
});
},
};

View File

@ -399,6 +399,10 @@ const KEY_MAPPING = {
envKey: "AGENT_SERPER_DEV_KEY",
checks: [],
},
AgentBingSearchApiKey: {
envKey: "AGENT_BING_SEARCH_API_KEY",
checks: [],
},
// TTS/STT Integration ENVS
TextToSpeechProvider: {
@ -762,6 +766,7 @@ async function dumpENV() {
"AGENT_GSE_CTX",
"AGENT_GSE_KEY",
"AGENT_SERPER_DEV_KEY",
"AGENT_BING_SEARCH_API_KEY",
];
// Simple sanitization of each value to prevent ENV injection via newline or quote escaping.