如何加载使用tool
- 加载预制tool的方法
- 几种tool的使用方式
langchain预制了大量的tools,基本这些工具能满足大部分需求,https://github.com/langchain-ai/langchain/tree/v0.0.352/docs/docs/integrations/tools
#添加预制工具的方法很简单
from langchain.agents import load_tools
tool_names = [...]
tools = load_tools(tool_names) #使用load方法
#有些tool需要单独设置llm
from langchain.agents import load_tools
tool_names = [...]
llm = ...
tools = load_tools(tool_names, llm=llm) #在load的时候指定llm
SerpAPI
最常见的聚合搜索引擎 https://serper.dev/dashboard
from langchain.utilities import SerpAPIWrapper
#serpapi的api key
import os
os.environ["SERPAPI_API_KEY"] = "hahaha"
search = SerpAPIWrapper()
search.run("Obama's first name?")
params = {
"engine": "bing",
"gl": "us",
"hl": "en",
}
search = SerpAPIWrapper(params=params)
使用Dall-E
Dall-E是openai出品的文到图AI大模型
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(
temperature=0,
model="gpt-4",
)
from langchain.agents import initialize_agent, load_tools
tools = load_tools(["dalle-image-generator"])
agent = initialize_agent(
tools,
llm,
agent="zero-shot-react-description",
verbose=True
)
output = agent.run("Create an image of a halloween night at a haunted museum")
Eleven Labs Text2Speech
ElevenLabs 是非常优秀的TTS合成API
from langchain.tools import ElevenLabsText2SpeechTool
text_to_speak = "Hello! 你好! Hola! नमस्ते! Bonjour! こんにちは! مرحبا! 안녕하세요! Ciao! Cześć! Привіт! வணக்கம்!"
tts = ElevenLabsText2SpeechTool(
voice="Bella",
text_to_speak=text_to_speak,
verbose=True
)
tts.name
speech_file = tts.run(text_to_speak)
#tts.play(speech_file)
tts.stream_speech(text_to_speak)
GraphQL
一种api查询语言,类似sql,我们用它来查询奈飞的数据库,查找一下和星球大战相关的电影,API地址https://swapi-graphql.netlify.app/.netlify/functions/index
from langchain.chat_models import ChatOpenAI
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.utilities import GraphQLAPIWrapper
llm = ChatOpenAI(
temperature=0,
model="gpt-4",
)
tools = load_tools(
["graphql"],
graphql_endpoint="https://swapi-graphql.netlify.app/.netlify/functions/index",
)
agent = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
graphql_fields = """allFilms {
films {
title
director
releaseDate
speciesConnection {
species {
name
classification
homeworld {
name
}
}
}
}
}
"""
suffix = "Search for the titles of all the stawars films stored in the graphql database that has this schema,and answer in chinese:"
agent.run(suffix + graphql_fields)