-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Python: Wrapper + Samples 1st #5177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TaoChenOSU
merged 13 commits into
feature/python-foundry-hosted-agent-vnext
from
feature/python-foundry-hosted-agent-vnext-dev
Apr 10, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e634488
Experiment
TaoChenOSU 951f61a
Update dependency and add non streaming
TaoChenOSU 359a721
Add more samples
TaoChenOSU b2fa3a6
Rename samples
TaoChenOSU 8403d57
Add invocations
TaoChenOSU ea625a7
Comments 1
TaoChenOSU f262087
Comments 2
TaoChenOSU 228c60c
Comments 3
TaoChenOSU 735c123
Improve README
TaoChenOSU 20ac97c
Add local shell sample
TaoChenOSU 8ecf274
WIP: Add eval and memory samples
TaoChenOSU 4d7c035
Update user agent prefix
TaoChenOSU 6f85956
Update user agent prefix doc
TaoChenOSU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| ], | ||
| "words": [ | ||
| "aeiou", | ||
| "agentserver", | ||
| "agui", | ||
| "aiplatform", | ||
| "azuredocindex", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
TaoChenOSU marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
13 changes: 13 additions & 0 deletions
13
python/packages/foundry_hosting/agent_framework_foundry_hosting/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] |
77 changes: 77 additions & 0 deletions
77
python/packages/foundry_hosting/agent_framework_foundry_hosting/_invocations.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
TaoChenOSU marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| yield update.text | ||
|
|
||
| return StreamingResponse( | ||
| stream_response(), | ||
TaoChenOSU marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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, | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.