Tookit

tookit是langchain已经封装好的一系列工具,一个工具包是一组工具来组合完成特定的任务


Azure认知服务 https://portal.azure.com/#allservices

  • AzureCogsFormRecognizerTool:从文档里提取文本
  • AzureCogsSpeech2TextTool:语音到文本
  • AzureCogsText2SpeechTool:文本到语音

#创建toolkit
from langchain.agents.agent_toolkits import AzureCognitiveServicesToolkit

toolkit = AzureCognitiveServicesToolkit()

[tool.name for tool in toolkit.get_tools()]


#agent使用
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
llm = ChatOpenAI(temperature=0,model="gpt-4-1106-preview")
agent = initialize_agent(
    tools=toolkit.get_tools(),
    llm=llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)
audio_file = agent.run("Tell me a joke and read it out for me.")

python

一个python代码机器人

from langchain_experimental.agents.agent_toolkits import create_python_agent
from langchain_experimental.tools import PythonREPLTool
from langchain_experimental.utilities import PythonREPL
from langchain.llms.openai import OpenAI
from langchain.agents.agent_types import AgentType
from langchain.chat_models import ChatOpenAI

agent_executor = create_python_agent(
    llm=ChatOpenAI(temperature=0, model="gpt-4-1106-preview"),
    tool=PythonREPLTool(),
    verbose=True,
    agent_type=AgentType.OPENAI_FUNCTIONS,
    agent_executor_kwargs={"handle_parsing_errors": True},
)

agent_executor.run("What is the 10th fibonacci number?")

SQL Database

使用SQLDatabaseChain构建的agent,用来根据数据库回答一般行动饿问题

db = SQLDatabase.from_uri("sqlite:///Chinook.db")
toolkit = SQLDatabaseToolkit(db=db, llm=OpenAI(temperature=0))

agent_executor = create_sql_agent(
    llm=ChatOpenAI(temperature=0, model="gpt-4-1106-preview"),
    toolkit=toolkit,
    verbose=True,
    agent_type=AgentType.OPENAI_FUNCTIONS
)

# 用自然语言描述一个数据库表
agent_executor.run("Describe the playlisttrack table")
# 用自然语言跑sql查询
agent_executor.run(
    "List the total sales per country. Which country's customers spent the most?"
)
Prev post

LangChain 021

Next post

LangChain 023