Skip to content

Commit 05b9115

Browse files
committed
Format cleanup
1 parent 93c88dd commit 05b9115

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

turtlebot4_openai_tutorials/launch/natural_language_nav_launch.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
from launch.substitutions import LaunchConfiguration
2222
from launch_ros.actions import Node
2323

24+
2425
def generate_launch_description():
2526
return LaunchDescription([
26-
DeclareLaunchArgument('openai_api_key', default_value='', description='API key set up via https://platform.openai.com/account/api-keys'),
27-
DeclareLaunchArgument('model_name', default_value='gpt-3.5-turbo', description='OpenAI model name as selected from https://platform.openai.com/docs/guides/gpt'),
28-
DeclareLaunchArgument('parking_brake', default_value='true', description='Set to false to execute commands on the robot'),
27+
DeclareLaunchArgument('openai_api_key', default_value='',
28+
description='API key set up via \
29+
https://platform.openai.com/account/api-keys'),
30+
DeclareLaunchArgument('model_name', default_value='gpt-3.5-turbo',
31+
description='OpenAI model name as selected from \
32+
https://platform.openai.com/docs/guides/gpt'),
33+
DeclareLaunchArgument('parking_brake', default_value='true',
34+
description='Set to false to execute commands on the robot'),
2935
Node(
3036
package='turtlebot4_openai_tutorials',
3137
executable='natural_language_nav',
@@ -37,4 +43,4 @@ def generate_launch_description():
3743
{'parking_brake': LaunchConfiguration('parking_brake')}
3844
]
3945
),
40-
])
46+
])

turtlebot4_openai_tutorials/setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
# Package.xml
1616
('share/' + package_name, ['package.xml']),
1717
# Launch files
18-
(os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.py'))),
18+
(os.path.join('share', package_name, 'launch'),
19+
glob(os.path.join('launch', '*launch.py'))),
1920
# Prompt files
20-
(os.path.join('share', package_name, 'prompts'), glob(os.path.join('prompts', '*.txt'))),
21+
(os.path.join('share', package_name, 'prompts'),
22+
glob(os.path.join('prompts', '*.txt'))),
2123
],
2224
install_requires=['setuptools'],
2325
zip_safe=True,

turtlebot4_openai_tutorials/turtlebot4_openai_tutorials/natural_language_nav.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818

1919
import os
2020

21-
import rclpy, ament_index_python
21+
import rclpy
22+
import ament_index_python
2223
from rclpy.node import Node
2324
from std_msgs.msg import String, Bool
2425
from turtlebot4_navigation.turtlebot4_navigator import TurtleBot4Directions, TurtleBot4Navigator
2526

2627
import openai
2728

29+
2830
class GPTNode(Node):
2931
def __init__(self, navigator):
3032
super().__init__('gpt_node')
31-
self.declare_parameter('openai_api_key','')
32-
self.declare_parameter('model_name','gpt-3.5-turbo')
33-
self.declare_parameter('parking_brake',True)
33+
self.declare_parameter('openai_api_key', '')
34+
self.declare_parameter('model_name', 'gpt-3.5-turbo')
35+
self.declare_parameter('parking_brake', True)
3436

3537
# OpenAI key, model, prompt setup
3638
openai.api_key = self.get_parameter('openai_api_key').value
@@ -61,26 +63,27 @@ def query(self, base_prompt, query, stop_tokens=None, query_kwargs=None, log=Tru
6163
]
6264
response = openai.ChatCompletion.create(
6365
messages=messages, stop=stop_tokens, **use_query_kwargs
64-
) ['choices'][0]['message']['content'].strip()
66+
)['choices'][0]['message']['content'].strip()
6567

6668
if log:
6769
self.info(query)
6870
self.info(response)
6971

7072
return response
71-
73+
7274
def publish_status(self, status):
73-
""" Publish whether or not the system is ready for more input """
75+
"""Publish whether or not the system is ready for more input."""
7476
msg = Bool()
7577
msg.data = status
7678
self.ready_for_input = status
7779
self.pub_ready.publish(msg)
78-
80+
7981
def user_input(self, msg):
80-
""" Process user input and optionally execute resulting code """
81-
# User input
82-
if not self.ready_for_input:
83-
# This doesn't seem to be an issue when there's only one publisher, but it's good practice
82+
"""Process user input and optionally execute resulting code."""
83+
# User input
84+
if not self.ready_for_input:
85+
# This doesn't seem to be an issue when there's only one publisher
86+
# but it's good practice
8487
self.info(f"Received input <{msg.data}> when not ready, skipping")
8588
return
8689
self.publish_status(False)
@@ -90,12 +93,14 @@ def user_input(self, msg):
9093
query = '# ' + msg.data
9194
result = self.query(f'{self.full_prompt}', query, ['#', 'objects = ['])
9295

96+
# This is because the example API calls 'navigator', not 'self.navigator'
97+
navigator = self.navigator
98+
9399
# Execute?
94-
navigator = self.navigator # This is because the example API calls 'navigator', not 'self.navigator'
95100
if not self.get_parameter('parking_brake').value:
96101
try:
97102
exec(result, globals(), locals())
98-
except Exception as e:
103+
except Exception:
99104
self.error("Failure to execute resulting code:")
100105
self.error("---------------\n"+result)
101106
self.error("---------------")
@@ -117,15 +122,16 @@ def debug(self, msg):
117122
self.get_logger().debug(msg)
118123
return
119124

125+
120126
def read_prompt_file(prompt_file):
121-
""" Read in a specified file which is located in the package 'prompts' directory
122-
"""
127+
"""Read in a specified file which is located in the package 'prompts' directory."""
123128
data_path = ament_index_python.get_package_share_directory('turtlebot4_openai_tutorials')
124129
prompt_path = os.path.join(data_path, 'prompts', prompt_file)
125-
130+
126131
with open(prompt_path, 'r') as file:
127132
return file.read()
128133

134+
129135
def main():
130136
rclpy.init()
131137

@@ -155,9 +161,10 @@ def main():
155161
navigator.undock()
156162
else:
157163
gpt.warn("Parking brake set, robot will not execute commands!")
158-
164+
159165
# Add custom context
160-
context = "destinations = {'wood crate': [0.0, 3.0, 0], 'steel barrels': [2.0, 2.0, 90], 'bathroom door': [-6.0, -6.0, 180] }"
166+
context = "destinations = {'wood crate': [0.0, 3.0, 0], 'steel barrels': [2.0, 2.0, 90], \
167+
'bathroom door': [-6.0, -6.0, 180] }"
161168
exec(context, globals())
162169
gpt.info("Entering input parsing loop with context:")
163170
gpt.info(context)
@@ -175,5 +182,6 @@ def main():
175182

176183
rclpy.shutdown()
177184

185+
178186
if __name__ == '__main__':
179-
main()
187+
main()

0 commit comments

Comments
 (0)