docs : Use more meaningful cnosdb examples (#7587)

This change makes the ecosystem integrations cnosdb documentation more
realistic and easy to understand.

- change examples of question and table
- modify typo and format
This commit is contained in:
Subsegment 2023-07-12 22:31:55 +08:00 committed by GitHub
parent f3c9bf5e4b
commit 6e1000dc8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -8,7 +8,7 @@ pip install cnos-connector
``` ```
## Connecting to CnosDB ## 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 ### Syntax
```python ```python
def SQLDatabase.from_cnosdb(url: str = "127.0.0.1:8902", def SQLDatabase.from_cnosdb(url: str = "127.0.0.1:8902",
@ -31,7 +31,6 @@ Args:
## Examples ## Examples
```python ```python
# Connecting to CnosDB with SQLDatabase Wrapper # Connecting to CnosDB with SQLDatabase Wrapper
from cnosdb_connector import make_cnosdb_langchain_uri
from langchain import SQLDatabase from langchain import SQLDatabase
db = SQLDatabase.from_cnosdb() db = SQLDatabase.from_cnosdb()
@ -43,7 +42,7 @@ from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo") 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. This example demonstrates the use of the SQL Chain for answering a question over a CnosDB.
```python ```python
from langchain import SQLDatabaseChain from langchain import SQLDatabaseChain
@ -51,15 +50,15 @@ from langchain import SQLDatabaseChain
db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True) db_chain = SQLDatabaseChain.from_llm(llm, db, verbose=True)
db_chain.run( 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 ```shell
> Entering new chain... > Entering new chain...
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?
SQLQuery:SELECT AVG(fa) FROM test WHERE time >= '2022-11-03' AND time < '2022-11-04' SQLQuery:SELECT AVG(temperature) FROM air WHERE station = 'XiaoMaiDao' AND time >= '2022-10-19' AND time < '2022-10-20'
SQLResult: [(2.0,)] SQLResult: [(68.0,)]
Answer:The average fa of the test table between November 3, 2022, and November 4, 2022, is 2.0. Answer:The average temperature of air at station XiaoMaiDao between October 19, 2022 and October 20, 2022 is 68.0.
> Finished chain. > Finished chain.
``` ```
### SQL Database Agent ### SQL Database Agent
@ -73,36 +72,39 @@ agent = create_sql_agent(llm=llm, toolkit=toolkit, verbose=True)
``` ```
```python ```python
agent.run( 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 ```shell
> Entering new chain... > Entering new chain...
Action: sql_db_list_tables Action: sql_db_list_tables
Action Input: "" Action Input: ""
Observation: test Observation: air
Thought:The relevant table is "test". I should query the schema of this table to see the column names. 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: sql_db_schema
Action Input: "test" Action Input: "air"
Observation: Observation:
CREATE TABLE test ( CREATE TABLE air (
pressure FLOAT,
station STRING,
temperature FLOAT,
time TIMESTAMP, time TIMESTAMP,
fa BIGINT visibility FLOAT
) )
/* /*
3 rows from test table: 3 rows from air table:
fa time pressure station temperature time visibility
1 2022-11-03T06:20:11 75.0 XiaoMaiDao 67.0 2022-10-19T03:40:00 54.0
2 2022-11-03T06:20:11.000000001 77.0 XiaoMaiDao 69.0 2022-10-19T04:40:00 56.0
3 2022-11-03T06:20:11.000000002 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: sql_db_query
Action Input: "SELECT AVG(fa) FROM test WHERE time >= '2022-11-03' AND time < '2022-11-04'" Action Input: "SELECT AVG(temperature) FROM air WHERE station = 'XiaoMaiDao' AND time >= '2022-10-19' AND time <= '2022-10-20'"
Observation: [(2.0,)] Observation: [(68.0,)]
Thought:The average "fa" of the "test" table between November 3, 2022 and November 4, 2022 is 2.0. Thought:The average temperature of air at station XiaoMaiDao between October 19, 2022 and October 20, 2022 is 68.0.
Final Answer: 2.0 Final Answer: 68.0
> Finished chain. > Finished chain.
``` ```