View a markdown version of this page

直接使用 AgentCore Code Interpreter - Amazon Bedrock AgentCore

直接使用 AgentCore Code Interpreter

下列各節說明如何在沒有代理程式架構的情況下直接使用 Amazon Bedrock AgentCore Code Interpreter。當您想要以程式設計方式執行特定程式碼片段時,這特別有用。在完成本節中的範例之前,請參閱先決條件

步驟 1:選擇您的方法並安裝相依性

Amazon Bedrock AgentCore 提供兩種與 AgentCore Code Interpreter 互動的方式:使用高階 SDK 用戶端或直接使用 boto3。

  • SDK 用戶端bedrock_agentcore SDK 提供簡化的界面,可處理工作階段管理詳細資訊。對大多數應用程式使用此方法。

  • Boto3 用戶端 : AWS 開發套件可讓您直接存取 AgentCore Code Interpreter API 操作。當您需要精細控制工作階段組態,或想要與現有的 boto3 型應用程式整合時,請使用此方法。

建立專案資料夾 (如果您之前沒有建立) 並安裝必要的套件:

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

在 Windows 上,使用: .venv\Scripts\activate

安裝必要的套件:

pip install bedrock-agentcore boto3

這些套件提供:

  • bedrock-agentcore :適用於 Amazon Bedrock AgentCore 的工具開發套件,包括 AgentCore Code Interpreter

  • boto3 : AWS SDK for Python (Boto3) 以建立、設定和管理 AWS 服務

步驟 2:執行程式碼

選擇下列其中一種方法來使用 AgentCore Code Interpreter 執行程式碼:

注意

將 取代<Region>為您實際 AWS 的區域 (例如 us-east-1us-west-2 )。

範例
SDK Client
  1. 建立名為 的檔案direct_code_execution_sdk.py,並新增下列程式碼:

    from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter import json # Initialize the Code Interpreter client for your region code_client = CodeInterpreter('<Region>') # Start a Code Interpreter session code_client.start() try: # Execute Python code response = code_client.invoke("executeCode", { "language": "python", "code": 'print("Hello World!!!")' }) # Process and print the response for event in response["stream"]: print(json.dumps(event["result"], indent=2)) finally: # Always clean up the session code_client.stop()

    此程式碼:

    • 為您的區域建立 AgentCore Code Interpreter 用戶端

    • 啟動工作階段 (在執行程式碼之前需要)

    • 執行 Python 程式碼,並使用完整的事件詳細資訊串流結果

    • 停止工作階段以清除資源

      執行指令碼

      執行以下 命令:

      python direct_code_execution_sdk.py

      預期的輸出

      您應該會在輸出內容Hello World!!!中看到 JSON 回應,其中包含 的執行結果。

Boto3
  1. 建立名為 的檔案direct_code_execution_boto3.py,並新增下列程式碼:

    import boto3 import json # Code to execute code_to_execute = """ print("Hello World!!!") """ # Initialize the bedrock-agentcore client client = boto3.client( "bedrock-agentcore", region_name="<Region>" ) # Start a Code Interpreter session session_response = client.start_code_interpreter_session( codeInterpreterIdentifier="aws.codeinterpreter.v1", name="my-code-session", sessionTimeoutSeconds=900 ) session_id = session_response["sessionId"] print(f"Started session: {session_id}\n\n") try: # Execute code in the session execute_response = client.invoke_code_interpreter( codeInterpreterIdentifier="aws.codeinterpreter.v1", sessionId=session_id, name="executeCode", arguments={ "language": "python", "code": code_to_execute } ) # Extract and print the text output from the stream for event in execute_response['stream']: if 'result' in event: result = event['result'] if 'content' in result: for content_item in result['content']: if content_item['type'] == 'text': print(content_item['text']) finally: # Stop the session when done client.stop_code_interpreter_session( codeInterpreterIdentifier="aws.codeinterpreter.v1", sessionId=session_id ) print(f"\n\nStopped session: {session_id}")

    此程式碼:

    • 為 bedrock-agentcore 服務建立 boto3 用戶端

    • 啟動具有 900 秒逾時的 AgentCore Code Interpreter 工作階段

    • 使用工作階段 ID 執行 Python 程式碼

    • 剖析串流回應以擷取文字輸出

    • 適當地停止工作階段以釋出資源

      boto3 方法需要明確的工作階段管理。您必須在執行程式碼start_code_interpreter_session之前和stop_code_interpreter_session完成後呼叫 。

      執行指令碼

      執行以下 命令:

      python direct_code_execution_boto3.py

      預期的輸出

      您應該會看到Hello World!!!列印為程式碼執行的結果,以及工作階段 ID 資訊。