Skip to content

Commit 2ee1561

Browse files
image generation using workers AI
1 parent 949a759 commit 2ee1561

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

integrations/llms/workers-ai.mdx

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,128 @@ Use the Portkey instance to send requests to Workers AI. You can also override t
9191

9292

9393

94+
### **4\. Invoke Image Generation with** Workers AI
95+
96+
Use the Portkey instance to send requests to Workers AI. You can also override the virtual key directly in the API call if needed.
97+
98+
Portkey supports the OpenAI signature to make text-to-image requests.
99+
100+
<Tabs>
101+
<Tab title="NodeJS">
102+
```js
103+
import Portkey from 'portkey-ai';
104+
105+
// Initialize the Portkey client
106+
const portkey = new Portkey({
107+
apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key
108+
virtualKey: "VIRTUAL_KEY" // Add your provider's virtual key
109+
});
110+
111+
async function main() {
112+
const image = await portkey.images.generate({
113+
model: "image_model_name",
114+
prompt: "Lucy in the sky with diamonds"
115+
});
116+
117+
console.log(image.data);
118+
}
119+
120+
main();
121+
```
122+
</Tab>
123+
<Tab title="Python">
124+
125+
```py
126+
from portkey_ai import Portkey
127+
from IPython.display import display, Image
128+
129+
# Initialize the Portkey client
130+
portkey = Portkey(
131+
api_key="PORTKEY_API_KEY", # Replace with your Portkey API key
132+
virtual_key="VIRTUAL_KEY" # Add your provider's virtual key
133+
)
134+
135+
image = portkey.images.generate(
136+
model="image_model_name",
137+
prompt="Lucy in the sky with diamonds"
138+
)
139+
140+
# Display the image
141+
display(Image(url=image.data[0].url))
142+
```
143+
</Tab>
144+
<Tab title="OpenAI NodeJS">
145+
146+
```js
147+
import OpenAI from 'openai'; // We're using the v4 SDK
148+
import { PORTKEY_GATEWAY_URL, createHeaders } from 'portkey-ai'
149+
150+
const openai = new OpenAI({
151+
apiKey: 'WORKERS_AI_API_KEY', // defaults to process.env["WORKERS_AI_API_KEY"],
152+
baseURL: PORTKEY_GATEWAY_URL,
153+
defaultHeaders: createHeaders({
154+
provider: "openai",
155+
apiKey: "PORTKEY_API_KEY" // defaults to process.env["PORTKEY_API_KEY"]
156+
})
157+
});
158+
159+
async function main() {
160+
const image = await openai.images.generate({
161+
model: "image_model_name",
162+
prompt: "Lucy in the sky with diamonds"
163+
});
164+
165+
console.log(image.data);
166+
}
167+
168+
main();
169+
```
170+
</Tab>
171+
<Tab title="OpenAI Python">
172+
173+
```py
174+
from openai import OpenAI
175+
from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders
176+
from IPython.display import display, Image
177+
178+
client = OpenAI(
179+
api_key='WORKERS_AI_API_KEY',
180+
base_url=PORTKEY_GATEWAY_URL,
181+
default_headers=createHeaders(
182+
provider="openai",
183+
api_key="PORTKEY_API_KEY"
184+
)
185+
)
186+
187+
image = client.images.generate(
188+
model="image_model_name",
189+
prompt="Lucy in the sky with diamonds",
190+
n=1,
191+
size="1024x1024"
192+
)
193+
194+
# Display the image
195+
display(Image(url=image.data[0].url))
196+
```
197+
</Tab>
198+
<Tab title="cURL">
199+
200+
```sh
201+
curl "https://api.portkey.ai/v1/images/generations" \
202+
-H "Content-Type: application/json" \
203+
-H "x-portkey-api-key: $PORTKEY_API_KEY" \
204+
-H "x-portkey-virtual-key: $WORKERS_AI_VIRTUAL_KEY" \
205+
-d '{
206+
"model": "image_model_name",
207+
"prompt": "Lucy in the sky with diamonds"
208+
}'
209+
```
210+
</Tab>
211+
</Tabs>
212+
213+
214+
215+
94216
## Managing Workers AI Prompts
95217

96218
You can manage all prompts to Workers AI in the [Prompt Library](/product/prompt-library). All the current models of Workers AI are supported and you can easily start testing different prompts.

0 commit comments

Comments
 (0)