86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
from botocore.exceptions import ClientError
|
|
from langchain_core.tools import StructuredTool
|
|
|
|
from .config import TABLE
|
|
from .dynamo import dynamodb
|
|
|
|
|
|
class ReportTools:
|
|
def __init__(self, id_mapping: dict[str, str]):
|
|
self.id_mapping = id_mapping
|
|
|
|
def get_variable_value(self, id: str, variable: str) -> str:
|
|
"""
|
|
Get a specific variable's value from DynamoDB for a specific id.
|
|
|
|
Args:
|
|
id: The id of the data
|
|
variable: The variable/column name to retrieve from the table
|
|
|
|
Returns:
|
|
The content of the specified variable for the given id
|
|
"""
|
|
real_id = self.id_mapping.get(id, id)
|
|
try:
|
|
table = dynamodb.Table(TABLE)
|
|
response = table.get_item(Key={"id": real_id})
|
|
|
|
if "Item" not in response:
|
|
return f"No report found for month: {id}"
|
|
|
|
item = response["Item"]
|
|
content = item.get(variable, "")
|
|
|
|
if not content:
|
|
return f"Variable '{variable}' not found for month: {id}"
|
|
|
|
return f"<{id}>\n{content}\n</{id}>"
|
|
|
|
except ClientError as e:
|
|
error_message = e.response["Error"]["Message"]
|
|
return f"Error fetching report: {error_message}"
|
|
|
|
def get_variables_list(self, id: str) -> str:
|
|
"""
|
|
Get the list of variables available in the table for a specific month.
|
|
|
|
Args:
|
|
id: The id of the data
|
|
|
|
Returns:
|
|
The list of available variables/keys for the specified data
|
|
"""
|
|
real_id = self.id_mapping.get(id, id)
|
|
try:
|
|
table = dynamodb.Table(TABLE)
|
|
response = table.get_item(Key={"id": real_id})
|
|
|
|
if "Item" not in response:
|
|
return f"No data found for month: {id}"
|
|
|
|
item = response["Item"]
|
|
chaves_consolidadas = item.get("chaves_consolidadas", "")
|
|
|
|
if not chaves_consolidadas:
|
|
return f"No consolidated keys found for id: {id}"
|
|
|
|
return chaves_consolidadas
|
|
|
|
except ClientError as e:
|
|
error_message = e.response["Error"]["Message"]
|
|
return f"Error fetching consolidated keys: {error_message}"
|
|
|
|
def as_tools(self) -> list:
|
|
return [
|
|
StructuredTool.from_function(
|
|
self.get_variable_value,
|
|
name="get_variable_value",
|
|
description="Get a specific variable's data from DynamoDB for a specific id.",
|
|
),
|
|
StructuredTool.from_function(
|
|
self.get_variables_list,
|
|
name="get_variable_list",
|
|
description="Get the list of variables available in the table for a specific id.",
|
|
),
|
|
]
|