1+ import os
2+ import json
3+
4+ import azure .identity
5+ import openai
6+ from dotenv import load_dotenv
7+
8+ # Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
9+ load_dotenv (override = True )
10+ API_HOST = os .getenv ("API_HOST" )
11+
12+ if API_HOST == "azure" :
13+
14+ token_provider = azure .identity .get_bearer_token_provider (
15+ azure .identity .DefaultAzureCredential (), "https://cognitiveservices.azure.com/.default"
16+ )
17+ client = openai .AzureOpenAI (
18+ api_version = os .getenv ("AZURE_OPENAI_VERSION" ),
19+ azure_endpoint = os .getenv ("AZURE_OPENAI_ENDPOINT" ),
20+ azure_ad_token_provider = token_provider ,
21+ )
22+ MODEL_NAME = os .getenv ("AZURE_OPENAI_DEPLOYMENT" )
23+
24+ elif API_HOST == "ollama" :
25+
26+ client = openai .OpenAI (
27+ base_url = os .getenv ("OLLAMA_ENDPOINT" ),
28+ api_key = "nokeyneeded" ,
29+ )
30+ MODEL_NAME = os .getenv ("OLLAMA_MODEL" )
31+
32+ elif API_HOST == "github" :
33+
34+ client = openai .OpenAI (base_url = "https://models.inference.ai.azure.com" , api_key = os .getenv ("GITHUB_TOKEN" ))
35+ MODEL_NAME = os .getenv ("GITHUB_MODEL" )
36+
37+ else :
38+
39+ client = openai .OpenAI (api_key = os .getenv ("OPENAI_KEY" ))
40+ MODEL_NAME = os .getenv ("OPENAI_MODEL" )
41+
42+
43+ def lookup_weather (city_name = None , zip_code = None ):
44+ """Lookup the weather for a given city name or zip code."""
45+ print (f"Looking up weather for { city_name or zip_code } ..." )
46+ return "It's sunny!"
47+
48+ tools = [
49+ {
50+ "type" : "function" ,
51+ "function" : {
52+ "name" : "lookup_weather" ,
53+ "description" : "Lookup the weather for a given city name or zip code." ,
54+ "parameters" : {
55+ "type" : "object" ,
56+ "properties" : {
57+ "city_name" : {
58+ "type" : "string" ,
59+ "description" : "The city name" ,
60+ },
61+ "zip_code" : {
62+ "type" : "string" ,
63+ "description" : "The zip code" ,
64+ },
65+ },
66+ "strict" : True ,
67+ "additionalProperties" : False ,
68+ },
69+ },
70+ }
71+ ]
72+
73+ response = client .chat .completions .create (
74+ model = "gpt-4o-mini" ,
75+ messages = [
76+ {"role" : "system" , "content" : "You are a weather chatbot." },
77+ {"role" : "user" , "content" : "is it sunny in that small city near sydney where anthony lives?" },
78+ ],
79+ tools = tools ,
80+ tool_choice = "auto" ,
81+ )
82+
83+ print ("Response:" )
84+ print (response .choices [0 ].message .tool_calls [0 ].function .name )
85+ print (response .choices [0 ].message .tool_calls [0 ].function .arguments )
86+
87+ # Now actually call the function as indicated
88+ if response .choices [0 ].message .tool_calls :
89+ function_name = response .choices [0 ].message .tool_calls [0 ].function .name
90+ arguments = json .loads (response .choices [0 ].message .tool_calls [0 ].function .arguments )
91+ if function_name == "lookup_weather" :
92+ lookup_weather (** arguments )
0 commit comments