DEV Community

Pablo Nieto
Pablo Nieto

Posted on

Dotando a IAs con: V1 Enrich Legal (ETL-D API)

CONTENT:

  • The Hallucination Problem:
    Large Language Models (LLMs) often face the challenge of 'hallucination,' where they generate plausible-sounding but incorrect or nonsensical answers. This is mainly due to their probabilistic nature, which can lead to guessing when precise data validation is needed. The given ETL-D endpoint /v1/enrich/legal serves as a crucial tool for LLMs in domains requiring exact validation, such as legal or financial data processing. By integrating this endpoint, the LLM can deterministically validate Tax Identification Numbers and IBANs, preventing the generation of incorrect information and ensuring compliance with financial regulations.

  • Agent Tool Architecture:
    In an LLM Agent setup, the endpoint acts as a deterministic middleware, allowing the agent to offload specific validation tasks to a robust external service. This separation ensures the agent uses verified data for decision-making processes. The architecture involves the LLM acting as an orchestrator that invokes deterministic tools, like the /v1/enrich/legal endpoint, whenever accurate legal or financial validation is needed. This enhances the reliability of decision-making in complex workflows.

  • Implementation:
    Below is a Python code block illustrating how to integrate the ETL-D SDK as a tool for LangChain. This setup allows an LLM agent to invoke the validation endpoint when required, effectively using it as a callable function:

  from langchain.agents import AgentExecutor, Tool
  from etld import Client

  class LegalValidationTool:
      def __init__(self, api_key):
          self.client = Client(api_key=api_key)

      def validate_legal_data(self, tax_id, country, iban):
          try:
              response = self.client.enrich_legal_v1_enrich_legal_post(
                  {
                      "tax_id": tax_id,
                      "country": country,
                      "iban": iban
                  }
              )
              return response
          except Exception as e:
              if '402' in str(e):
                  raise ValueError("Payment Required - Ensure sufficient credits.")
              elif '422' in str(e):
                  raise ValueError("Validation Error - Check input values.")
              else:
                  raise

  def create_legal_validation_tool(api_key):
      legal_tool = LegalValidationTool(api_key=api_key)

      return Tool(
          name="LegalValidation",
          func=legal_tool.validate_legal_data,
          description="Tool to validate Tax IDs and IBANs."
      )

  # Example usage with LangChain
  api_key = "your_api_key_here"
  legal_tool = create_legal_validation_tool(api_key)
  agent = AgentExecutor(tools=[legal_tool])

  # User request, invoking the tool
  result = agent.run_tool(
      tool_name="LegalValidation",
      tool_input={"tax_id": "B12345678", "country": "ES", "iban": "ES9821000000000000000000"}
  )
  print(result)
Enter fullscreen mode Exit fullscreen mode
  • Deterministic Output Specs: The output from the /v1/enrich/legal endpoint to the LLM contains the results of the validation process. Upon successful execution, the LLM receives a structured JSON response confirming the validation status and details of the Tax ID and IBAN checked. It can also indicate errors such as payment depletion or input validation issues, which are handled gracefully within the agent's architecture to maintain the system's robustness. This consistent output ensures that the LLM only processes and generates outputs grounded in validated data.

🔗 Get the Agent Tool Code: GitHub Gist

Top comments (0)