Skip to main content
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
有关此封装的更详细介绍,请参阅此笔记本

工具

您可以在智能体中这样使用该工具:
from langchain_community.utilities.dataherald import DataheraldAPIWrapper
from langchain_community.tools.dataherald.tool import DataheraldTextToSQL
from langchain_openai import ChatOpenAI
from langchain_classic import hub
from langchain.agents import AgentExecutor, create_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_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?"})
输出
> Entering new AgentExecutor chain...
I need to use a tool that can convert this question into SQL.
Action: dataherald
Action Input: How many employees are in the company?Answer: SELECT
    COUNT(*) FROM employeesI now know the final answer
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"}
有关工具的更多信息,请参阅此页面