Skip to content

Commit

Permalink
feat: add tavily search integration (#242)
Browse files Browse the repository at this point in the history
Co-authored-by: pratrivedi <[email protected]>
Co-authored-by: Joshua Croft <[email protected]>
  • Loading branch information
3 people authored Mar 13, 2024
1 parent 0d3d794 commit cb9d11c
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
58 changes: 58 additions & 0 deletions integrations/tavily-ai-agent-search /README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Tavily AI Agent Search Integration

## Overview
The Tavily AI Agent Search integration enhances the capabilities of an AI agent by leveraging the Tavily AI Search API. This integration allows the AI agent to perform advanced searches, providing users with relevant information based on their queries.

## Tavily Search
**Tavily Search** is a powerful AI-driven search API that enables intelligent content retrieval. It offers features such as configurable search depth, image inclusion/exclusion, and the ability to obtain raw content. The API is designed to enhance search experiences for applications and services.

## Usage
To use the Tavily AI Agent Search integration, create an instance of the `SearchRequest` model with a specific query and send it to the agent. The agent will utilize the Tavily Search API to perform the search and respond with formatted results.

# Getting Tavily API Key

To access the Tavily AI Search API, you need an API key. Follow these steps to obtain your API key:

1. Visit the Tavily website at [https://www.tavily.com](https://www.tavily.com).
2. Sign up or log in to your account.
3. Navigate to the API section.
4. Create a new API key.
5. Copy the generated API key.
6. Replace the placeholder in the script with your actual API key.

Ensure that you keep your API key secure and do not share it publicly. It is a sensitive credential that grants access to Tavily's services.

# Agent Secrets on Agentverse

1. Go to the Agentverse platform.
2. Navigate to the Agent Secrets section.
3. Create an agent and copy the code in it
4. Add a new secret with the key `API_KEY` and the value as your API KEY.

# Steps to Enroll an Agent as a Service on Agentverse

You can integrate into DeltaV your Agents created on your local computer, IoT devices, in the VMs, or agents created on Agentverse. The steps are the same.

Once your agents are run, the agent protocol manifests are uploaded to the Almanac contract in the form of protocol digests. After uploading the manifests, we take the agent addresses and enroll the agents as a service under the "Services" tab in Agentverse.

## Agent Validation on Agentverse Explorer
*Note: You can validate the procedure by searching for your agent's address on Agent Explorer, checking if the protocols have been uploaded successfully. If not, you need to wait for some time (1-2 minutes) until the protocols are uploaded successfully.*

## Create a Service Group

1. Start by creating a new service group on Agentverse.
2. Set up the service group as PRIVATE (you will only be able to see your own agents).
- If you set up your service group as Public, anyone will be able to see your agents.

**Service group has been created.**

## Create a Service

1. To register the agents as a service, input a concise title and description for the agent service.
2. Choose the service group for the agent service that you've created previously.
3. Fill in the agent address in the Agent field.
4. Set the task type to Task.

![Image](./image.png)

Now, your agents are enrolled as a service in Agentverse. You can manage and monitor them under the "Services" tab. Ensure that you follow the agent validation steps on Agent Explorer to confirm successful enrollment.
74 changes: 74 additions & 0 deletions integrations/tavily-ai-agent-search /agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import requests
from ai_engine import UAgentResponse, UAgentResponseType

# Define the Search Request model
class SearchRequest(Model):
query: str

# Define the protocol for Tavily Search
tavily_search_protocol = Protocol("Tavily Search")

def tavily_search(query, API_KEY):
"""Perform a search using the Tavily Search API and return results."""
endpoint = "https://api.tavily.com/search"
headers = {
"Content-Type": "application/json"
}
payload = {
"API_KEY": API_KEY,
"query": query,
"search_depth": "basic",
"include_images":False,
"include_answer":False,
"include_raw_content":False,
"max_results":5,
"include_domains":None,
"exclude_domains":None
}
try:
response = requests.post(endpoint, json=payload, headers=headers)
response.raise_for_status()
return response.json()
except requests.RequestException as e:
ctx.logger.info(f"Error during Tavily search: {e}")
return None

# Function to format the results into a simple string
def format_results(results):
formatted_string = f"Query: {results['query']}\n"
for result in results['results']:
formatted_string += f"Title: {result['title']}\nURL: {result['url']}\nContent: {result['content']}\n\n"
return formatted_string.strip()

@tavily_search_protocol.on_message(model=SearchRequest, replies=UAgentResponse)
async def on_search_request(ctx: Context, sender: str, msg: SearchRequest):
ctx.logger.info(f"Received search request from {sender} with query: {msg.query}")

try:
# Perform the search
search_results = tavily_search(msg.query, API_KEY)
if search_results is None:
raise Exception("Failed to get search results.")

# Send the search results response
formatted_string = format_results(search_results)
await ctx.send(
sender,
UAgentResponse(
message=f"{formatted_string}", # You may format this as needed
type=UAgentResponseType.FINAL # Assuming FINAL indicates a successful response
)
)

except Exception as exc:
ctx.logger.error(f"An error occurred: {exc}")
await ctx.send(
sender,
UAgentResponse(
message=f"Error: {exc}",
type=UAgentResponseType.ERROR # Assuming ERROR indicates an error response
)
)

# Include the Tavily Search protocol in your agent
agent.include(tavily_search_protocol)
Binary file added integrations/tavily-ai-agent-search /image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions integrations/tavily-ai-agent-search /project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "Tavily AI Agent Search",
"description": "This integration leverages the Tavily AI Search API to empower an AI agent with advanced search capabilities",
"categories": ["AI-Powered Search", "Semantic Search"],
"deltav": true
}

0 comments on commit cb9d11c

Please sign in to comment.