跳至主要內容
Open on GitHub

Dataherald

Dataherald 是一種自然語言到 SQL 的工具。

本頁涵蓋如何在 LangChain 中使用 Dataherald API

安裝與設定

  • 使用以下命令安裝需求
pip install dataherald
  • 前往 Dataherald 並在此處註冊 here
  • 建立應用程式並取得您的 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 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?"})

輸出

> 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"}

如需工具的詳細資訊,請參閱此頁面


此頁面是否對您有幫助?