Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import sys
|
|
@@ -55,28 +56,7 @@ def web_search(query: str, k: int = 6) -> str:
|
|
| 55 |
hits = ddg_search(query, k)
|
| 56 |
if hits:
|
| 57 |
return json.dumps(hits, ensure_ascii=False)
|
| 58 |
-
except Exception: # fall through to Tavily
|
| 59 |
-
pass
|
| 60 |
|
| 61 |
-
try:
|
| 62 |
-
tavily_results = TavilySearchResults(
|
| 63 |
-
max_results=5,
|
| 64 |
-
# include_answer=True,
|
| 65 |
-
# search_depth="advanced",
|
| 66 |
-
)
|
| 67 |
-
search_result = tavily_results.invoke({"query": query})
|
| 68 |
-
print(
|
| 69 |
-
f"[TOOL] TAVILY search is triggered with following response: {search_result}"
|
| 70 |
-
)
|
| 71 |
-
formatted = [
|
| 72 |
-
{
|
| 73 |
-
"title": d.get("title", "")[:500],
|
| 74 |
-
"snippet": d.get("content", "")[:750],
|
| 75 |
-
"link": d.get("url", "")[:300],
|
| 76 |
-
}
|
| 77 |
-
for d in search_result
|
| 78 |
-
]
|
| 79 |
-
return json.dumps(formatted, ensure_ascii=False)
|
| 80 |
except Exception as exc:
|
| 81 |
return f"search_error:{exc}"
|
| 82 |
|
|
@@ -105,22 +85,18 @@ def get_youtube_transcript(video_url: str) -> str:
|
|
| 105 |
return "TRANSCRIPT_UNAVAILABLE"
|
| 106 |
|
| 107 |
@tool
|
| 108 |
-
def describe_image(img_bytes, question: str) -> str:
|
| 109 |
"""Use a vision model to interpret or answer questions about an image file.
|
| 110 |
|
| 111 |
Args:
|
| 112 |
-
|
| 113 |
question: Specific question to ask about the image content.
|
| 114 |
|
| 115 |
Returns:
|
| 116 |
A text description or answer about the image content.
|
| 117 |
"""
|
| 118 |
-
|
| 119 |
-
mime_map = {"jpg": "image/jpeg", "jpeg": "image/jpeg", "png": "image/png",
|
| 120 |
-
"gif": "image/gif", "webp": "image/webp"}
|
| 121 |
mime_type = "image/png"
|
| 122 |
-
|
| 123 |
-
image_data = base64.standard_b64encode(img_bytes.read()).decode("utf-8")
|
| 124 |
|
| 125 |
payload = {
|
| 126 |
"model": "nvidia/nemotron-nano-12b-v2-vl:free",
|
|
|
|
| 1 |
+
import io
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
import sys
|
|
|
|
| 56 |
hits = ddg_search(query, k)
|
| 57 |
if hits:
|
| 58 |
return json.dumps(hits, ensure_ascii=False)
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as exc:
|
| 61 |
return f"search_error:{exc}"
|
| 62 |
|
|
|
|
| 85 |
return "TRANSCRIPT_UNAVAILABLE"
|
| 86 |
|
| 87 |
@tool
|
| 88 |
+
def describe_image(img_bytes: bytes, question: str) -> str:
|
| 89 |
"""Use a vision model to interpret or answer questions about an image file.
|
| 90 |
|
| 91 |
Args:
|
| 92 |
+
img_bytes: Raw image bytes.
|
| 93 |
question: Specific question to ask about the image content.
|
| 94 |
|
| 95 |
Returns:
|
| 96 |
A text description or answer about the image content.
|
| 97 |
"""
|
|
|
|
|
|
|
|
|
|
| 98 |
mime_type = "image/png"
|
| 99 |
+
image_data = base64.standard_b64encode(img_bytes).decode("utf-8")
|
|
|
|
| 100 |
|
| 101 |
payload = {
|
| 102 |
"model": "nvidia/nemotron-nano-12b-v2-vl:free",
|