Dataherald
Dataherald 是一个自然语言到 SQL 的工具。
此页面涵盖了如何在 LangChain 中使用 Dataherald API。
安装与设置
- 使用以下命令安装所需库
pip install dataherald
- 前往 dataherald 此处 注册账号
- 创建一个应用并获取您的
API KEY - 将您的
API KEY设置为环境变量DATAHERALD_API_KEY
封装器
工具类
存在一个名为 DataheraldAPIWrapper 的工具类,它封装了此 API。导入此类:
from langchain_community.utilities.dataherald import DataheraldAPIWrapper
API Reference:DataheraldAPIWrapper
有关此封装器的更详细教程,请参阅 此笔记本。
工具
您可以在 Agent 中像这样使用此工具:
from langchain_community.utilities.dataherald import DataheraldAPIWrapper
from langchain_community.tools.dataherald.tool import DataheraldTextToSQL
from langchain_openai import ChatOpenAI
from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent, load_tools
api_wrapper = DataheraldAPIWrapper(db_connection_id="<db_connection_id>")
tool = DataheraldTextToSQL(api_wrapper=api_wrapper)
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
prompt = hub.pull("hwchase17/react")
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True, handle_parsing_errors=True)
agent_executor.invoke({"input":"Return the sql for this question: How many employees are in the company?"})
API Reference:DataheraldAPIWrapper | DataheraldTextToSQL | ChatOpenAI | hub | AgentExecutor | create_react_agent | load_tools
输出
> Entering new AgentExecutor chain...
我需要使用一个能将问题转换为 SQL 的工具。
Action: dataherald
Action Input: How many employees are in the company?Answer: SELECT
COUNT(*) FROM employees我现在知道最终答案了
Final Answer: SELECT
COUNT(*)
FROM
employees
> Finished chain.
{'input': 'Return the sql for this question: How many employees are in the company?', 'output': "SELECT \n COUNT(*)\nFROM \n employees"}
有关工具的更多信息,请参阅 此页面。