LangChain-16 Using Tools LangChain封装好的工具集tools 原创

请添加图片描述

背景描述

LangChain的社区提供了很多封装好的工具,可以直接拿来用(有的需要申请和配置API KEY)

安装依赖

pip install --upgrade --quiet  langchain-core langchain langchain-openai

编写代码

这里使用 DuckDuckGoSearchRun的搜索功能,可以帮助我们调用 DuckDuck的搜索引擎。

from langchain.tools import DuckDuckGoSearchRun
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI


search = DuckDuckGoSearchRun()

template = """turn the following user input into a search query for a search engine:

{input}"""
prompt = ChatPromptTemplate.from_template(template)

# 使用GPT-4-Turbo 3.5完成的效果不好
model = ChatOpenAI(
    model="gpt-4-0125-preview"
)

chain = prompt | model | StrOutputParser() | search
message1 = chain.invoke({"input": "I'd like to figure out what games are tonight"})
print(f"message1: {message1}")

此外,它的工具包中,还封装了这些

__all__ = [
    "AINAppOps",
    "AINOwnerOps",
    "AINRuleOps",
    "AINTransfer",
    "AINValueOps",
    "AIPluginTool",
    "APIOperation",
    "ArxivQueryRun",
    "AzureCogsFormRecognizerTool",
    "AzureCogsImageAnalysisTool",
    "AzureCogsSpeech2TextTool",
    "AzureCogsText2SpeechTool",
    "BaseGraphQLTool",
    "BaseRequestsTool",
    "BaseSQLDatabaseTool",
    "BaseSparkSQLTool",
    "BaseTool",
    "BingSearchResults",
    "BingSearchRun",
    "BraveSearch",
    "ClickTool",
    "CopyFileTool",
    "CurrentWebPageTool",
    "DeleteFileTool",
    "DuckDuckGoSearchResults",
    "DuckDuckGoSearchRun",
    "EdenAiExplicitImageTool",
    "EdenAiObjectDetectionTool",
    "EdenAiParsingIDTool",
    "EdenAiParsingInvoiceTool",
    "EdenAiTextToSpeechTool",
    "EdenAiSpeechToTextTool",
    "EdenAiTextModerationTool",
    "EdenaiTool",
    "ElevenLabsText2SpeechTool",
    "ExtractHyperlinksTool",
    "ExtractTextTool",
    "FileSearchTool",
    "GetElementsTool",
    "GmailCreateDraft",
    "GmailGetMessage",
    "GmailGetThread",
    "GmailSearch",
    "GmailSendMessage",
    "GooglePlacesTool",
    "GoogleSearchResults",
    "GoogleSearchRun",
    "GoogleSerperResults",
    "GoogleSerperRun",
    "HumanInputRun",
    "IFTTTWebhook",
    "InfoPowerBITool",
    "InfoSQLDatabaseTool",
    "InfoSparkSQLTool",
    "JiraAction",
    "JsonGetValueTool",
    "JsonListKeysTool",
    "ListDirectoryTool",
    "ListPowerBITool",
    "ListSQLDatabaseTool",
    "ListSparkSQLTool",
    "MetaphorSearchResults",
    "MoveFileTool",
    "NavigateBackTool",
    "NavigateTool",
    "O365SearchEmails",
    "O365SearchEvents",
    "O365CreateDraftMessage",
    "O365SendMessage",
    "O365SendEvent",
    "authenticate",
    "OpenAPISpec",
    "OpenWeatherMapQueryRun",
    "PubmedQueryRun",
    "PythonAstREPLTool",
    "PythonREPLTool",
    "QueryCheckerTool",
    "QueryPowerBITool",
    "QuerySQLCheckerTool",
    "QuerySQLDataBaseTool",
    "QuerySparkSQLTool",
    "ReadFileTool",
    "RequestsDeleteTool",
    "RequestsGetTool",
    "RequestsPatchTool",
    "RequestsPostTool",
    "RequestsPutTool",
    "SceneXplainTool",
    "SearxSearchResults",
    "SearxSearchRun",
    "ShellTool",
    "SleepTool",
    "StdInInquireTool",
    "SteamshipImageGenerationTool",
    "StructuredTool",
    "Tool",
    "VectorStoreQATool",
    "VectorStoreQAWithSourcesTool",
    "WikipediaQueryRun",
    "WolframAlphaQueryRun",
    "WriteFileTool",
    "YouTubeSearchTool",
    "ZapierNLAListActions",
    "ZapierNLARunAction",
    "tool",
    "format_tool_to_openai_function",
    "BearlyInterpreterTool",
]

运行结果

➜ python3 test16.py
/Users/wuzikang/Desktop/py/langchain_test/own_learn/env/lib/python3.12/site-packages/langchain/tools/__init__.py:63: LangChainDeprecationWarning: Importing tools from langchain is deprecated. Importing from langchain will no longer be supported as of langchain==0.2.0. Please import from langchain-community instead:

`from langchain_community.tools import DuckDuckGoSearchRun`.

To install langchain-community run `pip install -U langchain-community`.
  warnings.warn(
message1: How to live stream NBA games tonight. Pelicans vs Thunder and Clippers vs Lakers will air on ESPN. Viewers can also stream NBA games on Sling TV. Fans in the U.S. can watch the biggest games of ... NBA Games on TV Tonight - 2023-2024 NBA TV Schedule What Channel and Time is NBA Games on TV Today, Tonight, Tomorrow? Watch and Stream live NBA games on TV tonight and today. NBA games are broadcast on ABC, ESPN, TNT, NBATV and local networks. NBA on TV Sunday, 3/17/2024. NBA REGULAR SEASON TIME ET TV; Phoenix at Milwaukee: What Channel and Time do NHL Games Play on TV Today, Tonight, Tomorrow? Watch and stream NHL live games today on TV. 2023-24 NHL TV Schedule for tonight includes games on ABC, ESPN, TNT, TBS & Hulu. NHL on TV Sunday, 3/17/2024. NHL REGULAR SEASON TIME ET TV; NY Islanders at NY Rangers: 1:00pm: TNT: New Jersey at Vegas: 3:30pm: TNT: The web page shows the schedule of NFL Network programs for the next two weeks. There are no games on tonight, only replays of previous games and shows. Tonight's schedule features two primetime games. The Tennessee Titans take on the Miami Dolphins at Hard Rock Stadium and the Green Bay Packers face the New York Giants at MetLife. Both games take place at 8:15 PM. ... One regular season game and one NFL Playoff game will only be available to Peacock subscribers. The first game will take ...

在这里插入图片描述

posted @   武子康  阅读(0)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示