Skip to content

Commit

Permalink
Validate the URL before calling OpenAI APIs. (#42)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoya Fujita <[email protected]>
  • Loading branch information
fujitatomoya committed Sep 14, 2024
1 parent 0fb1a0e commit d18b223
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ https://github.com/fujitatomoya/ros2ai/assets/43395114/2af4fd44-2ccf-472c-9153-c
- `rolling` / `jazzy`

```bash
pip install openai ollama --break-system-packages
pip install openai ollama validators --break-system-packages
```

> [!NOTE]
Expand All @@ -60,7 +60,7 @@ https://github.com/fujitatomoya/ros2ai/assets/43395114/2af4fd44-2ccf-472c-9153-c
- `iron` / `humble`

```bash
pip install openai ollama
pip install openai ollama validators
```

### Build
Expand Down
6 changes: 3 additions & 3 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ RUN apt-get update \

# Adapt pip install option based on distro (technically ubuntu version)
RUN if [ "$ROS_DISTRO" = "humble" ]; then \
pip install openai ollama; \
pip install openai ollama validators; \
elif [ "$ROS_DISTRO" = "iron" ]; then \
pip install openai ollama; \
pip install openai ollama validators; \
else \
pip install openai ollama --break-system-packages; \
pip install openai ollama validators --break-system-packages; \
fi

# Build and source colcon workspace
Expand Down
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

<exec_depend>curl</exec_depend>
<exec_depend>python3-openai-pip</exec_depend>
<exec_depend>python3-ollama-pip</exec_depend>
<exec_depend>python3-validotors-pip</exec_depend>
<exec_depend>ros2cli</exec_depend>

<test_depend>ament_copyright</test_depend>
Expand Down
14 changes: 9 additions & 5 deletions ros2ai/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import validators
import os

import ros2ai.api.constants as constants
Expand All @@ -36,7 +37,6 @@ def get_ai_model() -> str:
"""
model_name = os.environ.get(constants.ROS_OPENAI_MODEL_NAME_ENV_VAR)
if not model_name:
print('AI model is not set, defaults to ' + constants.ROS_OPENAI_DEFAULT_MODEL)
return constants.ROS_OPENAI_DEFAULT_MODEL
else:
return model_name
Expand All @@ -48,12 +48,16 @@ def get_endpoint_url() -> str:
:return: string of OpenAI API service endpoint URL, could be None.
"""
url = os.environ.get(constants.ROS_OPENAI_ENDPOINT_ENV_VAR)
# TODO(@fujitatomoya):check if that is valid url before return.
if not url:
print('AI model is not set, defaults to ' + constants.ROS_OPENAI_DEFAULT_ENDPOINT)
url_valid = False
try:
url_valid = validators.url(url)
except Exception as e:
print(f"Error validating URL: {e}")
return constants.ROS_OPENAI_DEFAULT_ENDPOINT
else:
if url and url_valid:
return url
else:
return constants.ROS_OPENAI_DEFAULT_ENDPOINT

def get_temperature() -> float:
"""
Expand Down
4 changes: 2 additions & 2 deletions scripts/github_workflows.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ function install_prerequisites () {
# TODO@fujitatomoya: should install openai via package.xml
apt install -y pip
if [ $UBUNTU_VERSION == "24.04" ]; then
pip install openai ollama --break-system-packages
pip install openai ollama validators --break-system-packages
else
pip install openai ollama
pip install openai ollama validators
fi
#apt install -y ros-${ROS_DISTRO}-desktop --no-install-recommends
cd $there
Expand Down

0 comments on commit d18b223

Please sign in to comment.