Skip to content

Commit 8f42924

Browse files
authored
Merge pull request #536 from NVIDIA/fix/issue-304-wrong-packaging
Fix/issue #304 NeMo Guardrails packaging
2 parents 386f002 + 70afb23 commit 8f42924

File tree

6 files changed

+47
-28
lines changed

6 files changed

+47
-28
lines changed

.github/workflows/build-wheel.yml

+4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ jobs:
2424
- name: Build Wheel
2525
run: |
2626
pip install build
27+
cp -r chat-ui nemoguardrails/chat-ui
28+
cp -r examples nemoguardrails/examples
2729
python -m build --wheel
30+
rm -r nemoguardrails/chat-ui
31+
rm -r nemoguardrails/examples
2832
echo "WHEEL_FILE=$(ls dist/*.whl)" >> $GITHUB_ENV
2933
3034
- name: Upload Artifact

examples/__init__.py

-14
This file was deleted.

examples/configs/rag/multi_kb/config.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from langchain.vectorstores import FAISS
2929
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3030

31-
from examples.configs.rag.multi_kb.tabular_llm import TabularLLM
3231
from nemoguardrails import LLMRails, RailsConfig
3332
from nemoguardrails.actions import action
3433
from nemoguardrails.actions.actions import ActionResult
@@ -38,6 +37,8 @@
3837
register_llm_provider,
3938
)
4039

40+
from .tabular_llm import TabularLLM
41+
4142

4243
def _get_model_config(config: RailsConfig, type: str):
4344
"""Quick helper to return the config for a specific model type."""

nemoguardrails/server/api.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from starlette.responses import JSONResponse, StreamingResponse
3030
from starlette.staticfiles import StaticFiles
3131

32-
from nemoguardrails import LLMRails, RailsConfig
32+
from nemoguardrails import LLMRails, RailsConfig, utils
3333
from nemoguardrails.rails.llm.options import (
3434
GenerationLog,
3535
GenerationOptions,
@@ -81,9 +81,8 @@
8181
)
8282

8383
# By default, we use the rails in the examples folder
84-
app.rails_config_path = os.path.normpath(
85-
os.path.join(os.path.dirname(__file__), "..", "..", "examples", "bots")
86-
)
84+
85+
app.rails_config_path = utils.get_examples_data_path("bots")
8786

8887
# Weather the chat UI is enabled or not.
8988
app.disable_chat_ui = False
@@ -430,9 +429,7 @@ async def startup_event():
430429
# Finally, we register the static frontend UI serving
431430

432431
if not app.disable_chat_ui:
433-
FRONTEND_DIR = os.path.join(
434-
os.path.dirname(__file__), "..", "..", "chat-ui", "frontend"
435-
)
432+
FRONTEND_DIR = utils.get_chat_ui_data_path("frontend")
436433

437434
app.mount(
438435
"/",

nemoguardrails/utils.py

+30
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
# limitations under the License.
1515
import asyncio
1616
import dataclasses
17+
import importlib.resources as pkg_resources
1718
import json
19+
import os
1820
import uuid
1921
from collections import namedtuple
2022
from datetime import datetime, timezone
@@ -218,3 +220,31 @@ def get_or_create_event_loop():
218220
asyncio.set_event_loop(loop)
219221

220222
return loop
223+
224+
225+
def get_data_path(package_name: str, file_path: str) -> str:
226+
"""Helper to get the path to the data directory."""
227+
try:
228+
# Try to get the path from the package resources
229+
path = pkg_resources.files(package_name).joinpath(file_path)
230+
if path.exists():
231+
return str(path)
232+
except FileNotFoundError:
233+
pass
234+
235+
# If that fails, try to get the path from the local file system
236+
path = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", file_path))
237+
if os.path.exists(path):
238+
return path
239+
240+
raise FileNotFoundError(f"File not found: {file_path}")
241+
242+
243+
def get_examples_data_path(file_path: str) -> str:
244+
"""Helper to get the path to the examples data directory."""
245+
return get_data_path("nemoguardrails", f"examples/{file_path}")
246+
247+
248+
def get_chat_ui_data_path(file_path: str) -> str:
249+
"""Helper to get the path to the chat-ui data directory."""
250+
return get_data_path("nemoguardrails", f"chat-ui/{file_path}")

pyproject.toml

+7-6
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,23 @@ license-files = [
106106
[tool.setuptools.packages.find]
107107
where = ["."]
108108
include = ["nemoguardrails*"]
109-
110-
[tool.setuptools.dynamic]
111-
version = {attr = "nemoguardrails.__version__"}
109+
exclude = ["chat-ui*", "examples*"]
112110

113111
[tool.setuptools.package-data]
114112
nemoguardrails = [
113+
"chat-ui/**/*",
114+
"examples/**/*",
115115
"**/*.yml",
116116
"**/*.co",
117117
"**/*.txt",
118118
"**/*.json",
119119
"**/*.lark",
120-
"../examples/**/*",
121-
"../chat-ui/**/*",
122120
"eval/data/**/*",
123121
]
124122

123+
[tool.setuptools.dynamic]
124+
version = { attr = "nemoguardrails.__version__" }
125+
125126
[tool.pytest.ini_options]
126127
addopts = "-p no:warnings"
127128
log-level = "DEBUG"
@@ -133,5 +134,5 @@ log-level = "DEBUG"
133134
log_cli = "False"
134135

135136
[build-system]
136-
requires = ["setuptools"]
137+
requires = ["setuptools>=64"]
137138
build-backend = "setuptools.build_meta"

0 commit comments

Comments
 (0)