Skip to content
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

Use !include to read text files into context for the assistant #98

Merged
merged 2 commits into from
Nov 17, 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
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)
Loading