Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Fix create config CLI command (#287)
Browse files Browse the repository at this point in the history
* use simple file load for template CLI command and add a load config template utility

* lint

* Create a Directory class to hold the lib paths

* test fix

* fix config test

* dummy change to toml
  • Loading branch information
acatav authored Feb 13, 2024
1 parent 1a06f4f commit ae69371
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ grpc = ["pinecone-client"]


[tool.poetry.group.dev.dependencies]
jupyter = "^1.0.0"
pytest = "^7.3.2"
jupyter = "^1.0.0"
mypy = "^1.4.1"
flake8 = "^6.1.0"
pytest-html = "^4.1.0"
Expand Down Expand Up @@ -104,4 +104,4 @@ skip-checking-raises = true
canopy = "canopy_cli.cli:cli"

[tool.pytest.ini_options]
log_cli = true
log_cli = true
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions src/canopy/utils/directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pathlib import Path


class Directory:
"""Stores the directory paths for Canopy library"""

ROOT = Path(__file__).parent.parent
CONFIG_TEMPLATES = ROOT.joinpath("config_templates")
44 changes: 17 additions & 27 deletions src/canopy_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
import shutil
from typing import Dict, Any, Optional, List, Iterable

import click
import importlib.resources as pkg_resources
from prompt_toolkit import prompt
import time

from pathlib import Path
import requests
import yaml
from dotenv import load_dotenv
Expand All @@ -22,6 +22,7 @@
from canopy.chat_engine import ChatEngine
from canopy.models.data_models import Document, UserMessage
from canopy.tokenizer import Tokenizer
from canopy.utils.directory import Directory
from canopy_cli.data_loader import (
load_from_path,
IDsNotUniqueError,
Expand Down Expand Up @@ -211,38 +212,27 @@ def health(url):
return


@cli.command(help="Writes a config template YAML file to the specified path.")
@click.argument("path", type=click.Path(), required=True)
@click.option("--template", default="default", help="The name of the template to use.")
def create_config(path, template):
@cli.command(help="Writes the config templates to a directory.")
@click.argument("out_path", type=click.Path(), required=True)
def create_config(out_path):

out_path = Path(out_path)

if not template.endswith('.yaml'):
template += '.yaml'
if out_path.is_file():
raise CLIError(f"Path expected to be a directory,"
f"but found a file at {out_path}")

if os.path.exists(path):
click.confirm(click.style(f"File {path} already exists. Overwrite?", fg="red"),
if out_path.exists() and any(out_path.iterdir()):
click.confirm(click.style(f"Path {out_path} is not empty. Overwrite?",
fg="red"),
abort=True)

try:
with pkg_resources.open_text('canopy.config', template) as f:
config = yaml.safe_load(f)
except FileNotFoundError:
files = pkg_resources.files('canopy.config')
template_names = [f.name for f in files.iterdir()]
click.echo(f"Template '{template}' not found."
f"Available templates: {template_names}",
err=True)
return
shutil.copytree(Directory.CONFIG_TEMPLATES, out_path, dirs_exist_ok=True)
except Exception as e:
click.echo(f"Failed to load template '{template}'. Reason: {e}"
f"Make sure you pip installed `canopy-sdk`.",
err=True)
return

with open(path, 'w') as dst_file:
yaml.dump(config, dst_file)
raise CLIError(f"Failed to write config template to {out_path}. Reason:\n{e}")

click.echo(f"Config template '{template}' written to {path}")
click.echo(click.style(f"Config templates written to {out_path}", fg="green"))


@cli.command(
Expand Down
9 changes: 5 additions & 4 deletions tests/system/utils/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from canopy.chat_engine import ChatEngine
from canopy.context_engine import ContextEngine
from canopy.knowledge_base import KnowledgeBase

DEFAULT_CONFIG_PATH = 'src/canopy/config/default.yaml'
from canopy.utils.directory import Directory


@pytest.fixture(scope='module')
Expand All @@ -24,8 +23,10 @@ def temp_index_name():


def test_default_config_matches_code_defaults(temp_index_name):
with open(DEFAULT_CONFIG_PATH) as f:
default_config = yaml.safe_load(f)

with open(Directory.CONFIG_TEMPLATES.joinpath("default.yaml")) as file:
default_config = yaml.safe_load(file)

chat_engine_config = default_config['chat_engine']

loaded_chat_engine = ChatEngine.from_config(chat_engine_config)
Expand Down

0 comments on commit ae69371

Please sign in to comment.