Skip to content

Commit 802165c

Browse files
authored
Merge pull request #11 from pamelafox/funccall
More function calling examples
2 parents c7de0ec + 723550f commit 802165c

4 files changed

+193
-3
lines changed

.pre-commit-config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.5.0
3+
rev: v5.0.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
77
- id: trailing-whitespace
88
- repo: https://github.com/astral-sh/ruff-pre-commit
9-
rev: v0.1.0
9+
rev: v0.9.7
1010
hooks:
1111
- id: ruff
1212
- repo: https://github.com/psf/black
13-
rev: 23.9.1
13+
rev: 25.1.0
1414
hooks:
1515
- id: black

function_calling.py

+1
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

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import json
2+
import os
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+
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+
client = openai.OpenAI(
25+
base_url=os.getenv("OLLAMA_ENDPOINT"),
26+
api_key="nokeyneeded",
27+
)
28+
MODEL_NAME = os.getenv("OLLAMA_MODEL")
29+
30+
elif API_HOST == "github":
31+
client = openai.OpenAI(base_url="https://models.inference.ai.azure.com", api_key=os.getenv("GITHUB_TOKEN"))
32+
MODEL_NAME = os.getenv("GITHUB_MODEL")
33+
34+
else:
35+
client = openai.OpenAI(api_key=os.getenv("OPENAI_KEY"))
36+
MODEL_NAME = os.getenv("OPENAI_MODEL")
37+
38+
39+
def lookup_weather(city_name=None, zip_code=None):
40+
"""Lookup the weather for a given city name or zip code."""
41+
print(f"Looking up weather for {city_name or zip_code}...")
42+
return "It's sunny!"
43+
44+
45+
tools = [
46+
{
47+
"type": "function",
48+
"function": {
49+
"name": "lookup_weather",
50+
"description": "Lookup the weather for a given city name or zip code.",
51+
"parameters": {
52+
"type": "object",
53+
"properties": {
54+
"city_name": {
55+
"type": "string",
56+
"description": "The city name",
57+
},
58+
"zip_code": {
59+
"type": "string",
60+
"description": "The zip code",
61+
},
62+
},
63+
"strict": True,
64+
"additionalProperties": False,
65+
},
66+
},
67+
}
68+
]
69+
70+
response = client.chat.completions.create(
71+
model="gpt-4o-mini",
72+
messages=[
73+
{"role": "system", "content": "You are a weather chatbot."},
74+
{"role": "user", "content": "is it sunny in that small city near sydney where anthony lives?"},
75+
],
76+
tools=tools,
77+
tool_choice="auto",
78+
)
79+
80+
print("Response:")
81+
print(response.choices[0].message.tool_calls[0].function.name)
82+
print(response.choices[0].message.tool_calls[0].function.arguments)
83+
84+
# Now actually call the function as indicated
85+
if response.choices[0].message.tool_calls:
86+
function_name = response.choices[0].message.tool_calls[0].function.name
87+
arguments = json.loads(response.choices[0].message.tool_calls[0].function.arguments)
88+
if function_name == "lookup_weather":
89+
lookup_weather(**arguments)

function_calling_multiple.py

+100
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)