AWS Strands를 통한 AgentCore 코드 해석기 사용
다음 섹션에서는 Amazon Bedrock AgentCore 코드 인터프리터를 Strands SDK와 함께 사용하는 방법을 보여줍니다. 이 섹션의 예제를 살펴보기 전에 사전 조건을 참조하세요.
1단계: 종속성 설치
프로젝트 폴더를 생성하고 필요한 패키지를 설치합니다.
참고
Windows에서는 다음을 사용합니다. .venv\Scripts\activate
mkdir agentcore-tools-quickstart cd agentcore-tools-quickstart python3 -m venv .venv source .venv/bin/activate
필수 패키지를 설치합니다.
pip install bedrock-agentcore strands-agents strands-agents-tools
이러한 패키지는 다음을 제공합니다.
-
bedrock-agentcore: AgentCore 코드 해석기를 포함한 Amazon Bedrock AgentCore용 SDK 도구 -
strands-agents: Strands 에이전트 프레임워크 -
strands-agents-tools: Strands 에이전트 프레임워크가 제공하는 도구
2단계: AgentCore 코드 해석기를 사용하여 에이전트 생성
라는 파일을 생성하고 다음 코드를 code_interpreter_agent.py 추가합니다.
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"])
이 코드는 다음과 같습니다.
-
리전의 AgentCore 코드 해석기 도구를 초기화합니다.
-
검증에 코드 실행을 사용하도록 구성된 에이전트를 생성합니다.
-
에이전트에게 Fibonacci 번호를 계산하도록 요청하는 프롬프트를 보냅니다.
-
에이전트의 응답을 인쇄합니다.
3단계: 에이전트 실행
스크립트를 실행합니다.
python code_interpreter_agent.py
예상 출력
처음 10개의 Fibonacci 번호가 포함된 에이전트의 응답이 표시됩니다. 에이전트는 Python 코드를 작성하여 시퀀스를 계산하고 코드와 결과를 모두 반환합니다.
오류가 발생하면 다음을 확인합니다.
-
IAM 역할에 올바른 권한 및 신뢰 정책이 있음
-
Amazon Bedrock 콘솔에서 모델 액세스가 활성화되어 있습니다.
-
자격 AWS 증명이 올바르게 구성됨