community[minor]: Add Datahareld tool (#19680)

**Description:** Integrate [dataherald](https://www.dataherald.com)
tool, It is a natural language-to-SQL tool.
**Dependencies:** Install dataherald sdk to use it,
```
pip install dataherald
```

---------

Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
Co-authored-by: Christophe Bornet <cbornet@hotmail.com>
This commit is contained in:
Juan Carlos José Camacho
2024-04-13 17:27:16 -06:00
committed by William Fu-Hinthorn
parent e07c444285
commit 2fe136ce16
11 changed files with 306 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
# Dataherald
>[Dataherald](https://www.dataherald.com) is a natural language-to-SQL.
This page covers how to use the `Dataherald API` within LangChain.
## Installation and Setup
- Install requirements with
```bash
pip install dataherald
```
- Go to dataherald and sign up [here](https://www.dataherald.com)
- Create an app and get your `API KEY`
- Set your `API KEY` as an environment variable `DATAHERALD_API_KEY`
## Wrappers
### Utility
There exists a DataheraldAPIWrapper utility which wraps this API. To import this utility:
```python
from langchain_community.utilities.dataherald import DataheraldAPIWrapper
```
For a more detailed walkthrough of this wrapper, see [this notebook](/docs/integrations/tools/dataherald).
### Tool
You can use the tool in an agent like this:
```python
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)
agent_executor.invoke({"input":"Return the sql for this question: How many employees are in the company?"})
```
Output
```shell
> 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"}
```
For more information on tools, see [this page](/docs/modules/tools/).

View File

@@ -0,0 +1,117 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"id": "245a954a",
"metadata": {},
"source": [
"# Dataherald\n",
"\n",
"This notebook goes over how to use the dataherald component.\n",
"\n",
"First, you need to set up your Dataherald account and get your API KEY:\n",
"\n",
"1. Go to dataherald and sign up [here](https://www.dataherald.com/)\n",
"2. Once you are logged in your Admin Console, create an API KEY\n",
"3. pip install dataherald\n",
"\n",
"Then we will need to set some environment variables:\n",
"1. Save your API KEY into DATAHERALD_API_KEY env variable"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "961b3689",
"metadata": {
"vscode": {
"languageId": "shellscript"
}
},
"outputs": [],
"source": [
"pip install dataherald"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "34bb5968",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"DATAHERALD_API_KEY\"] = \"\""
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "ac4910f8",
"metadata": {},
"outputs": [],
"source": [
"from langchain_community.utilities.dataherald import DataheraldAPIWrapper"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "84b8f773",
"metadata": {},
"outputs": [],
"source": [
"dataherald = DataheraldAPIWrapper(db_connection_id=\"65fb766367dd22c99ce1a12d\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "068991a6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'select COUNT(*) from employees'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dataherald.run(\"How many employees are in the company?\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
},
"vscode": {
"interpreter": {
"hash": "53f3bc57609c7a84333bb558594977aa5b4026b1d6070b93987956689e367341"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}