You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/playground/agents/tools/function_to_openai_exec.py

40 lines
1.1 KiB

from typing import Annotated
from swarms import create_openai_tool
from openai import OpenAI
# Create an instance of the OpenAI client
client = OpenAI()
# Define the user messages for the chat conversation
messages = [
{
"role": "user",
"content": "What's the weather like in San Francisco, Tokyo, and Paris?",
}
]
# Define the BMI calculator tool using the create_openai_tool decorator
@create_openai_tool(
name="BMI Calculator",
description="Calculate the Body Mass Index (BMI)",
)
def calculate_bmi(
weight: Annotated[float, "Weight in kilograms"],
height: Annotated[float, "Height in meters"],
) -> Annotated[float, "Body Mass Index"]:
"""Calculate the Body Mass Index (BMI) given a person's weight and height."""
return weight / (height**2)
# Create a chat completion request using the OpenAI client
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
tools=calculate_bmi,
tool_choice="auto", # auto is default, but we'll be explicit
)
# Print the generated response from the chat completion
print(response.choices[0].message["content"])