Skip to content

Python: Concept of self-hosted agent #10624

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

Closed
wants to merge 8 commits into from

Conversation

ankitbko
Copy link
Member

@ankitbko ankitbko commented Feb 20, 2025

Motivation and Context

In the agentic world, different agents are created and maintained by different teams in an organization. Each team has their own way of creating agents and hosting them which are independent of each other. To utilize these agents when creating a multi-agent application, semantic kernel will need to integrate with agents which lies outside the semantic kernel application. For semantic kernel, these agents are black box services that have their own implementation of "intelligence" but could still use the plugins registered in kernel as needed. AgentGroupChat should be able to orchestrate between these agents as usual.

Description

Although there could be multiple ways to implement such concept, this PR takes the approach of deriving from ChatCompletionClientBase and taking inspiration from OpenAIChatCompletionBase to implement SelfHostedChatCompletion which makes a REST request to externally hosted agents. Instances of SelfHostedChatCompletion are registered as service in kernel

kernel.add_service(
    SelfHostedChatCompletion(
        url=os.getenv("REVIEWER_AGENT_URL") or "", ai_model_id=REVIEWER_NAME, service_id=REVIEWER_NAME
    )
)

ChatCompletionAgent is used to create agents and the appropriate service is referenced using service_id.

agent_reviewer = ChatCompletionAgent(
    service_id="artdirector",
    kernel=kernel,
    name=REVIEWER_NAME,
    arguments=KernelArguments(settings=PromptExecutionSettings(service_id=REVIEWER_NAME)),
)

The agents themselves are implemented in a fastapi server under agents folder. AgentGroupChat is used to orchestrate between the agents.

Contribution Checklist

@ankitbko ankitbko requested a review from a team as a code owner February 20, 2025 16:07
@eavanvalkenburg eavanvalkenburg added the python Pull requests for the Python Semantic Kernel label Mar 14, 2025
@eavanvalkenburg eavanvalkenburg force-pushed the ankitbko/self-hosted-agents branch from 74777f2 to adec526 Compare March 14, 2025 08:53
@github-actions github-actions bot changed the title Concept of self-hosted agent Python: Concept of self-hosted agent Mar 14, 2025
ankitbko and others added 2 commits April 14, 2025 09:45
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
@ankitbko
Copy link
Member Author

@eavanvalkenburg bumping it up for your review

Copy link
Member

@eavanvalkenburg eavanvalkenburg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of small notes

"""


class APIRequestFormat(BaseModel, ABC):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this a ABC?

messages = request.messages
logger.info("Financial Coach agent")
logger.info(messages)
openai = await client.inference.get_azure_openai_client(api_version="2024-06-01")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to see SK used for these agents as well, and maybe even different types of agents... CC: @moonbox3


agent_reviewer = ChatCompletionAgent(
id="artdirector",
kernel=kernel,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add a kernel anymore


agent_writer = ChatCompletionAgent(
id="copywriter",
kernel=kernel,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@moonbox3
Copy link
Contributor

So sorry for the delay on this. Will have a look tomorrow.

@moonbox3
Copy link
Contributor

I want to chat with the team a bit more around this new idea. We're going to be introducing some new orchestration patterns for agents soon, and I want to make sure that things will align properly.

self._handle_structured_output(settings, settings_dict)
if settings.tools is None:
settings_dict.pop("parallel_tool_calls", None)
async with httpx.AsyncClient(timeout=30) as client:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One can already pass in a custom client and service:

from httpx import AsyncClient

async with AsyncClient(timeout=30) as http_client:
    custom_openai_client = AsyncOpenAI(http_client=http_client)
    custom_openai_service = OpenAIChatCompletion(async_client=custom_openai_client)
    agent = ChatCompletionAgent(
        service=custom_openai_service,
        name="Host",
        instructions="Answer questions about the menu.",
    )

By doing so, one can leverage the existing code in the OpenAIChatCompletionBase

@moonbox3
Copy link
Contributor

moonbox3 commented Jun 19, 2025

I think it's okay if you want to show a demo using custom httpx.AsyncClient, but I don't think that requires the need to re-write a class when you can already pass in that custom httpx client and it will be used as part of the OpenAIChatCompletionBase.

Please update to simplify the demo/code, and re-open the PR if you choose to move forward with it. Thank you.

@moonbox3 moonbox3 closed this Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agents documentation python Pull requests for the Python Semantic Kernel
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants