

# Utilizzo di AgentCore Code Interpreter tramite AWS Filamenti
<a name="code-interpreter-using-strands"></a>

Le seguenti sezioni mostrano come utilizzare Amazon Bedrock AgentCore Code Interpreter con l'SDK Strands. [Prima di esaminare gli esempi in questa sezione, consulta Prerequisiti.](code-interpreter-getting-started.md#code-interpreter-prerequisites)

**Topics**
+ [Fase 1: Installare le dipendenze](#code-interpreter-strands-install)
+ [Passaggio 2: crea il tuo agente con AgentCore Code Interpreter](#code-interpreter-strands-create)
+ [Fase 3: Esegui l'agente](#code-interpreter-strands-run)

## Fase 1: Installare le dipendenze
<a name="code-interpreter-strands-install"></a>

Crea una cartella di progetto e installa i pacchetti richiesti:

**Nota**  
In Windows, usa: `.venv\Scripts\activate` 

```
mkdir agentcore-tools-quickstart
cd agentcore-tools-quickstart
python3 -m venv .venv
source .venv/bin/activate
```

Installa i pacchetti richiesti:

```
pip install bedrock-agentcore strands-agents strands-agents-tools
```

Questi pacchetti forniscono:
+  `bedrock-agentcore`: L'SDK per gli AgentCore strumenti Amazon Bedrock, incluso AgentCore Code Interpreter
+  `strands-agents`: Il framework degli agenti Strands
+  `strands-agents-tools`: Gli strumenti offerti dal framework degli agenti Strands

## Passaggio 2: crea il tuo agente con AgentCore Code Interpreter
<a name="code-interpreter-strands-create"></a>

Crea un file denominato `code_interpreter_agent.py` e aggiungi il seguente codice:

```
from strands import Agent
from strands_tools.code_interpreter import AgentCoreCodeInterpreter

# Initialize the Code Interpreter tool
code_interpreter_tool = AgentCoreCodeInterpreter(region="<Region>")

# Define the agent's system prompt
SYSTEM_PROMPT = """You are an AI assistant that validates answers through code execution.
When asked about code, algorithms, or calculations, write Python code to verify your answers."""

# Create an agent with the Code Interpreter tool
agent = Agent(
    tools=[code_interpreter_tool.code_interpreter],
    system_prompt=SYSTEM_PROMPT
)

# Test the agent with a sample prompt
prompt = "Calculate the first 10 Fibonacci numbers."
print(f"\n\nPrompt: {prompt}\n\n")

response = agent(prompt)
print(response.message["content"][0]["text"])
```

Questo codice:
+ Inizializza lo strumento AgentCore Code Interpreter per la tua regione
+ Crea un agente configurato per utilizzare l'esecuzione del codice per la convalida
+ Invia un messaggio chiedendo all'agente di calcolare i numeri di Fibonacci
+ Stampa la risposta dell'agente

## Fase 3: Esegui l'agente
<a name="code-interpreter-strands-run"></a>

Esegui lo script:

```
python code_interpreter_agent.py
```

 **Output previsto** 

Dovresti vedere la risposta dell'agente contenente i primi 10 numeri di Fibonacci. L'agente scriverà codice Python per calcolare la sequenza e restituire sia il codice che i risultati.

Se riscontri errori, verifica:
+ Il tuo ruolo IAM dispone delle autorizzazioni e della policy di fiducia corrette
+ Hai abilitato l'accesso al modello nella console Amazon Bedrock
+  AWS Le tue credenziali sono configurate correttamente