Skip to content
Merged
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
1 change: 1 addition & 0 deletions python/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
],
"words": [
"aeiou",
"agentserver",
"agui",
"aiplatform",
"azuredocindex",
Expand Down
31 changes: 25 additions & 6 deletions python/packages/core/agent_framework/_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
HTTP_USER_AGENT: Final[str] = "agent-framework-python"
AGENT_FRAMEWORK_USER_AGENT = f"{HTTP_USER_AGENT}/{version_info}" # type: ignore[has-type]

_user_agent_prefixes: list[str] = []


def append_to_user_agent(prefix: str) -> None:
"""Prepend a prefix to the agent framework user agent string.

This is useful for hosting layers that want to identify themselves in telemetry.
Duplicate prefixes are ignored.

Args:
prefix: The prefix to prepend (e.g. "foundry-hosting").
"""
if prefix and prefix not in _user_agent_prefixes:
_user_agent_prefixes.append(prefix)


def _get_user_agent() -> str:
"""Return the full user agent string including any prepended prefixes."""
if not _user_agent_prefixes:
return AGENT_FRAMEWORK_USER_AGENT
return f"{'/'.join(_user_agent_prefixes)}/{AGENT_FRAMEWORK_USER_AGENT}"


def prepend_agent_framework_to_user_agent(headers: dict[str, Any] | None = None) -> dict[str, Any]:
"""Prepend "agent-framework" to the User-Agent in the headers.
Expand Down Expand Up @@ -57,12 +79,9 @@ def prepend_agent_framework_to_user_agent(headers: dict[str, Any] | None = None)
"""
if not IS_TELEMETRY_ENABLED:
return headers or {}
user_agent = _get_user_agent()
if not headers:
return {USER_AGENT_KEY: AGENT_FRAMEWORK_USER_AGENT}
headers[USER_AGENT_KEY] = (
f"{AGENT_FRAMEWORK_USER_AGENT} {headers[USER_AGENT_KEY]}"
if USER_AGENT_KEY in headers
else AGENT_FRAMEWORK_USER_AGENT
)
return {USER_AGENT_KEY: user_agent}
headers[USER_AGENT_KEY] = f"{user_agent} {headers[USER_AGENT_KEY]}" if USER_AGENT_KEY in headers else user_agent

return headers
21 changes: 21 additions & 0 deletions python/packages/foundry_hosting/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Microsoft Corporation.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
11 changes: 11 additions & 0 deletions python/packages/foundry_hosting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Foundry Hosting

This package provides the integration of Agent Framework agents and workflows with the Foundry Agent Server, which can be hosted on Foundry infrastructure.

## Responses

TODO

## Invocations

TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright (c) Microsoft. All rights reserved.

import importlib.metadata

from ._invocations import InvocationsHostServer
from ._responses import ResponsesHostServer

try:
__version__ = importlib.metadata.version(__name__)
except importlib.metadata.PackageNotFoundError:
__version__ = "0.0.0"

__all__ = ["InvocationsHostServer", "ResponsesHostServer"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright (c) Microsoft. All rights reserved.

from agent_framework import AgentSession, BaseAgent, SupportsAgentRun
from agent_framework._telemetry import append_to_user_agent
from azure.ai.agentserver.invocations import InvocationAgentServerHost
from starlette.requests import Request
from starlette.responses import JSONResponse, Response, StreamingResponse
from typing_extensions import Any, AsyncGenerator, Optional


class InvocationsHostServer(InvocationAgentServerHost):
"""An invocations server host for an agent."""

USER_AGENT_PREFIX = "foundry-hosting"

def __init__(
self,
agent: BaseAgent,
*,
stream: bool = False,
openapi_spec: Optional[dict[str, Any]] = None,
**kwargs: Any,
) -> None:
"""Initialize an InvocationsHostServer.

Args:
agent: The agent to handle responses for.
stream: Whether to stream the responses. Defaults to True.
openapi_spec: The OpenAPI specification for the server.
**kwargs: Additional keyword arguments.

This host will expect the request to be a JSON body with a "message" field.
The response from the host will be a JSON object with a "response" field containing
the agent's response and a "session_id" field containing the session ID.
"""
super().__init__(openapi_spec=openapi_spec, **kwargs)

if not isinstance(agent, SupportsAgentRun):
raise TypeError("Agent must support the SupportsAgentRun interface")

append_to_user_agent(self.USER_AGENT_PREFIX)
self._agent = agent
self._stream = stream
self._sessions: dict[str, AgentSession] = {}
self.invoke_handler(self._handle_invoke) # pyright: ignore[reportUnknownMemberType]

async def _handle_invoke(self, request: Request) -> Response:
"""Invoke the agent with the given request."""
data = await request.json()
session_id: str = request.state.session_id

user_message = data.get("message", None)
if user_message is None:
error = "Missing 'message' in request"
if self._stream:
return StreamingResponse(content=error, status_code=400)
return Response(content=error, status_code=400)

session = self._sessions.setdefault(session_id, AgentSession(session_id=session_id))

if self._stream:

async def stream_response() -> AsyncGenerator[str]:
async for update in self._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 self._agent.run([user_message], session=session, stream=self._stream)
return JSONResponse({
"response": response.text,
"session_id": session_id,
})
Loading