Skip to content
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

Python: New Feature: Support Foundry SDK in Python #9899

Closed
johnsinco opened this issue Dec 6, 2024 · 3 comments
Closed

Python: New Feature: Support Foundry SDK in Python #9899

johnsinco opened this issue Dec 6, 2024 · 3 comments
Assignees
Labels
ai connector Anything related to AI connectors python Pull requests for the Python Semantic Kernel

Comments

@johnsinco
Copy link


Support Foundry SDK in Python


The recently released Azure AI Foundry Python SDK supports retrieving Azure OpenAI connections from your Foundry project. This improves project security as you are not dealing with the AOAI endpoints and secret keys. I have not been able to find an integration point where I can initialize the SK-Python Kernel object in such a way to utilize the Foundry initialized AOAI instance.

Proposed Change

Allow the AOAI instance openai.AzureOpenAI to be injected similar to the below...

from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from semantic_kernel import Kernel
import openai.AzureOpenAI

project_connection_string="<<AI Foundry connection string>>"

project = AIProjectClient.from_connection_string(conn_str=project_connection_string, credential=DefaultAzureCredential())  # or Managed Identity, etc.

# retrieve the AOAI instance from the Foundry project
injected_aoai_client = project.inference.get_azure_openai_client(api_version="2024-06-01")  # this is an openai.AzureOpenAI object

# Create the Kernel object and add the existing AOAI service retrieved from the Foundry Project
kernel = Kernel(services={"azure_openai": injected_aoai_clieint})
@markwallace-microsoft markwallace-microsoft added python Pull requests for the Python Semantic Kernel triage labels Dec 6, 2024
@github-actions github-actions bot changed the title New Feature: Support Foundry SDK in Python Python: New Feature: Support Foundry SDK in Python Dec 6, 2024
@moonbox3
Copy link
Contributor

moonbox3 commented Dec 7, 2024

Hi @johnsinco, thanks for opening this item. I was looking for the code for the get_azure_openai_client method and it returns an AsyncAzureOpenAI client:

async def get_azure_openai_client(self, *, api_version: Optional[str] = None, **kwargs) -> "AsyncAzureOpenAI":
    ...

You could inject this custom client into our AzureChatCompletion class like:

chat = AzureChatCompletion(
    async_client=<your_custom_foundry_client>
)

@moonbox3 moonbox3 self-assigned this Dec 7, 2024
@moonbox3 moonbox3 added ai connector Anything related to AI connectors and removed triage labels Dec 7, 2024
@alliscode
Copy link
Member

@johnsinco This issue appears to be resolved so I'm closing it for now. Please reopen if needed. Thanks!

@johnsinco
Copy link
Author

Thanks @moonbox3 for the quick reply. I was able to adapt your hints above and plug AI Foundry to the SK kernel like so...

import asyncio
  # from azure.ai.projects import AIProjectClient
from azure.ai.projects.aio import AIProjectClient  # <<-- identically named async version of the client
from azure.identity import DefaultAzureCredential
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

# Retrieve the connection string from environment variables
project_connection_string = os.getenv("PROJECT_CONNECTION_STRING")

project = AIProjectClient.from_connection_string(
  conn_str=project_connection_string,
  credential=DefaultAzureCredential())

aoai_chat = AzureChatCompletion(
    deployment_name="gpt-4o",
    async_client=aoai_client
)

# Create the Kernel object and add the Azure OpenAI Chat Completion service
kernel = Kernel()
service_id="agent"
kernel.add_service(AzureChatCompletion(service_id=service_id, async_client=aoai_client, deployment_name="gpt-4o"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai connector Anything related to AI connectors python Pull requests for the Python Semantic Kernel
Projects
Archived in project
Development

No branches or pull requests

4 participants