44import random
55from datetime import datetime
66
7- import openai
87from agents import Agent , OpenAIChatCompletionsModel , Runner , function_tool , set_tracing_disabled
9- from azure .identity import DefaultAzureCredential
10- from azure .identity .aio import get_bearer_token_provider
8+ from azure .identity .aio import DefaultAzureCredential , get_bearer_token_provider
119from dotenv import load_dotenv
10+ from openai import AsyncOpenAI
1211from rich .logging import RichHandler
1312
1413# Setup logging with rich
2120# Setup the OpenAI client to use either Azure OpenAI or GitHub Models
2221load_dotenv (override = True )
2322API_HOST = os .getenv ("API_HOST" , "github" )
24- if API_HOST == "github" :
25- client = openai . AsyncOpenAI ( base_url = "https://models.inference.ai.azure.com" , api_key = os . environ [ "GITHUB_TOKEN" ])
26- MODEL_NAME = os . getenv ( "GITHUB_MODEL" , "gpt-4o" )
27- elif API_HOST == "azure" :
28- token_provider = get_bearer_token_provider (DefaultAzureCredential () , "https://cognitiveservices.azure.com/.default" )
29- client = openai . AsyncOpenAI (
23+
24+ async_credential = None
25+ if API_HOST == "azure" :
26+ async_credential = DefaultAzureCredential ()
27+ token_provider = get_bearer_token_provider (async_credential , "https://cognitiveservices.azure.com/.default" )
28+ client = AsyncOpenAI (
3029 base_url = os .environ ["AZURE_OPENAI_ENDPOINT" ] + "/openai/v1" ,
3130 api_key = token_provider ,
3231 )
3332 MODEL_NAME = os .environ ["AZURE_OPENAI_CHAT_DEPLOYMENT" ]
33+ elif API_HOST == "github" :
34+ client = AsyncOpenAI (api_key = os .environ ["GITHUB_TOKEN" ], base_url = "https://models.inference.ai.azure.com" )
35+ MODEL_NAME = os .getenv ("GITHUB_MODEL" , "gpt-4o" )
3436elif API_HOST == "ollama" :
35- client = openai . AsyncOpenAI (base_url = os .environ .get ("OLLAMA_ENDPOINT" , "http://localhost:11434/v1" ), api_key = "none" )
37+ client = AsyncOpenAI (base_url = os .environ .get ("OLLAMA_ENDPOINT" , "http://localhost:11434/v1" ), api_key = "none" )
3638 MODEL_NAME = os .environ ["OLLAMA_MODEL" ]
39+ else :
40+ client = AsyncOpenAI (api_key = os .environ ["OPENAI_API_KEY" ])
41+ MODEL_NAME = os .environ .get ("OPENAI_MODEL" , "gpt-4o" )
3742
3843
3944@function_tool
@@ -72,7 +77,11 @@ def get_current_date() -> str:
7277
7378agent = Agent (
7479 name = "Weekend Planner" ,
75- instructions = "You help users plan their weekends and choose the best activities for the given weather. If an activity would be unpleasant in the weather, don't suggest it. Include the date of the weekend in your response." ,
80+ instructions = (
81+ "You help users plan their weekends and choose the best activities for the given weather."
82+ "If an activity would be unpleasant in the weather, don't suggest it."
83+ "Include the date of the weekend in your response."
84+ ),
7685 tools = [get_weather , get_activities , get_current_date ],
7786 model = OpenAIChatCompletionsModel (model = MODEL_NAME , openai_client = client ),
7887)
@@ -82,6 +91,9 @@ async def main():
8291 result = await Runner .run (agent , input = "hii what can I do this weekend in Seattle?" )
8392 print (result .final_output )
8493
94+ if async_credential :
95+ await async_credential .close ()
96+
8597
8698if __name__ == "__main__" :
8799 logger .setLevel (logging .INFO )
0 commit comments