Utils
docculus.utils ¶
Shared helper utilities.
docculus.utils.fake ¶
Contain functions to generate fake documents.
It is designed to be used for testing and debugging purposes.
docculus.utils.fake.generate_fake_documents ¶
generate_fake_documents(
n: int = 5,
seed: int | None = None,
nb_sentences: int = 5,
) -> list[Document]
Generate synthetic LangChain Documents with Faker-generated content.
Each document gets a unique id ("doc-{i}"), a Faker-generated
paragraph as its content, and metadata containing a fake author name
and a single-word topic.
Note
Content is not guaranteed to be unique: Faker.paragraph()
has no built-in uniqueness constraint, so with a large enough
n (or a small enough nb_sentences), two documents could
coincidentally receive identical text. If strict uniqueness
matters for your use case, deduplicate the result or use
Faker's unique proxy (e.g. fake.unique.paragraph(...),
which raises once its internal pool is exhausted).
Warning
Faker.seed() seeds Faker's shared, process-wide random
generator, not just the local fake instance created here.
Passing seed therefore affects the reproducibility of any
other Faker() instance used elsewhere in the same process
after this function runs, not only calls made through this
function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of documents to generate. Must be non-negative;
|
5
|
seed
|
int | None
|
Optional seed for reproducible output. If |
None
|
nb_sentences
|
int
|
Number of sentences per document's generated
paragraph, passed through to |
5
|
Returns:
| Type | Description |
|---|---|
list[Document]
|
A list of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
RuntimeError
|
If the optional |
Example
>>> from docculus.utils.fake import generate_fake_documents
>>> docs = generate_fake_documents(n=3, seed=42)
>>> len(docs)
3
>>> [doc.id for doc in docs]
['doc-0', 'doc-1', 'doc-2']
>>> docs2 = generate_fake_documents(n=3, seed=42)
>>> [doc.page_content for doc in docs] == [doc.page_content for doc in docs2]
True
docculus.utils.imports ¶
Helpers to detect and validate optional dependencies.
docculus.utils.imports.check_faker ¶
check_faker() -> None
Check if the faker package is installed.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the |
Example
>>> from docculus.utils.imports import check_faker
>>> check_faker()
docculus.utils.imports.check_persista ¶
check_persista() -> None
Check if the persista package is installed.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if the |
Example
>>> from docculus.utils.imports import check_persista
>>> check_persista()
docculus.utils.imports.faker_available ¶
faker_available(fn: F) -> F
Implement a decorator to execute a function only if faker
package is installed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
F
|
The function to execute. |
required |
Returns:
| Type | Description |
|---|---|
F
|
A wrapper around |
Example
>>> from docculus.utils.imports import faker_available
>>> @faker_available
... def my_function(n: int = 0) -> int:
... return 42 + n
...
>>> my_function()
docculus.utils.imports.is_faker_available
cached
¶
is_faker_available() -> bool
Indicate if the faker package is installed or not.
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from docculus.utils.imports import is_faker_available
>>> is_faker_available()
docculus.utils.imports.is_persista_available
cached
¶
is_persista_available() -> bool
Indicate if the persista package is installed or not.
Returns:
| Type | Description |
|---|---|
bool
|
|
Example
>>> from docculus.utils.imports import is_persista_available
>>> is_persista_available()
docculus.utils.imports.persista_available ¶
persista_available(fn: F) -> F
Implement a decorator to execute a function only if persista
package is installed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
F
|
The function to execute. |
required |
Returns:
| Type | Description |
|---|---|
F
|
A wrapper around |
Example
>>> from docculus.utils.imports import persista_available
>>> @persista_available
... def my_function(n: int = 0) -> int:
... return 42 + n
...
>>> my_function()
docculus.utils.imports.raise_faker_missing_error ¶
raise_faker_missing_error() -> NoReturn
Raise a RuntimeError to indicate the faker package is
missing.
docculus.utils.imports.raise_persista_missing_error ¶
raise_persista_missing_error() -> NoReturn
Raise a RuntimeError to indicate the persista package is
missing.