mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-04 11:25:11 +00:00
Use docusaurus versioning with a callout, merged master as well @hwchase17 @baskaryan --------- Signed-off-by: Weichen Xu <weichen.xu@databricks.com> Signed-off-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: Leonid Ganeline <leo.gan.57@gmail.com> Co-authored-by: Leonid Kuligin <lkuligin@yandex.ru> Co-authored-by: Averi Kitsch <akitsch@google.com> Co-authored-by: Erick Friis <erick@langchain.dev> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Nuno Campos <nuno@boringbits.io> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Martín Gotelli Ferenaz <martingotelliferenaz@gmail.com> Co-authored-by: Fayfox <admin@fayfox.com> Co-authored-by: Eugene Yurtsev <eugene@langchain.dev> Co-authored-by: Dawson Bauer <105886620+djbauer2@users.noreply.github.com> Co-authored-by: Ravindu Somawansa <ravindu.somawansa@gmail.com> Co-authored-by: Dhruv Chawla <43818888+Dominastorm@users.noreply.github.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: WeichenXu <weichen.xu@databricks.com> Co-authored-by: Benito Geordie <89472452+benitoThree@users.noreply.github.com> Co-authored-by: kartikTAI <129414343+kartikTAI@users.noreply.github.com> Co-authored-by: Kartik Sarangmath <kartik@thirdai.com> Co-authored-by: Sevin F. Varoglu <sfvaroglu@octoml.ai> Co-authored-by: MacanPN <martin.triska@gmail.com> Co-authored-by: Prashanth Rao <35005448+prrao87@users.noreply.github.com> Co-authored-by: Hyeongchan Kim <kozistr@gmail.com> Co-authored-by: sdan <git@sdan.io> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: Rahul Triptahi <rahul.psit.ec@gmail.com> Co-authored-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: pjb157 <84070455+pjb157@users.noreply.github.com> Co-authored-by: Eun Hye Kim <ehkim1440@gmail.com> Co-authored-by: kaijietti <43436010+kaijietti@users.noreply.github.com> Co-authored-by: Pengcheng Liu <pcliu.fd@gmail.com> Co-authored-by: Tomer Cagan <tomer@tomercagan.com> Co-authored-by: Christophe Bornet <cbornet@hotmail.com>
111 lines
3.9 KiB
Plaintext
111 lines
3.9 KiB
Plaintext
# CnosDB
|
|
> [CnosDB](https://github.com/cnosdb/cnosdb) is an open-source distributed time series database with high performance, high compression rate and high ease of use.
|
|
|
|
## Installation and Setup
|
|
|
|
```python
|
|
pip install cnos-connector
|
|
```
|
|
|
|
## Connecting to CnosDB
|
|
You can connect to CnosDB using the `SQLDatabase.from_cnosdb()` method.
|
|
### Syntax
|
|
```python
|
|
def SQLDatabase.from_cnosdb(url: str = "127.0.0.1:8902",
|
|
user: str = "root",
|
|
password: str = "",
|
|
tenant: str = "cnosdb",
|
|
database: str = "public")
|
|
```
|
|
Args:
|
|
1. url (str): The HTTP connection host name and port number of the CnosDB
|
|
service, excluding "http://" or "https://", with a default value
|
|
of "127.0.0.1:8902".
|
|
2. user (str): The username used to connect to the CnosDB service, with a
|
|
default value of "root".
|
|
3. password (str): The password of the user connecting to the CnosDB service,
|
|
with a default value of "".
|
|
4. tenant (str): The name of the tenant used to connect to the CnosDB service,
|
|
with a default value of "cnosdb".
|
|
5. database (str): The name of the database in the CnosDB tenant.
|
|
## Examples
|
|
```python
|
|
# Connecting to CnosDB with SQLDatabase Wrapper
|
|
from langchain_community.utilities import SQLDatabase
|
|
|
|
db = SQLDatabase.from_cnosdb()
|
|
```
|
|
```python
|
|
# Creating a OpenAI Chat LLM Wrapper
|
|
from langchain_openai import ChatOpenAI
|
|
|
|
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
|
|
```
|
|
|
|
### SQL Database Chain
|
|
This example demonstrates the use of the SQL Chain for answering a question over a CnosDB.
|
|
```python
|
|
from langchain_community.utilities import SQLDatabaseChain
|
|
|
|
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
|
|
|
|
db_chain.run(
|
|
"What is the average temperature of air at station XiaoMaiDao between October 19, 2022 and Occtober 20, 2022?"
|
|
)
|
|
```
|
|
```shell
|
|
> Entering new chain...
|
|
What is the average temperature of air at station XiaoMaiDao between October 19, 2022 and Occtober 20, 2022?
|
|
SQLQuery:SELECT AVG(temperature) FROM air WHERE station = 'XiaoMaiDao' AND time >= '2022-10-19' AND time < '2022-10-20'
|
|
SQLResult: [(68.0,)]
|
|
Answer:The average temperature of air at station XiaoMaiDao between October 19, 2022 and October 20, 2022 is 68.0.
|
|
> Finished chain.
|
|
```
|
|
### SQL Database Agent
|
|
This example demonstrates the use of the SQL Database Agent for answering questions over a CnosDB.
|
|
```python
|
|
from langchain.agents import create_sql_agent
|
|
from langchain_community.agent_toolkits import SQLDatabaseToolkit
|
|
|
|
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
|
|
agent = create_sql_agent(llm=llm, toolkit=toolkit, verbose=True)
|
|
```
|
|
```python
|
|
agent.run(
|
|
"What is the average temperature of air at station XiaoMaiDao between October 19, 2022 and Occtober 20, 2022?"
|
|
)
|
|
```
|
|
```shell
|
|
> Entering new chain...
|
|
Action: sql_db_list_tables
|
|
Action Input: ""
|
|
Observation: air
|
|
Thought:The "air" table seems relevant to the question. I should query the schema of the "air" table to see what columns are available.
|
|
Action: sql_db_schema
|
|
Action Input: "air"
|
|
Observation:
|
|
CREATE TABLE air (
|
|
pressure FLOAT,
|
|
station STRING,
|
|
temperature FLOAT,
|
|
time TIMESTAMP,
|
|
visibility FLOAT
|
|
)
|
|
|
|
/*
|
|
3 rows from air table:
|
|
pressure station temperature time visibility
|
|
75.0 XiaoMaiDao 67.0 2022-10-19T03:40:00 54.0
|
|
77.0 XiaoMaiDao 69.0 2022-10-19T04:40:00 56.0
|
|
76.0 XiaoMaiDao 68.0 2022-10-19T05:40:00 55.0
|
|
*/
|
|
Thought:The "temperature" column in the "air" table is relevant to the question. I can query the average temperature between the specified dates.
|
|
Action: sql_db_query
|
|
Action Input: "SELECT AVG(temperature) FROM air WHERE station = 'XiaoMaiDao' AND time >= '2022-10-19' AND time <= '2022-10-20'"
|
|
Observation: [(68.0,)]
|
|
Thought:The average temperature of air at station XiaoMaiDao between October 19, 2022 and October 20, 2022 is 68.0.
|
|
Final Answer: 68.0
|
|
|
|
> Finished chain.
|
|
```
|