Exa Tools

from exa_py import Exa
from praisonai_tools import BaseTool

class ExaSearch:
    name: str = "ExaSearch"
    description: str = "Perform a search using this ExaSearch tool and returns search results with url"

    def __init__(self, api_key: str):
        self.exa = Exa(api_key=api_key)

    def run(self, query: str):
        results = self.exa.search_and_contents(
            query,
            text={"include_html_tags": True, "max_characters": 1000},
        )
        return results


class ExaSimilar:
    name: str = "ExaSimilar"
    description: str = "Search for webpages similar to a given URL using ExaSimilar tool"

    def __init__(self, api_key: str):
        self.exa = Exa(api_key=api_key)

    def run(self, url: str):
        """Search for webpages similar to a given URL.
        The url passed in should be a URL returned from `search`.
        """
        results = self.exa.find_similar(url, num_results=3)
        return results


class ExaContents:
    name: str = "ExaContents"
    description: str = "Get the contents of a webpage using a list of urls using ExaContents tool"

    def __init__(self, api_key: str):
        self.exa = Exa(api_key=api_key)

    def run(self, ids: list):
        """Get the contents of a webpage.
        The ids must be passed in as a list, a list of ids returned from `search`.
        """
        contents = self.exa.get_contents(ids)
        contents_str = str(contents)
        split_contents = contents_str.split("URL:")
        trimmed_contents = [content[:1000] for content in split_contents]
        return "\n\n".join(trimmed_contents)

# Example usage
if __name__ == "__main__":
    api_key = "Enter your exa key"
    tool = ExaSearch(api_key=api_key)
    search_query = "latest AI News"
    search_results = tool.run(search_query)
    print("Search Results:", search_results)

    # Find similar webpages
    find_similar_url = "https://boomi.com/"  # Valid URL
    similar_results = tool._run_similar(find_similar_url)
    print("Similar Results:", similar_results)

    # Get contents using ids
    content_ids = ["tesla.com"]  # Replace with actual IDs
    contents = tool._run_get_contents(content_ids)
    print("Contents:", contents)

Leave a comment

Your email address will not be published. Required fields are marked *