diff --git a/README.md b/README.md index 55d1316..6b988ad 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,23 @@ $ gpt pirate Ahoy, matey! What be bringing ye to these here waters? Be it treasure or adventure ye seek, we be sailing the high seas together. Ready yer map and compass, for we have a long voyage ahead! ``` +### Read other context to the assistant with !include + +You can read in files to the assistant's context with !include . + +```yaml +default_assistant: dev +markdown: True +openai_api_key: +assistants: + pirate: + model: gpt-4 + temperature: 1.0 + messages: + - { role: system, content: !include "pirate.txt" } +``` + + ### Customize OpenAI API URL If you are using other models compatible with the OpenAI Python SDK, you can configure them by modifying the `openai_base_url` setting in the config file or using the `OPENAI_BASE_URL` environment variable . diff --git a/gptcli/config.py b/gptcli/config.py index a23753b..042cc63 100644 --- a/gptcli/config.py +++ b/gptcli/config.py @@ -1,12 +1,12 @@ import os from typing import Dict, List, Optional -from attr import dataclass + import yaml +from attr import dataclass from gptcli.assistant import AssistantConfig from gptcli.providers.llama import LLaMAModelConfig - CONFIG_FILE_PATHS = [ os.path.join(os.path.expanduser("~"), ".config", "gpt-cli", "gpt.yml"), os.path.join(os.path.expanduser("~"), ".gptrc"), @@ -38,9 +38,24 @@ def choose_config_file(paths: List[str]) -> str: return "" +# Custom YAML Loader with !include support +class CustomLoader(yaml.SafeLoader): + pass + + +def include_constructor(loader, node): + # Get the file path from the node + file_path = loader.construct_scalar(node) + # Read and return the content of the included file + with open(file_path, "r") as include_file: + return include_file.read() + + +# Register the !include constructor +CustomLoader.add_constructor("!include", include_constructor) + + def read_yaml_config(file_path: str) -> GptCliConfig: with open(file_path, "r") as file: - config = yaml.safe_load(file) - return GptCliConfig( - **config, - ) + config = yaml.load(file, Loader=CustomLoader) + return GptCliConfig(**config)