mirror of
https://github.com/hwchase17/langchain
synced 2024-11-02 09:40:22 +00:00
160a8eb178
Thank you for contributing to LangChain! - [ ] **PR title**: "package: description" - Where "package" is whichever of langchain, community, core, experimental, etc. is being modified. Use "docs: ..." for purely docs changes, "templates: ..." for template changes, "infra: ..." for CI changes. - Example: "community: add foobar LLM" - [ ] **PR message**: ***Delete this entire checklist*** and replace with - **Description:** Adding oracle autonomous database document loader integration. This will allow users to connect to oracle autonomous database through connection string or TNS configuration. https://www.oracle.com/autonomous-database/ - **Issue:** None - **Dependencies:** oracledb python package https://pypi.org/project/oracledb/ - **Twitter handle:** if your PR gets announced, and you'd like a mention, we'll gladly shout you out! - [ ] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. Unit test and doc are added. - [ ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, hwchase17. --------- Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Bagatur <baskaryan@gmail.com>
125 lines
4.1 KiB
Python
125 lines
4.1 KiB
Python
from typing import Any, Dict, List, Optional
|
|
|
|
from langchain_core.documents import Document
|
|
|
|
from langchain_community.document_loaders.base import BaseLoader
|
|
|
|
|
|
class OracleAutonomousDatabaseLoader(BaseLoader):
|
|
"""
|
|
Load from oracle adb
|
|
|
|
Autonomous Database connection can be made by either connection_string
|
|
or tns name. wallet_location and wallet_password are required
|
|
for TLS connection.
|
|
Each document will represent one row of the query result.
|
|
Columns are written into the `page_content` and 'metadata' in
|
|
constructor is written into 'metadata' of document,
|
|
by default, the 'metadata' is None.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
query: str,
|
|
user: str,
|
|
password: str,
|
|
*,
|
|
schema: Optional[str] = None,
|
|
tns_name: Optional[str] = None,
|
|
config_dir: Optional[str] = None,
|
|
wallet_location: Optional[str] = None,
|
|
wallet_password: Optional[str] = None,
|
|
connection_string: Optional[str] = None,
|
|
metadata: Optional[List[str]] = None,
|
|
):
|
|
"""
|
|
init method
|
|
:param query: sql query to execute
|
|
:param user: username
|
|
:param password: user password
|
|
:param schema: schema to run in database
|
|
:param tns_name: tns name in tnsname.ora
|
|
:param config_dir: directory of config files(tnsname.ora, wallet)
|
|
:param wallet_location: location of wallet
|
|
:param wallet_password: password of wallet
|
|
:param connection_string: connection string to connect to adb instance
|
|
:param metadata: metadata used in document
|
|
"""
|
|
# Mandatory required arguments.
|
|
self.query = query
|
|
self.user = user
|
|
self.password = password
|
|
|
|
# Schema
|
|
self.schema = schema
|
|
|
|
# TNS connection Method
|
|
self.tns_name = tns_name
|
|
self.config_dir = config_dir
|
|
|
|
# Wallet configuration is required for mTLS connection
|
|
self.wallet_location = wallet_location
|
|
self.wallet_password = wallet_password
|
|
|
|
# Connection String connection method
|
|
self.connection_string = connection_string
|
|
|
|
# metadata column
|
|
self.metadata = metadata
|
|
|
|
# dsn
|
|
self.dsn: Optional[str]
|
|
self._set_dsn()
|
|
|
|
def _set_dsn(self) -> None:
|
|
if self.connection_string:
|
|
self.dsn = self.connection_string
|
|
elif self.tns_name:
|
|
self.dsn = self.tns_name
|
|
|
|
def _run_query(self) -> List[Dict[str, Any]]:
|
|
try:
|
|
import oracledb
|
|
except ImportError as e:
|
|
raise ImportError(
|
|
"Could not import oracledb, "
|
|
"please install with 'pip install oracledb'"
|
|
) from e
|
|
connect_param = {"user": self.user, "password": self.password, "dsn": self.dsn}
|
|
if self.dsn == self.tns_name:
|
|
connect_param["config_dir"] = self.config_dir
|
|
if self.wallet_location and self.wallet_password:
|
|
connect_param["wallet_location"] = self.wallet_location
|
|
connect_param["wallet_password"] = self.wallet_password
|
|
|
|
try:
|
|
connection = oracledb.connect(**connect_param)
|
|
cursor = connection.cursor()
|
|
if self.schema:
|
|
cursor.execute(f"alter session set current_schema={self.schema}")
|
|
cursor.execute(self.query)
|
|
columns = [col[0] for col in cursor.description]
|
|
data = cursor.fetchall()
|
|
data = [dict(zip(columns, row)) for row in data]
|
|
except oracledb.DatabaseError as e:
|
|
print("Got error while connecting: " + str(e)) # noqa: T201
|
|
data = []
|
|
finally:
|
|
cursor.close()
|
|
connection.close()
|
|
|
|
return data
|
|
|
|
def load(self) -> List[Document]:
|
|
data = self._run_query()
|
|
documents = []
|
|
metadata_columns = self.metadata if self.metadata else []
|
|
for row in data:
|
|
metadata = {
|
|
key: value for key, value in row.items() if key in metadata_columns
|
|
}
|
|
doc = Document(page_content=str(row), metadata=metadata)
|
|
documents.append(doc)
|
|
|
|
return documents
|