openaivec.prompt
This module provides a builder for creating few‑shot prompts, which are used to train large language models (LLMs) by providing them with examples of input/output pairs. The builder allows for the construction of a prompt in a structured way, including setting the purpose, adding cautions, and providing examples.
from openaivec.prompt import FewShotPromptBuilder
prompt_str: str = (
FewShotPromptBuilder()
.purpose("some purpose")
.caution("some caution")
.caution("some other caution")
.example("some input", "some output")
.example("some other input", "some other output")
.build()
)
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>
Example
Bases: BaseModel
Represents a single input/output example used in few‑shot prompts.
Attributes:
Name | Type | Description |
---|---|---|
input |
str
|
The input text that will be passed to the model. |
output |
str
|
The expected output corresponding to the given input. |
Source code in src/openaivec/prompt.py
62 63 64 65 66 67 68 69 70 71 |
|
FewShotPrompt
Bases: BaseModel
Represents a prompt definition used for few‑shot learning.
The data collected in this model is later rendered into XML and sent to a large‑language model as part of the system prompt.
Attributes:
Name | Type | Description |
---|---|---|
purpose |
str
|
A concise, human‑readable statement describing the goal of the prompt. |
cautions |
list[str]
|
A list of warnings, edge cases, or pitfalls that the model should be aware of when generating answers. |
examples |
list[Example]
|
Input/output pairs demonstrating the expected behaviour for a variety of scenarios. |
Source code in src/openaivec/prompt.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
FewShotPromptBuilder
Source code in src/openaivec/prompt.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
|
__init__()
Initialize an empty FewShotPromptBuilder.
Source code in src/openaivec/prompt.py
334 335 336 |
|
build()
Build and return the prompt as XML.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
XML representation of the prompt. |
Source code in src/openaivec/prompt.py
495 496 497 498 499 500 501 502 |
|
build_json(**kwargs)
Build and return the prompt as a JSON string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Keyword arguments forwarded to |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
JSON representation of the prompt. |
Source code in src/openaivec/prompt.py
504 505 506 507 508 509 510 511 512 513 514 |
|
build_xml()
Alias for :py:meth:build
for explicit XML generation.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
XML representation of the prompt. |
Source code in src/openaivec/prompt.py
516 517 518 519 520 521 522 523 |
|
caution(caution)
Append a cautionary note to the prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
caution
|
str
|
A caution or edge‑case description. |
required |
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
The current builder instance. |
Source code in src/openaivec/prompt.py
373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
example(input_value, output_value)
Add a single input/output example.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_value
|
str | BaseModel
|
Example input; if a Pydantic model is provided it is serialised to JSON. |
required |
output_value
|
str | BaseModel
|
Expected output; serialised if needed. |
required |
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
The current builder instance. |
Source code in src/openaivec/prompt.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
explain()
Pretty‑print the diff of each improvement iteration.
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
The current builder instance. |
Source code in src/openaivec/prompt.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
|
get_object()
Return the underlying FewShotPrompt object.
Returns:
Name | Type | Description |
---|---|---|
FewShotPrompt |
FewShotPrompt
|
The validated prompt object. |
Source code in src/openaivec/prompt.py
486 487 488 489 490 491 492 493 |
|
improve(client, model_name, temperature=0.0, top_p=1.0)
Iteratively refine the prompt using an LLM.
The method calls a single LLM request that returns multiple editing steps and stores each step for inspection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OpenAI
|
Configured OpenAI client. |
required |
model_name
|
str
|
Model identifier (e.g. |
required |
temperature
|
float
|
Sampling temperature. Defaults to 0.0. |
0.0
|
top_p
|
float
|
Nucleus sampling parameter. Defaults to 1.0. |
1.0
|
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
The current builder instance containing the refined prompt and iteration history. |
Source code in src/openaivec/prompt.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
of(prompt)
classmethod
Create a builder pre‑populated with an existing prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
FewShotPrompt
|
The prompt to start from. |
required |
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
A new builder instance. |
Source code in src/openaivec/prompt.py
338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
of_empty()
classmethod
Create a builder.
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
A new builder instance with an empty prompt. |
Source code in src/openaivec/prompt.py
352 353 354 355 356 357 358 359 |
|
purpose(purpose)
Set the purpose of the prompt.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
purpose
|
str
|
A concise statement describing the prompt’s goal. |
required |
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
The current builder instance (for chaining). |
Source code in src/openaivec/prompt.py
361 362 363 364 365 366 367 368 369 370 371 |
|
Step
Bases: BaseModel
A single refinement iteration produced by the LLM.
Attributes:
Name | Type | Description |
---|---|---|
id |
int
|
Sequential identifier of the iteration ( |
analysis |
str
|
Natural‑language explanation of the issue addressed in this iteration and why the change was necessary. |
prompt |
FewShotPrompt
|
The updated prompt after applying the described modification. |
Source code in src/openaivec/prompt.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|