Skip to content

Commit 3fb2024

Browse files
committed
Document use() API in README.md
1 parent 3b5200b commit 3fb2024

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

README.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,153 @@ output2 = flux_dev(prompt="str") # output2 will be typed as `str`
710710
711711
In future we hope to provide tooling to generate and provide these models as packages to make working with them easier. For now you may wish to create your own.
712712

713+
### API Reference
714+
715+
The Replicate Python Library provides several key classes and functions for working with models in pipelines:
716+
717+
#### `use()` Function
718+
719+
Creates a callable function wrapper for a Replicate model.
720+
721+
```py
722+
def use(
723+
ref: FunctionRef,
724+
*,
725+
streaming: bool = False,
726+
use_async: bool = False
727+
) -> Function | AsyncFunction
728+
729+
def use(
730+
ref: str,
731+
*,
732+
hint: Callable | None = None,
733+
streaming: bool = False,
734+
use_async: bool = False
735+
) -> Function | AsyncFunction
736+
```
737+
738+
**Parameters:**
739+
740+
| Parameter | Type | Default | Description |
741+
|-----------|------|---------|-------------|
742+
| `ref` | `str \| FunctionRef` | Required | Model reference (e.g., "owner/model" or "owner/model:version") |
743+
| `hint` | `Callable \| None` | `None` | Function signature for type hints |
744+
| `streaming` | `bool` | `False` | Return OutputIterator for streaming results |
745+
| `use_async` | `bool` | `False` | Return AsyncFunction instead of Function |
746+
747+
**Returns:**
748+
- `Function` - Synchronous model wrapper (default)
749+
- `AsyncFunction` - Asynchronous model wrapper (when `use_async=True`)
750+
751+
#### `Function` Class
752+
753+
A synchronous wrapper for calling Replicate models.
754+
755+
**Methods:**
756+
757+
| Method | Signature | Description |
758+
|--------|-----------|-------------|
759+
| `__call__()` | `(*args, **inputs) -> Output` | Execute the model and return final output |
760+
| `create()` | `(*args, **inputs) -> Run` | Start a prediction and return Run object |
761+
762+
**Properties:**
763+
764+
| Property | Type | Description |
765+
|----------|------|-------------|
766+
| `openapi_schema` | `dict` | Model's OpenAPI schema for inputs/outputs |
767+
| `default_example` | `dict \| None` | Default example inputs (not yet implemented) |
768+
769+
#### `AsyncFunction` Class
770+
771+
An asynchronous wrapper for calling Replicate models.
772+
773+
**Methods:**
774+
775+
| Method | Signature | Description |
776+
|--------|-----------|-------------|
777+
| `__call__()` | `async (*args, **inputs) -> Output` | Execute the model and return final output |
778+
| `create()` | `async (*args, **inputs) -> AsyncRun` | Start a prediction and return AsyncRun object |
779+
780+
**Properties:**
781+
782+
| Property | Type | Description |
783+
|----------|------|-------------|
784+
| `openapi_schema()` | `async () -> dict` | Model's OpenAPI schema for inputs/outputs |
785+
| `default_example` | `dict \| None` | Default example inputs (not yet implemented) |
786+
787+
#### `Run` Class
788+
789+
Represents a running prediction with access to output and logs.
790+
791+
**Methods:**
792+
793+
| Method | Signature | Description |
794+
|--------|-----------|-------------|
795+
| `output()` | `() -> Output` | Get prediction output (blocks until complete) |
796+
| `logs()` | `() -> str \| None` | Get current prediction logs |
797+
798+
**Behavior:**
799+
- When `streaming=True`: Returns `OutputIterator` immediately
800+
- When `streaming=False`: Waits for completion and returns final result
801+
802+
#### `AsyncRun` Class
803+
804+
Asynchronous version of Run for async model calls.
805+
806+
**Methods:**
807+
808+
| Method | Signature | Description |
809+
|--------|-----------|-------------|
810+
| `output()` | `async () -> Output` | Get prediction output (awaits completion) |
811+
| `logs()` | `async () -> str \| None` | Get current prediction logs |
812+
813+
#### `OutputIterator` Class
814+
815+
Iterator wrapper for streaming model outputs.
816+
817+
**Methods:**
818+
819+
| Method | Signature | Description |
820+
|--------|-----------|-------------|
821+
| `__iter__()` | `() -> Iterator[T]` | Synchronous iteration over output chunks |
822+
| `__aiter__()` | `() -> AsyncIterator[T]` | Asynchronous iteration over output chunks |
823+
| `__str__()` | `() -> str` | Convert to string (concatenated or list representation) |
824+
| `__await__()` | `() -> List[T] \| str` | Await all results (string for concatenate, list otherwise) |
825+
826+
#### `URLPath` Class
827+
828+
A path-like object that downloads files on first access.
829+
830+
**Methods:**
831+
832+
| Method | Signature | Description |
833+
|--------|-----------|-------------|
834+
| `__fspath__()` | `() -> str` | Get local file path (downloads if needed) |
835+
| `__str__()` | `() -> str` | String representation of local path |
836+
837+
**Usage:**
838+
- Compatible with `open()`, `pathlib.Path()`, and most file operations
839+
- Downloads file automatically on first filesystem access
840+
- Cached locally in temporary directory
841+
842+
#### `get_path_url()` Function
843+
844+
Helper function to extract original URLs from `URLPath` objects.
845+
846+
```py
847+
def get_path_url(path: Any) -> str | None
848+
```
849+
850+
**Parameters:**
851+
852+
| Parameter | Type | Description |
853+
|-----------|------|-------------|
854+
| `path` | `Any` | Path object (typically `URLPath`) |
855+
856+
**Returns:**
857+
- `str` - Original URL if path is a `URLPath`
858+
- `None` - If path is not a `URLPath` or has no URL
859+
713860
### TODO
714861

715862
There are several key things still outstanding:

0 commit comments

Comments
 (0)