Basics of prompt¶
This notebook demonstrates how to use FewShotPromptBuilder
to create structured prompts for OpenAI models.
In [1]:
Copied!
# Import FewShotPromptBuilder from openaivec
from openaivec.prompt import FewShotPromptBuilder
# Import FewShotPromptBuilder from openaivec
from openaivec.prompt import FewShotPromptBuilder
Basic Usage¶
Create a simple prompt with purpose, cautions, and examples.
In [2]:
Copied!
# Build a basic prompt with purpose, cautions, and examples
prompt_str: str = (
FewShotPromptBuilder()
.purpose("some purpose")
.caution("some caution")
.caution("some other caution")
.example(
input_value="some input",
output_value="some output"
)
.example(
input_value="some other input",
output_value="some other output"
)
.build()
)
# Print the generated prompt
print(prompt_str)
# Build a basic prompt with purpose, cautions, and examples
prompt_str: str = (
FewShotPromptBuilder()
.purpose("some purpose")
.caution("some caution")
.caution("some other caution")
.example(
input_value="some input",
output_value="some output"
)
.example(
input_value="some other input",
output_value="some other output"
)
.build()
)
# Print the generated prompt
print(prompt_str)
<Prompt> <Purpose>some purpose</Purpose> <Cautions> <Caution>some caution</Caution> <Caution>some other caution</Caution> </Cautions> <Examples> <Example> <Input>some input</Input> <Output>some output</Output> </Example> <Example> <Input>some other input</Input> <Output>some other output</Output> </Example> </Examples> </Prompt>
Structured Output Example¶
Demonstrate how to use a structured output with Pydantic models.
In [3]:
Copied!
# Import BaseModel from pydantic for structured outputs
from pydantic import BaseModel
# Define a structured result model
class Result(BaseModel):
field1: str
field2: str
# Build a prompt using structured examples
prompt_str: str = (
FewShotPromptBuilder()
.purpose("some purpose")
.caution("some caution")
.caution("some other caution")
.example(
input_value="some input",
output_value=Result(field1="some field", field2="some other field")
)
.example(
input_value="some other input",
output_value=Result(field1="some field", field2="some other field")
)
.build()
)
# Print the structured prompt
print(prompt_str)
# Import BaseModel from pydantic for structured outputs
from pydantic import BaseModel
# Define a structured result model
class Result(BaseModel):
field1: str
field2: str
# Build a prompt using structured examples
prompt_str: str = (
FewShotPromptBuilder()
.purpose("some purpose")
.caution("some caution")
.caution("some other caution")
.example(
input_value="some input",
output_value=Result(field1="some field", field2="some other field")
)
.example(
input_value="some other input",
output_value=Result(field1="some field", field2="some other field")
)
.build()
)
# Print the structured prompt
print(prompt_str)
<Prompt> <Purpose>some purpose</Purpose> <Cautions> <Caution>some caution</Caution> <Caution>some other caution</Caution> </Cautions> <Examples> <Example> <Input>some input</Input> <Output>{"field1":"some field","field2":"some other field"}</Output> </Example> <Example> <Input>some other input</Input> <Output>{"field1":"some field","field2":"some other field"}</Output> </Example> </Examples> </Prompt>
Improving Prompts with LLM¶
Use an OpenAI model to automatically improve and explain the prompt.
In [4]:
Copied!
# Import OpenAI client
from openai import OpenAI
# Initialize OpenAI client
client = OpenAI()
# Define a structured model for fruits
class Fruit(BaseModel):
name: str
color: str
# Build and improve the prompt using OpenAI's GPT model
prompt: str = (
FewShotPromptBuilder()
.purpose("Return the color of given fruit")
.caution("The fruit name should be in English")
.example("Apple", Fruit(name="Apple", color="Red"))
.example("Peach", Fruit(name="Peach", color="Pink"))
.example("Banana", Fruit(name="Banana", color="Yellow"))
.example("Strawberry", Fruit(name="Strawberry", color="Red"))
.example("Blueberry", Fruit(name="Blueberry", color="Blue"))
.improve(client=client, model_name="gpt-4o-mini")
.explain()
.build()
)
# Import OpenAI client
from openai import OpenAI
# Initialize OpenAI client
client = OpenAI()
# Define a structured model for fruits
class Fruit(BaseModel):
name: str
color: str
# Build and improve the prompt using OpenAI's GPT model
prompt: str = (
FewShotPromptBuilder()
.purpose("Return the color of given fruit")
.caution("The fruit name should be in English")
.example("Apple", Fruit(name="Apple", color="Red"))
.example("Peach", Fruit(name="Peach", color="Pink"))
.example("Banana", Fruit(name="Banana", color="Yellow"))
.example("Strawberry", Fruit(name="Strawberry", color="Red"))
.example("Blueberry", Fruit(name="Blueberry", color="Blue"))
.improve(client=client, model_name="gpt-4o-mini")
.explain()
.build()
)
=== Iteration 1 === Instruction: The original purpose was too simplistic and did not specify that the task involves returning a structured JSON response. This lack of clarity could lead to confusion about the expected output format. In this iteration, we refined the purpose to explicitly state that the goal is to return a JSON object containing the fruit's name and color. --- before +++ after @@ -1,7 +1,7 @@ <Prompt> - <Purpose>Return the color of given fruit</Purpose> + <Purpose>Return a JSON object containing the name and color of the given fruit.</Purpose> <Cautions> - <Caution>The fruit name should be in English</Caution> + <Caution>The fruit name should be in English.</Caution> </Cautions> <Examples> <Example> === Iteration 2 === Instruction: Next, we focused on the cautions section. The original caution only mentioned that the fruit name should be in English, which is too narrow. It did not address potential issues such as the need for correct spelling or the possibility of ambiguous fruit names. This could lead to incorrect outputs. Therefore, we added cautions regarding spelling and ambiguity. --- before +++ after @@ -2,6 +2,8 @@ <Purpose>Return a JSON object containing the name and color of the given fruit.</Purpose> <Cautions> <Caution>The fruit name should be in English.</Caution> + <Caution>Ensure the spelling of the fruit name is correct.</Caution> + <Caution>Be aware of ambiguous fruit names that may have multiple interpretations.</Caution> </Cautions> <Examples> <Example> === Iteration 3 === Instruction: In this step, we improved the examples section to cover a broader range of fruits and their colors. The original examples were limited and did not include fruits that might be less common or have different colors. By adding more diverse examples, we enhance the understanding of the task and ensure better coverage. This iteration only modifies the examples section, leaving purpose and cautions intact. --- before +++ after @@ -26,5 +26,21 @@ <Input>Blueberry</Input> <Output>{"name":"Blueberry","color":"Blue"}</Output> </Example> + <Example> + <Input>Grape</Input> + <Output>{"name":"Grape","color":"Purple"}</Output> + </Example> + <Example> + <Input>Orange</Input> + <Output>{"name":"Orange","color":"Orange"}</Output> + </Example> + <Example> + <Input>Kiwi</Input> + <Output>{"name":"Kiwi","color":"Brown"}</Output> + </Example> + <Example> + <Input>Watermelon</Input> + <Output>{"name":"Watermelon","color":"Green"}</Output> + </Example> </Examples> </Prompt> === Iteration 4 === Instruction: In this final iteration, we reviewed the entire prompt to ensure no issues remain. We found that the examples were well-rounded, but we needed to ensure that the output format was consistent. We standardized the output format to ensure it is always a JSON string, which is crucial for clarity and consistency. This final adjustment ensures that the prompt is clear and unambiguous.
Display Improved Prompt¶
Output the improved and explained prompt.
In [5]:
Copied!
# Print the improved prompt
print(prompt)
# Print the improved prompt
print(prompt)
<Prompt> <Purpose>Return a JSON object containing the name and color of the given fruit.</Purpose> <Cautions> <Caution>The fruit name should be in English.</Caution> <Caution>Ensure the spelling of the fruit name is correct.</Caution> <Caution>Be aware of ambiguous fruit names that may have multiple interpretations.</Caution> </Cautions> <Examples> <Example> <Input>Apple</Input> <Output>{"name":"Apple","color":"Red"}</Output> </Example> <Example> <Input>Peach</Input> <Output>{"name":"Peach","color":"Pink"}</Output> </Example> <Example> <Input>Banana</Input> <Output>{"name":"Banana","color":"Yellow"}</Output> </Example> <Example> <Input>Strawberry</Input> <Output>{"name":"Strawberry","color":"Red"}</Output> </Example> <Example> <Input>Blueberry</Input> <Output>{"name":"Blueberry","color":"Blue"}</Output> </Example> <Example> <Input>Grape</Input> <Output>{"name":"Grape","color":"Purple"}</Output> </Example> <Example> <Input>Orange</Input> <Output>{"name":"Orange","color":"Orange"}</Output> </Example> <Example> <Input>Kiwi</Input> <Output>{"name":"Kiwi","color":"Brown"}</Output> </Example> <Example> <Input>Watermelon</Input> <Output>{"name":"Watermelon","color":"Green"}</Output> </Example> </Examples> </Prompt>
Conclusion¶
This notebook illustrated how to effectively use FewShotPromptBuilder
to create, structure, and enhance prompts for OpenAI models.