Skip to content

Commit

Permalink
Use !include to read text files into context for the assistant (#98)
Browse files Browse the repository at this point in the history
Read text files into assistant context with:

```yaml
default_assistant: dev
markdown: True
openai_api_key: <openai_api_key>
assistants:
  pirate:
    model: gpt-4
    temperature: 1.0
    messages:
      - { role: system, content: !include "pirate.txt" }
```
  • Loading branch information
samleeney authored Nov 17, 2024
1 parent 5f686f9 commit e448f07
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file_path>.
```yaml
default_assistant: dev
markdown: True
openai_api_key: <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 .
Expand Down
27 changes: 21 additions & 6 deletions gptcli/config.py
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down Expand Up @@ -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)

0 comments on commit e448f07

Please sign in to comment.