Main Package API¶
The main openaivec
package provides the core classes for AI-powered data processing.
Core Classes¶
All core functionality is accessible through the main package imports:
openaivec.BatchResponses
dataclass
¶
BatchResponses(
client: OpenAI,
model_name: str,
system_message: str,
response_format: type[ResponseFormat] = str,
cache: BatchingMapProxy[str, ResponseFormat] = (
lambda: BatchingMapProxy(batch_size=None)
)(),
api_kwargs: dict[
str, int | float | str | bool
] = dict(),
)
Bases: Generic[ResponseFormat]
Stateless façade that turns OpenAI's JSON‑mode API into a batched API.
This wrapper allows you to submit multiple user prompts in one JSON‑mode request and receive the answers in the original order.
Example
Attributes:
Name | Type | Description |
---|---|---|
client |
OpenAI
|
Initialised OpenAI client. |
model_name |
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
system_message |
str
|
System prompt prepended to every request. |
response_format |
type[ResponseFormat]
|
Expected Pydantic model class or |
cache |
BatchingMapProxy[str, ResponseFormat]
|
Order‑preserving batching proxy with de‑duplication and caching. |
Notes
Internally the work is delegated to two helpers:
_predict_chunk
– fragments the workload and restores ordering._request_llm
– performs a single OpenAI API call.
Functions¶
of
classmethod
¶
of(
client: OpenAI,
model_name: str,
system_message: str,
response_format: type[ResponseFormat] = str,
batch_size: int | None = None,
**api_kwargs,
) -> BatchResponses
Factory constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OpenAI
|
OpenAI client. |
required |
model_name
|
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
required |
system_message
|
str
|
System prompt for the model. |
required |
response_format
|
type[ResponseFormat]
|
Expected output type. Defaults to |
str
|
batch_size
|
int | None
|
Max unique prompts per API call. Defaults to None (automatic batch size optimization). Set to a positive integer for fixed batch size. |
None
|
**api_kwargs
|
Additional OpenAI API parameters (temperature, top_p, etc.). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
BatchResponses |
BatchResponses
|
Configured instance backed by a batching proxy. |
Source code in src/openaivec/_responses.py
of_task
classmethod
¶
of_task(
client: OpenAI,
model_name: str,
task: PreparedTask[ResponseFormat],
batch_size: int | None = None,
) -> BatchResponses
Factory from a PreparedTask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OpenAI
|
OpenAI client. |
required |
model_name
|
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
required |
task
|
PreparedTask
|
Prepared task with instructions and response format. |
required |
batch_size
|
int | None
|
Max unique prompts per API call. Defaults to None (automatic batch size optimization). Set to a positive integer for fixed batch size. |
None
|
Returns:
Name | Type | Description |
---|---|---|
BatchResponses |
BatchResponses
|
Configured instance backed by a batching proxy. |
Source code in src/openaivec/_responses.py
parse ¶
Batched predict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
list[str]
|
Prompts that require responses. Duplicates are de‑duplicated. |
required |
Returns:
Type | Description |
---|---|
list[ResponseFormat | None]
|
list[ResponseFormat | None]: Assistant responses aligned to |
Source code in src/openaivec/_responses.py
openaivec.AsyncBatchResponses
dataclass
¶
AsyncBatchResponses(
client: AsyncOpenAI,
model_name: str,
system_message: str,
response_format: type[ResponseFormat] = str,
cache: AsyncBatchingMapProxy[str, ResponseFormat] = (
lambda: AsyncBatchingMapProxy(
batch_size=None, max_concurrency=8
)
)(),
api_kwargs: dict[
str, int | float | str | bool
] = dict(),
)
Bases: Generic[ResponseFormat]
Stateless façade that turns OpenAI's JSON-mode API into a batched API (Async version).
This wrapper allows you to submit multiple user prompts in one JSON-mode request and receive the answers in the original order asynchronously. It also controls the maximum number of concurrent requests to the OpenAI API.
Example
import asyncio
from openai import AsyncOpenAI
from openaivec import AsyncBatchResponses
openai_async_client = AsyncOpenAI() # initialize your client
vector_llm = AsyncBatchResponses.of(
client=openai_async_client,
model_name="gpt-4.1-mini",
system_message="You are a helpful assistant.",
batch_size=64,
max_concurrency=5,
)
questions = [
"What is the capital of France?",
"Explain quantum physics simply.",
]
async def main():
answers = await vector_llm.parse(questions)
print(answers)
asyncio.run(main())
Attributes:
Name | Type | Description |
---|---|---|
client |
AsyncOpenAI
|
Initialised OpenAI async client. |
model_name |
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
system_message |
str
|
System prompt prepended to every request. |
response_format |
type[ResponseFormat]
|
Expected Pydantic model class or |
cache |
AsyncBatchingMapProxy[str, ResponseFormat]
|
Async batching proxy with de‑duplication and concurrency control. |
Functions¶
of
classmethod
¶
of(
client: AsyncOpenAI,
model_name: str,
system_message: str,
response_format: type[ResponseFormat] = str,
batch_size: int | None = None,
max_concurrency: int = 8,
**api_kwargs,
) -> AsyncBatchResponses
Factory constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
AsyncOpenAI
|
OpenAI async client. |
required |
model_name
|
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
required |
system_message
|
str
|
System prompt. |
required |
response_format
|
type[ResponseFormat]
|
Expected output type. Defaults to |
str
|
batch_size
|
int | None
|
Max unique prompts per API call. Defaults to None (automatic batch size optimization). Set to a positive integer for fixed batch size. |
None
|
max_concurrency
|
int
|
Max concurrent API calls. Defaults to 8. |
8
|
**api_kwargs
|
Additional OpenAI API parameters (temperature, top_p, etc.). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
AsyncBatchResponses |
AsyncBatchResponses
|
Configured instance backed by an async batching proxy. |
Source code in src/openaivec/_responses.py
of_task
classmethod
¶
of_task(
client: AsyncOpenAI,
model_name: str,
task: PreparedTask[ResponseFormat],
batch_size: int | None = None,
max_concurrency: int = 8,
) -> AsyncBatchResponses
Factory from a PreparedTask.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
AsyncOpenAI
|
OpenAI async client. |
required |
model_name
|
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
required |
task
|
PreparedTask
|
Prepared task with instructions and response format. |
required |
batch_size
|
int | None
|
Max unique prompts per API call. Defaults to None (automatic batch size optimization). Set to a positive integer for fixed batch size. |
None
|
max_concurrency
|
int
|
Max concurrent API calls. Defaults to 8. |
8
|
Returns:
Name | Type | Description |
---|---|---|
AsyncBatchResponses |
AsyncBatchResponses
|
Configured instance backed by an async batching proxy. |
Source code in src/openaivec/_responses.py
parse
async
¶
Batched predict (async).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
list[str]
|
Prompts that require responses. Duplicates are de‑duplicated. |
required |
Returns:
Type | Description |
---|---|
list[ResponseFormat | None]
|
list[ResponseFormat | None]: Assistant responses aligned to |
Source code in src/openaivec/_responses.py
openaivec.BatchEmbeddings
dataclass
¶
BatchEmbeddings(
client: OpenAI,
model_name: str,
cache: BatchingMapProxy[str, NDArray[float32]] = (
lambda: BatchingMapProxy(batch_size=None)
)(),
api_kwargs: dict[
str, int | float | str | bool
] = dict(),
)
Thin wrapper around the OpenAI embeddings endpoint (synchronous).
Attributes:
Name | Type | Description |
---|---|---|
client |
OpenAI
|
Configured OpenAI client. |
model_name |
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name
(e.g., |
cache |
BatchingMapProxy[str, NDArray[float32]]
|
Batching proxy for ordered, cached mapping. |
api_kwargs |
dict[str, Any]
|
Additional OpenAI API parameters stored at initialization. |
Functions¶
of
classmethod
¶
of(
client: OpenAI,
model_name: str,
batch_size: int | None = None,
**api_kwargs,
) -> BatchEmbeddings
Factory constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OpenAI
|
OpenAI client. |
required |
model_name
|
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
required |
batch_size
|
int | None
|
Max unique inputs per API call. Defaults to None (automatic batch size optimization). Set to a positive integer for fixed batch size. |
None
|
**api_kwargs
|
Additional OpenAI API parameters (e.g., dimensions for text-embedding-3 models). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
BatchEmbeddings |
BatchEmbeddings
|
Configured instance backed by a batching proxy. |
Source code in src/openaivec/_embeddings.py
create ¶
Generate embeddings for inputs using cached, ordered batching.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
list[str]
|
Input strings. Duplicates allowed. |
required |
Returns:
Type | Description |
---|---|
list[NDArray[float32]]
|
list[NDArray[np.float32]]: Embedding vectors aligned to |
Source code in src/openaivec/_embeddings.py
openaivec.AsyncBatchEmbeddings
dataclass
¶
AsyncBatchEmbeddings(
client: AsyncOpenAI,
model_name: str,
cache: AsyncBatchingMapProxy[str, NDArray[float32]] = (
lambda: AsyncBatchingMapProxy(
batch_size=None, max_concurrency=8
)
)(),
api_kwargs: dict[
str, int | float | str | bool
] = dict(),
)
Thin wrapper around the OpenAI embeddings endpoint (asynchronous).
This class provides an asynchronous interface for generating embeddings using OpenAI models. It manages concurrency, handles rate limits automatically, and efficiently processes batches of inputs, including de-duplication.
Example
import asyncio
import numpy as np
from openai import AsyncOpenAI
from openaivec import AsyncBatchEmbeddings
# Assuming openai_async_client is an initialized AsyncOpenAI client
openai_async_client = AsyncOpenAI() # Replace with your actual client initialization
embedder = AsyncBatchEmbeddings.of(
client=openai_async_client,
model_name="text-embedding-3-small",
batch_size=128,
max_concurrency=8,
)
texts = ["This is the first document.", "This is the second document.", "This is the first document."]
# Asynchronous call
async def main():
embeddings = await embedder.create(texts)
# embeddings will be a list of numpy arrays (float32)
# The embedding for the third text will be identical to the first
# due to automatic de-duplication.
print(f"Generated {len(embeddings)} embeddings.")
print(f"Shape of first embedding: {embeddings[0].shape}")
assert np.array_equal(embeddings[0], embeddings[2])
# Run the async function
asyncio.run(main())
Attributes:
Name | Type | Description |
---|---|---|
client |
AsyncOpenAI
|
Configured OpenAI async client. |
model_name |
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
cache |
AsyncBatchingMapProxy[str, NDArray[float32]]
|
Async batching proxy. |
api_kwargs |
dict
|
Additional OpenAI API parameters stored at initialization. |
Functions¶
of
classmethod
¶
of(
client: AsyncOpenAI,
model_name: str,
batch_size: int | None = None,
max_concurrency: int = 8,
**api_kwargs,
) -> AsyncBatchEmbeddings
Factory constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
AsyncOpenAI
|
OpenAI async client. |
required |
model_name
|
str
|
For Azure OpenAI, use your deployment name. For OpenAI, use the model name. |
required |
batch_size
|
int | None
|
Max unique inputs per API call. Defaults to None (automatic batch size optimization). Set to a positive integer for fixed batch size. |
None
|
max_concurrency
|
int
|
Max concurrent API calls. Defaults to 8. |
8
|
**api_kwargs
|
Additional OpenAI API parameters (e.g., dimensions for text-embedding-3 models). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
AsyncBatchEmbeddings |
AsyncBatchEmbeddings
|
Configured instance with an async batching proxy. |
Source code in src/openaivec/_embeddings.py
create
async
¶
Generate embeddings for inputs using proxy batching (async).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs
|
list[str]
|
Input strings. Duplicates allowed. |
required |
Returns:
Type | Description |
---|---|
list[NDArray[float32]]
|
list[NDArray[np.float32]]: Embedding vectors aligned to |
Source code in src/openaivec/_embeddings.py
Prompt Building¶
openaivec.FewShotPromptBuilder ¶
Builder for creating few-shot prompts with validation.
Usage
builder = (FewShotPromptBuilder() .purpose("Your task description") .example("input1", "output1") # At least one required .example("input2", "output2") .build())
Note
Both .purpose() and at least one .example() call are required before calling .build(), .improve(), or .get_object().
Initialize an empty FewShotPromptBuilder.
Note
You must call .purpose() and at least one .example() before building.
Source code in src/openaivec/_prompt.py
Functions¶
of
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
of_empty
classmethod
¶
Create a builder.
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
A new builder instance with an empty prompt. |
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
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
example ¶
Add a single input/output example.
At least one example is required before calling .build(), .improve(), or .get_object().
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
improve ¶
improve(
client: OpenAI | None = None,
model_name: str | None = None,
**api_kwargs,
) -> FewShotPromptBuilder
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.
When client is None, automatically creates a client using environment variables:
- For OpenAI: OPENAI_API_KEY
- For Azure OpenAI: AZURE_OPENAI_API_KEY
, AZURE_OPENAI_BASE_URL
, AZURE_OPENAI_API_VERSION
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
OpenAI | None
|
Configured OpenAI client. If None, uses DI container with environment variables. |
None
|
model_name
|
str | None
|
Model identifier. If None, uses default |
None
|
**api_kwargs
|
Additional OpenAI API parameters (temperature, top_p, etc.). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
FewShotPromptBuilder |
FewShotPromptBuilder
|
The current builder instance containing the refined prompt and iteration history. |
Raises:
Type | Description |
---|---|
ValueError
|
If the prompt is not valid (missing purpose or examples). |
Source code in src/openaivec/_prompt.py
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
get_object ¶
Return the underlying FewShotPrompt object.
Returns:
Name | Type | Description |
---|---|---|
FewShotPrompt |
FewShotPrompt
|
The validated prompt object. |
build ¶
Build and return the prompt as XML.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
XML representation of the prompt. |
build_json ¶
Build and return the prompt as a JSON string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Keyword arguments forwarded to Pydantic's |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
JSON representation of the prompt. |
Source code in src/openaivec/_prompt.py
build_xml ¶
Alias for meth:
build
for explicit XML generation.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
XML representation of the prompt. |