Skip to content

Commit 68c2d42

Browse files
committed
More function calling examples
1 parent c7de0ec commit 68c2d42

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

function_calling.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@
7373
)
7474

7575
print("Response:")
76+
print(response.choices[0].message.tool_calls[0].function.name)
7677
print(response.choices[0].message.tool_calls[0].function.arguments)

function_calling_call.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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)

function_calling_multiple.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import os
2+
3+
import azure.identity
4+
import openai
5+
from dotenv import load_dotenv
6+
7+
# Setup the OpenAI client to use either Azure, OpenAI.com, or Ollama API
8+
load_dotenv(override=True)
9+
API_HOST = os.getenv("API_HOST")
10+
11+
if API_HOST == "azure":
12+
13+
token_provider = azure.identity.get_bearer_token_provider(
14+
azure.identity.DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
15+
)
16+
client = openai.AzureOpenAI(
17+
api_version=os.getenv("AZURE_OPENAI_VERSION"),
18+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
19+
azure_ad_token_provider=token_provider,
20+
)
21+
MODEL_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT")
22+
23+
elif API_HOST == "ollama":
24+
25+
client = openai.OpenAI(
26+
base_url=os.getenv("OLLAMA_ENDPOINT"),
27+
api_key="nokeyneeded",
28+
)
29+
MODEL_NAME = os.getenv("OLLAMA_MODEL")
30+
31+
elif API_HOST == "github":
32+
33+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
34+
MODEL_NAME = os.getenv("GITHUB_MODEL")
35+
36+
else:
37+
38+
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
39+
MODEL_NAME = os.getenv("OPENAI_MODEL")
40+
41+
42+
tools = [
43+
{
44+
"type": "function",
45+
"function": {
46+
"name": "lookup_weather",
47+
"description": "Lookup the weather for a given city name or zip code.",
48+
"parameters": {
49+
"type": "object",
50+
"properties": {
51+
"city_name": {
52+
"type": "string",
53+
"description": "The city name",
54+
},
55+
"zip_code": {
56+
"type": "string",
57+
"description": "The zip code",
58+
},
59+
},
60+
"additionalProperties": False,
61+
},
62+
},
63+
},
64+
{
65+
"type": "function",
66+
"function": {
67+
"name": "lookup_movies",
68+
"description": "Lookup movies playing in a given city name or zip code.",
69+
"parameters": {
70+
"type": "object",
71+
"properties": {
72+
"city_name": {
73+
"type": "string",
74+
"description": "The city name",
75+
},
76+
"zip_code": {
77+
"type": "string",
78+
"description": "The zip code",
79+
},
80+
},
81+
"additionalProperties": False,
82+
},
83+
},
84+
}
85+
]
86+
87+
response = client.chat.completions.create(
88+
model=MODEL_NAME,
89+
messages=[
90+
{"role": "system", "content": "You are a tourism chatbot."},
91+
{"role": "user", "content": "is it rainy enough in sydney to watch movies and which ones are on?"},
92+
],
93+
tools=tools,
94+
tool_choice="auto"
95+
)
96+
97+
print("Response:")
98+
for message in response.choices[0].message.tool_calls:
99+
print(message.function.name)
100+
print(message.function.arguments)

0 commit comments

Comments
 (0)