Skip to content

Commit ae1589f

Browse files
committed
Document the use() functionality
1 parent bae5dc8 commit ae1589f

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,106 @@ replicate = Client(
503503
> Never hardcode authentication credentials like API tokens into your code.
504504
> Instead, pass them as environment variables when running your program.
505505
506+
## Experimental `use()` interface
507+
508+
The latest versions of `replicate >= 1.0.8` include a new experimental `use()` function that is intended to make running a model closer to calling a function rather than an API request.
509+
510+
Some key differences to `replicate.run()`.
511+
512+
1. You "import" the model using the `use()` syntax, after that you call the model like a function.
513+
2. The output type matches the model definition. i.e. if the model uses an iterator output will be an iterator.
514+
3. Files will be downloaded output as `Path` objects*.
515+
516+
> [!NOTE]
517+
518+
\* We've replaced the `FileOutput` implementation with `Path` objects. However to avoid unnecessary downloading of files until they are needed we've implemented a `PathProxy` class that will defer the download until the first time the object is used. If you need the underlying URL of the `Path` object you can use the `get_path_url(path: Path) -> str` helper.
519+
520+
### Examples
521+
522+
To use a model:
523+
524+
> [!IMPORTANT]
525+
> For now `use()` MUST be called in the top level module scope. We may relax this in future.
526+
527+
```py
528+
from replicate import use
529+
530+
flux_dev = use("black-forest-labs/flux-dev")
531+
outputs = flux_dev(prompt="a cat wearing an amusing hat")
532+
533+
for output in outputs:
534+
print(output) # Path(/tmp/output.webp)
535+
```
536+
537+
Models that output iterators will return iterators:
538+
539+
540+
```py
541+
claude = use("anthropic/claude-4-sonnet")
542+
543+
output = claude(prompt="Give me a recipe for tasty smashed avocado on sourdough toast that could feed all of California.")
544+
545+
for token in output:
546+
print(token) # "Here's a recipe"
547+
```
548+
549+
You can call `str()` on a language model to get the full output when done rather than iterating over tokens:
550+
551+
```py
552+
str(output) # "Here's a recipe to feed all of California (about 39 million people)! ..."
553+
```
554+
555+
You can pass the results of one model directly into another:
556+
557+
```py
558+
from replicate import use
559+
560+
flux_dev = use("black-forest-labs/flux-dev")
561+
claude = use("anthropic/claude-4-sonnet")
562+
563+
images = flux_dev(prompt="a cat wearing an amusing hat")
564+
565+
result = claude(prompt="describe this image for me", image=images[0])
566+
567+
print(str(result)) # "This shows an image of a cat wearing a hat ..."
568+
```
569+
570+
To create an individual prediction that has not yet resolved, use the `create()` method:
571+
572+
```
573+
claude = use("anthropic/claude-4-sonnet")
574+
575+
prediction = claude.create(prompt="Give me a recipe for tasty smashed avocado on sourdough toast that could feed all of California.")
576+
577+
prediction.logs() # get current logs (WIP)
578+
579+
prediction.output() # get the output
580+
```
581+
582+
You can access the underlying URL for a Path object returned from a model call by using the `get_path_url()` helper.
583+
584+
```py
585+
from replicate import use
586+
from replicate.use import get_url_path
587+
588+
flux_dev = use("black-forest-labs/flux-dev")
589+
outputs = flux_dev(prompt="a cat wearing an amusing hat")
590+
591+
for output in outputs:
592+
print(get_url_path(output)) # "https://replicate.delivery/xyz"
593+
```
594+
595+
### TODO
596+
597+
There are several key things still outstanding:
598+
599+
1. Support for asyncio.
600+
2. Support for typing the return value.
601+
3. Support for streaming text when available (rather than polling)
602+
4. Support for streaming files when available (rather than polling)
603+
5. Support for cleaning up downloaded files.
604+
6. Support for streaming logs using `OutputIterator`.
605+
506606
## Development
507607

508608
See [CONTRIBUTING.md](CONTRIBUTING.md)

0 commit comments

Comments
 (0)