Skip to content

feat: adding support for Azure OpenAI in Semantic Kernel #505

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 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
cbbb235
adding support for Azure OpenAI - commands are commented
oliverlabs May 12, 2025
6886399
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 13, 2025
ec8ff8e
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 13, 2025
147d6cc
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 14, 2025
62257e6
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 15, 2025
a164d56
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 20, 2025
a19a5ed
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 21, 2025
217bcff
implemented ENABLE_AZURE_OPENAI flag to select between OpenAI and Azu…
oliverlabs May 22, 2025
89eb57f
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 22, 2025
d94e9b7
reverted the import of OpenAIChatCompletion as it got ommitted.
oliverlabs May 22, 2025
5663c33
Merge branch 'feature/sk-aoai-support' of https://github.com/oliverla…
oliverlabs May 22, 2025
4dbaa6c
removed an unused variable from the .envexample
oliverlabs May 22, 2025
5fc942a
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 22, 2025
e05e39a
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 22, 2025
ab808ef
Merge branch 'main' into feature/sk-aoai-support
oliverlabs May 27, 2025
2ac8ab2
implemented the changes proposed after Evan's review.
oliverlabs May 27, 2025
3c22180
Merge branch 'feature/sk-aoai-support' of https://github.com/oliverla…
oliverlabs May 27, 2025
153d8a7
updated readme and .envexample to be in line with the original text
oliverlabs May 27, 2025
b59ef0d
trying new .env variable logic.
oliverlabs May 27, 2025
497198e
restoring old logic
oliverlabs May 27, 2025
70f76e7
fix pydantic logic
oliverlabs May 27, 2025
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
9 changes: 9 additions & 0 deletions samples/python/agents/semantickernel/.envexample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For OpenAI (standard)
OPENAI_API_KEY="your_api_key_here"
OPENAI_CHAT_MODEL_ID="your-model-id"

# For Azure OpenAI
AZURE_OPENAI_API_KEY="your-azure-api-key-here"
AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME="your-chat-deployment-name"
AZURE_OPENAI_API_VERSION="2024-12-01-preview"
15 changes: 14 additions & 1 deletion samples/python/agents/semantickernel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,26 @@ sequenceDiagram
cd samples/python/agents/semantickernel
```

2. **Create an environment file (.env) with your API key and the model ID (e.g., "gpt-4.1"):**:
2. **Configure environment variables**:

Create a `.env` file based on `.envexample`. The agent automatically detects whether to use Azure OpenAI or standard OpenAI based on which environment variables are set.

**For OpenAI (standard):**
```bash
OPENAI_API_KEY="your_api_key_here"
OPENAI_CHAT_MODEL_ID="your-model-id"
```

**For Azure OpenAI:**
```bash
AZURE_OPENAI_API_KEY="your-azure-api-key-here"
AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME="your-deployment-name"
AZURE_OPENAI_API_VERSION="2024-12-01-preview"
```

> Note: The agent will use Azure OpenAI if all four Azure variables are set, otherwise it will use standard OpenAI if both OpenAI variables are set.

3. **Set up the Python Environment**:

> Note: pin the Python version to your desired version (3.10+)
Expand Down
53 changes: 31 additions & 22 deletions samples/python/agents/semantickernel/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pydantic import BaseModel
from semantic_kernel.agents import ChatCompletionAgent, ChatHistoryAgentThread
from semantic_kernel.connectors.ai.open_ai import (
AzureChatCompletion,
OpenAIChatCompletion,
OpenAIChatPromptExecutionSettings,
)
Expand All @@ -22,7 +23,6 @@
)
from semantic_kernel.functions import KernelArguments, kernel_function


if TYPE_CHECKING:
from semantic_kernel.contents import ChatMessageContent

Expand Down Expand Up @@ -65,7 +65,7 @@ def get_exchange_rate(
rate = data['rates'][currency_to]
return f'1 {currency_from} = {rate} {currency_to}'
except Exception as e:
return f'Currency API call failed: {str(e)}'
return f'Currency API call failed: {e!s}'


# endregion
Expand Down Expand Up @@ -93,18 +93,33 @@ class SemanticKernelTravelAgent:
SUPPORTED_CONTENT_TYPES = ['text', 'text/plain']

def __init__(self):
api_key = os.getenv('OPENAI_API_KEY', None)
if not api_key:
raise ValueError('OPENAI_API_KEY environment variable not set.')

model_id = os.getenv('OPENAI_CHAT_MODEL_ID', 'gpt-4.1')

# Define a CurrencyExchangeAgent to handle currency-related tasks
# Check Azure OpenAI environment variables and create variables if all are set
azure_vars = [
os.getenv('AZURE_OPENAI_ENDPOINT'),
os.getenv('AZURE_OPENAI_API_KEY'),
os.getenv('AZURE_OPENAI_CHAT_DEPLOYMENT_NAME'),
os.getenv('AZURE_OPENAI_API_VERSION')
]

# Check if OpenAI environment variables are available
openai_vars = [
os.getenv('OPENAI_API_KEY'),
os.getenv('OPENAI_MODEL_ID')
]

if all(azure_vars):
chat_service = AzureChatCompletion()
elif all(openai_vars):
chat_service = OpenAIChatCompletion()
else:
raise ValueError(
'Either Azure OpenAI environment variables (AZURE_OPENAI_ENDPOINT, '
'AZURE_OPENAI_API_KEY, AZURE_OPENAI_CHAT_DEPLOYMENT_NAME, AZURE_OPENAI_API_VERSION) or OpenAI '
'environment variables (OPENAI_API_KEY, OPENAI_MODEL_ID) must be set.'
)

currency_exchange_agent = ChatCompletionAgent(
service=OpenAIChatCompletion(
api_key=api_key,
ai_model_id=model_id,
),
service=chat_service,
name='CurrencyExchangeAgent',
instructions=(
'You specialize in handling currency-related requests from travelers. '
Expand All @@ -117,10 +132,7 @@ def __init__(self):

# Define an ActivityPlannerAgent to handle activity-related tasks
activity_planner_agent = ChatCompletionAgent(
service=OpenAIChatCompletion(
api_key=api_key,
ai_model_id=model_id,
),
service=chat_service,
name='ActivityPlannerAgent',
instructions=(
'You specialize in planning and recommending activities for travelers. '
Expand All @@ -133,10 +145,7 @@ def __init__(self):

# Define the main TravelManagerAgent to delegate tasks to the appropriate agents
self.agent = ChatCompletionAgent(
service=OpenAIChatCompletion(
api_key=api_key,
ai_model_id=model_id,
),
service=chat_service,
name='TravelManagerAgent',
instructions=(
"Your role is to carefully analyze the traveler's request and forward it to the appropriate agent based on the "
Expand Down Expand Up @@ -301,4 +310,4 @@ async def _ensure_thread_exists(self, session_id: str) -> None:
self.thread = ChatHistoryAgentThread(thread_id=session_id)


# endregion
# endregion