Định nghĩa ranh giới công cụ
Cung cấp cho đại lý của bạn công cụ nhỏ với đầu vào và đầu ra rõ ràng: tìm kiếm web, discover Các video, và đọc các bản ghi âm của băng video. Người đại diện của anh chọn khi nào gọi cho họ và cách sử dụng bằng chứng.
Mã bên dưới cung cấp chức năng phục hồi. Đăng ký chúng với khuôn khổ tác nhân hiện có của bạn; chúng hoạt động độc lập trong thư viện bạn chọn.
# Replace YOUR_API_KEY near the top of the code below.Thi hành công cụ thu hồi
Những điều sau Python 3 Bộ mã dùng thư viện chuẩn. Hãy giữ lấy nó. tools.py. Chạy nó trực tiếp làm cho một Google Yêu cầu tìm kiếm; nhập nó cho phép ứng dụng của bạn đăng ký ba chức năng làm công cụ.
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.pyKết nối các công cụ với đại diện của bạn
Mô tả tham số và kết quả của từng công cụ trong lược đồ công cụ của framework. Giữ query, video_id và language là dữ liệu nhập rõ ràng để mô hình có thể yêu cầu dữ liệu cần thiết.
search_web(query)trở lại Google kết quả với nguồn URLs và những mảnh vụn.find_videos(query)video trở lại và các IDs Cần thiết cho yêu cầu tiếp theo.read_video_transcript(video_id, language)trả lại các đoạn phụ đề có nhãn thời gian.
Trở về các lỗi công cụ có ích khi yêu cầu bị lỗi hoặc không có đường dẫn tiêu đề. Hãy áp dụng ngân sách và tín dụng của bạn, và để người đại diện quyết định thử một yêu cầu khác hoặc dùng một nguồn khác.
Giữ dấu thời gian
Đoạn tìm kiếm là ô xem thử trang. Bảo tồn URL của nó và tránh xem những mảnh vụn là bài viết đầy đủ. Để ghi chép, hãy giữ lại video IDNgôn ngữ và phân đoạn lần để tác nhân có thể trích dẫn đoạn văn gốc.
Để có được một đoạn phim về kiến thức, hãy lưu giữ tên video và đường dẫn thời gian được dán nhãn với mỗi đoạn văn. Hệ thống thu hồi của bạn có thể trả lại nội dung và tham khảo được dùng để hỗ trợ một câu trả lời.