wlchee commited on
Commit
96f906d
·
verified ·
1 Parent(s): 81917a3

Upload agent.py

Browse files
Files changed (1) hide show
  1. agent.py +101 -0
agent.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import Tool, HfAgent
2
+ from huggingface_hub import list_models
3
+ import requests
4
+ from typing import Optional, List
5
+ import random
6
+
7
+ # First, let's define some custom tools the agent can use
8
+
9
+ class WebSearchTool(Tool):
10
+ name = "web_search"
11
+ description = ("A tool that performs a web search using a search engine API. "
12
+ "Input should be a search query. Output will be search results.")
13
+
14
+ inputs = ["text"]
15
+ outputs = ["text"]
16
+
17
+ def __call__(self, query: str):
18
+ # In a real implementation, you would call a search API here
19
+ # For demonstration, we'll return mock results
20
+ return f"Search results for '{query}': 1. Relevant result 1, 2. Relevant result 2"
21
+
22
+ class CalculatorTool(Tool):
23
+ name = "calculator"
24
+ description = ("A tool for performing mathematical calculations. "
25
+ "Input should be a mathematical expression. Output will be the result.")
26
+
27
+ inputs = ["text"]
28
+ outputs = ["text"]
29
+
30
+ def __call__(self, expression: str):
31
+ try:
32
+ result = eval(expression) # Note: In production, use a safer eval method
33
+ return str(result)
34
+ except:
35
+ return "Error: Could not evaluate the expression"
36
+
37
+ class CurrentTimeTool(Tool):
38
+ name = "get_current_time"
39
+ description = ("A tool that returns the current time in UTC. "
40
+ "No input needed. Output will be the current time.")
41
+
42
+ inputs = []
43
+ outputs = ["text"]
44
+
45
+ def __call__(self):
46
+ from datetime import datetime
47
+ return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
48
+
49
+ class WikipediaTool(Tool):
50
+ name = "wikipedia_search"
51
+ description = ("A tool that searches Wikipedia. "
52
+ "Input should be a search term. Output will be a summary from Wikipedia.")
53
+
54
+ inputs = ["text"]
55
+ outputs = ["text"]
56
+
57
+ def __call__(self, term: str):
58
+ # Mock implementation - in real use, you'd call the Wikipedia API
59
+ return f"Wikipedia summary for '{term}': This is a summary about {term}."
60
+
61
+ # Now let's create the agent with these tools and some pre-trained tools
62
+
63
+ def create_agent():
64
+ # Load pre-trained tools from the Hub
65
+ agent = HfAgent(
66
+ "https://api-inference.huggingface.co/models/bigcode/starcoder",
67
+ additional_tools=[
68
+ WebSearchTool(),
69
+ CalculatorTool(),
70
+ CurrentTimeTool(),
71
+ WikipediaTool()
72
+ ],
73
+ # These parameters help with performance
74
+ max_new_tokens=200,
75
+ temperature=0.7,
76
+ top_p=0.9,
77
+ )
78
+ return agent
79
+
80
+ # Example usage of the agent
81
+ if __name__ == "__main__":
82
+ agent = create_agent()
83
+
84
+ # Test the agent with some sample queries
85
+ queries = [
86
+ "What's the capital of France?",
87
+ "Calculate 123 * 45",
88
+ "What time is it now?",
89
+ "Tell me about Albert Einstein",
90
+ "Search for the latest news about AI"
91
+ ]
92
+
93
+ for query in queries:
94
+ print(f"Query: {query}")
95
+ result = agent.run(query)
96
+ print(f"Result: {result}\n")
97
+
98
+ # To evaluate on the benchmark, you would use:
99
+ # from transformers.benchmarks import evaluate_agent
100
+ # benchmark_score = evaluate_agent(agent)
101
+ # print(f"Benchmark score: {benchmark_score}")