Examples inside Prompts
Examples inside Prompts
The `gollm` package supports the use of examples in prompts, which can help guide the Language Model to produce more accurate and consistent responses. Examples can be added directly in the code or loaded from a file.
How it works
Examples are additional context provided to the LLM to demonstrate the desired output format or style. They can be particularly useful for:
1. Demonstrating specific formats or structures you want the LLM to follow.
2. Providing context for domain-specific tasks.
3. Guiding the tone or style of the LLM's responses.
Usage
Adding Examples Directly in Code
You can add examples directly when creating a new prompt using the `WithExamples` option:
Loading Examples from a File
You can also load examples from a file using the WithExamplesFromFile
option:
The file should contain one example per line for .txt
files, or be in JSON Lines format for .jsonl
files.
Example: Creative Writing Assistant
Here's a more comprehensive example demonstrating how to use examples in a creative writing assistant:
In this example:
We create an LLM instance with appropriate settings.
We demonstrate two ways of using examples: a. Inline examples for a short story prompt. b. Examples loaded from a file for a poetry prompt.
We use additional directives to further guide the LLM's output.
File Format for Examples
When using WithExamplesFromFile
, the file should be formatted as follows:
For .txt
files:
For .jsonl
files:
Considerations
Quality over Quantity: A few well-chosen examples are often more effective than many mediocre ones.
Relevance: Ensure your examples are closely related to the task you're asking the LLM to perform.
Variety: If possible, provide examples that cover different aspects or styles within your task.
Token Limits: Remember that examples count towards your token limit, so balance the number of examples with the space needed for the LLM's response.
Advanced Usage
For more advanced usage, including dynamic example selection or generation, refer to the examples/advanced_examples.go
file in the repository.
Last updated