Skip to content
Open
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
83 changes: 83 additions & 0 deletions examples/test_allowed_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from openai import OpenAI

client = OpenAI()

tools = [
{
"type": "function",
"function":{
"name": "get_horoscope",
"description": "Get today's horoscope for an astrological sign.",
"parameters": {
"type": "object",
"properties": {
"sign": {
"type": "string",
"description": "An astrological sign like Taurus or Aquarius",
},
},
"required": ["sign"],
},
}
},
{
"type": "function",
"function": {
"name": "say",
"description": "say something to the user when no other tools are available",
"parameters": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "say something to the user",
}
},
"required": ["text"],
},
}
},
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country name e.g. Bogotá, Colombia",
}
},
"required": ["location"],
},
}
},
]

tool_choice = {
"type": "allowed_tools",
"mode": "auto",
"allowed_tools": {
"mode": "auto",
"tools": [
{"type": "function", "function": {"name": 'get_weather'}},
{"type": "function", "function": {"name": 'say'}},
]
}

}

messages = [ {"role": "user", "content": "What is my horoscope? I am an Aquarius."} ]

response = client.chat.completions.create(
messages = messages,
model = "gpt-4o-2024-11-20",
stream = False,
tools = tools,
tool_choice = tool_choice
)

print("Response:", response.choices[0].message.content)
print("Hit Tools:", [ i.function for i in response.choices[0].message.tool_calls] )