@ -8,7 +8,7 @@ pip install cnos-connector
```
## Connecting to CnosDB
You can connect to CnosDB using the SQLDatabase.from_cnosdb() method.
You can connect to CnosDB using the ` SQLDatabase.from_cnosdb()` method.
### Syntax
```python
def SQLDatabase.from_cnosdb(url: str = "127.0.0.1:8902",
@ -31,7 +31,6 @@ Args:
## Examples
```python
# Connecting to CnosDB with SQLDatabase Wrapper
from cnosdb_connector import make_cnosdb_langchain_uri
from langchain import SQLDatabase
db = SQLDatabase.from_cnosdb()
@ -43,7 +42,7 @@ from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
```
### SQL Chain
### SQL Database Chain
This example demonstrates the use of the SQL Chain for answering a question over a CnosDB.
```python
from langchain import SQLDatabaseChain
@ -51,15 +50,15 @@ from langchain import SQLDatabaseChain
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
db_chain.run(
"What is the average fa of test table that time between November 3,2022 and November 4 , 2022?"
"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 fa of test table that time between November 3, 2022 and November 4 , 2022?
SQLQuery:SELECT AVG(fa) FROM test WHERE time >= '2022-11-03' AND time < '2022-11-04 '
SQLResult: [(2 .0,)]
Answer:The average fa of the test table between November 3, 2022, and November 4, 2022, is 2 .0.
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
@ -73,36 +72,39 @@ agent = create_sql_agent(llm=llm, toolkit=toolkit, verbose=True)
```
```python
agent.run(
"What is the average fa of test table that time between November 3, 2022 and November 4 , 2022?"
"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: test
Thought:The relevant table is "test". I should query the schema of this table to see the column names .
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: "test "
Action Input: "air "
Observation:
CREATE TABLE test (
CREATE TABLE air (
pressure FLOAT,
station STRING,
temperature FLOAT,
time TIMESTAMP,
fa BIGINT
visibility FLOA T
)
/*
3 rows from test table:
fa time
1 2022-11-03T06:20:11
2 2022-11-03T06:20:11.000000001
3 2022-11-03T06:20:11.000000002
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 relevant column is "fa" in the "test" table. I can now construct the query to calculate the average "fa" between the specified time range .
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(fa) FROM test WHERE time >= '2022-11-03' AND time < '2022-11-04 '"
Observation: [(2 .0,)]
Thought:The average "fa" of the "test" table between November 3, 2022 and November 4, 2022 is 2 .0.
Final Answer: 2 .0
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.
```