File size: 3,011 Bytes
403840c
e361dab
20d36d3
 
 
 
403840c
 
 
 
93350d8
 
 
 
 
403840c
 
 
93350d8
 
 
 
 
 
403840c
 
 
93350d8
 
 
 
 
 
403840c
 
 
93350d8
 
 
 
 
 
403840c
 
 
 
 
93350d8
 
 
 
 
 
403840c
b41276c
 
f0dc3e1
20d36d3
 
 
 
 
 
 
 
 
f27e78c
583f1bc
f27e78c
 
 
 
 
 
 
 
 
20d36d3
f27e78c
20d36d3
 
25ab983
20d36d3
f27e78c
583f1bc
f27e78c
 
 
 
 
 
 
 
 
 
 
 
0bec91d
f27e78c
 
 
25ab983
20d36d3
403840c
d80498a
b31a011
403840c
 
 
 
 
 
 
f0dc3e1
20d36d3
25ab983
 
403840c
 
 
54410b0
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import json
import os
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, tool, InferenceClientModel, Tool
from duckduckgo_search import DDGS
import requests
from bs4 import BeautifulSoup

#========Tools========#
@tool
def multiply(a: int, b: int) -> int:
    """Multiply two numbers.
    Args:
        a: first int
        b: second int
    """
    return a * b
@tool
def add(a: int, b: int) -> int:
    """Add two numbers.
    
    Args:
        a: first int
        b: second int
    """
    return a + b
@tool
def subtract(a: int, b: int) -> int:
    """Subtract two numbers.
    
    Args:
        a: first int
        b: second int
    """
    return a - b
@tool
def divide(a: int, b: int) -> int:
    """Divide two numbers.
    
    Args:
        a: first int
        b: second int
    """
    if b == 0:
        raise ValueError("Cannot divide by zero.")
    return a / b
@tool
def modulus(a: int, b: int) -> int:
    """Get the modulus of two numbers.
    
    Args:
        a: first int
        b: second int
    """
    return a % b

search_tool = DuckDuckGoSearchTool()
web_search = VisitWebpageTool()

def process_tool_output(tool_output):
    """Convert tool output to a string."""
    if isinstance(tool_output, list):
        return '\n'.join([item.get('title', str(item)) for item in tool_output if item]) or 'No results found'
    elif isinstance(tool_output, dict):
        return tool_output.get('title', str(tool_output))
    return str(tool_output)

@tool
def CustomDuckDuckGoSearchTool(query: str) -> str:
    """
    Perform a DuckDuckGo search and return a string result.

    Args:
        query: the question you input
    """
    try:
        with DDGS() as ddgs:
            results = list(ddgs.text(query, max_results=3))  # Convert generator to list
            return process_tool_output(results)
    except Exception as e:
            print(f"Error in DuckDuckGoSearchTool: {e}")
            return f"Search error: {e}"


@tool
def CustomVisitWebpageTool(query: str) -> str:
    """
    Visit a webpage and extract content as a string.

    Args:
        query: the question you input
    """
    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()
        soup = BeautifulSoup(response.text, 'html.parser')
        # Extract text from the webpage
        text = soup.get_text(separator=' ', strip=True)
        return text[:100]  # Limit output length to avoid overload
    except Exception as e:
        print(f"Error in VisitWebpageTool: {e}")
        return f"Webpage access error: {e}"


#=======Agent========#
hf_token = os.environ.get("HF_TOKEN")
model = InferenceClientModel(token = hf_token, model_id = 'meta-llama/Llama-2-7b-chat-hf', provider = "auto")
agent = CodeAgent(
    tools=[
    multiply,
    add,
    subtract,
    divide,
    modulus,
    search_tool,
    web_search,
   CustomDuckDuckGoSearchTool,
   CustomVisitWebpageTool],
    model = model,
    add_base_tools=True,
    planning_interval=3
)