Skip to content

Commit

Permalink
Tavily search web search agent support (#2395)
Browse files Browse the repository at this point in the history
* support tavily search web search agent

* lint

* remove unneeded comments
  • Loading branch information
shatfield4 authored Oct 1, 2024
1 parent 348d9c8 commit 5ac6020
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,38 @@ export function SearXNGOptions({ settings }) {
</div>
);
}

export function TavilySearchOptions({ settings }) {
return (
<>
<p className="text-sm text-white/60 my-2">
You can get an API key{" "}
<a
href="https://tavily.com/"
target="_blank"
rel="noreferrer"
className="text-blue-300 underline"
>
from Tavily.
</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-3">
API Key
</label>
<input
type="password"
name="env::AgentTavilyApiKey"
className="border-none bg-zinc-900 text-white placeholder:text-white/20 text-sm rounded-lg focus:outline-primary-button active:outline-primary-button outline-none block w-full p-2.5"
placeholder="Tavily API Key"
defaultValue={settings?.AgentTavilyApiKey ? "*".repeat(20) : ""}
required={true}
autoComplete="off"
spellCheck={false}
/>
</div>
</div>
</>
);
}
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 @@ -7,6 +7,7 @@ import SerperDotDevIcon from "./icons/serper.png";
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 {
CaretUpDown,
MagnifyingGlass,
Expand All @@ -22,6 +23,7 @@ import {
BingSearchOptions,
SerplySearchOptions,
SearXNGOptions,
TavilySearchOptions,
} from "./SearchProviderOptions";

const SEARCH_PROVIDERS = [
Expand Down Expand Up @@ -81,6 +83,14 @@ const SEARCH_PROVIDERS = [
description:
"Free, open-source, internet meta-search engine with no tracking.",
},
{
name: "Tavily Search",
value: "tavily-search",
logo: TavilySearchIcon,
options: (settings) => <TavilySearchOptions settings={settings} />,
description:
"Tavily Search API. Offers a free tier with 1000 queries per month.",
},
];

export default function AgentWebSearchSelection({
Expand Down
2 changes: 2 additions & 0 deletions server/models/systemSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const SystemSettings = {
"bing-search",
"serply-engine",
"searxng-engine",
"tavily-search",
].includes(update)
)
throw new Error("Invalid SERP provider.");
Expand Down Expand Up @@ -242,6 +243,7 @@ const SystemSettings = {
AgentBingSearchApiKey: !!process.env.AGENT_BING_SEARCH_API_KEY || null,
AgentSerplyApiKey: !!process.env.AGENT_SERPLY_API_KEY || null,
AgentSearXNGApiUrl: process.env.AGENT_SEARXNG_API_URL || null,
AgentTavilyApiKey: !!process.env.AGENT_TAVILY_API_KEY || null,
};
},

Expand Down
56 changes: 56 additions & 0 deletions server/utils/agents/aibitat/plugins/web-browsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ const webBrowsing = {
case "searxng-engine":
engine = "_searXNGEngine";
break;
case "tavily-search":
engine = "_tavilySearch";
break;
default:
engine = "_googleSearchEngine";
}
Expand Down Expand Up @@ -436,6 +439,59 @@ const webBrowsing = {
});
});

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);
},
_tavilySearch: async function (query) {
if (!process.env.AGENT_TAVILY_API_KEY) {
this.super.introspect(
`${this.caller}: I can't use Tavily searching because the user has not defined the required API key.\nVisit: https://tavily.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.`;
}

this.super.introspect(
`${this.caller}: Using Tavily to search for "${
query.length > 100 ? `${query.slice(0, 100)}...` : query
}"`
);

const url = "https://api.tavily.com/search";
const { response, error } = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
api_key: process.env.AGENT_TAVILY_API_KEY,
query: query,
}),
})
.then((res) => res.json())
.then((data) => {
return { response: data, error: null };
})
.catch((e) => {
return { response: null, error: e.message };
});

if (error)
return `There was an error searching for content. ${error}`;

const data = [];
response.results?.forEach((searchResult) => {
const { title, url, content } = searchResult;
data.push({
title,
link: url,
snippet: content,
});
});

if (data.length === 0)
return `No information was found online for the search query.`;
this.super.introspect(
Expand Down
4 changes: 4 additions & 0 deletions server/utils/helpers/updateENV.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ const KEY_MAPPING = {
envKey: "AGENT_SEARXNG_API_URL",
checks: [],
},
AgentTavilyApiKey: {
envKey: "AGENT_TAVILY_API_KEY",
checks: [],
},

// TTS/STT Integration ENVS
TextToSpeechProvider: {
Expand Down

0 comments on commit 5ac6020

Please sign in to comment.