Name: wikipedia
Description: A wrapper around Wikipedia. Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.
args schema: {'query':{'description':'query to look up on wikipedia', 'title':'Query', 'type':'string'}}
returns directly?: False
Page: LangChain
Summary: LangChain is a software framework that helps facilitate the integration of large language models (LLMs) into applications. As a language model integration framework, LangChain's use-cases largely overlap with those of language models in general, including document analysis and summarization, chatbots, and code analysis.
# 导入WikipediaQueryRun工具,用于执行维基百科查询from langchain_community.tools import WikipediaQueryRun
# 导入WikipediaAPIWrapper,用于封装维基百科API的调用from langchain_community.utilities import WikipediaAPIWrapper
# 导入pydantic模块中的BaseModel和Field,用于定义输入数据模型from pydantic import BaseModel, Field# 定义输入模型类WikiInputs,继承自BaseModelclassWikiInputs(BaseModel):"""Inputs to the wikipedia tool."""query:str= Field(description="query to look up in Wikipedia, should be 3 or less words")# 初始化WikipediaAPIWrapper实例,设置返回最多1个结果,每个结果的最大字符数为1000
api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=1000)# 创建WikipediaQueryRun工具实例,并传入封装好的WikipediaAPIWrapper
tool = WikipediaQueryRun(name="wiki-tool",# 设置工具名称description="look up things in wikipedia",# 设置工具描述信息args_schema=WikiInputs,# 指定输入参数的数据模型api_wrapper=api_wrapper,# 绑定使用的API封装器return_direct=True# 是否直接返回结果(不经过Agent处理))# 打印工具的相关属性信息print(f"Name: {tool.name}")print(f"Description: {tool.description}")print(f"args schema: {tool.args}")print(f"returns directly?: {tool.return_direct}")# 调用tool的invoke方法,执行查询"langchain"并打印结果print(tool.invoke({"query":"langchain"}))"""
invoke 是 LangChain 中定义的一个方法,用于接收一个包含输入参数的字典。
"query" 字段需要符合 WikiInputs 模型中定义的字段要求(此处为字符串类型,建议不超过3个词)。
适用于与链式调用(chain)或其他需要结构化输入输出的场景集成。
"""# 使用tool.run方法直接执行查询print(tool.run("langchain"))"""
run是工具类的一个便捷方法,直接接受位置参数。
在这里传入的是一个字符串 "langchain",它会自动适配到 WikiInputs 模型的 query 字段。
更适合直接快速调用工具的情况,语法更简洁。
"""
Name: wiki-tool
Description: look up things in wikipedia
args schema: {'query':{'description':'query to look up in Wikipedia, should be 3 or less words', 'title':'Query', 'type':'string'}}
returns directly?: True
Page: LangChain
Summary: LangChain is a software framework that helps facilitate the integration of large language models (LLMs) into applications. As a language model integration framework, LangChain's use-cases largely overlap with those of language models in general, including document analysis and summarization, chatbots, and code analysis.
Page: LangChain
Summary: LangChain is a software framework that helps facilitate the integration of large language models (LLMs) into applications. As a language model integration framework, LangChain's use-cases largely overlap with those of language models in general, including document analysis and summarization, chatbots, and code analysis.