คำตอบสั้น
ฉันจะมอบเครื่องมือค้นหาเว็บให้ agent ของ LangChain ได้อย่างไร
ตกแต่งฟังก์ชัน Python ด้วย @tool จาก langchain_core.tools ให้มันเรียก https://serpcrypto.com/api/v1/search และคืนข้อความสั้น ๆ จากนั้นส่งเข้าไปใน tools=[...] เมื่อคุณสร้าง agent ด้วย SerpCrypto การค้นหาแต่ละครั้งที่ agent รันจะใช้ 1 เครดิต เริ่มต้นที่ US$0.30/1K การค้นหา และการเรียกที่ล้มเหลวไม่มีค่าใช้จ่าย เครดิตจ่ายด้วยคริปโต ไม่ต้อง KYC
ฉันจะเขียน tool ค้นหาได้อย่างไร
tool ของ LangChain คือฟังก์ชันที่มี type และ docstring ตัว decorator จะแปลง signature เป็น schema ของอาร์กิวเมนต์ และแปลง docstring เป็นคำอธิบายที่โมเดลอ่าน ดังนั้นทั้งสองส่วนจึงควรใส่ใจ ตั้งชื่ออาร์กิวเมนต์ในแบบที่โมเดลควรคิดถึงมัน: query, country, language
คืนข้อความเป็นข้อความ ไม่ใช่ JSON ทั้งก้อน ผลลัพธ์ห้ารายการพร้อม title, link และ snippet ให้ข้อมูลกับโมเดลเพียงพอที่จะตอบและอ้างอิง ด้วยจำนวน token เพียงเศษเสี้ยวของคำตอบดิบ คืนข้อความสั้น ๆ เมื่อเกิดข้อผิดพลาดแทนที่จะ raise เพื่อให้ agent อธิบายหรือลองใหม่ได้
"""A LangChain tool that searches Google through the SerpCrypto SERP API."""
import os
import requests
from langchain_core.tools import tool
API_URL = "https://serpcrypto.com/api/v1/search"
@tool
def google_search(query: str, gl: str = "us", hl: str = "en") -> str:
"""Search Google for current information. Returns the top results with title, link and snippet.
Args:
query: the search query, as a person would type it.
gl: two-letter country code of the results, for example us, gb or de.
hl: language code, for example en, de or pt-BR.
"""
r = requests.get(
API_URL,
params={"q": query, "gl": gl, "hl": hl},
headers={"Authorization": "Bearer " + os.environ["SERPCRYPTO_API_KEY"]},
timeout=90,
)
if r.status_code != 200:
return f"Search failed with HTTP {r.status_code}."
results = r.json().get("organic_results", [])[:5]
return "\n\n".join(f"{x['title']}\n{x['link']}\n{x.get('snippet', '')}" for x in results) or "No results."ฉันจะปล่อยให้ agent ตัดสินใจเองว่าเมื่อใดควรค้นหาได้อย่างไร
ส่ง tool ให้กับ create_agent ซึ่งมีให้ใช้ตั้งแต่ LangChain 1.0 โมเดลจะอ่านคำอธิบายและเรียก tool เมื่อคำถามต้องการข้อเท็จจริงใหม่ ๆ ตั้งค่า AGENT_MODEL เป็นสตริงโมเดลที่บัญชีของคุณรองรับ เช่น openai: หรือ anthropic: ตามด้วยชื่อโมเดล โมเดลแชตใดก็ได้ที่รองรับ tool calling ใช้ได้
system prompt สำคัญพอ ๆ กับโค้ด การขอให้ agent ค้นหาเฉพาะข้อมูลที่ใหม่กว่าข้อมูลฝึกของมัน และให้อ้างอิงลิงก์ ช่วยให้คำตอบมีหลักฐานรองรับและจำกัดจำนวนการค้นหาที่เสียเงิน
"""An agent that decides when to search (LangChain 1.0 or later)."""
import os
from langchain.agents import create_agent
from search_tool import google_search
agent = create_agent(
model=os.environ["AGENT_MODEL"], # a provider:model string, such as one of your OpenAI or Anthropic models
tools=[google_search],
system_prompt="Answer with current facts. Use google_search when you need information newer than your training data, and cite the links you used.",
)
result = agent.invoke({"messages": [{"role": "user", "content": "What changed in Google's search results pages this year?"}]})
print(result["messages"][-1].content)ฉันจะทำแบบเดียวกันกับ LlamaIndex ได้อย่างไร
เขียนฟังก์ชันธรรมดาพร้อม type hints และ docstring จากนั้นห่อด้วย FunctionTool.from_defaults LlamaIndex จะอ่านชื่อ, type hints และ docstring เพื่อสร้าง schema ของ tool เช่นเดียวกับ LangChain เพิ่ม tool นี้ให้ agent ของ LlamaIndex ตัวใดก็ได้ที่รับ list ของ tool
"""The same search as a LlamaIndex FunctionTool."""
import os
import requests
from llama_index.core.tools import FunctionTool
def google_search(query: str, gl: str = "us", hl: str = "en") -> str:
"""Search Google for current information and return the top results with title, link and snippet."""
r = requests.get(
"https://serpcrypto.com/api/v1/search",
params={"q": query, "gl": gl, "hl": hl},
headers={"Authorization": "Bearer " + os.environ["SERPCRYPTO_API_KEY"]},
timeout=90,
)
r.raise_for_status()
results = r.json().get("organic_results", [])[:5]
return "\n\n".join(f"{x['title']}\n{x['link']}\n{x.get('snippet', '')}" for x in results)
search_tool = FunctionTool.from_defaults(fn=google_search)
# Pass search_tool in the tools list of your LlamaIndex agent.tool ควรคืน JSON หรือข้อความ
ข้อความ ใน большинстве กรณี โมเดลอ่านข้อความสั้น ๆ ที่มี label ได้ดี และใช้ token น้อยกว่า JSON ที่ซ้อนกัน คืน JSON เฉพาะเมื่อขั้นตอนถัดไปใน chain ของคุณต้อง parse มัน เช่น เพื่อจัดเก็บอันดับ
เก็บลิงก์ไว้ใน output ลิงก์ช่วยให้โมเดลอ้างอิงแหล่งที่มา และช่วยให้คุณตรวจย้อนหลังได้ว่าคำตอบพึ่งพาหน้าใดบ้าง
ฉันจะควบคุมค่าใช้จ่ายการค้นหาใน agent ได้อย่างไร
ทุก tool call ที่สำเร็จคือหนึ่งเครดิต สามนิสัยช่วยให้จำนวนต่ำ: บอกโมเดลใน system prompt ว่าเมื่อใดไม่ควรค้นหา จำกัดจำนวน tool call ต่อรอบใน agent loop ของคุณ และแคชคำตอบสำหรับคำถามซ้ำในโค้ดของคุณเอง
แพ็ก 100,000 การค้นหา ราคา US$56.25 และ เครดิตการค้นหาแบบเติมเงินไม่มีวันหมดอายุ: คำค้นหาที่ไม่ได้ใช้จะคงอยู่ในบัญชีของคุณจนกว่าคุณจะใช้ ดังนั้นเดือนที่เงียบก็ไม่เสียเปล่า
คำถามเกี่ยวกับ LangChain
นี่คือการเชื่อมต่อ LangChain อย่างเป็นทางการหรือไม่
ไม่ใช่ เป็น tool ขนาดเล็กที่สร้างเองบน decorator @tool แบบสาธารณะของ LangChain เนื่องจากเป็นโค้ดของคุณ คุณจึงเปลี่ยนรูปแบบ output เพิ่มพารามิเตอร์เช่น location หรือ engine หรือเพิ่ม tool ที่สองสำหรับ Google News ได้
tool ค้นหาเมืองเฉพาะได้หรือไม่
ได้ เพิ่มอาร์กิวเมนต์ location และส่งเข้าไปใน params เขียนเป็น City, Region, Country ดู locations สำหรับรูปแบบและตัวอย่าง
agent จ่ายเงินสำหรับการค้นหาที่ล้มเหลวหรือไม่
ไม่ 1 เครดิต = 1 คำขอค้นหาที่สำเร็จ ซึ่งคืนผลลัพธ์หนึ่งหน้าบนเอนจินที่รองรับ คำขอที่ล้มเหลวจะไม่ถูกคิดเครดิต tool call ที่เจอ timeout หรือข้อผิดพลาดชั่วคราวจะไม่ใช้เครดิต
โมเดลใดใช้ tool นี้ได้บ้าง
โมเดลแชตใดก็ได้ที่รองรับ tool calling ใน LangChain หรือ LlamaIndex tool เองเพียงส่งคำขอ HTTPS ดังนั้นจึงไม่ขึ้นกับผู้ให้บริการโมเดล