Skip to content

Fix/issue #304 NeMo Guardrails packaging #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/build-wheel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ jobs:
- name: Build Wheel
run: |
pip install build
cp -r chat-ui nemoguardrails/chat-ui
cp -r examples nemoguardrails/examples
python -m build --wheel
rm -r nemoguardrails/chat-ui
rm -r nemoguardrails/examples
echo "WHEEL_FILE=$(ls dist/*.whl)" >> $GITHUB_ENV

- name: Upload Artifact
Expand Down
14 changes: 0 additions & 14 deletions examples/__init__.py

This file was deleted.

3 changes: 2 additions & 1 deletion examples/configs/rag/multi_kb/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from langchain.vectorstores import FAISS
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline

from examples.configs.rag.multi_kb.tabular_llm import TabularLLM
from nemoguardrails import LLMRails, RailsConfig
from nemoguardrails.actions import action
from nemoguardrails.actions.actions import ActionResult
Expand All @@ -38,6 +37,8 @@
register_llm_provider,
)

from .tabular_llm import TabularLLM


def _get_model_config(config: RailsConfig, type: str):
"""Quick helper to return the config for a specific model type."""
Expand Down
11 changes: 4 additions & 7 deletions nemoguardrails/server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from starlette.responses import JSONResponse, StreamingResponse
from starlette.staticfiles import StaticFiles

from nemoguardrails import LLMRails, RailsConfig
from nemoguardrails import LLMRails, RailsConfig, utils
from nemoguardrails.rails.llm.options import (
GenerationLog,
GenerationOptions,
Expand Down Expand Up @@ -81,9 +81,8 @@
)

# By default, we use the rails in the examples folder
app.rails_config_path = os.path.normpath(
os.path.join(os.path.dirname(__file__), "..", "..", "examples", "bots")
)

app.rails_config_path = utils.get_examples_data_path("bots")

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

if not app.disable_chat_ui:
FRONTEND_DIR = os.path.join(
os.path.dirname(__file__), "..", "..", "chat-ui", "frontend"
)
FRONTEND_DIR = utils.get_chat_ui_data_path("frontend")

app.mount(
"/",
Expand Down
30 changes: 30 additions & 0 deletions nemoguardrails/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
# limitations under the License.
import asyncio
import dataclasses
import importlib.resources as pkg_resources
import json
import os
import uuid
from collections import namedtuple
from datetime import datetime, timezone
Expand Down Expand Up @@ -218,3 +220,31 @@ def get_or_create_event_loop():
asyncio.set_event_loop(loop)

return loop


def get_data_path(package_name: str, file_path: str) -> str:
"""Helper to get the path to the data directory."""
try:
# Try to get the path from the package resources
path = pkg_resources.files(package_name).joinpath(file_path)
if path.exists():
return str(path)
except FileNotFoundError:
pass

# If that fails, try to get the path from the local file system
path = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", file_path))
if os.path.exists(path):
return path

raise FileNotFoundError(f"File not found: {file_path}")


def get_examples_data_path(file_path: str) -> str:
"""Helper to get the path to the examples data directory."""
return get_data_path("nemoguardrails", f"examples/{file_path}")


def get_chat_ui_data_path(file_path: str) -> str:
"""Helper to get the path to the chat-ui data directory."""
return get_data_path("nemoguardrails", f"chat-ui/{file_path}")
13 changes: 7 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,23 @@ license-files = [
[tool.setuptools.packages.find]
where = ["."]
include = ["nemoguardrails*"]

[tool.setuptools.dynamic]
version = {attr = "nemoguardrails.__version__"}
exclude = ["chat-ui*", "examples*"]

[tool.setuptools.package-data]
nemoguardrails = [
"chat-ui/**/*",
"examples/**/*",
"**/*.yml",
"**/*.co",
"**/*.txt",
"**/*.json",
"**/*.lark",
"../examples/**/*",
"../chat-ui/**/*",
"eval/data/**/*",
]

[tool.setuptools.dynamic]
version = { attr = "nemoguardrails.__version__" }

[tool.pytest.ini_options]
addopts = "-p no:warnings"
log-level = "DEBUG"
Expand All @@ -133,5 +134,5 @@ log-level = "DEBUG"
log_cli = "False"

[build-system]
requires = ["setuptools"]
requires = ["setuptools>=64"]
build-backend = "setuptools.build_meta"
Loading