تحديد حدود الأداة
أعطوا وكيلكم أدوات صغيرة بمدخلات ونواتج واضحة: ابحثوا في الشبكة، وأشرطة فيديو discover، وقرأوا نسخة مستنسخة بالفيديو. عميلك يختار متى يتصل بهم وكيف يستخدم الأدلة
والرمز الوارد أدناه يزود مهام الاسترجاع. سجلهم بإطار عملائك الحالي، يعملون بشكل مستقل عن أيّ مكتبة نموذجية أو مركزية تختارها.
# Replace YOUR_API_KEY near the top of the code below.تنفيذ أدوات الاسترجاع
The following Python 3 code uses the standard library. احفظه كما هو 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 وSnippets.find_videos(query)وتعيد أشرطة الفيديو والرقم IDs اللازمة لطلبات المتابعة.read_video_transcript(video_id, language)يُعيدُ الأجزاءَ المُتاحَةَ من الأزمانِ.
إعادة أخطاء الأدوات المفيدة عندما يفشل الطلب أو لا يكون هناك مسار للاختفاء. تطبيق ائتمانات طلبكم وميزانيات طلبكم، والسماح للوكيل بأن يقرر ما إذا كان سيحاول إجراء استفسار مختلف أو يستخدم مصدرا آخر.
الحفاظ على المصادر والأوقات
قنبلة بحث هي عرض للصفحة. حافظوا على هذا URL وتجنب معاملة الدمية على أنها المادة الكاملة. بالنسبة للمحاضرات، احتفظ بالفيديو ID، واللغة، وفترات جزئية حتى يمكن للوكيل أن يستشهد بالمقطع الأصلي.
من أجل قاعدة معرفية بالفيديو، تخزين عنوان الفيديو وربطة المصدر المزمن مع كل قطعة نصية. نظام استرجاعك يمكن أن يعيد كل من المحتوى والإشارة المستخدمة لدعم إجابة.