File size: 2,729 Bytes
f6fc677
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
from llama_index.core.tools import FunctionTool
from llama_index.tools.tavily_research import TavilyToolSpec
from llama_index.readers.wikipedia import WikipediaReader
from llama_index.readers.papers import ArxivReader
from llama_index.core.tools.ondemand_loader_tool import OnDemandLoaderTool
from huggingface_hub import list_models

# Math functions

def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    return a * b

def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b

def subtract(a: int, b: int) -> int:
    """Subtract two numbers."""
    return a - b

def divide(a: int, b: int) -> int:
    """Divide two numbers."""
    if b == 0:
        raise ValueError("Cannot divide by zero.")
    return a / b

def modulus(a: int, b: int) -> int:
    """Get the modulus of two numbers."""
    return a % b

# Hugging Face Hub stats tool

def get_hub_stats(author: str) -> str:
    """Fetches the most downloaded model from a specific author on the Hugging Face Hub."""
    try:
        models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
        if models:
            model = models[0]
            return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads."
        else:
            return f"No models found for author {author}."
    except Exception as e:
        return f"Error fetching models for {author}: {str(e)}"

# Tool initializations (as functions to be called in agent)

def get_tavily_tool():
    return TavilyToolSpec(api_key=os.getenv("TAVILY_API_KEY"))

def get_arxiv_reader():
    return ArxivReader()

def get_wikipedia_reader():
    return WikipediaReader()

def get_wikipedia_tool(wikipedia_reader=None):
    if wikipedia_reader is None:
        wikipedia_reader = get_wikipedia_reader()
    return OnDemandLoaderTool.from_defaults(
        wikipedia_reader,
        name="Wikipedia Tool",
        description="A tool for loading data and querying articles from Wikipedia",
    )

def get_arxiv_tool(arxiv_reader=None):
    if arxiv_reader is None:
        arxiv_reader = get_arxiv_reader()
    return OnDemandLoaderTool.from_defaults(
        arxiv_reader,
        name="Arxiv Tool",
        description="A tool for loading data and querying articles from Arxiv",
    )

def get_search_tool():
    return get_tavily_tool().to_tool_list()

def get_calculator_tool():
    return [
        FunctionTool.from_defaults(multiply),
        FunctionTool.from_defaults(add),
        FunctionTool.from_defaults(subtract),
        FunctionTool.from_defaults(divide),
        FunctionTool.from_defaults(modulus),
    ]

def get_hub_stats_tool():
    return FunctionTool.from_defaults(get_hub_stats)