mirror of
https://github.com/hwchase17/langchain.git
synced 2025-12-01 22:10:11 +00:00
```python
"""python scripts/update_mypy_ruff.py"""
import glob
import tomllib
from pathlib import Path
import toml
import subprocess
import re
ROOT_DIR = Path(__file__).parents[1]
def main():
for path in glob.glob(str(ROOT_DIR / "libs/**/pyproject.toml"), recursive=True):
print(path)
with open(path, "rb") as f:
pyproject = tomllib.load(f)
try:
pyproject["tool"]["poetry"]["group"]["typing"]["dependencies"]["mypy"] = (
"^1.10"
)
pyproject["tool"]["poetry"]["group"]["lint"]["dependencies"]["ruff"] = (
"^0.5"
)
except KeyError:
continue
with open(path, "w") as f:
toml.dump(pyproject, f)
cwd = "/".join(path.split("/")[:-1])
completed = subprocess.run(
"poetry lock --no-update; poetry install --with typing; poetry run mypy . --no-color",
cwd=cwd,
shell=True,
capture_output=True,
text=True,
)
logs = completed.stdout.split("\n")
to_ignore = {}
for l in logs:
if re.match("^(.*)\:(\d+)\: error:.*\[(.*)\]", l):
path, line_no, error_type = re.match(
"^(.*)\:(\d+)\: error:.*\[(.*)\]", l
).groups()
if (path, line_no) in to_ignore:
to_ignore[(path, line_no)].append(error_type)
else:
to_ignore[(path, line_no)] = [error_type]
print(len(to_ignore))
for (error_path, line_no), error_types in to_ignore.items():
all_errors = ", ".join(error_types)
full_path = f"{cwd}/{error_path}"
try:
with open(full_path, "r") as f:
file_lines = f.readlines()
except FileNotFoundError:
continue
file_lines[int(line_no) - 1] = (
file_lines[int(line_no) - 1][:-1] + f" # type: ignore[{all_errors}]\n"
)
with open(full_path, "w") as f:
f.write("".join(file_lines))
subprocess.run(
"poetry run ruff format .; poetry run ruff --select I --fix .",
cwd=cwd,
shell=True,
capture_output=True,
text=True,
)
if __name__ == "__main__":
main()
```
142 lines
4.8 KiB
Python
142 lines
4.8 KiB
Python
"""Tools for interacting with an Apache Cassandra database."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import traceback
|
|
from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence, Type, Union
|
|
|
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
from langchain_core.pydantic_v1 import BaseModel, Field
|
|
from langchain_core.tools import BaseTool
|
|
|
|
from langchain_community.utilities.cassandra_database import CassandraDatabase
|
|
|
|
if TYPE_CHECKING:
|
|
from cassandra.cluster import ResultSet
|
|
|
|
|
|
class BaseCassandraDatabaseTool(BaseModel):
|
|
"""Base tool for interacting with an Apache Cassandra database."""
|
|
|
|
db: CassandraDatabase = Field(exclude=True)
|
|
|
|
class Config(BaseTool.Config):
|
|
pass
|
|
|
|
|
|
class _QueryCassandraDatabaseToolInput(BaseModel):
|
|
query: str = Field(..., description="A detailed and correct CQL query.")
|
|
|
|
|
|
class QueryCassandraDatabaseTool(BaseCassandraDatabaseTool, BaseTool):
|
|
"""Tool for querying an Apache Cassandra database with provided CQL."""
|
|
|
|
name: str = "cassandra_db_query"
|
|
description: str = """
|
|
Execute a CQL query against the database and get back the result.
|
|
If the query is not correct, an error message will be returned.
|
|
If an error is returned, rewrite the query, check the query, and try again.
|
|
"""
|
|
args_schema: Type[BaseModel] = _QueryCassandraDatabaseToolInput
|
|
|
|
def _run(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> Union[str, Sequence[Dict[str, Any]], ResultSet]:
|
|
"""Execute the query, return the results or an error message."""
|
|
try:
|
|
return self.db.run(query)
|
|
except Exception as e:
|
|
"""Format the error message"""
|
|
return f"Error: {e}\n{traceback.format_exc()}"
|
|
|
|
|
|
class _GetSchemaCassandraDatabaseToolInput(BaseModel):
|
|
keyspace: str = Field(
|
|
...,
|
|
description=("The name of the keyspace for which to return the schema."),
|
|
)
|
|
|
|
|
|
class GetSchemaCassandraDatabaseTool(BaseCassandraDatabaseTool, BaseTool):
|
|
"""Tool for getting the schema of a keyspace in an Apache Cassandra database."""
|
|
|
|
name: str = "cassandra_db_schema"
|
|
description: str = """
|
|
Input to this tool is a keyspace name, output is a table description
|
|
of Apache Cassandra tables.
|
|
If the query is not correct, an error message will be returned.
|
|
If an error is returned, report back to the user that the keyspace
|
|
doesn't exist and stop.
|
|
"""
|
|
|
|
args_schema: Type[BaseModel] = _GetSchemaCassandraDatabaseToolInput
|
|
|
|
def _run(
|
|
self,
|
|
keyspace: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Get the schema for a keyspace."""
|
|
try:
|
|
tables = self.db.get_keyspace_tables(keyspace)
|
|
return "".join([table.as_markdown() + "\n\n" for table in tables])
|
|
except Exception as e:
|
|
"""Format the error message"""
|
|
return f"Error: {e}\n{traceback.format_exc()}"
|
|
|
|
|
|
class _GetTableDataCassandraDatabaseToolInput(BaseModel):
|
|
keyspace: str = Field(
|
|
...,
|
|
description=("The name of the keyspace containing the table."),
|
|
)
|
|
table: str = Field(
|
|
...,
|
|
description=("The name of the table for which to return data."),
|
|
)
|
|
predicate: str = Field(
|
|
...,
|
|
description=("The predicate for the query that uses the primary key."),
|
|
)
|
|
limit: int = Field(
|
|
...,
|
|
description=("The maximum number of rows to return."),
|
|
)
|
|
|
|
|
|
class GetTableDataCassandraDatabaseTool(BaseCassandraDatabaseTool, BaseTool):
|
|
"""
|
|
Tool for getting data from a table in an Apache Cassandra database.
|
|
Use the WHERE clause to specify the predicate for the query that uses the
|
|
primary key. A blank predicate will return all rows. Avoid this if possible.
|
|
Use the limit to specify the number of rows to return. A blank limit will
|
|
return all rows.
|
|
"""
|
|
|
|
name: str = "cassandra_db_select_table_data"
|
|
description: str = """
|
|
Tool for getting data from a table in an Apache Cassandra database.
|
|
Use the WHERE clause to specify the predicate for the query that uses the
|
|
primary key. A blank predicate will return all rows. Avoid this if possible.
|
|
Use the limit to specify the number of rows to return. A blank limit will
|
|
return all rows.
|
|
"""
|
|
args_schema: Type[BaseModel] = _GetTableDataCassandraDatabaseToolInput
|
|
|
|
def _run(
|
|
self,
|
|
keyspace: str,
|
|
table: str,
|
|
predicate: str,
|
|
limit: int,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Get data from a table in a keyspace."""
|
|
try:
|
|
return self.db.get_table_data(keyspace, table, predicate, limit)
|
|
except Exception as e:
|
|
"""Format the error message"""
|
|
return f"Error: {e}\n{traceback.format_exc()}"
|