added tools
Browse files- zekee-agent.py +115 -0
zekee-agent.py
CHANGED
|
@@ -7,3 +7,118 @@ from google.genai import types
|
|
| 7 |
import wikipedia as wiki
|
| 8 |
from markdownify import markdownify as to_markdown
|
| 9 |
from functools import lru_cache
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import wikipedia as wiki
|
| 8 |
from markdownify import markdownify as to_markdown
|
| 9 |
from functools import lru_cache
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# --- Tools to use---
|
| 14 |
+
|
| 15 |
+
class VideoWatchingTool(Tool):
|
| 16 |
+
name = "watch_video"
|
| 17 |
+
description = "Watch a video and answer a question about its content."
|
| 18 |
+
inputs = {
|
| 19 |
+
"video_url": {"type": "string", "description": "URL of the video."},
|
| 20 |
+
"user_query": {"type": "string", "description": "What to answer about the video."}
|
| 21 |
+
}
|
| 22 |
+
output_type = "string"
|
| 23 |
+
|
| 24 |
+
def __init__(self, model_name, **kwargs):
|
| 25 |
+
super().__init__(**kwargs)
|
| 26 |
+
self.model_name = model_name
|
| 27 |
+
|
| 28 |
+
def forward(self, video_url: str, user_query: str) -> str:
|
| 29 |
+
payload = {
|
| 30 |
+
"model": f"models/{self.model_name}",
|
| 31 |
+
"contents": [
|
| 32 |
+
{"fileData": {"fileUri": video_url}},
|
| 33 |
+
{"text": f"Please watch the entire video and then answer: {user_query}"}
|
| 34 |
+
]
|
| 35 |
+
}
|
| 36 |
+
api_url = (
|
| 37 |
+
f"https://generativelanguage.googleapis.com/v1beta/models/"
|
| 38 |
+
f"{self.model_name}:generateContent?key='AIzaSyDz6V86ekxxKPYZiX1UaqMfmZb8fNMjuJk'"
|
| 39 |
+
)
|
| 40 |
+
r = requests.post(api_url, json=payload, timeout=30)
|
| 41 |
+
r.raise_for_status()
|
| 42 |
+
parts = r.json()["candidates"][0]["content"]["parts"]
|
| 43 |
+
return "".join(p.get("text", "") for p in parts).strip()
|
| 44 |
+
|
| 45 |
+
class GoogleSearchTool(Tool):
|
| 46 |
+
name = "google_search"
|
| 47 |
+
description = "Perform a quick Google search and return the top snippets."
|
| 48 |
+
inputs = {"query": {"type": "string", "description": "Search query."}}
|
| 49 |
+
output_type = "string"
|
| 50 |
+
|
| 51 |
+
def __init__(self, client, model_name, **kwargs):
|
| 52 |
+
super().__init__(**kwargs)
|
| 53 |
+
self.client = client
|
| 54 |
+
self.model_name = model_name
|
| 55 |
+
|
| 56 |
+
def forward(self, query: str) -> str:
|
| 57 |
+
# Use genai.Tool for structured search
|
| 58 |
+
tool = types.Tool(google_search=types.GoogleSearch())
|
| 59 |
+
resp = self.client.models.generate_content(
|
| 60 |
+
model=self.model_name,
|
| 61 |
+
contents=f"Search the web for: {query}",
|
| 62 |
+
config=types.GenerateContentConfig(
|
| 63 |
+
tools=[tool],
|
| 64 |
+
response_modalities=["TEXT"]
|
| 65 |
+
)
|
| 66 |
+
)
|
| 67 |
+
return resp.text.strip()
|
| 68 |
+
|
| 69 |
+
class WikipediaTitleSearchTool(Tool):
|
| 70 |
+
name = "check_wikipedia_page_titles"
|
| 71 |
+
description = "Search Wikipedia and return a comma-separated list of matching page titles."
|
| 72 |
+
inputs = {"query": {"type": "string", "description": "Search term."}}
|
| 73 |
+
output_type = "string"
|
| 74 |
+
|
| 75 |
+
@lru_cache(maxsize=128)
|
| 76 |
+
def forward(self, query: str) -> str:
|
| 77 |
+
results = wiki.search(query)
|
| 78 |
+
return ", ".join(results) if results else "No results found."
|
| 79 |
+
|
| 80 |
+
class WikipediaPageTool(Tool):
|
| 81 |
+
name = "get_wikipedia_page"
|
| 82 |
+
description = "Fetch a Wikipedia page and return its Markdown-ified content."
|
| 83 |
+
inputs = {"page_title": {"type": "string", "description": "Exact page title."}}
|
| 84 |
+
output_type = "string"
|
| 85 |
+
|
| 86 |
+
@lru_cache(maxsize=64)
|
| 87 |
+
def forward(self, page_title: str) -> str:
|
| 88 |
+
try:
|
| 89 |
+
page = wiki.page(page_title)
|
| 90 |
+
except wiki.exceptions.PageError:
|
| 91 |
+
return f"Page '{page_title}' not found."
|
| 92 |
+
return to_markdown(page.html())
|
| 93 |
+
|
| 94 |
+
class FileAttachmentQueryTool(Tool):
|
| 95 |
+
name = "run_query_with_file"
|
| 96 |
+
description = "Download an attached file and answer a question about it."
|
| 97 |
+
inputs = {
|
| 98 |
+
"task_id": {"type": "string", "description": "ID to download the file."},
|
| 99 |
+
"mime_type": {"type": "string", "nullable": True, "description": "Optional MIME type."},
|
| 100 |
+
"user_query": {"type": "string", "description": "Question about the file."}
|
| 101 |
+
}
|
| 102 |
+
output_type = "string"
|
| 103 |
+
|
| 104 |
+
def __init__(self, client, model_name, **kwargs):
|
| 105 |
+
super().__init__(**kwargs)
|
| 106 |
+
self.client = client
|
| 107 |
+
self.model_name = model_name
|
| 108 |
+
|
| 109 |
+
def forward(self, task_id: str, mime_type: Optional[str], user_query: str) -> str:
|
| 110 |
+
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 111 |
+
resp = requests.get(url, timeout=15)
|
| 112 |
+
resp.raise_for_status()
|
| 113 |
+
data = resp.content
|
| 114 |
+
mime = mime_type or resp.headers.get("Content-Type", "application/octet-stream")
|
| 115 |
+
response = self.client.models.generate_content(
|
| 116 |
+
model=self.model_name,
|
| 117 |
+
contents=[
|
| 118 |
+
types.Part.from_bytes(data=data, mime_type=mime),
|
| 119 |
+
types.Part(text=user_query),
|
| 120 |
+
]
|
| 121 |
+
)
|
| 122 |
+
return response.text.strip()
|
| 123 |
+
|
| 124 |
+
|