This commit is contained in:
apirrone 2025-04-08 12:21:24 +02:00
parent 9755c43fe0
commit cbcaa4acce
2 changed files with 37 additions and 8 deletions

View file

@ -43,11 +43,16 @@ class Sounds:
sound_name = random.choice(list(self.sounds.keys())) sound_name = random.choice(list(self.sounds.keys()))
self.sounds[sound_name].play() self.sounds[sound_name].play()
def play_happy(self):
self.sounds["happy1.wav"].play()
# Example usage # Example usage
if __name__ == "__main__": if __name__ == "__main__":
sound_player = Sounds(1.0, "../assets/") sound_player = Sounds(1.0, "../assets/")
time.sleep(1) time.sleep(1)
while True: while True:
sound_player.play_random_sound() # sound_player.play_random_sound()
sound_player.play_happy()
time.sleep(3) time.sleep(3)

View file

@ -86,6 +86,9 @@ class Tools:
print("Picture taken") print("Picture taken")
return url return url
def play_happy_sound(self):
self.rl_walk.sounds.play_happy()
return "Played happy sound"
# Tool instance # Tool instance
tools_instance = Tools() tools_instance = Tools()
@ -165,6 +168,16 @@ openai_tools = [
# No required properties for taking a picture # No required properties for taking a picture
}, },
}, },
{
"type": "function",
"name": "play_happy_sound",
"description": "Play a happy sound",
"parameters": {
"type": "object",
"properties": {},
# No required properties for playing a sound
},
}
] ]
# Mapping function names to actual methods # Mapping function names to actual methods
@ -181,7 +194,7 @@ messages = [
"role": "system", "role": "system",
"content": ( "content": (
"You are a cute little biped robot that can move around using the following tools: " "You are a cute little biped robot that can move around using the following tools: "
"`move_forward`, `move_backward`, `turn_left`, `turn_right` and 'take_picture'. " "`move_forward`, `move_backward`, `turn_left`, `turn_right`, 'play_happy_sound' and 'take_picture'. "
"moving forward for 1 second will make you move forward by about 15 centimeters" "moving forward for 1 second will make you move forward by about 15 centimeters"
"turning for 1 second will make you turn about 45 degrees" "turning for 1 second will make you turn about 45 degrees"
"You can only perform one action at a time. If multiple actions are needed, call them step by step." "You can only perform one action at a time. If multiple actions are needed, call them step by step."
@ -189,13 +202,19 @@ messages = [
"Always start by taking a picture of the environment so you can see where you are. " "Always start by taking a picture of the environment so you can see where you are. "
"When you take a picture, describe what you see in the image. " "When you take a picture, describe what you see in the image. "
"make sure not to hit any walls or objects. Take pictures regularly to know where you are." "make sure not to hit any walls or objects. Take pictures regularly to know where you are."
"When given a goal to find something, if you found it, navigate to be in front of it, facing it" "Maybe it's a good idea to turn 360 degrees to check all directions. (no need if you already found it)"
"Maybe it's a good idea to turn 360 degrees to check all directions" "When given a goal to find something, if you found it, navigate to be in front of it, facing it. You want to be about 1 meter close to it"
"When you are 1 meter close to the object, play the happy sound"
""
), ),
}, },
# {
# "role": "user",
# "content": "Find the yellow vaccum cleaner !",
# },
{ {
"role": "user", "role": "user",
"content": "Find the yellow vaccum cleaner !", "content": "Explore, and when you see a person, play the happy sound",
}, },
] ]
@ -207,6 +226,7 @@ function_map = {
"turn_left": tools_instance.turn_left, "turn_left": tools_instance.turn_left,
"turn_right": tools_instance.turn_right, "turn_right": tools_instance.turn_right,
"take_picture": tools_instance.take_picture, "take_picture": tools_instance.take_picture,
"play_happy_sound": tools_instance.play_happy_sound,
} }
@ -224,13 +244,15 @@ def call_function(name, args):
return function_map[name](args["seconds"]) return function_map[name](args["seconds"])
elif name == "take_picture": elif name == "take_picture":
return function_map[name]() return function_map[name]()
elif name == "play_happy_sound":
return function_map[name]()
else: else:
raise ValueError(f"Unknown function name: {name}") raise ValueError(f"Unknown function name: {name}")
while True: while True:
response = client.responses.create( response = client.responses.create(
model="gpt-4o", model="gpt-4o-mini",
input=messages, input=messages,
tools=openai_tools, tools=openai_tools,
) )
@ -238,8 +260,10 @@ while True:
if len(response.output) == 1 and response.output[0].type == "function_call": if len(response.output) == 1 and response.output[0].type == "function_call":
print("Only function call, no text response") print("Only function call, no text response")
else: else:
print(response.output[0].content[0].text) try:
print(response.output[0].content[0].text)
except:
print("Error occurred while processing response")
for tool_call in response.output: for tool_call in response.output:
if tool_call.type != "function_call": if tool_call.type != "function_call":
continue continue