工具功能
目前该工具具有以下功能:- 通过关键词搜索从 Google Books API 获取相关信息
- 将信息格式化为可读输出,并将结果返回给 Agent
配置
确保已安装langchain-community。
Copy
pip install -qU langchain-community
凭据
您需要 Google Books 的 API 密钥。请访问 https://developers.google.com/books/docs/v1/using#APIKey 并按照步骤获取。 然后将环境变量GOOGLE_BOOKS_API_KEY 设置为您的 Google Books API 密钥。
实例化
导入 Google Books 工具并设置您的凭据以实例化该工具。Copy
import os
from langchain_community.tools.google_books import GoogleBooksQueryRun
from langchain_community.utilities.google_books import GoogleBooksAPIWrapper
os.environ["GOOGLE_BOOKS_API_KEY"] = "<your Google Books API key>"
tool = GoogleBooksQueryRun(api_wrapper=GoogleBooksAPIWrapper())
调用
调用run 方法来使用该工具。
Copy
tool.run("ai")
Copy
'Here are 5 suggestions for books related to ai:\n\n1. "AI's Take on the Stigma Against AI-Generated Content" by Sandy Y. Greenleaf: In a world where artificial intelligence (AI) is rapidly advancing and transforming various industries, a new form of content creation has emerged: AI-generated content. However, despite its potential to revolutionize the way we produce and consume information, AI-generated content often faces a significant stigma. "AI's Take on the Stigma Against AI-Generated Content" is a groundbreaking book that delves into the heart of this issue, exploring the reasons behind the stigma and offering a fresh, unbiased perspective on the topic. Written from the unique viewpoint of an AI, this book provides readers with a comprehensive understanding of the challenges and opportunities surrounding AI-generated content. Through engaging narratives, thought-provoking insights, and real-world examples, this book challenges readers to reconsider their preconceptions about AI-generated content. It explores the potential benefits of embracing this technology, such as increased efficiency, creativity, and accessibility, while also addressing the concerns and drawbacks that contribute to the stigma. As you journey through the pages of this book, you'll gain a deeper understanding of the complex relationship between humans and AI in the realm of content creation. You'll discover how AI can be used as a tool to enhance human creativity, rather than replace it, and how collaboration between humans and machines can lead to unprecedented levels of innovation. Whether you're a content creator, marketer, business owner, or simply someone curious about the future of AI and its impact on our society, "AI's Take on the Stigma Against AI-Generated Content" is an essential read. With its engaging writing style, well-researched insights, and practical strategies for navigating this new landscape, this book will leave you equipped with the knowledge and tools needed to embrace the AI revolution and harness its potential for success. Prepare to have your assumptions challenged, your mind expanded, and your perspective on AI-generated content forever changed. Get ready to embark on a captivating journey that will redefine the way you think about the future of content creation.\nYou can read more at https://play.google.com/store/books/details?id=4iH-EAAAQBAJ&source=gbs_api'
直接使用参数调用
以下是直接调用的示例。Copy
import os
from langchain_community.tools.google_books import GoogleBooksQueryRun
from langchain_community.utilities.google_books import GoogleBooksAPIWrapper
os.environ["GOOGLE_BOOKS_API_KEY"] = "<your Google Books API key>"
tool = GoogleBooksQueryRun(api_wrapper=GoogleBooksAPIWrapper())
tool.run("ai")
使用 ToolCall 调用
以下是工具调用示例。Copy
import getpass
import os
from langchain_community.tools.google_books import GoogleBooksQueryRun
from langchain_community.utilities.google_books import GoogleBooksAPIWrapper
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
os.environ["OPENAI_API_KEY"] = getpass.getpass()
os.environ["GOOGLE_BOOKS_API_KEY"] = "<your Google Books API key>"
tool = GoogleBooksQueryRun(api_wrapper=GoogleBooksAPIWrapper())
llm = ChatOpenAI(model="gpt-4.1-mini")
prompt = PromptTemplate.from_template(
"Return the keyword, and only the keyword, that the user is looking for from this text: {text}"
)
def suggest_books(query):
chain = prompt | llm | StrOutputParser()
keyword = chain.invoke({"text": query})
return tool.run(keyword)
suggestions = suggest_books("I need some information on AI")
print(suggestions)
链式调用
以下是链式调用示例。Copy
import getpass
import os
from langchain_classic import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_community.tools.google_books import GoogleBooksQueryRun
from langchain_community.utilities.google_books import GoogleBooksAPIWrapper
from langchain_openai import ChatOpenAI
os.environ["OPENAI_API_KEY"] = getpass.getpass()
os.environ["GOOGLE_BOOKS_API_KEY"] = "<your Google Books API key>"
tool = GoogleBooksQueryRun(api_wrapper=GoogleBooksAPIWrapper())
llm = ChatOpenAI(model="gpt-4.1-mini")
instructions = """You are a book suggesting assistant."""
base_prompt = hub.pull("langchain-ai/openai-functions-template")
prompt = base_prompt.partial(instructions=instructions)
tools = [tool]
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
)
agent_executor.invoke({"input": "Can you recommend me some books related to ai?"})
API 参考
Google Books API 详情请访问:https://developers.google.com/booksConnect these docs to Claude, VSCode, and more via MCP for real-time answers.

