PromptoGen¶
Bridging LLMs and Python Seamlessly.
Documentation: https://promptogen.zawakin.dev
Source Code: https://github.com/zawakin/promptogen
For the Quick Start Guide, click here.
PromptoGen: Bridging LLMs and Python Seamlessly.¶
Vision¶
"Seamlessly bridge the gap between LLM and Python, ensuring efficient, future-ready communication."
Key Challenges in LLM Libraries:¶
- Lack of an ecosystem for prompt engineering., making prompt creation and sharing difficult.
- Strong dependency on specific LLM versions, making them vulnerable to LLM updates.
- Complex implementations, hindering customization.
Why Choose PromptoGen?¶
- Seamless Conversion between LLM I/O and Python Objects: Streamlining LLM interactions.
- Flexible & Unique Interface: Guaranteeing user customizability and extensibility.
- Future-Proof Design: Stay ahead with reduced dependency on LLM evolutions.
Compared to Other Libraries: Many are tied to specific LLM versions, lacking the adaptability that PromptoGen offers. With a dependency only on the Pydantic
data class library, PromptoGen serves as the ideal bridge between LLM strings and Python objects.
Core Features:¶
Prompt
Data Class: Standardizing LLM communication and supporting prompt engineering.TextLLM
Interface: Independence from LLM specifics.PromptFormatter
Interface: High customizability for users.
Benefits:¶
- Modular & Extendable: Flexibly mix, match, and add custom components.
- Future-Proof: Stand strong against new model updates.
- Maintainability: Ensuring easy debugging and minimal adjustments for different LLMs.
Unsupported Features:¶
- Direct LLM Communication: We prioritize efficient interfacing over direct LLM conversations.
- Prompt Version Management: To keep things streamlined, we avoid adding versioning complexities.
- Specific LLM Optimization: Our focus is on adaptability across LLMs rather than optimizing for any single one.
Learn More¶
Dive deep into the About PromptoGen for a comprehensive understanding.
Requirements¶
Python 3.8 or higher
Installation¶
Importing¶
How to Use¶
TextLLM: Flexible LLM Integration¶
Through pg.TextLLM
, PromptoGen achieves collaboration with a variety of large-scale language models (LLM).
import promptogen as pg
class YourTextLLM(pg.TextLLM):
def __init__(self, model: str):
self.model = model
def generate(self, text: str) -> str:
return generate_by_your_text_llm(text, self.model)
text_llm = YourTextLLM(model="your-model")
By adopting this interface, PromptoGen can seamlessly incorporate different LLMs and their versions. Users can utilize various LLMs in a consistent manner regardless of the specific LLM.
Creating your Prompt¶
import promptogen as pg
summarizer = pg.Prompt(
name="Text Summarizer and Keyword Extractor",
description="Summarize text and extract keywords.",
input_parameters=[
pg.ParameterInfo(name="text", description="Text to be summarized"),
],
output_parameters=[
pg.ParameterInfo(name="summary", description="Summarized text"),
pg.ParameterInfo(name="keywords", description="Extracted keywords from the text"),
],
template=pg.IOExample(
input={'text': "This is a sample text for summarization."},
output={
'summary': "This is a summary of the text.",
'keywords': ["sample", "text", "summarization"],
},
),
examples=[
pg.IOExample(
input={
'text': "One sunny afternoon, a group of friends opted to meet at the nearby park to indulge in various sports and activities. They engaged in soccer, badminton, and basketball, reveling in the joy of camaraderie and crafting unforgettable moments together."},
output={
'summary': "A bunch of friends relished an afternoon of sports and bonding at a neighborhood park.",
'keywords': ["friends", "park", "sports", "moments"],
},
)
],
)
Saving the Prompt¶
Loading the Prompt¶
Format your Prompt as a String¶
To send an instance of the Prompt
class to the LLM (Large Language Model) in actual use, you need to convert it into a string. With PromptoGen, you can use pg.PromptFormatter
to convert the prompt into a string in any desired format.
Basic Structure of Prompt Format¶
The basic structure of the prompt format is as follows:
<Prompt Description>
Input Parameters:
- <Description of Input Parameter 1>
- <Description of Input Parameter 2>
- ...
Output Parameters:
- <Description of Output Parameter 1>
- <Description of Output Parameter 2>
- ...
Template:
Input:
<Name of Input Parameter 1>: <Example of Input Parameter 1>
<Name of Input Parameter 2>: <Example of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Example of Output Parameter 1>
<Name of Output Parameter 2>: <Example of Output Parameter 2>
...
Example 1:
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Value of Output Parameter 1>
<Name of Output Parameter 2>: <Value of Output Parameter 2>
...
...
--------
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Prompt Description>
Input Parameters:
- <Description of Input Parameter 1>
- <Description of Input Parameter 2>
- ...
Output Parameters:
- <Description of Output Parameter 1>
- <Description of Output Parameter 2>
- ...
Template:
Input:
<Name of Input Parameter 1>: <Example of Input Parameter 1>
<Name of Input Parameter 2>: <Example of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Example of Output Parameter 1>
<Name of Output Parameter 2>: <Example of Output Parameter 2>
...
Example 1:
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Value of Output Parameter 1>
<Name of Output Parameter 2>: <Value of Output Parameter 2>
...
...
--------
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Prompt Description>
Input Parameters:
- <Description of Input Parameter 1>
- <Description of Input Parameter 2>
- ...
Output Parameters:
- <Description of Output Parameter 1>
- <Description of Output Parameter 2>
- ...
Template:
Input:
<Name of Input Parameter 1>: <Example of Input Parameter 1>
<Name of Input Parameter 2>: <Example of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Example of Output Parameter 1>
<Name of Output Parameter 2>: <Example of Output Parameter 2>
...
Example 1:
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Value of Output Parameter 1>
<Name of Output Parameter 2>: <Value of Output Parameter 2>
...
...
--------
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Prompt Description>
Input Parameters:
- <Description of Input Parameter 1>
- <Description of Input Parameter 2>
- ...
Output Parameters:
- <Description of Output Parameter 1>
- <Description of Output Parameter 2>
- ...
Template:
Input:
<Name of Input Parameter 1>: <Example of Input Parameter 1>
<Name of Input Parameter 2>: <Example of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Example of Output Parameter 1>
<Name of Output Parameter 2>: <Example of Output Parameter 2>
...
Example 1:
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Value of Output Parameter 1>
<Name of Output Parameter 2>: <Value of Output Parameter 2>
...
...
--------
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Prompt Description>
Input Parameters:
- <Description of Input Parameter 1>
- <Description of Input Parameter 2>
- ...
Output Parameters:
- <Description of Output Parameter 1>
- <Description of Output Parameter 2>
- ...
Template:
Input:
<Name of Input Parameter 1>: <Example of Input Parameter 1>
<Name of Input Parameter 2>: <Example of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Example of Output Parameter 1>
<Name of Output Parameter 2>: <Example of Output Parameter 2>
...
Example 1:
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Value of Output Parameter 1>
<Name of Output Parameter 2>: <Value of Output Parameter 2>
...
...
--------
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Prompt Description>
Input Parameters:
- <Description of Input Parameter 1>
- <Description of Input Parameter 2>
- ...
Output Parameters:
- <Description of Output Parameter 1>
- <Description of Output Parameter 2>
- ...
Template:
Input:
<Name of Input Parameter 1>: <Example of Input Parameter 1>
<Name of Input Parameter 2>: <Example of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Example of Output Parameter 1>
<Name of Output Parameter 2>: <Example of Output Parameter 2>
...
Example 1:
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
<Name of Output Parameter 1>: <Value of Output Parameter 1>
<Name of Output Parameter 2>: <Value of Output Parameter 2>
...
...
--------
Input:
<Name of Input Parameter 1>: <Value of Input Parameter 1>
<Name of Input Parameter 2>: <Value of Input Parameter 2>
...
Output:
Various Formatting Styles¶
PromptoGen supports various formatting styles:
- KeyValue Format:
key: value
- JSON Format:
{"key": "value"}
- etc.
To use the key: value
format for the prompt, use the pg.KeyValuePromptFormatter
.
Whether to display parameter information or templates can be set with PromptFormatterConfig
.
By using the formatter.format_prompt
method, you can convert the prompt and its corresponding input into a string.
Summarize text and extract keywords.
Input Parameters:
- text: Text to summarize
Output Parameters:
- summary: Summary of text
- keywords: Keywords extracted from text
Template:
Input:
text: "This is a sample text to summarize."
Output:
summary: "This is a summary of the text."
keywords: ['sample', 'text', 'summarize']
Example 1:
Input:
text: "One sunny afternoon, a group of friends decided to gather at the nearby park to engage in various games and activities. They played soccer, badminton, and basketball, laughing and enjoying each other's company while creating unforgettable memories together."
Output:
summary: "A group of friends enjoyed an afternoon playing sports and making memories at a local park."
keywords: ['friends', 'park', 'sports', 'memories']
--------
Input:
text: "In the realm of software engineering, ..."
Output:
Summarize text and extract keywords.
Output a JSON-formatted string without outputting any other strings.
Be careful with the order of brackets in the json.
Input Parameters:
- text: Text to summarize
Output Parameters:
- summary: Summary of text
- keywords: Keywords extracted from text
Template:
Input:
```json
{
"text": "This is a sample text to summarize."
}```
Output:
```json
{
"summary": "This is a summary of the text.",
"keywords": [
"sample",
"text",
"summarize"
]
}```
Example 1:
Input:
```json
{
"text": "One sunny afternoon, a group of friends decided to gather at the nearby park to engage in various games and activities. They played soccer, badminton, and basketball, laughing and enjoying each other's company while creating unforgettable memories together."
}```
Output:
```json
{
"summary": "A group of friends enjoyed an afternoon playing sports and making memories at a local park.",
"keywords": [
"friends",
"park",
"sports",
"memories"
]
}```
--------
Input:
```json
{
"text": "In the realm of software engineering, ..."
}```
Output:
Parsing Outputs from Large Language Models¶
After receiving the prompt string as input, you obtain an output from a large language model (like GPT-3.5, GPT-4).
You can parse this output as:
PromptRunner: Execute Prompts Efficiently¶
pg.PromptRunner
supports the execution of prompts simply and efficiently.
import promptogen as pg
# Prepare an LLM that implements the `pg.TextLLM` interface
text_llm = YourTextLLM(model="your-model")
formatter = pg.KeyValuePromptFormatter()
runner = pg.TextLLMPromptRunner(llm=text_llm, formatter=formatter)
summarizer = pg.Prompt(
name="Text Summarizer and Keyword Extractor",
# ...
)
input_value = {
"text": "In the realm of software engineering, ...",
}
output_value = runner.run_prompt(summarizer, input_value)
print(output_value)
Advantages of this tool:
- Abstraction: Users can execute prompts without being aware of the specific LLM implementation.
- Consistency: Changes are minimized when executing the same prompt with different LLMs.
- Extensibility: Adding new prompts or modifying existing ones is easy.
pg.PromptRunner
is a key tool for making prompt execution more intuitive and efficient using PromptoGen.
Quick Start Guide¶
Please refer to the Quick Start Guide.
Application Examples¶
Refer to Application Examples.
Dependent Libraries¶
PromptoGen only depends on Pydantic to define the data class.
Limitations¶
- With updates to PromptoGen, compatibility with prompts outputted in JSON may be lost.
- The large language models tested for operation are OpenAI's
gpt-3.5-turbo
,gpt-4
, and Meta'sLlama 2
. Other large language models have not been tested for operation. In particular, there may be cases where the parser does not work correctly, so please be cautious.
Contribution¶
Bug reports, proposals for new features, pull requests, etc., are all welcome! For more details, please see Contribution.
License¶
MIT License