กําหนดขอบเขตเครื่องมือ
ให้เครื่องมือเล็กๆ กับเอเจ้นท์ของคุณ ที่มีการป้อนข้อมูลและผลลัพธ์ที่ชัดเจน: สืบค้นหน้าเว็บ 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 ภาษาและส่วนต่าง ๆ ไว้ เพื่อให้ตัวแทนสามารถอ้างอิงข้อความต้นฉบับได้
สําหรับ ข้อมูล พื้น ฐาน ใน วี ดิ ทัศน์ จง เก็บ ชื่อ เสียง และ การ เชื่อม โยง แหล่ง เวลา ไว้ กับ เนื้อ ความ แต่ ละ ส่วน. แล้ว ระบบ ที่ คุณ จะ กู้ มา ก็ จะ สามารถ คืน ทั้ง เนื้อหา และ ข้อ อ้างอิง ที่ ใช้ เพื่อ สนับสนุน คํา ตอบ.