Skip to content

The agent engineering platform.

docs : add cnosdb to Ecosystem Integrations (#7316)

Subsegment152dc59060c0
diff --git a/docs/extras/ecosystem/integrations/cnosdb.mdx b/docs/extras/ecosystem/integrations/cnosdb.mdx
new file mode 100644
index 0000000000..078cd72fa3
--- /dev/null
+++ b/docs/extras/ecosystem/integrations/cnosdb.mdx
@@ -0,0 +1,108 @@
+# 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 cnosdb_connector import make_cnosdb_langchain_uri
+from langchain import SQLDatabase
+
+db = SQLDatabase.from_cnosdb()
+```
+```python
+# Creating a OpenAI Chat LLM Wrapper
+from langchain.chat_models import ChatOpenAI
+
+llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo")
+```
+
+### SQL Chain
+This example demonstrates the use of the SQL Chain for answering a question over a CnosDB.
+```python
+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?"
+)
+```
+```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.
+> 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.agents.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 fa of test table that time between November 3, 2022 and November 4, 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.
+Action: sql_db_schema
+Action Input: "test"
+Observation:
+CREATE TABLE test (
+ time TIMESTAMP,
+ fa BIGINT
+)
+
+/*
+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
+*/
+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.
+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
+
+> Finished chain.
+```
diff --git a/langchain/sql_database.py b/langchain/sql_database.py
index 5bdfd8cc56..19d9f81165 100644
--- a/langchain/sql_database.py
+++ b/langchain/sql_database.py
@@ -222,6 +222,47 @@ class SQLDatabase:
)
return cls.from_uri(database_uri=uri, engine_args=engine_args, **kwargs)
+ @classmethod
+ def from_cnosdb(
+ cls,
+ url: str = "127.0.0.1:8902",
+ user: str = "root",
+ password: str = "",
+ tenant: str = "cnosdb",
+ database: str = "public",
+ ) -> SQLDatabase:
+ """
+ Class method to create an SQLDatabase instance from a CnosDB connection.
+ This method requires the 'cnos-connector' package. If not installed, it
+ can be added using `pip install cnos-connector`.
+
+ Args:
+ 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".
+ user (str): The username used to connect to the CnosDB service, with a
+ default value of "root".
+ password (str): The password of the user connecting to the CnosDB service,
+ with a default value of "".
+ tenant (str): The name of the tenant used to connect to the CnosDB service,
+ with a default value of "cnosdb".
+ database (str): The name of the database in the CnosDB tenant.
+
+ Returns:
+ SQLDatabase: An instance of SQLDatabase configured with the provided
+ CnosDB connection details.
+ """
+ try:
+ from cnosdb_connector import make_cnosdb_langchain_uri
+
+ uri = make_cnosdb_langchain_uri(url, user, password, tenant, database)
+ return cls.from_uri(database_uri=uri)
+ except ImportError:
+ raise ValueError(
+ "cnos-connector package not found, please install with"
+ " `pip install cnos-connector`"
+ )
+
@property
def dialect(self) -> str:
"""Return string representation of dialect to use."""