import os
from openai import OpenAI
import json
client = OpenAI(
api_key=os.environ["REDPILL_AI_API_KEY"],
base_url="https://api.redpill.ai/v1"
)
# Define available functions
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
}]
# Make request
response = client.chat.completions.create(
model="openai/gpt-5",
messages=[{"role": "user", "content": "What's the weather in SF?"}],
tools=tools
)
# Check if model wants to call function
tool_call = response.choices[0].message.tool_calls[0]
if tool_call.function.name == "get_weather":
args = json.loads(tool_call.function.arguments)
# Call your actual function
weather = get_weather(args["location"], args.get("unit", "celsius"))
# Send result back to model
response = client.chat.completions.create(
model="openai/gpt-5",
messages=[
{"role": "user", "content": "What's the weather in SF?"},
response.choices[0].message,
{
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(weather)
}
],
tools=tools
)
print(response.choices[0].message.content)