Aracın sınırlarını Tanımlayın
Ajanınızı net giriş ve çıkışlarla küçük araçları verin: web, discover videoları arayın ve bir videonun mevcut transkriptini okuyun. Ajanınız onları çağırmak ve kanıtları nasıl kullanacağınızı seçer.
Aşağıdaki kod retrieval fonksiyonlarını sağlar. Mevcut acente çerçevenizle kayıt olun; hangisini seçtiğiniz model veya orkestratasyon kütüphanesinden bağımsız olarak çalışırlar.
# Replace YOUR_API_KEY near the top of the code below.Retrieval aletlerini uygulama
Aşağıdaki Python 3 kodu standart kütüphaneyi kullanır. Onu kurtarmak olarak tools.py. Doğrudan bir Google arama isteği yapar; uygulamanızın üç işlevi araç olarak kaydetmesine izin verir.
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.pyAracınızı acentenize bağlayın
Her aracın argümanlarını ve sonuçlarını çerçevenizin araç şemanızda açıklayın. Keep Keep Keep query, video_id Ve ve language Açık girişler olarak, model ihtiyacı olan verileri isteyebilir.
search_web(query)Google sonuçları kaynak URLs parçaları ile döndürür.find_videos(query)Videoları döndürür ve IDs takip talepleri için gereklidir.read_video_transcript(video_id, language)Boş zamanlar ile mevcut satır segmentleri döndürür.
Bir istek başarısız olduğunda veya bir satır pisti kullanılamadığında faydalı araç hataları döndürür. Uygulamanızın kredisini uygulayın ve bütçe talep edin ve acentenin farklı bir sorguya denemek veya başka bir kaynağı kullanmak isteyip karar vermesine izin verin.
Kaynakları ve zamanları tutun
Bir arama parçaları bir sayfanın bir önizlemesidir. URL'unu koru ve parçaları tam makale olarak tedavi etmekten kaçın. Transkriptler için, videoyu ID, dil ve segment kez tutun, böylece ajan orijinal pasajı silebilir.
Bir video bilgisi üssü için, video unvanı ve her metin chunk ile kesintili kaynak bağlantısını saklayın. Retrieval sisteminiz sonra hem içeriği hem de bir cevabı desteklemek için kullanılan referansı geri döndürebilir.