Skip to content

Commit

Permalink
Merge pull request #168 from narengogi/feat/replicate
Browse files Browse the repository at this point in the history
Add Replicate integration documentation
  • Loading branch information
vrushankportkey authored Jan 30, 2025
2 parents 36a8753 + 35b58dd commit df0dae6
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 2 deletions.
181 changes: 181 additions & 0 deletions integrations/llms/replicate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
---
title: "Replicate"
---

[Replicate](https://replicate.com/) is a platform for building and running machine learning models.

Replicate does not have a standarized JSON body format for their inference API, hence it is not possible to use unified API to interact with Replicate.
Portkey instead provides a proxy to Replicate, allowing you to use virtual keys and observability features.

## Portkey SDK Integration with Replicate

To integrate Replicate with Portkey:

### 1\. Install the Portkey SDK

Add the Portkey SDK to your application to interact with Replicate through Portkey's gateway.

<Tabs>
<Tab title="NodeJS">
```sh
npm install --save portkey-ai
```
</Tab>
<Tab title="Python">
```sh
pip install portkey-ai
```
</Tab>
</Tabs>

### 2\. Initialize Portkey with a Virtual Key

To use Replicate with Portkey, get your Replicate API key from [here](https://replicate.com/account/api-tokens), then add it to Portkey to create your [Replicate virtual key](/product/ai-gateway/virtual-keys#using-virtual-keys).

<Tabs>
<Tab title="NodeJS SDK">

```js
import Portkey from 'portkey-ai'

const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"]
virtualKey: "VIRTUAL_KEY" // Your Replicate Virtual Key
})
```

</Tab>
<Tab title="Python SDK">

```python
from portkey_ai import Portkey

portkey = Portkey(
api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
virtual_key="VIRTUAL_KEY" # Replace with your virtual key for Replicate
)
```
</Tab>
<Tab title="OpenAI Python SDK">

```python
from openai import OpenAI

from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

client = OpenAI(
api_key="REPLICATE_API_KEY",
base_url=PORTKEY_GATEWAY_URL,
default_headers=createHeaders(
api_key="PORTKEY_API_KEY",
provider="replicate"
)
)
```

</Tab>
<Tab title="OpenAI Node SDK">

```js
import OpenAI from "openai";

import { PORTKEY_GATEWAY_URL, createHeaders } from "portkey-ai";

const client = new OpenAI({
apiKey: "REPLICATE_API_KEY",
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({
provider: "replicate",
apiKey: "PORTKEY_API_KEY",
}),
});
```
</Tab>
</Tabs>

### 3\. Use the Portkey SDK to interact with Replicate

<Tabs>
<Tab title="Python">
```python
from portkey_ai import Portkey

portkey = Portkey(
api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
virtual_key="REPLICATE_VIRTUAL_KEY",
)

response = portkey.post(
url="predictions", # Replace with the endpoint you want to call
)

print(response)
```
</Tab>
<Tab title="NodeJS">
```javascript
import Portkey from 'portkey-ai';

// Initialize the Portkey client
const portkey = new Portkey({
apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key
virtualKey: "REPLICATE_VIRTUAL_KEY", // Add your Replicate's virtual key
});

response = portkey.post(
url="predictions", # Replace with the endpoint you want to call
)

print(response)
```
</Tab>
<Tab title="OpenAI NodeJS">
```javascript
import OpenAI from 'openai'; // We're using the v4 SDK
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'

const openai = new OpenAI({
apiKey: 'REPLICATE_API_KEY', // defaults to process.env["OPENAI_API_KEY"],
baseURL: PORTKEY_GATEWAY_URL,
defaultHeaders: createHeaders({
virtualKey: "REPLICATE_VIRTUAL_KEY",
apiKey: "PORTKEY_API_KEY", // defaults to process.env["PORTKEY_API_KEY"]
})
});

response = openai.post(
url="predictions", # Replace with the endpoint you want to call
)

print(response)
```
</Tab>
<Tab title="OpenAI Python">
```python
from openai import OpenAI
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders

openai = OpenAI(
api_key='REPLICATE_API_KEY',
base_url=PORTKEY_GATEWAY_URL,
default_headers=createHeaders(
provider="replicate",
api_key="PORTKEY_API_KEY"
)
)

response = openai.post(
url="predictions", # Replace with the endpoint you want to call
)

print(response)
```
</Tab>
<Tab title="cURL">
```sh
curl --location --request POST 'https://api.portkey.ai/v1/predictions' \
--header 'x-portkey-virtual-key: REPLICATE_VIRTUAL_KEY' \
--header 'x-portkey-api-key: PORTKEY_API_KEY'
```
</Tab>
</Tabs>
7 changes: 5 additions & 2 deletions integrations/vector-databases/milvus.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ client = OpenAI(
base_url=PORTKEY_GATEWAY_URL,
default_headers=createHeaders(
api_key="PORTKEY_API_KEY",
provider="milvus"
provider="milvus",
custom_host="MILVUS_HOST" # Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com
)
)
```
Expand All @@ -87,6 +88,7 @@ const client = new OpenAI({
defaultHeaders: createHeaders({
provider: "milvus",
apiKey: "PORTKEY_API_KEY",
customHost: "MILVUS_HOST" # Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com
}),
});
```
Expand Down Expand Up @@ -163,7 +165,8 @@ print(response)
base_url=PORTKEY_GATEWAY_URL,
default_headers=createHeaders(
provider="milvus",
api_key="PORTKEY_API_KEY"
api_key="PORTKEY_API_KEY",
custom_host="MILVUS_HOST" # Replace with your Milvus host example: https://in03-34d7b37f7ee12c7.serverless.gcp-us-west1.cloud.zilliz.com
)
)

Expand Down
1 change: 1 addition & 0 deletions mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
"integrations/llms/workers-ai",
"integrations/llms/x-ai",
"integrations/llms/zhipu",
"integrations/llms/replicate",
"integrations/llms/suggest-a-new-integration"
]
},
Expand Down

0 comments on commit df0dae6

Please sign in to comment.