Create Strands Agents in Python. The simple way.

Strand Agents is all about an open source framework and SDK (in Python and TypeScript) designed to build, orchestrate, and deploy autonomous Artificial Intelligence agents.

Strand agents are very versatile, but more flexible than traditional agents since running a Python script is enough for it to start executing tasks. Using it is very simple.

Run PIP with the following requirement:

strands-agents>=1.55,<2

Inside a python file:

import os

from strands import Agent
from strands.models import BedrockModel

from schema import ConstanciaSituacionFiscal

MODEL_ID = os.environ.get("BEDROCK_MODEL_ID", "global.anthropic.claude-sonnet-4-6")
REGION = os.environ.get("BEDROCK_REGION") or None  # None -> 
MAX_TOKENS = int(os.environ.get("MAX_TOKENS", "4096"))

We tell the program the model we will use, the region, and the maximum tokens per transaction.

Now we define the prompt that will give personality to the strand agent:

SYSTEM_PROMPT = """You are a document data extractor.
You receive a PDF and return only the data in the requested schema.

Rules:
- Copy the values as they appear in the document; do not make up or fill in missing data (use null).
- Normalize all dates to ISO format YYYY-MM-DD (e.g. "JUNE 17, 2019" -> "2019-06-17").
- Percentages as a number (100, not "100%").
- If the PDF is not readable, respond document_valid=false
  with a brief reason and leave the rest in null.
"""

What our agent does is read PDF documents and scramble their values.

# The model is stateless: it is created once per process.
_model = BedrockModel(model_id=MODEL_ID, region_name=REGION, temperature=0, max_tokens=MAX_TOKENS)


def extract(pdf_bytes: bytes) -> dict:
    """Returns the PDF content as a dict ready to serialize to JSON."""
    # The Agent keeps conversation history: a new one per document.
    agent = Agent(model=_model, system_prompt=SYSTEM_PROMPT, callback_handler=None)
    result = agent(
        [
            {"document": {"format": "pdf", "name": "documento", "source": {"bytes": pdf_bytes}}},
            {"text": "Extrae los datos de este documento."},
        ],
        structured_output_model=DefinirJSONSchemeAqui,
    )
    return result.structured_output.model_dump()

Done, the agent processes the PDF and returns a json defined in the schema we want.

Artículos Relacionados

Comentarios