Skip to content

feat: added Azure OpenAI chat compeltion, environment variables sample and update self.thread._thread_id method in semantic kernel agent sample #545

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions samples/python/agents/semantickernel/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# OPENAI_API_KEY="your_api_key_here"
# OPENAI_CHAT_MODEL_ID="your-model-id"

AZURE_OPENAI_API_KEY="your_azure_api_key_here"
AZURE_OPENAI_API_BASE="https://your-azure-openai-endpoint"
AZURE_OPENAI_API_VERSION="2023-05-15"
AZURE_OPENAI_CHAT_DEPLOYMENT="your-azure-chat-deployment"
61 changes: 35 additions & 26 deletions samples/python/agents/semantickernel/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
OpenAIChatCompletion,
OpenAIChatPromptExecutionSettings,
)
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.contents import (
FunctionCallContent,
FunctionResultContent,
Expand Down Expand Up @@ -93,18 +94,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')
# Determine which OpenAI service to use based on environment variables
azure_api_key = os.getenv('AZURE_OPENAI_API_KEY', None)
if azure_api_key:
azure_api_base = os.getenv('AZURE_OPENAI_API_BASE')
azure_api_version = os.getenv('AZURE_OPENAI_API_VERSION')
azure_deployment = os.getenv('AZURE_OPENAI_CHAT_DEPLOYMENT')
ServiceClass = AzureChatCompletion
service_kwargs = {
'api_key': azure_api_key,
'endpoint': azure_api_base,
'api_version': azure_api_version,
'deployment_name': azure_deployment,
}
else:
openai_api_key = os.getenv('OPENAI_API_KEY', None)
if not openai_api_key:
raise ValueError('OPENAI_API_KEY environment variable not set.')
model_id = os.getenv('OPENAI_CHAT_MODEL_ID', 'gpt-4.1')
ServiceClass = OpenAIChatCompletion
service_kwargs = {
'api_key': openai_api_key,
'ai_model_id': model_id,
}

# Define a CurrencyExchangeAgent to handle currency-related tasks
currency_exchange_agent = ChatCompletionAgent(
service=OpenAIChatCompletion(
api_key=api_key,
ai_model_id=model_id,
),
service=ServiceClass(**service_kwargs),
name='CurrencyExchangeAgent',
instructions=(
'You specialize in handling currency-related requests from travelers. '
Expand All @@ -117,10 +133,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=ServiceClass(**service_kwargs),
name='ActivityPlannerAgent',
instructions=(
'You specialize in planning and recommending activities for travelers. '
Expand All @@ -133,10 +146,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=ServiceClass(**service_kwargs),
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 @@ -270,17 +280,16 @@ def _get_agent_response(

return default_response

async def _ensure_thread_exists(self, session_id: str) -> None:
"""Ensure the thread exists for the given session ID.

Args:
session_id (str): Unique identifier for the session.
"""
async def _ensure_thread_exists(self, session_id: str) -> None:
"""Ensure the thread exists for the given session ID.
Args:
session_id (str): Unique identifier for the session.
"""
# Replace check with self.thread.id when
# https://github.com/microsoft/semantic-kernel/issues/11535 is fixed
if self.thread is None or self.thread._thread_id != session_id:
await self.thread.delete() if self.thread else None
if self.thread is None or self.thread.id != session_id: # fixed _thread_id issue
await self.thread.delete() if self.thread else None
self.thread = ChatHistoryAgentThread(thread_id=session_id)


# endregion