Skip to content

Commit e38af36

Browse files
Refactor oracledb to pass make lint tests
1 parent c2fad14 commit e38af36

File tree

9 files changed

+251
-702
lines changed

9 files changed

+251
-702
lines changed

libs/oracledb/langchain_oracledb/document_loaders/oracleadb_loader.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
oracleadb_loader.py
55
6-
Contains OracleAutonomousDatabaseLoader for connecting to
6+
Contains OracleAutonomousDatabaseLoader for connecting to
77
Oracle Autonomous Database (ADB).
88
"""
99

@@ -98,11 +98,7 @@ def _run_query(self) -> List[Dict[str, Any]]:
9898
columns = [col[0] for col in cursor.description]
9999
data = cursor.fetchall()
100100
data = [
101-
{
102-
i: (j if not isinstance(j, oracledb.LOB) else j.read())
103-
for i, j in zip(columns, row)
104-
}
105-
for row in data
101+
{i: (j if not isinstance(j, oracledb.LOB) else j.read()) for i, j in zip(columns, row)} for row in data
106102
]
107103
except oracledb.DatabaseError as e:
108104
print("Got error while connecting: " + str(e)) # noqa: T201
@@ -118,9 +114,7 @@ def load(self) -> List[Document]:
118114
documents = []
119115
metadata_columns = self.metadata if self.metadata else []
120116
for row in data:
121-
metadata = {
122-
key: value for key, value in row.items() if key in metadata_columns
123-
}
117+
metadata = {key: value for key, value in row.items() if key in metadata_columns}
124118
doc = Document(page_content=str(row), metadata=metadata)
125119
documents.append(doc)
126120

libs/oracledb/langchain_oracledb/document_loaders/oracleai.py

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
oracleai.py
55
6-
Defines OracleDocLoader and OracleTextSplitter for loading
6+
Defines OracleDocLoader and OracleTextSplitter for loading
77
and splitting documents using Oracle AI Vector Search.
88
99
Authors:
@@ -101,18 +101,14 @@ def generate_object_id(input_string: Union[str, None] = None) -> str:
101101
# binary object id
102102
object_id = timestamp_bin + hashval_bin + counter_bin # 16 bytes
103103
object_id_hex = object_id.hex() # 32 bytes
104-
object_id_hex = object_id_hex.zfill(
105-
out_length
106-
) # fill with zeros if less than 32 bytes
104+
object_id_hex = object_id_hex.zfill(out_length) # fill with zeros if less than 32 bytes
107105

108106
object_id_hex = object_id_hex[:out_length]
109107

110108
return object_id_hex
111109

112110
@staticmethod
113-
def read_file(
114-
conn: Connection, file_path: str, params: dict
115-
) -> Union[Document, None]:
111+
def read_file(conn: Connection, file_path: str, params: dict) -> Union[Document, None]:
116112
"""Read a file using OracleReader
117113
Args:
118114
conn: Oracle Connection,
@@ -155,9 +151,7 @@ def read_file(
155151
metadata = {}
156152
else:
157153
doc_data = str(mdata.getvalue())
158-
if doc_data.startswith("<!DOCTYPE html") or doc_data.startswith(
159-
"<HTML>"
160-
):
154+
if doc_data.startswith("<!DOCTYPE html") or doc_data.startswith("<HTML>"):
161155
p = ParseOracleDocMetadata()
162156
p.feed(doc_data)
163157
metadata = p.get_metadata()
@@ -243,10 +237,7 @@ def load(self) -> List[Document]:
243237
self.mdata_cols = self.params.get("mdata_cols")
244238
if self.mdata_cols is not None:
245239
if len(self.mdata_cols) > 3:
246-
raise Exception(
247-
"Exceeds the max number of columns "
248-
+ "you can request for metadata."
249-
)
240+
raise Exception("Exceeds the max number of columns " + "you can request for metadata.")
250241

251242
# execute a query to get column data types
252243
sql = (
@@ -274,8 +265,7 @@ def load(self) -> List[Document]:
274265
"VARCHAR2",
275266
]:
276267
raise Exception(
277-
"The datatype for the column requested "
278-
+ "for metadata is not supported."
268+
"The datatype for the column requested " + "for metadata is not supported."
279269
)
280270

281271
self.mdata_cols_sql = ", rowid"
@@ -306,22 +296,14 @@ def load(self) -> List[Document]:
306296

307297
if row is None:
308298
doc_id = OracleDocReader.generate_object_id(
309-
self.conn.username
310-
+ "$"
311-
+ self.owner
312-
+ "$"
313-
+ self.tablename
314-
+ "$"
315-
+ self.colname
299+
self.conn.username + "$" + self.owner + "$" + self.tablename + "$" + self.colname
316300
)
317301
metadata["_oid"] = doc_id
318302
results.append(Document(page_content="", metadata=metadata))
319303
else:
320304
if row[0] is not None:
321305
data = str(row[0])
322-
if data.startswith("<!DOCTYPE html") or data.startswith(
323-
"<HTML>"
324-
):
306+
if data.startswith("<!DOCTYPE html") or data.startswith("<HTML>"):
325307
p = ParseOracleDocMetadata()
326308
p.feed(data)
327309
metadata = p.get_metadata()
@@ -348,15 +330,9 @@ def load(self) -> List[Document]:
348330
metadata[self.mdata_cols[i]] = row[i + 2]
349331

350332
if row[1] is None:
351-
results.append(
352-
Document(page_content="", metadata=metadata)
353-
)
333+
results.append(Document(page_content="", metadata=metadata))
354334
else:
355-
results.append(
356-
Document(
357-
page_content=str(row[1]), metadata=metadata
358-
)
359-
)
335+
results.append(Document(page_content=str(row[1]), metadata=metadata))
360336
except Exception as ex:
361337
logger.info(f"An exception occurred :: {ex}")
362338
traceback.print_exc()
@@ -403,8 +379,7 @@ def split_text(self, text: str) -> List[str]:
403379

404380
cursor.setinputsizes(content=oracledb.CLOB)
405381
cursor.execute(
406-
"select t.column_value from "
407-
+ "dbms_vector_chain.utl_to_chunks(:content, json(:params)) t",
382+
"select t.column_value from " + "dbms_vector_chain.utl_to_chunks(:content, json(:params)) t",
408383
content=text,
409384
params=self._json.dumps(self.params),
410385
)

libs/oracledb/langchain_oracledb/embeddings/oracleai.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
"""
44
oracleai.py
55
6-
Implements OracleEmbeddings for generating and handling
6+
Implements OracleEmbeddings for generating and handling
77
vector embeddings with Oracle AI Vector Search.
88
99
Authors:
1010
- Harichandan Roy (hroy)
1111
- David Jiang (ddjiang)
1212
"""
13+
1314
from __future__ import annotations
1415

1516
import json
@@ -55,9 +56,7 @@ def __init__(self, **kwargs: Any):
5556
"""
5657

5758
@staticmethod
58-
def load_onnx_model(
59-
conn: Connection, dir: str, onnx_file: str, model_name: str
60-
) -> None:
59+
def load_onnx_model(conn: Connection, dir: str, onnx_file: str, model_name: str) -> None:
6160
"""Load an ONNX model to Oracle Database.
6261
Args:
6362
conn: Oracle Connection,
@@ -111,9 +110,7 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:
111110
cursor = self.conn.cursor()
112111

113112
if self.proxy:
114-
cursor.execute(
115-
"begin utl_http.set_proxy(:proxy); end;", proxy=self.proxy
116-
)
113+
cursor.execute("begin utl_http.set_proxy(:proxy); end;", proxy=self.proxy)
117114

118115
chunks = []
119116
for i, text in enumerate(texts, start=1):
@@ -124,9 +121,7 @@ def embed_documents(self, texts: List[str]) -> List[List[float]]:
124121
inputs = vector_array_type.newobject(chunks)
125122
cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
126123
cursor.execute(
127-
"select t.* "
128-
+ "from dbms_vector_chain.utl_to_embeddings(:1, "
129-
+ "json(:2)) t",
124+
"select t.* " + "from dbms_vector_chain.utl_to_embeddings(:1, " + "json(:2)) t",
130125
[inputs, self.params],
131126
)
132127

libs/oracledb/langchain_oracledb/utilities/oracleai.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Harichandan Roy (hroy)
1010
- David Jiang (ddjiang)
1111
"""
12+
1213
from __future__ import annotations
1314

1415
import json
@@ -45,9 +46,7 @@ class OracleSummary:
4546
proxy: Proxy
4647
"""
4748

48-
def __init__(
49-
self, conn: Connection, params: Dict[str, Any], proxy: Optional[str] = None
50-
):
49+
def __init__(self, conn: Connection, params: Dict[str, Any], proxy: Optional[str] = None):
5150
self.conn = conn
5251
self.proxy = proxy
5352
self.summary_params = params
@@ -70,9 +69,7 @@ def get_summary(self, docs: Any) -> List[str]:
7069
cursor.outputtypehandler = output_type_handler
7170

7271
if self.proxy:
73-
cursor.execute(
74-
"begin utl_http.set_proxy(:proxy); end;", proxy=self.proxy
75-
)
72+
cursor.execute("begin utl_http.set_proxy(:proxy); end;", proxy=self.proxy)
7673

7774
if isinstance(docs, str):
7875
results = []
@@ -149,10 +146,7 @@ def get_summary(self, docs: Any) -> List[str]:
149146

150147
value = summary.getvalue(i)
151148

152-
results = [
153-
"" if value is None else str(value)
154-
for i in range(summary.actual_elements)
155-
]
149+
results = ["" if value is None else str(value) for i in range(summary.actual_elements)]
156150

157151
else:
158152
raise Exception("Invalid input type")

0 commit comments

Comments
 (0)