From 7d3d4672670ee2e2d9731ada384e41a24abe18fe Mon Sep 17 00:00:00 2001 From: Amnon Catav Date: Thu, 8 Feb 2024 16:56:13 +0200 Subject: [PATCH] add gen-template command --- README.md | 10 ++++++++++ src/canopy_cli/cli.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bf4afd93..4f7651b7 100644 --- a/README.md +++ b/README.md @@ -195,6 +195,16 @@ INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) ### Stopping the server To stop the server, simply press `CTRL+C` in the terminal where you started it. +### Dumping a config Yaml template file + +The first step in writing your own configuration file is to start from a valid template. You can dump a configuration file template using the following command: + +```bash +canopy gen-config /path/to/my-config.yaml --template cohere.yaml +``` + +You can find the available templates under [src/canopy/config](src/canopy/config/). + ## Evaluation chat tool diff --git a/src/canopy_cli/cli.py b/src/canopy_cli/cli.py index 6fe0c696..6fff9ba0 100644 --- a/src/canopy_cli/cli.py +++ b/src/canopy_cli/cli.py @@ -2,6 +2,7 @@ from typing import Dict, Any, Optional, List, Iterable import click +import importlib.resources as pkg_resources from prompt_toolkit import prompt import time @@ -178,7 +179,7 @@ def __init__(self, name=None, commands=None, **attrs): "health": 4, "stop": 5, "api-docs": 6, - + "gen-config": 7, } def list_commands(self, ctx): @@ -210,6 +211,36 @@ 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 gen_config(path, template): + + if not template.endswith('.yaml'): + template += '.yaml' + + 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 + 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) + + click.echo(f"Config template '{template}' written to {path}") + + @cli.command( help=( """