mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b48604a28 | ||
|
|
8bc7c3a7a8 |
@@ -42,15 +42,34 @@ from azure.ai.agentserver.responses.models import (
|
||||
MessageContentOutputTextContent,
|
||||
MessageContentReasoningTextContent,
|
||||
MessageContentRefusalContent,
|
||||
OAuthConsentRequestOutputItem,
|
||||
OutputItem,
|
||||
OutputItemApplyPatchToolCall,
|
||||
OutputItemApplyPatchToolCallOutput,
|
||||
OutputItemCodeInterpreterToolCall,
|
||||
OutputItemComputerToolCall,
|
||||
OutputItemComputerToolCallOutputResource,
|
||||
OutputItemCustomToolCall,
|
||||
OutputItemCustomToolCallOutput,
|
||||
OutputItemFileSearchToolCall,
|
||||
OutputItemFunctionShellCall,
|
||||
OutputItemFunctionShellCallOutput,
|
||||
OutputItemFunctionToolCall,
|
||||
OutputItemImageGenToolCall,
|
||||
OutputItemLocalShellToolCall,
|
||||
OutputItemLocalShellToolCallOutput,
|
||||
OutputItemMcpApprovalRequest,
|
||||
OutputItemMcpApprovalResponseResource,
|
||||
OutputItemMcpToolCall,
|
||||
OutputItemMessage,
|
||||
OutputItemOutputMessage,
|
||||
OutputItemReasoningItem,
|
||||
OutputItemWebSearchToolCall,
|
||||
OutputMessageContent,
|
||||
OutputMessageContentOutputTextContent,
|
||||
OutputMessageContentRefusalContent,
|
||||
ResponseStreamEvent,
|
||||
StructuredOutputsOutputItem,
|
||||
SummaryTextContent,
|
||||
TextContent,
|
||||
)
|
||||
@@ -572,6 +591,203 @@ def _to_message(item: OutputItem) -> Message:
|
||||
contents.append(Content.from_text(summary.text))
|
||||
return Message(role="assistant", contents=contents)
|
||||
|
||||
if item.type == "mcp_call":
|
||||
mcp = cast(OutputItemMcpToolCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[
|
||||
Content.from_mcp_server_tool_call(
|
||||
mcp.id,
|
||||
mcp.name,
|
||||
server_name=mcp.server_label,
|
||||
arguments=mcp.arguments,
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
if item.type == "mcp_approval_request":
|
||||
mcp_req = cast(OutputItemMcpApprovalRequest, item)
|
||||
fc = Content.from_mcp_server_tool_call(
|
||||
mcp_req.id,
|
||||
mcp_req.name,
|
||||
server_name=mcp_req.server_label,
|
||||
arguments=mcp_req.arguments,
|
||||
)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[Content.from_function_approval_request(mcp_req.id, fc)],
|
||||
)
|
||||
|
||||
if item.type == "mcp_approval_response":
|
||||
mcp_resp = cast(OutputItemMcpApprovalResponseResource, item)
|
||||
# Build a placeholder function_call Content since the original call details are not available
|
||||
fc = Content.from_function_call(mcp_resp.approval_request_id, "mcp_approval")
|
||||
return Message(
|
||||
role="user",
|
||||
contents=[Content.from_function_approval_response(mcp_resp.approve, mcp_resp.id, fc)],
|
||||
)
|
||||
|
||||
if item.type == "code_interpreter_call":
|
||||
ci = cast(OutputItemCodeInterpreterToolCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[Content.from_code_interpreter_tool_call(call_id=ci.id)],
|
||||
)
|
||||
|
||||
if item.type == "image_generation_call":
|
||||
ig = cast(OutputItemImageGenToolCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[Content.from_image_generation_tool_call(image_id=ig.id)],
|
||||
)
|
||||
|
||||
if item.type == "shell_call":
|
||||
sc = cast(OutputItemFunctionShellCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[
|
||||
Content.from_shell_tool_call(
|
||||
call_id=sc.call_id,
|
||||
commands=sc.action.commands,
|
||||
status=str(sc.status),
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
if item.type == "shell_call_output":
|
||||
sco = cast(OutputItemFunctionShellCallOutput, item)
|
||||
outputs = [
|
||||
Content.from_shell_command_output(
|
||||
stdout=out.stdout or "",
|
||||
stderr=out.stderr or "",
|
||||
exit_code=getattr(out.outcome, "exit_code", None) if hasattr(out, "outcome") else None,
|
||||
)
|
||||
for out in (sco.output or [])
|
||||
]
|
||||
return Message(
|
||||
role="tool",
|
||||
contents=[
|
||||
Content.from_shell_tool_result(
|
||||
call_id=sco.call_id,
|
||||
outputs=outputs,
|
||||
max_output_length=sco.max_output_length,
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
if item.type == "local_shell_call":
|
||||
lsc = cast(OutputItemLocalShellToolCall, item)
|
||||
commands = lsc.action.command if hasattr(lsc.action, "command") and lsc.action.command else []
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[
|
||||
Content.from_shell_tool_call(
|
||||
call_id=lsc.call_id,
|
||||
commands=commands,
|
||||
status=str(lsc.status),
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
if item.type == "local_shell_call_output":
|
||||
lsco = cast(OutputItemLocalShellToolCallOutput, item)
|
||||
return Message(
|
||||
role="tool",
|
||||
contents=[
|
||||
Content.from_shell_tool_result(
|
||||
call_id=lsco.id,
|
||||
outputs=[Content.from_shell_command_output(stdout=lsco.output)],
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
if item.type == "file_search_call":
|
||||
fs = cast(OutputItemFileSearchToolCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[
|
||||
Content.from_function_call(
|
||||
fs.id,
|
||||
"file_search",
|
||||
arguments=json.dumps({"queries": fs.queries}),
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
if item.type == "web_search_call":
|
||||
ws = cast(OutputItemWebSearchToolCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[Content.from_function_call(ws.id, "web_search")],
|
||||
)
|
||||
|
||||
if item.type == "computer_call":
|
||||
cc = cast(OutputItemComputerToolCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[
|
||||
Content.from_function_call(
|
||||
cc.call_id,
|
||||
"computer_use",
|
||||
arguments=str(cc.action),
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
if item.type == "computer_call_output":
|
||||
cco = cast(OutputItemComputerToolCallOutputResource, item)
|
||||
return Message(
|
||||
role="tool",
|
||||
contents=[Content.from_function_result(cco.call_id, result=str(cco.output))],
|
||||
)
|
||||
|
||||
if item.type == "custom_tool_call":
|
||||
ct = cast(OutputItemCustomToolCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[Content.from_function_call(ct.call_id, ct.name, arguments=ct.input)],
|
||||
)
|
||||
|
||||
if item.type == "custom_tool_call_output":
|
||||
cto = cast(OutputItemCustomToolCallOutput, item)
|
||||
output = cto.output if isinstance(cto.output, str) else str(cto.output)
|
||||
return Message(
|
||||
role="tool",
|
||||
contents=[Content.from_function_result(cto.call_id, result=output)],
|
||||
)
|
||||
|
||||
if item.type == "apply_patch_call":
|
||||
ap = cast(OutputItemApplyPatchToolCall, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[
|
||||
Content.from_function_call(
|
||||
ap.call_id,
|
||||
"apply_patch",
|
||||
arguments=str(ap.operation),
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
if item.type == "apply_patch_call_output":
|
||||
apo = cast(OutputItemApplyPatchToolCallOutput, item)
|
||||
return Message(
|
||||
role="tool",
|
||||
contents=[Content.from_function_result(apo.call_id, result=apo.output or "")],
|
||||
)
|
||||
|
||||
if item.type == "oauth_consent_request":
|
||||
oauth = cast(OAuthConsentRequestOutputItem, item)
|
||||
return Message(
|
||||
role="assistant",
|
||||
contents=[Content.from_oauth_consent_request(oauth.consent_link)],
|
||||
)
|
||||
|
||||
if item.type == "structured_outputs":
|
||||
so = cast(StructuredOutputsOutputItem, item)
|
||||
text = json.dumps(so.output) if not isinstance(so.output, str) else so.output
|
||||
return Message(role="assistant", contents=[Content.from_text(text)])
|
||||
|
||||
raise ValueError(f"Unsupported OutputItem type: {item.type}")
|
||||
|
||||
|
||||
@@ -752,7 +968,7 @@ async def _to_outputs(stream: ResponseEventStream, content: Content) -> AsyncIte
|
||||
yield event
|
||||
else:
|
||||
# Log a warning for unsupported content types instead of raising an error to avoid breaking the response stream.
|
||||
logger.warning(f"Content type '{content.type}' is not supported yet.")
|
||||
logger.warning(f"Content type '{content.type}' is not supported yet. This is usually safe to ignore.")
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
@@ -29,6 +29,7 @@ from azure.ai.agentserver.responses import InMemoryResponseProvider
|
||||
from typing_extensions import Any
|
||||
|
||||
from agent_framework_foundry_hosting import ResponsesHostServer
|
||||
from agent_framework_foundry_hosting._responses import _to_message # pyright: ignore[reportPrivateUsage]
|
||||
|
||||
# region Helpers
|
||||
|
||||
@@ -522,3 +523,395 @@ class TestStreaming:
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region _to_message conversion
|
||||
|
||||
|
||||
class TestToMessage:
|
||||
"""Tests for _to_message covering all supported OutputItem types."""
|
||||
|
||||
def test_output_message(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemOutputMessage, OutputMessageContentOutputTextContent
|
||||
|
||||
item = OutputItemOutputMessage({
|
||||
"type": "output_message",
|
||||
"role": "assistant",
|
||||
"content": [OutputMessageContentOutputTextContent({"type": "output_text", "text": "hello"})],
|
||||
"status": "completed",
|
||||
"id": "msg-1",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert len(msg.contents) == 1
|
||||
assert msg.contents[0].type == "text"
|
||||
assert msg.contents[0].text == "hello"
|
||||
|
||||
def test_message(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import MessageContentInputTextContent, OutputItemMessage
|
||||
|
||||
item = OutputItemMessage({
|
||||
"type": "message",
|
||||
"role": "user",
|
||||
"content": [MessageContentInputTextContent({"type": "input_text", "text": "hi"})],
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "user"
|
||||
assert len(msg.contents) == 1
|
||||
assert msg.contents[0].text == "hi"
|
||||
|
||||
def test_function_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemFunctionToolCall
|
||||
|
||||
item = OutputItemFunctionToolCall({
|
||||
"type": "function_call",
|
||||
"call_id": "call_1",
|
||||
"name": "get_weather",
|
||||
"arguments": '{"city": "NYC"}',
|
||||
"status": "completed",
|
||||
"id": "fc-1",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "function_call"
|
||||
assert msg.contents[0].call_id == "call_1"
|
||||
assert msg.contents[0].name == "get_weather"
|
||||
|
||||
def test_function_call_output(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import FunctionCallOutputItemParam
|
||||
|
||||
item = FunctionCallOutputItemParam({"type": "function_call_output", "call_id": "call_1", "output": "sunny"})
|
||||
msg = _to_message(item) # type: ignore[arg-type]
|
||||
assert msg.role == "tool"
|
||||
assert msg.contents[0].type == "function_result"
|
||||
assert msg.contents[0].call_id == "call_1"
|
||||
assert msg.contents[0].result == "sunny"
|
||||
|
||||
def test_reasoning(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemReasoningItem, SummaryTextContent
|
||||
|
||||
item = OutputItemReasoningItem({
|
||||
"type": "reasoning",
|
||||
"id": "r-1",
|
||||
"summary": [SummaryTextContent({"type": "summary_text", "text": "thinking hard"})],
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert len(msg.contents) == 1
|
||||
assert msg.contents[0].text == "thinking hard"
|
||||
|
||||
def test_reasoning_no_summary(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemReasoningItem
|
||||
|
||||
item = OutputItemReasoningItem({"type": "reasoning", "id": "r-2"})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents == []
|
||||
|
||||
def test_mcp_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemMcpToolCall
|
||||
|
||||
item = OutputItemMcpToolCall({
|
||||
"type": "mcp_call",
|
||||
"id": "mcp-1",
|
||||
"server_label": "my_server",
|
||||
"name": "search",
|
||||
"arguments": '{"q": "test"}',
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "mcp_server_tool_call"
|
||||
assert msg.contents[0].server_name == "my_server"
|
||||
assert msg.contents[0].tool_name == "search"
|
||||
|
||||
def test_mcp_approval_request(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemMcpApprovalRequest
|
||||
|
||||
item = OutputItemMcpApprovalRequest({
|
||||
"type": "mcp_approval_request",
|
||||
"id": "apr-1",
|
||||
"server_label": "srv",
|
||||
"name": "dangerous_tool",
|
||||
"arguments": "{}",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "function_approval_request"
|
||||
|
||||
def test_mcp_approval_response(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemMcpApprovalResponseResource
|
||||
|
||||
item = OutputItemMcpApprovalResponseResource({
|
||||
"type": "mcp_approval_response",
|
||||
"id": "resp-1",
|
||||
"approval_request_id": "apr-1",
|
||||
"approve": True,
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "user"
|
||||
assert msg.contents[0].type == "function_approval_response"
|
||||
assert msg.contents[0].approved is True
|
||||
|
||||
def test_code_interpreter_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemCodeInterpreterToolCall
|
||||
|
||||
item = OutputItemCodeInterpreterToolCall({
|
||||
"type": "code_interpreter_call",
|
||||
"id": "ci-1",
|
||||
"status": "completed",
|
||||
"container_id": "c-1",
|
||||
"code": "print('hi')",
|
||||
"outputs": [],
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "code_interpreter_tool_call"
|
||||
|
||||
def test_image_generation_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemImageGenToolCall
|
||||
|
||||
item = OutputItemImageGenToolCall({"type": "image_generation_call", "id": "ig-1", "status": "completed"})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "image_generation_tool_call"
|
||||
|
||||
def test_shell_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import (
|
||||
FunctionShellAction,
|
||||
FunctionShellCallEnvironment,
|
||||
OutputItemFunctionShellCall,
|
||||
)
|
||||
|
||||
item = OutputItemFunctionShellCall({
|
||||
"type": "shell_call",
|
||||
"id": "sc-1",
|
||||
"call_id": "call_sc",
|
||||
"action": FunctionShellAction({"commands": ["ls", "-la"], "timeout_ms": 5000, "max_output_length": 1024}),
|
||||
"status": "completed",
|
||||
"environment": FunctionShellCallEnvironment({"type": "local"}),
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "shell_tool_call"
|
||||
assert msg.contents[0].commands == ["ls", "-la"]
|
||||
assert msg.contents[0].call_id == "call_sc"
|
||||
|
||||
def test_shell_call_output(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import (
|
||||
FunctionShellCallOutputContent,
|
||||
FunctionShellCallOutputExitOutcome,
|
||||
OutputItemFunctionShellCallOutput,
|
||||
)
|
||||
|
||||
item = OutputItemFunctionShellCallOutput({
|
||||
"type": "shell_call_output",
|
||||
"id": "sco-1",
|
||||
"call_id": "call_sc",
|
||||
"status": "completed",
|
||||
"output": [
|
||||
FunctionShellCallOutputContent({
|
||||
"stdout": "file.txt",
|
||||
"stderr": "",
|
||||
"outcome": FunctionShellCallOutputExitOutcome({"exit_code": 0}),
|
||||
})
|
||||
],
|
||||
"max_output_length": 1024,
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "tool"
|
||||
assert msg.contents[0].type == "shell_tool_result"
|
||||
assert msg.contents[0].call_id == "call_sc"
|
||||
|
||||
def test_local_shell_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import LocalShellExecAction, OutputItemLocalShellToolCall
|
||||
|
||||
item = OutputItemLocalShellToolCall({
|
||||
"type": "local_shell_call",
|
||||
"id": "lsc-1",
|
||||
"call_id": "call_lsc",
|
||||
"action": LocalShellExecAction({"type": "exec", "command": ["echo", "hello"], "env": {}}),
|
||||
"status": "completed",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "shell_tool_call"
|
||||
assert msg.contents[0].commands == ["echo", "hello"]
|
||||
|
||||
def test_local_shell_call_output(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemLocalShellToolCallOutput
|
||||
|
||||
item = OutputItemLocalShellToolCallOutput({
|
||||
"type": "local_shell_call_output",
|
||||
"id": "lsco-1",
|
||||
"output": "hello\n",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "tool"
|
||||
assert msg.contents[0].type == "shell_tool_result"
|
||||
|
||||
def test_file_search_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemFileSearchToolCall
|
||||
|
||||
item = OutputItemFileSearchToolCall({
|
||||
"type": "file_search_call",
|
||||
"id": "fs-1",
|
||||
"status": "completed",
|
||||
"queries": ["what is AI"],
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "function_call"
|
||||
assert msg.contents[0].name == "file_search"
|
||||
assert '"what is AI"' in (msg.contents[0].arguments or "")
|
||||
|
||||
def test_web_search_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemWebSearchToolCall, WebSearchActionSearch
|
||||
|
||||
item = OutputItemWebSearchToolCall({
|
||||
"type": "web_search_call",
|
||||
"id": "ws-1",
|
||||
"status": "completed",
|
||||
"action": WebSearchActionSearch({"type": "search", "query": "test"}),
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "function_call"
|
||||
assert msg.contents[0].name == "web_search"
|
||||
|
||||
def test_computer_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import ComputerAction, OutputItemComputerToolCall
|
||||
|
||||
item = OutputItemComputerToolCall({
|
||||
"type": "computer_call",
|
||||
"id": "cc-1",
|
||||
"call_id": "call_cc",
|
||||
"action": ComputerAction({"type": "click"}),
|
||||
"pending_safety_checks": [],
|
||||
"status": "completed",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "function_call"
|
||||
assert msg.contents[0].name == "computer_use"
|
||||
|
||||
def test_computer_call_output(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import (
|
||||
ComputerScreenshotImage,
|
||||
OutputItemComputerToolCallOutputResource,
|
||||
)
|
||||
|
||||
item = OutputItemComputerToolCallOutputResource({
|
||||
"type": "computer_call_output",
|
||||
"call_id": "call_cc",
|
||||
"output": ComputerScreenshotImage({
|
||||
"type": "computer_screenshot",
|
||||
"image_url": "data:image/png;base64,abc",
|
||||
}),
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "tool"
|
||||
assert msg.contents[0].type == "function_result"
|
||||
assert msg.contents[0].call_id == "call_cc"
|
||||
|
||||
def test_custom_tool_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemCustomToolCall
|
||||
|
||||
item = OutputItemCustomToolCall({
|
||||
"type": "custom_tool_call",
|
||||
"call_id": "call_ct",
|
||||
"name": "my_tool",
|
||||
"input": '{"key": "value"}',
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "function_call"
|
||||
assert msg.contents[0].name == "my_tool"
|
||||
assert msg.contents[0].arguments == '{"key": "value"}'
|
||||
|
||||
def test_custom_tool_call_output(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemCustomToolCallOutput
|
||||
|
||||
item = OutputItemCustomToolCallOutput({
|
||||
"type": "custom_tool_call_output",
|
||||
"call_id": "call_ct",
|
||||
"output": "result text",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "tool"
|
||||
assert msg.contents[0].type == "function_result"
|
||||
assert msg.contents[0].result == "result text"
|
||||
|
||||
def test_apply_patch_call(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import ApplyPatchUpdateFileOperation, OutputItemApplyPatchToolCall
|
||||
|
||||
item = OutputItemApplyPatchToolCall({
|
||||
"type": "apply_patch_call",
|
||||
"id": "ap-1",
|
||||
"call_id": "call_ap",
|
||||
"status": "completed",
|
||||
"operation": ApplyPatchUpdateFileOperation({
|
||||
"type": "update_file",
|
||||
"path": "file.py",
|
||||
"diff": "+ new line",
|
||||
}),
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "function_call"
|
||||
assert msg.contents[0].name == "apply_patch"
|
||||
|
||||
def test_apply_patch_call_output(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItemApplyPatchToolCallOutput
|
||||
|
||||
item = OutputItemApplyPatchToolCallOutput({
|
||||
"type": "apply_patch_call_output",
|
||||
"id": "apo-1",
|
||||
"call_id": "call_ap",
|
||||
"status": "completed",
|
||||
"output": "patch applied",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "tool"
|
||||
assert msg.contents[0].type == "function_result"
|
||||
assert msg.contents[0].result == "patch applied"
|
||||
|
||||
def test_oauth_consent_request(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OAuthConsentRequestOutputItem
|
||||
|
||||
item = OAuthConsentRequestOutputItem({
|
||||
"type": "oauth_consent_request",
|
||||
"id": "oauth-1",
|
||||
"consent_link": "https://example.com/consent",
|
||||
"server_label": "my_server",
|
||||
})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "oauth_consent_request"
|
||||
assert msg.contents[0].consent_link == "https://example.com/consent"
|
||||
|
||||
def test_structured_outputs_dict(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import StructuredOutputsOutputItem
|
||||
|
||||
item = StructuredOutputsOutputItem({"type": "structured_outputs", "id": "so-1", "output": {"answer": 42}})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].type == "text"
|
||||
assert json.loads(msg.contents[0].text or "") == {"answer": 42}
|
||||
|
||||
def test_structured_outputs_string(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import StructuredOutputsOutputItem
|
||||
|
||||
item = StructuredOutputsOutputItem({"type": "structured_outputs", "id": "so-2", "output": "plain text"})
|
||||
msg = _to_message(item)
|
||||
assert msg.role == "assistant"
|
||||
assert msg.contents[0].text == "plain text"
|
||||
|
||||
def test_unsupported_type_raises(self) -> None:
|
||||
from azure.ai.agentserver.responses.models import OutputItem
|
||||
|
||||
item = OutputItem({"type": "some_unknown_type"})
|
||||
with pytest.raises(ValueError, match="Unsupported OutputItem type: some_unknown_type"):
|
||||
_to_message(item)
|
||||
|
||||
|
||||
# endregion
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
# Foundry Hosted Agents Samples
|
||||
|
||||
This directory contains samples that demonstrate how to use the Agent Framework to host agents on Foundry with different capabilities and configurations. Each sample includes a README with instructions on how to set up, run, and interact with the agent.
|
||||
|
||||
Read more about Foundry Hosted Agents [here](https://learn.microsoft.com/en-us/azure/foundry/agents/concepts/hosted-agents).
|
||||
|
||||
## Environment setup
|
||||
|
||||
1. Navigate to the sample directory you want to run. For example:
|
||||
|
||||
```bash
|
||||
python -m venv .venv
|
||||
|
||||
# Windows
|
||||
.venv\Scripts\Activate
|
||||
|
||||
# macOS/Linux
|
||||
source .venv/bin/activate
|
||||
```
|
||||
|
||||
2. Install dependencies:
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
3. Create a `.env` file with your Foundry configuration following the `env.example` file in the sample.
|
||||
|
||||
4. Make sure you are logged in with the Azure CLI:
|
||||
|
||||
```bash
|
||||
az login
|
||||
```
|
||||
|
||||
## Deploying to a Docker container
|
||||
|
||||
Navigate to the sample directory and build the Docker image:
|
||||
|
||||
```bash
|
||||
docker build -t hosted-agent-sample .
|
||||
```
|
||||
|
||||
Run the container, passing in the required environment variables:
|
||||
|
||||
```bash
|
||||
docker run -p 8088:8088 \
|
||||
-e FOUNDRY_PROJECT_ENDPOINT=<your-endpoint> \
|
||||
-e FOUNDRY_MODEL=<your-model> \
|
||||
hosted-agent-sample
|
||||
```
|
||||
|
||||
The server will be available at `http://localhost:8088`. You can send requests using the same `curl` command shown above.
|
||||
|
||||
## Deploying to Foundry
|
||||
|
||||
Follow this [guide](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/deploy-hosted-agent?tabs=bash#configure-your-agent) to deploy your agent to Foundry.
|
||||
@@ -1,13 +0,0 @@
|
||||
# Basic example of hosting an agent with the `invocations` API
|
||||
|
||||
Run the following command to start the server:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
Send a POST request to the server with a JSON body containing a "message" field to interact with the agent. For example:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/invocations -H "Content-Type: application/json" -d '{"message": "Hi!"}'
|
||||
```
|
||||
@@ -0,0 +1,6 @@
|
||||
.venv
|
||||
__pycache__
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.pyd
|
||||
.Python
|
||||
@@ -0,0 +1,2 @@
|
||||
FOUNDRY_PROJECT_ENDPOINT= "..."
|
||||
MODEL_DEPLOYMENT_NAME="..."
|
||||
@@ -0,0 +1,16 @@
|
||||
FROM python:3.12-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . user_agent/
|
||||
WORKDIR /app/user_agent
|
||||
|
||||
RUN if [ -f requirements.txt ]; then \
|
||||
pip install -r requirements.txt; \
|
||||
else \
|
||||
echo "No requirements.txt found"; \
|
||||
fi
|
||||
|
||||
EXPOSE 8088
|
||||
|
||||
CMD ["python", "main.py"]
|
||||
@@ -0,0 +1,44 @@
|
||||
# Basic example of hosting an agent with the `invocations` API
|
||||
|
||||
## Running the server locally
|
||||
|
||||
### Environment setup
|
||||
|
||||
Follow the instructions in the [Environment setup](../../README.md#environment-setup) section of the README in the parent directory to set up your environment and install dependencies.
|
||||
|
||||
Run the following command to start the server:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
### Interacting with the agent
|
||||
|
||||
Send a POST request to the server with a JSON body containing a "message" field to interact with the agent. For example:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/invocations -i -H "Content-Type: application/json" -d '{"message": "Hi"}'
|
||||
```
|
||||
|
||||
The server will respond with a JSON object containing the response text. The `-i` flag in the `curl` command includes the HTTP response headers in the output, which includes the session ID that can be used for multi-turn conversations. Here is an example of the response:
|
||||
|
||||
```
|
||||
HTTP/1.1 200
|
||||
content-length: 34
|
||||
content-type: application/json
|
||||
x-agent-invocation-id: ec04d020-a0e7-441e-ae83-db75635a9f83
|
||||
x-agent-session-id: 9370b9d4-cd13-4436-a57f-03b843ac0e17
|
||||
x-platform-server: azure-ai-agentserver-core/2.0.0a20260410006 (python/3.12)
|
||||
date: Fri, 17 Apr 2026 23:46:44 GMT
|
||||
server: hypercorn-h11
|
||||
|
||||
{"response":"Hi! How can I help?"}
|
||||
```
|
||||
|
||||
### Multi-turn conversation
|
||||
|
||||
To have a multi-turn conversation with the agent, take the session ID from the response headers of the previous request and include it in URL parameters for the next request. For example:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/invocations?agent_session_id=9370b9d4-cd13-4436-a57f-03b843ac0e17 -i -H "Content-Type: application/json" -d '{"message": "How are you?"}'
|
||||
```
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
name: agent-framework-agent-basic-invocations
|
||||
description: >
|
||||
A basic Agent Framework agent hosted by Foundry.
|
||||
metadata:
|
||||
tags:
|
||||
- Agent Framework
|
||||
- AI Agent Hosting
|
||||
- Azure AI AgentServer
|
||||
- Invocations Protocol
|
||||
- Streaming
|
||||
template:
|
||||
name: agent-framework-agent-basic-invocations
|
||||
kind: hosted
|
||||
protocols:
|
||||
- protocol: invocations
|
||||
version: 1.0.0
|
||||
environment_variables:
|
||||
- name: MODEL_DEPLOYMENT_NAME
|
||||
value: "{{MODEL_DEPLOYMENT_NAME}}"
|
||||
resources:
|
||||
- kind: model
|
||||
id: gpt-4.1-mini
|
||||
name: MODEL_DEPLOYMENT_NAME
|
||||
@@ -0,0 +1,9 @@
|
||||
# yaml-language-server: $schema=https://raw.githubusercontent.com/microsoft/AgentSchema/refs/heads/main/schemas/v1.0/ContainerAgent.yaml
|
||||
kind: hosted
|
||||
name: agent-framework-agent-basic-invocations
|
||||
protocols:
|
||||
- protocol: invocations
|
||||
version: 1.0.0
|
||||
resources:
|
||||
cpu: '0.25'
|
||||
memory: '0.5Gi'
|
||||
+1
-1
@@ -15,7 +15,7 @@ load_dotenv()
|
||||
def main():
|
||||
client = FoundryChatClient(
|
||||
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
|
||||
model=os.environ["FOUNDRY_MODEL"],
|
||||
model=os.environ["MODEL_DEPLOYMENT_NAME"],
|
||||
credential=AzureCliCredential(),
|
||||
)
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
.venv
|
||||
__pycache__
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.pyd
|
||||
.Python
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
FOUNDRY_PROJECT_ENDPOINT= "..."
|
||||
MODEL_DEPLOYMENT_NAME="..."
|
||||
@@ -0,0 +1,16 @@
|
||||
FROM python:3.12-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . user_agent/
|
||||
WORKDIR /app/user_agent
|
||||
|
||||
RUN if [ -f requirements.txt ]; then \
|
||||
pip install -r requirements.txt; \
|
||||
else \
|
||||
echo "No requirements.txt found"; \
|
||||
fi
|
||||
|
||||
EXPOSE 8088
|
||||
|
||||
CMD ["python", "main.py"]
|
||||
@@ -0,0 +1,44 @@
|
||||
# Basic example of hosting an agent with the `invocations` API
|
||||
|
||||
## Running the server locally
|
||||
|
||||
### Environment setup
|
||||
|
||||
Follow the instructions in the [Environment setup](../../README.md#environment-setup) section of the README in the parent directory to set up your environment and install dependencies.
|
||||
|
||||
Run the following command to start the server:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
### Interacting with the agent
|
||||
|
||||
Send a POST request to the server with a JSON body containing a "message" field to interact with the agent. For example:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/invocations -i -H "Content-Type: application/json" -d '{"message": "Hi"}'
|
||||
```
|
||||
|
||||
The server will respond with a JSON object containing the response text. The `-i` flag in the `curl` command includes the HTTP response headers in the output, which includes the session ID that can be used for multi-turn conversations. Here is an example of the response:
|
||||
|
||||
```
|
||||
HTTP/1.1 200
|
||||
content-length: 34
|
||||
content-type: application/json
|
||||
x-agent-invocation-id: ec04d020-a0e7-441e-ae83-db75635a9f83
|
||||
x-agent-session-id: 9370b9d4-cd13-4436-a57f-03b843ac0e17
|
||||
x-platform-server: azure-ai-agentserver-core/2.0.0a20260410006 (python/3.12)
|
||||
date: Fri, 17 Apr 2026 23:46:44 GMT
|
||||
server: hypercorn-h11
|
||||
|
||||
{"response":"Hi! How can I help?"}
|
||||
```
|
||||
|
||||
### Multi-turn conversation
|
||||
|
||||
To have a multi-turn conversation with the agent, take the session ID from the response headers of the previous request and include it in URL parameters for the next request. For example:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/invocations?agent_session_id=9370b9d4-cd13-4436-a57f-03b843ac0e17 -i -H "Content-Type: application/json" -d '{"message": "How are you?"}'
|
||||
```
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
name: agent-framework-agent-basic-invocations
|
||||
description: >
|
||||
A basic Agent Framework agent hosted by Foundry.
|
||||
metadata:
|
||||
tags:
|
||||
- Agent Framework
|
||||
- AI Agent Hosting
|
||||
- Azure AI AgentServer
|
||||
- Invocations Protocol
|
||||
- Streaming
|
||||
template:
|
||||
name: agent-framework-agent-basic-invocations
|
||||
kind: hosted
|
||||
protocols:
|
||||
- protocol: invocations
|
||||
version: 1.0.0
|
||||
environment_variables:
|
||||
- name: MODEL_DEPLOYMENT_NAME
|
||||
value: "{{MODEL_DEPLOYMENT_NAME}}"
|
||||
resources:
|
||||
- kind: model
|
||||
id: gpt-4.1-mini
|
||||
name: MODEL_DEPLOYMENT_NAME
|
||||
@@ -0,0 +1,9 @@
|
||||
# yaml-language-server: $schema=https://raw.githubusercontent.com/microsoft/AgentSchema/refs/heads/main/schemas/v1.0/ContainerAgent.yaml
|
||||
kind: hosted
|
||||
name: agent-framework-agent-basic-invocations
|
||||
protocols:
|
||||
- protocol: invocations
|
||||
version: 1.0.0
|
||||
resources:
|
||||
cpu: '0.25'
|
||||
memory: '0.5Gi'
|
||||
@@ -0,0 +1,74 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import os
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
from agent_framework import Agent, AgentSession
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.ai.agentserver.invocations import InvocationAgentServerHost
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from dotenv import load_dotenv
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse, Response, StreamingResponse
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
|
||||
# In-memory session store — keyed by session ID.
|
||||
# WARNING: This is lost on restart. Use durable storage in production.
|
||||
_sessions: dict[str, AgentSession] = {}
|
||||
|
||||
# Create the agent
|
||||
client = FoundryChatClient(
|
||||
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
|
||||
model=os.environ["MODEL_DEPLOYMENT_NAME"],
|
||||
credential=DefaultAzureCredential(),
|
||||
)
|
||||
|
||||
agent = Agent(
|
||||
client=client,
|
||||
instructions="You are a friendly assistant. Keep your answers brief.",
|
||||
# History will be managed by the hosting infrastructure, thus there
|
||||
# is no need to store history by the service. Learn more at:
|
||||
# https://developers.openai.com/api/reference/resources/responses/methods/create
|
||||
default_options={"store": False},
|
||||
)
|
||||
|
||||
app = InvocationAgentServerHost()
|
||||
|
||||
|
||||
@app.invoke_handler
|
||||
async def handle_invoke(request: Request):
|
||||
"""Handle streaming multi-turn chat with Azure OpenAI via SSE."""
|
||||
data = await request.json()
|
||||
session_id = request.state.session_id
|
||||
|
||||
stream = data.get("stream", False)
|
||||
user_message = data.get("message", None)
|
||||
if user_message is None:
|
||||
error = "Missing 'message' in request"
|
||||
if stream:
|
||||
return StreamingResponse(content=error, status_code=400)
|
||||
return Response(content=error, status_code=400)
|
||||
|
||||
session = _sessions.setdefault(session_id, AgentSession(session_id=session_id))
|
||||
|
||||
if stream:
|
||||
|
||||
async def stream_response() -> AsyncGenerator[str]:
|
||||
async for update in agent.run(user_message, session=session, stream=True):
|
||||
yield update.text
|
||||
|
||||
return StreamingResponse(
|
||||
stream_response(),
|
||||
media_type="text/event-stream",
|
||||
headers={"Cache-Control": "no-cache", "Connection": "keep-alive"},
|
||||
)
|
||||
|
||||
response = await agent.run([user_message], session=session, stream=stream)
|
||||
return JSONResponse({"response": response.text})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
agent-framework
|
||||
azure-ai-agentserver-invocations
|
||||
@@ -0,0 +1,8 @@
|
||||
# Hosting agents with Foundry Hosting and the `invocations` API
|
||||
|
||||
This folder contains a list of samples that show how to host agents using the `invocations` API and deploy them to Foundry Hosting.
|
||||
|
||||
| Sample | Description |
|
||||
| --- | --- |
|
||||
| [01_basic](./01_basic) | A basic example of hosting an agent with the `invocations` API and carrying on a multi-turn conversation. |
|
||||
| [02_break_glass](./02-break-glass) | An example of hosting an agent with the `invocations` API and a "break glass" scenario where you can create your own `invoke_handler` to handle specific types of invocations. |
|
||||
@@ -2,6 +2,18 @@
|
||||
|
||||
This agent only contains an instruction (personal). It's the most basic agent with an LLM and no tools.
|
||||
|
||||
## Running the server locally
|
||||
|
||||
### Environment setup
|
||||
|
||||
Follow the instructions in the [Environment setup](../../README.md#environment-setup) section of the README in the parent directory to set up your environment and install dependencies.
|
||||
|
||||
Run the following command to start the server:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
## Interacting with the agent
|
||||
|
||||
Send a POST request to the server with a JSON body containing a "message" field to interact with the agent. For example:
|
||||
@@ -10,12 +22,6 @@ Send a POST request to the server with a JSON body containing a "message" field
|
||||
curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"input": "Hi"}'
|
||||
```
|
||||
|
||||
### Invoke with `azd`
|
||||
|
||||
```bash
|
||||
azd ai agent invoke --local "Hi"
|
||||
```
|
||||
|
||||
## Multi-turn conversation
|
||||
|
||||
To have a multi-turn conversation with the agent, include the previous response id in the request body. For example:
|
||||
@@ -23,11 +29,3 @@ To have a multi-turn conversation with the agent, include the previous response
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"input": "How are you?", "previous_response_id": "REPLACE_WITH_PREVIOUS_RESPONSE_ID"}'
|
||||
```
|
||||
|
||||
Invoke with `azd`:
|
||||
|
||||
```bash
|
||||
azd ai agent invoke --local "Hi!" --conversation-id "my_conv"
|
||||
|
||||
azd ai agent invoke --local "How are you?" --conversation-id "my_conv"
|
||||
```
|
||||
|
||||
@@ -4,6 +4,18 @@ This agent is equipped with with a function tool and a local shell tool.
|
||||
|
||||
> We recommend deploying this sample on a local container or to Foundry Hosting because the agent has access to a local shell tool, which can run arbitrary commands on the machine.
|
||||
|
||||
## Running the server locally
|
||||
|
||||
### Environment setup
|
||||
|
||||
Follow the instructions in the [Environment setup](../../README.md#environment-setup) section of the README in the parent directory to set up your environment and install dependencies.
|
||||
|
||||
Run the following command to start the server:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
## Interacting with the agent
|
||||
|
||||
Send a POST request to the server with a JSON body containing a "message" field to interact with the agent. For example:
|
||||
@@ -13,11 +25,3 @@ curl -X POST http://localhost:8088/responses -H "Content-Type: application/json"
|
||||
|
||||
curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"input": "List the files in the current directory."}'
|
||||
```
|
||||
|
||||
Invoke with `azd`:
|
||||
|
||||
```bash
|
||||
azd ai agent invoke --local "What is the weather in Seattle?"
|
||||
|
||||
azd ai agent invoke --local "List the files in the current directory."
|
||||
```
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
FOUNDRY_PROJECT_ENDPOINT="..."
|
||||
MODEL_DEPLOYMENT_NAME="..."
|
||||
FOUNDRY_AGENT_TOOLBOX_NAME="..."
|
||||
TOOLBOX_NAME="..."
|
||||
GITHUB_PAT="..."
|
||||
@@ -4,6 +4,18 @@ This agent is equipped with a GitHub MCP server and a Foundry Toolbox, which are
|
||||
|
||||
> Note that there are other ways to interact with Foundry toolboxes. Using it as a MCP is just one of the options.
|
||||
|
||||
## Running the server locally
|
||||
|
||||
### Environment setup
|
||||
|
||||
Follow the instructions in the [Environment setup](../../README.md#environment-setup) section of the README in the parent directory to set up your environment and install dependencies.
|
||||
|
||||
Run the following command to start the server:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
## Interacting with the agent
|
||||
|
||||
Send a POST request to the server with a JSON body containing a "message" field to interact with the agent. For example:
|
||||
@@ -11,9 +23,3 @@ Send a POST request to the server with a JSON body containing a "message" field
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"input": "List all the repositories I own on GitHub."}'
|
||||
```
|
||||
|
||||
Invoke with `azd`:
|
||||
|
||||
```bash
|
||||
azd ai agent invoke --local "List all the repositories I own on GitHub."
|
||||
```
|
||||
|
||||
@@ -32,7 +32,7 @@ def main():
|
||||
|
||||
# Foundry Toolbox as a MCP tool
|
||||
project_endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]
|
||||
toolbox_name = os.environ["FOUNDRY_AGENT_TOOLBOX_NAME"]
|
||||
toolbox_name = os.environ["TOOLBOX_NAME"]
|
||||
toolbox_endpoint = f"{project_endpoint.rstrip('/')}/toolboxes/{toolbox_name}/mcp?api-version=v1"
|
||||
http_client = httpx.AsyncClient(auth=ToolboxAuth(), headers={"Foundry-Features": "Toolboxes=V1Preview"})
|
||||
foundry_mcp_tool = MCPStreamableHTTPTool(
|
||||
|
||||
@@ -2,6 +2,18 @@
|
||||
|
||||
This sample demonstrates how to host a workflow using the `responses` API.
|
||||
|
||||
## Running the server locally
|
||||
|
||||
### Environment setup
|
||||
|
||||
Follow the instructions in the [Environment setup](../../README.md#environment-setup) section of the README in the parent directory to set up your environment and install dependencies.
|
||||
|
||||
Run the following command to start the server:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
## Interacting with the agent
|
||||
|
||||
Send a POST request to the server with a JSON body containing a "message" field to interact with the agent. For example:
|
||||
@@ -9,9 +21,3 @@ Send a POST request to the server with a JSON body containing a "message" field
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"input": "Create a slogan for a new electric SUV that is affordable and fun to drive."}'
|
||||
```
|
||||
|
||||
Invoke with `azd`:
|
||||
|
||||
```bash
|
||||
azd ai agent invoke --local "Create a slogan for a new electric SUV that is affordable and fun to drive."
|
||||
```
|
||||
|
||||
@@ -4,7 +4,6 @@ import os
|
||||
|
||||
from agent_framework import Agent, AgentExecutor, WorkflowBuilder
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from agent_framework.orchestrations import GroupChatState
|
||||
from agent_framework_foundry_hosting import ResponsesHostServer
|
||||
from azure.identity import AzureCliCredential
|
||||
from dotenv import load_dotenv
|
||||
@@ -13,13 +12,6 @@ from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
|
||||
def round_robin_selector(state: GroupChatState) -> str:
|
||||
"""A round-robin selector function that picks the next speaker based on the current round index."""
|
||||
|
||||
participant_names = list(state.participants.keys())
|
||||
return participant_names[state.current_round % len(participant_names)]
|
||||
|
||||
|
||||
def main():
|
||||
client = FoundryChatClient(
|
||||
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
|
||||
|
||||
@@ -8,58 +8,4 @@ This folder contains a list of samples that show how to host agents using the `r
|
||||
| [02_local_tools](./02_local_tools) | An example of hosting an agent with the `responses` API and local tools including a function tool and a local shell tool. |
|
||||
| [03_remote_mcp](./03_remote_mcp) | An example of hosting an agent with the `responses` API and remote MCPs, including a GitHub MCP server and a Foundry Toolboox. |
|
||||
| [04_workflows](./04_workflows) | An example of hosting a workflow with the `responses` API. |
|
||||
|
||||
## Running the server locally
|
||||
|
||||
Navigate to the sample directory and run the following command to start the server:
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
## Interacting with the agent
|
||||
|
||||
There two ways to interact with the agent: sending HTTP requests to the server or using the `azd` CLI:
|
||||
|
||||
### Invoke with `azd`
|
||||
|
||||
```bash
|
||||
azd ai agent invoke --local "Hi"
|
||||
```
|
||||
|
||||
### Sending HTTP requests
|
||||
|
||||
Send a POST request to the server with a JSON body containing a "message" field to interact with the agent. For example:
|
||||
|
||||
```bash
|
||||
curl -X POST http://localhost:8088/responses -H "Content-Type: application/json" -d '{"input": "Hi"}'
|
||||
```
|
||||
|
||||
> See the individual samples for more examples of interacting with the agent.
|
||||
|
||||
## Deploying to a Docker container
|
||||
|
||||
Navigate to the sample directory and build the Docker image:
|
||||
|
||||
```bash
|
||||
docker build -t hosted-agent-sample .
|
||||
```
|
||||
|
||||
Run the container, passing in the required environment variables:
|
||||
|
||||
```bash
|
||||
docker run -p 8088:8088 \
|
||||
-e FOUNDRY_PROJECT_ENDPOINT=<your-endpoint> \
|
||||
-e FOUNDRY_MODEL=<your-model> \
|
||||
hosted-agent-sample
|
||||
```
|
||||
|
||||
The server will be available at `http://localhost:8088`. You can send requests using the same `curl` command shown above.
|
||||
|
||||
## Deploying to Foundry
|
||||
|
||||
TODO
|
||||
|
||||
## Using the deployed agent in Agent Framework
|
||||
|
||||
After deploying the agent, you can also try to use the agent in Agent Framework. Refer to the [using_deployed_agent.py](./using_deployed_agent.py) sample for an example of how to do this.
|
||||
| [using_deployed_agent.py](./using_deployed_agent.py) | An example of how to use the deployed agent in Agent Framework. |
|
||||
|
||||
Reference in New Issue
Block a user