उपकरण सीमा को परिभाषित करें
अपने एजेंट को स्पष्ट इनपुट और आउटपुट के साथ छोटे उपकरण दें: वेब, discover वीडियो खोजें और एक वीडियो के उपलब्ध ट्रांसक्रिप्ट को पढ़ें। आपका एजेंट उन्हें कॉल करने और सबूत का उपयोग करने के लिए कैसे चुनता है।
नीचे कोड पुनर्प्राप्ति कार्यों की आपूर्ति करता है। उन्हें अपने मौजूदा एजेंट फ्रेमवर्क के साथ पंजीकृत करें; वे स्वतंत्र रूप से काम करते हैं, जिसमें से मॉडल या ऑर्केस्ट्रेशन लाइब्रेरी आप चुनती हैं।
# Replace YOUR_API_KEY near the top of the code below.पुनर्प्राप्ति उपकरण लागू करना
निम्नलिखित Python 3 कोड मानक पुस्तकालय का उपयोग करता है। इसे सेव करें tools.py. यह सीधे एक Google खोज अनुरोध बनाता है; यह आयात करने से आपके आवेदन को उपकरण के रूप में तीन कार्यों को पंजीकृत करने की अनुमति मिलती है।
import json
import gzip
import os
import urllib.error
import urllib.request
API_URL = "https://data.flashdata.dev"
API_KEY = "YOUR_API_KEY"
def api_request(path, payload=None, response_headers=None, base_url=API_URL):
target = path if path.startswith(("https://", "http://")) else base_url + path
request = urllib.request.Request(
target,
data=json.dumps(payload).encode() if payload is not None else None,
headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
method="POST" if payload is not None else "GET",
)
with urllib.request.urlopen(request, timeout=60) as response:
if response_headers is not None:
response_headers.update(response.headers)
return json.load(response)
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, request, file_pointer, code, message, headers, new_url):
return None
def result_request(url):
request = urllib.request.Request(url, headers={"X-API-Key": API_KEY}, method="GET")
try:
with urllib.request.build_opener(NoRedirect).open(request, timeout=60):
raise RuntimeError("Results endpoint did not redirect to storage")
except urllib.error.HTTPError as response:
if response.code != 302 or not response.headers.get("Location"):
raise
storage_url = response.headers["Location"]
response.close()
storage_request = urllib.request.Request(storage_url, headers={"Accept-Encoding": "gzip"})
with urllib.request.urlopen(storage_request, timeout=60) as response:
body = response.read()
if response.headers.get("Content-Encoding") == "gzip":
body = gzip.decompress(body)
return json.loads(body)["results"]
def search_web(query):
"""Search the web and return titles, URLs and result snippets."""
response = api_request("/v1/queries/realtime", {
"source": "google_search", "query": query,
})
return response["results"][0]
def find_videos(query):
"""Discover YouTube videos and their video IDs."""
response = api_request("/v1/queries/realtime", {
"source": "youtube_search", "query": query,
"params": {"max_results": 5},
})
return response["results"][0]["results"]
def read_video_transcript(video_id, language="en"):
"""Read available captions with timestamps; captions may be unavailable."""
response = api_request("/v1/queries/realtime", {
"source": "youtube_transcript", "video_id": video_id,
"params": {"language": language, "auto": True},
})
return response["results"][0]
# Register these functions as tools in your own agent framework.
# The agent chooses when to call them and how to use the evidence.
if __name__ == "__main__":
print(json.dumps(search_web("distributed systems"), indent=2))
python3 tools.pyउपकरण को अपने एजेंट से कनेक्ट करें
प्रत्येक उपकरण के तर्कों का वर्णन करें और अपने ढांचे के टूल स्कीमा में परिणाम दें। रखना query, video_id और language चूंकि स्पष्ट इनपुट इसलिए मॉडल उस डेटा की आवश्यकता का अनुरोध कर सकता है।
search_web(query)Google के परिणाम स्रोत URLs और स्निपेट्स के साथ मिलते हैं।find_videos(query)रिटर्न वीडियो और IDs फ़ॉलो-अप अनुरोधों के लिए आवश्यक है।read_video_transcript(video_id, language)टाइमस्टैम्प के साथ उपलब्ध कैप्शन सेगमेंट लौटाएं।
जब कोई अनुरोध विफल हो जाता है या एक कैप्शन ट्रैक अनुपलब्ध हो जाता है तो उपयोगी टूल त्रुटियों को वापस करें। अपने आवेदन के क्रेडिट और अनुरोध बजट को लागू करें और एजेंट को यह तय करने दें कि क्या एक अलग क्वेरी की कोशिश की जाए या किसी अन्य स्रोत का उपयोग किया जाए।
सूत्रों और टाइमटैम्प्स को रखें
एक खोज स्निपेट एक पृष्ठ का पूर्वावलोकन है। अपने URL को संरक्षित करें और पूरे लेख के रूप में स्निपेट के इलाज से बचें। ट्रांसक्रिप्ट्स के लिए, वीडियो ID, भाषा और सेगमेंट टाइम को बनाए रखें ताकि एजेंट मूल मार्ग को उद्धृत कर सके।
एक वीडियो ज्ञान आधार के लिए, प्रत्येक टेक्स्ट चंक के साथ वीडियो शीर्षक और टाइमटाम्प्ड सोर्स लिंक को स्टोर करें। इसके बाद आपकी पुनर्प्राप्ति प्रणाली दोनों सामग्री और संदर्भ को वापस कर सकती है जिसका उपयोग उत्तर का समर्थन करने के लिए किया जाता है।