|
1 | | -# Replicate Python API library |
| 1 | +# Replicate Python API SDK |
2 | 2 |
|
3 | 3 | <!-- prettier-ignore --> |
4 | 4 | [)](https://pypi.org/project/replicate/) |
5 | 5 |
|
6 | | -The Replicate Python library provides convenient access to the Replicate REST API from any Python 3.8+ |
7 | | -application. The library includes type definitions for all request params and response fields, |
8 | | -and offers both synchronous and asynchronous clients powered by [httpx](https://github.com/encode/httpx). |
| 6 | +This is the repo for Replicate's official v2 Python SDK, which provides access to Replicate's HTTP API from any Python 3.8+ |
| 7 | +application. |
9 | 8 |
|
10 | | -It is generated with [Stainless](https://www.stainless.com/). |
| 9 | +## Docs |
11 | 10 |
|
12 | | -## Documentation |
| 11 | +- https://sdks.replicate.com/python |
| 12 | +- https://replicate.com/docs/reference/http |
13 | 13 |
|
14 | | -The REST API documentation can be found on [replicate.com](https://replicate.com/docs/reference/http). The full API of this library can be found in [api.md](api.md). |
15 | 14 |
|
16 | 15 | ## Installation |
17 | 16 |
|
| 17 | +The [`replicate`](https://pypi.org/project/replicate/) package is available on PyPI. Install it with [pip](https://pip.pypa.io/en/stable/): |
| 18 | + |
18 | 19 | ```sh |
19 | | -# install from PyPI |
20 | 20 | pip install --pre replicate |
21 | 21 | ``` |
22 | 22 |
|
23 | 23 | ## Usage |
24 | 24 |
|
25 | | -The full API of this library can be found in [api.md](api.md). |
| 25 | +Start by getting a [Replicate API token](https://replicate.com/account/api-tokens), then set it as `REPLICATE_API_TOKEN` in your environment: |
| 26 | + |
| 27 | +```sh |
| 28 | +export REPLICATE_API_TOKEN="r8_..." |
| 29 | +``` |
| 30 | + |
| 31 | +Then in your Python code, import the library and use it: |
| 32 | + |
| 33 | +```python |
| 34 | +import replicate |
| 35 | + |
| 36 | +claude = replicate.use("anthropic/claude-4.5-sonnet") |
| 37 | +seedream = replicate.use("bytedance/seedream-4") |
| 38 | +veo = replicate.use("google/veo-3-fast") |
| 39 | + |
| 40 | +# Enhance a simple prompt |
| 41 | +image_prompt = claude(prompt="bananas wearing cowboy hats", system_prompt="turn prompts into image prompts") |
| 42 | + |
| 43 | +# Generate an image from the enhanced prompt |
| 44 | +images = seedream(prompt=image_prompt) |
| 45 | + |
| 46 | +# Generate a video from the image |
| 47 | +video = veo(prompt="dancing bananas", image_input=images[0]) |
| 48 | + |
| 49 | +open(video) |
| 50 | +``` |
| 51 | + |
| 52 | +### Initialization and authentication |
| 53 | + |
| 54 | +The library uses the `REPLICATE_API_TOKEN` environment variable by default to implicitly initialize a client, but you can also initialize a client explicitly and set the `bearer_token` parameter: |
26 | 55 |
|
27 | 56 | ```python |
28 | 57 | import os |
29 | 58 | from replicate import Replicate |
30 | 59 |
|
31 | | -replicate = Replicate( |
32 | | - bearer_token=os.environ.get("REPLICATE_API_TOKEN"), # This is the default and can be omitted |
33 | | -) |
34 | | - |
35 | | -prediction = replicate.predictions.get( |
36 | | - prediction_id="gm3qorzdhgbfurvjtvhg6dckhu", |
| 60 | +client = Replicate( |
| 61 | + bearer_token=os.environ.get("REPLICATE_API_TOKEN") |
37 | 62 | ) |
38 | | -print(prediction.id) |
39 | 63 | ``` |
40 | 64 |
|
41 | | -While you can provide a `bearer_token` keyword argument, |
42 | | -we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/) |
43 | | -to add `REPLICATE_API_TOKEN="My Bearer Token"` to your `.env` file |
44 | | -so that your Bearer Token is not stored in source control. |
45 | | - |
46 | 65 | ## Run a model |
47 | 66 |
|
48 | 67 | You can run a model synchronously using `replicate.run()`: |
|
0 commit comments