Skip to content

Commit

Permalink
fetch ROS_DISTRO env value to set completion request. (#24)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoya.Fujita <tomoya.fujita825@gmail.com>
  • Loading branch information
fujitatomoya authored Jan 30, 2024
1 parent 1c4c728 commit 71ab26f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
8 changes: 6 additions & 2 deletions ros2ai/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
# However note that the system message is optional and the model’s behavior without a system message
# is likely to be similar to using a generic message such as "You are a helpful assistant."
# TODO@fujitatomoya: ROS_DISTRO would be better to assistant setting.
ROLE_SYSTEM_QUERY_DEFAULT = 'You are a ROS 2 helpful assistant.'
ROLE_SYSTEM_EXEC_DEFAULT = 'You are a ROS 2 command line executor, provides executable commands only without any comments.'
ROLE_SYSTEM_QUERY_DEFAULT = \
'You are a Robot Operating System version 2 (as known as ROS2) {} distribution ' \
'professional assistant who can provide helpful answers against any questions.'
ROLE_SYSTEM_EXEC_DEFAULT = \
'You are a Robot Operating System 2 (as known as ROS2) {} distribution command line executor, ' \
'provides executable commands only without any comments.'

# Temperature controls the consistency for behavior. (range 0.0 - 2.0)
# The lower the temperature is, the more deterministic behavior that OpenAI does.
Expand Down
13 changes: 13 additions & 0 deletions ros2ai/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import signal
import subprocess
import os

def run_command(*, command, argv = None, prefix = None):
"""
Expand Down Expand Up @@ -56,3 +57,15 @@ def run_executable(*, command, argv = None, prefix=None):
if -process.returncode in signal.valid_signals():
print(signal.strsignal(-process.returncode))
return process.returncode

def get_ros_distro() -> str:
"""
Fetch ROS_DISTRO environmental variable.
:return: string of distribution name.
"""
distro = os.environ.get('ROS_DISTRO')
if not distro:
print('ROS_DISTRO env value is not set.')
return None
return distro.lower()
7 changes: 6 additions & 1 deletion ros2ai/verb/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from ros2ai.api import add_global_arguments
from ros2ai.api.utils import run_executable
from ros2ai.api.openai import ChatCompletionClient, ChatCompletionParameters
from ros2ai.api.utils import get_ros_distro
from ros2ai.verb import VerbExtension

import ros2ai.api.constants as constants
Expand Down Expand Up @@ -43,8 +44,12 @@ def main(self, *, args):
if (request == ''):
print('Please insert your request! (I am not AI)')

distro = get_ros_distro()
if distro is None:
distro = 'rolling' # fallback to rolling in default
system_role = constants.ROLE_SYSTEM_EXEC_DEFAULT.format(distro)
user_request = [
{"role": "system", "content": f"{constants.ROLE_SYSTEM_EXEC_DEFAULT}"},
{"role": "system", "content": f"{system_role}"},
{"role": "user", "content": f"{request}"}
]

Expand Down
7 changes: 6 additions & 1 deletion ros2ai/verb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from ros2ai.api import add_global_arguments
from ros2ai.api.openai import ChatCompletionClient, ChatCompletionParameters
from ros2ai.api.utils import get_ros_distro
from ros2ai.verb import VerbExtension

import ros2ai.api.constants as constants
Expand Down Expand Up @@ -47,8 +48,12 @@ def main(self, *, args):
if (sentence == ''):
print('Dont be shy, put some questions! (I am not AI)')

distro = get_ros_distro()
if distro is None:
distro = 'rolling' # fallback to rolling in default
system_role = constants.ROLE_SYSTEM_QUERY_DEFAULT.format(distro)
user_messages = [
{"role": "system", "content": f"{constants.ROLE_SYSTEM_QUERY_DEFAULT}"},
{"role": "system", "content": f"{system_role}"},
{"role": "user", "content": f"{sentence}"}
]

Expand Down

0 comments on commit 71ab26f

Please sign in to comment.