Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 65 additions & 5 deletions 2-notebooks/2-agent_service/6-agents-az-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"\n",
"from azure.identity import DefaultAzureCredential\n",
"from azure.ai.projects import AIProjectClient\n",
"from azure.ai.projects.models import AzureFunctionTool, AzureFunctionStorageQueue, MessageRole\n",
"from azure.ai.projects.models import AzureFunctionTool, AzureFunctionStorageQueue, MessageRole, SubmitToolOutputsAction, RequiredFunctionToolCall, ToolOutput\n",
"\n",
"# Load env variables from .env in parent dir\n",
"notebook_path = Path().absolute()\n",
Expand Down Expand Up @@ -243,15 +243,75 @@
" print(f\"💬 Created user message, ID: {message.id}\")\n",
"\n",
" # 3) Create and process agent run\n",
" run = project_client.agents.create_and_process_run(\n",
" run = project_client.agents.create_run(\n",
" thread_id=thread.id,\n",
" agent_id=agent_id\n",
" )\n",
" print(f\"🤖 Run finished with status: {run.status}\")\n",
" print(f\"🤖 Created run, ID: {run.id}\")\n",
" \n",
" # 4) Poll for run completion with proper REQUIRES_ACTION handling\n",
" max_retries = 60 # Maximum wait time in seconds\n",
" retry_count = 0\n",
" \n",
" while run.status in [\"queued\", \"in_progress\", \"requires_action\"]:\n",
" time.sleep(1) # Wait 1 second between status checks\n",
" run = project_client.agents.get_run(\n",
" thread_id=thread.id,\n",
" run_id=run.id\n",
" )\n",
" print(f\"📊 Run status: {run.status}\")\n",
" \n",
" retry_count += 1\n",
" if retry_count >= max_retries:\n",
" print(\"⏰ Run timed out after 60 seconds\")\n",
" break\n",
" \n",
" # Handle REQUIRES_ACTION status for Azure Function tools\n",
" if run.status == \"requires_action\":\n",
" if hasattr(run, \"required_action\") and isinstance(run.required_action, SubmitToolOutputsAction):\n",
" tool_calls = run.required_action.submit_tool_outputs.tool_calls\n",
" if not tool_calls:\n",
" print(\"❌ No tool calls found in required action\")\n",
" break\n",
" \n",
" print(f\"🔧 Processing {len(tool_calls)} tool call(s)...\")\n",
" tool_outputs = []\n",
" \n",
" for tool_call in tool_calls:\n",
" if isinstance(tool_call, RequiredFunctionToolCall):\n",
" print(f\"🔧 Executing tool call: {tool_call.function.name}\")\n",
" \n",
" # For Azure Function tools, we need to wait for the function to process\n",
" # the message from the input queue and respond via output queue.\n",
" # The actual function execution happens asynchronously.\n",
" # We'll provide a simple acknowledgment as output.\n",
" tool_output = f\"Azure Function {tool_call.function.name} call initiated with arguments: {tool_call.function.arguments}\"\n",
" \n",
" tool_outputs.append(\n",
" ToolOutput(\n",
" tool_call_id=tool_call.id,\n",
" output=tool_output\n",
" )\n",
" )\n",
" \n",
" if tool_outputs:\n",
" print(f\"📤 Submitting {len(tool_outputs)} tool output(s)...\")\n",
" run = project_client.agents.submit_tool_outputs_to_run(\n",
" thread_id=thread.id,\n",
" run_id=run.id,\n",
" tool_outputs=tool_outputs\n",
" )\n",
" print(f\"✅ Tool outputs submitted, run status: {run.status}\")\n",
" else:\n",
" print(\"❌ REQUIRES_ACTION status but no valid required_action found\")\n",
" break\n",
" \n",
" print(f\"🏁 Run finished with status: {run.status}\")\n",
" \n",
" if run.status == \"failed\":\n",
" print(f\"Run failed: {run.last_error}\")\n",
" print(f\"Run failed: {run.last_error}\")\n",
"\n",
" # 4) Retrieve messages\n",
" # 5) Retrieve messages\n",
" messages = project_client.agents.list_messages(thread_id=thread.id)\n",
" print(\"\\n🗣️ Conversation:\")\n",
" for m in reversed(messages.data): # oldest first\n",
Expand Down
Loading