6 个版本
0.3.1 | 2023 年 12 月 15 日 |
---|---|
0.3.0 | 2023 年 12 月 14 日 |
0.2.0 | 2023 年 12 月 8 日 |
0.1.2 | 2023 年 12 月 4 日 |
0.1.0 | 2023 年 11 月 29 日 |
#67 在 模板引擎 中
210KB
4K SLoC
提示框
提示框允许维护 LLM 提示模板库,可以从命令行进行填充和提交。它可以向各种主机提交提示,包括 Together、Ollama 以及与 OpenAI API 兼容的任何主机。
模板文件
- 使用 TOML 构建
- 可以使用 Tera 模板(类似于 Jinja),并引用其他文件中的模板
- 定义命令行参数,可以包括对文件的引用
- 文件名格式为
<NAME>.pb.toml
# File: summarize.pb.toml
description = "Summarize some files"
# Optional system prompt
# Or `system_prompt_path` to read from a template file
system_prompt = "You are a great summarizer."
# This can also be template_path to read from another file.
template = '''
Create a {{style}} summary of the below files
which are on the topic of {{topic}}. The summary should be about {{ len }} sentences long.
{% for f in file -%}
File {{ f.filename }}:
{{ f.contents }}
{%- endfor %}
'''
[model]
# These model options can also be defined in a config file to apply to the whole directory of templates.
model = "gpt-3.5-turbo"
temperature = 0.7
# Also supports top_p, frequency_penalty, presence_penalty, stop, and max_tokens
# And format = "json"
[options]
len = { type = "int", description = "The length of the summary", default = 4 }
topic = { type = "string", description = "The topic of the summary" }
style = { type = "string", default = "concise" }
file = { type = "file", array = true, description = "The files to summarize" }
# For multimodal models
image = { type = "image", array = true, description = "The images to summarize" }
对于多模态模型,将自动将图像参数添加到请求中,并且不需要在提示模板中引用。
然后运行它
> promptbox run summarize --topic software --file README.md
The README.md file provides an overview of the PromptBox utility, which is used for maintaining libraries of
LLM prompt templates that can be filled in and submitted from the command line. It explains that template files
are built in TOML and can use Tera templating. The file also includes an example template for summarizing files
on a specific topic, with options for length, formality, and the files to summarize. Additionally, it mentions the
presence of configuration files that can set default model options and inherit settings from parent directories.
> promptbox run summarize --topic software --file README.md --style excited
Introducing PromptBox, a powerful utility for maintaining libraries of LLM prompt templates! With PromptBox, you can
easily fill in and submit prompt templates from the command line. These template files, built in TOML, can utilize
Tera templating and reference templates in other files. They also define command-line arguments, including references
to files. The excitement doesn't stop there! PromptBox even supports configuration files, allowing you to set default
model options and inherit settings from parent directories. Get ready to revolutionize your software experience
with PromptBox!
附加输入
提示框可以接受额外的命令行参数输入,或从另一个命令中通过管道传入。
cat"transcript.txt" |pb run summarize"这里是记录"
默认情况下,此内容将追加到提示的末尾,但模板可以将其引用为 {{extra}}
以将其放置在提示的另一个位置,如本例所示。
Below is a transcript of a video named "{{title}}":
{{extra}}
Create a detailed outline of the above transcript.
当与在提示末尾放置指令效果最好的模型一起使用时,这可能会很有帮助。
模型选择
主机选择
提示框支持一些内置的模型主机
- lm-studio
- ollama
- openai
- openrouter
- together
虽然可以显式选择主机,但提示框将尝试根据模型名称使用此逻辑来选择主机
- 以 "gpt-3.5" 或 "gpt-4" 开头的任何模型名称将选择 OpenAI。
- 值 "lm-studio" 将导致调用 LM Studio。LM Studio 的 API 目前不支持选择模型,因此您需要在 GUI 中自行切换。
- 任何其他型号名称都表示使用默认型号,除非另外配置,否则为Ollama。
请参阅本README文件的末尾,了解如何定义您自己的主机。
可以通过指定包含要使用的主机对象来覆盖默认的主机选择。
model = { model = "mistralai/Mistral-7B-v0.1", host = "together" }
别名
模型也可以使用别名。在模板或配置文件中,您可以添加一个model.alias
部分。
[model.alias]
phind = "phind-codellama:34b-v2-q5_K_M"
deepseek = "deepseek-coder:7b"
together-mistral = { model = "mistralai/Mistral-7B-v0.1", host = "together" }
然后可以使用这些模型别名来代替实际模型名称。
上下文长度管理
当您的提示及其输入变得很大时,PromptBox会将其截断以适应。有一些选项可以更好地控制截断行为。
[model.context]
# Override the context length limit from the model. Usually you can omit this unless you
# want to artificially decrease the context length to save time, money, etc.
limit = 384
# Make sure the context has enough room for this many tokens of output.
# This defaults to 256 if not otherwise specified.
# The prompt will contain roughly `limit - reserve_output` tokens.
reserve_output = 256
# When trimming context, should it keep the "start" or the "end"
keep = "start"
# keep = "end"
# The names of arguments to trim context from. If omitted, the entire prompt is trimmed to fit.
trim_args = ["extra", "files"]
# When trimming array arguments, whether to preserve the first arguments,
# the last arguments, or try to trim equally.
array_priority = "first"
# array_priority = "last"
# array_priority = "equal"
目前无论选择哪种模型,都使用Llama 2分词器。这不会为每个模型提供精确的结果,但对于大多数情况来说足够接近。
配置文件
每个模板目录都包含一个配置文件,可以设置默认模型选项。配置文件从当前目录读取到父目录。
在搜索的每个目录中,PromptBox都会在该目录及其promptbox
子目录中查找配置文件。
还会读取全局配置目录,如.config/promptbox/promptbox.toml
。
配置文件会继承其父目录配置文件的设置,对于它没有设置的选项也是如此。配置文件中的所有设置都是可选的。
# By default the templates are in the same directory as the configuration file, but this can be overridden
# by setting the templates option
templates = ["template_dir"]
# This can be set to true to tell PromptBox to stop looking in parent directories for
# configurations and templates.
top_level = false
# Set this to false to tell PromptBox to not read the global configuration file.
use_global_config = true
# Use this host for models that aren't otherwise specified and aren't "lm-studio" or a GPT-3.5/4 model.
default_host = "ollama"
[model]
# Set a default model. All the other options from the template's `model` section can be used here.
model = "gpt-3.5-turbo"
自定义主机
除了内置主机外,PromptBox还支持使用配置文件中的此格式添加其他主机。
[host.my_custom_host]
# The base URL to use for the API.
# For OpenAI, `chat/completions` will be added automatically.
endpoint = "https://super-fast-llm.example.com/api/v1"
# protocol can be openai, ollama, or together
protocol = "openai"
# Whether or not PromptBox should limit the context length sent to the host.
# Some hosts do not provide good information on this, or have their own methods of context
# compression.
limit_context_length = true
# The name of the environment variable that holds the API key. To promote good security hygiene,
# it is not possible to embed the key directly in the configuration file.
# This can be omitted if an API key is not required for the host.
api_key = "MY_HOST_API_KEY"
然后可以通过将default_host = "my_custom_host"
或如上所述设置单个模型的主机来使用自定义主机。
修改内置主机
此语法也可以用来更改内置主机的行为。例如,这将更改Ollama主机使用的端点。
[host.ollama]
endpoint = "https://127.0.0.1:12345"
依赖项
~22–32MB
~560K SLoC