class Car:
def __init__(self, name, model):
self.name = name
self.model = model
def makesound(self):
return print(f"Car {self.name} {self.model} makes sound")
def __str__(self):
return f"{self.name} {self.model}"
def to_dict(self):
return {"name": self.name, "model": self.model}
def make_car(name, model):
car = Car(name, model)
return car
def make_sound(car : Car):
return car + ' makes sound pwweeee'
main.py
def run_conversation(user_prompt):
messages = [
{
"role": "system",
"content": """
You are a function calling LLM that can call make_car funtion to make a car with given name and model and it return and car object.
""",
},
{
"role": "user",
"content": user_prompt,
},
]
tools = [
{
"type": "function",
"function": {
"name": "make_car",
"description": "Make a car object with the given name and model",
"parameters": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the car (eg: 'Volkswagen')",
},
"model": {
"type": "string",
"description": "The model of the car (eg: 'Vento', 'Polo', etc.)",
},
},
"required": ["name", "model"],
},
},
},
{
"type": "function",
"function": {
"name": "make_sound",
"description": "Make the car sound",
"parameters": {
"type": "object",
"properties": {
"car": {
"type": "object",
"description": "The car object to make sound (eg: car object will be returned by make_car function)",
},
},
"required": ["car"],
},
},
},
]
response = client.chat.completions.create(
model=MODEL,
messages=messages,
tools=tools,
tool_choice="auto",
max_tokens=4096,
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
if tool_calls:
available_functions = {
"make_car": make_car,
"make_sound": make_sound,
}
messages.append(response_message)
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
if function_name == "make_car":
function_response = function_to_call(
name=function_args.get("name"),
model=function_args.get("model"),
)
elif function_name == "make_sound":
function_response = function_to_call(
car=function_args.get("car"),
)
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response
}
)
second_response = client.chat.completions.create(
model=MODEL, messages=messages
)
return second_response.choices[0].message
user_prompt = "Make a car with name 'Volkswagen' and model any model also make sound"
print(run_conversation(user_prompt))
is there any way to pass a car object to the make_sound funtion?