|
| 1 | +--- |
| 2 | +title: Baseten |
| 3 | +pcx_content_type: get-started |
| 4 | +--- |
| 5 | + |
| 6 | +import { Render, Tabs, TabItem } from "~/components"; |
| 7 | + |
| 8 | +[Baseten](https://www.baseten.co/) provides infrastructure for building and deploying machine learning models at scale. Baseten offers access to various language models through a unified chat completions API. |
| 9 | + |
| 10 | +## Endpoint |
| 11 | + |
| 12 | +```txt |
| 13 | +https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/baseten |
| 14 | +``` |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +When making requests to Baseten, ensure you have the following: |
| 19 | + |
| 20 | +- Your AI Gateway Account ID. |
| 21 | +- Your AI Gateway gateway name. |
| 22 | +- An active Baseten API token. |
| 23 | +- The name of the Baseten model you want to use. |
| 24 | + |
| 25 | +## OpenAI-compatible chat completions API |
| 26 | + |
| 27 | +Baseten provides an OpenAI-compatible chat completions API for supported models. |
| 28 | + |
| 29 | +### cURL |
| 30 | + |
| 31 | +```bash title="Example fetch request" |
| 32 | +curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/baseten/v1/chat/completions \ |
| 33 | + --header 'Authorization: Bearer {baseten_api_token}' \ |
| 34 | + --header 'Content-Type: application/json' \ |
| 35 | + --data '{ |
| 36 | + "model": "openai/gpt-oss-120b", |
| 37 | + "messages": [ |
| 38 | + { |
| 39 | + "role": "user", |
| 40 | + "content": "What is Cloudflare?" |
| 41 | + } |
| 42 | + ] |
| 43 | + }' |
| 44 | +``` |
| 45 | + |
| 46 | +### Use OpenAI SDK with JavaScript |
| 47 | + |
| 48 | +<Tabs syncKey="sdk"> |
| 49 | +<TabItem label="Javascript" icon="seti:javascript"> |
| 50 | + |
| 51 | +```js title="JavaScript" |
| 52 | +import OpenAI from "openai"; |
| 53 | + |
| 54 | +const apiKey = "{baseten_api_token}"; |
| 55 | +const accountId = "{account_id}"; |
| 56 | +const gatewayId = "{gateway_id}"; |
| 57 | +const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/baseten`; |
| 58 | + |
| 59 | +const openai = new OpenAI({ |
| 60 | + apiKey, |
| 61 | + baseURL, |
| 62 | +}); |
| 63 | + |
| 64 | +const model = "openai/gpt-oss-120b"; |
| 65 | +const messages = [{ role: "user", content: "What is Cloudflare?" }]; |
| 66 | + |
| 67 | +const chatCompletion = await openai.chat.completions.create({ |
| 68 | + model, |
| 69 | + messages, |
| 70 | +}); |
| 71 | + |
| 72 | +console.log(chatCompletion); |
| 73 | +``` |
| 74 | + |
| 75 | +</TabItem> |
| 76 | +<TabItem label="Javascript (Workers)" icon="seti:javascript"> |
| 77 | + |
| 78 | +```js title="JavaScript" |
| 79 | +import OpenAI from "openai"; |
| 80 | +import { useAIGateway, ProviderAuth } from "@cloudflare/ai-gateway"; |
| 81 | + |
| 82 | +export default { |
| 83 | + async fetch(request, env) { |
| 84 | + useAIGateway({ binding: env.AI, gateway: "test-gateway" }); |
| 85 | + |
| 86 | + const unifiedAuth = ProviderAuth.unifiedBilling(); |
| 87 | + |
| 88 | + const openai = new OpenAI({ |
| 89 | + apiKey: unifiedAuth, |
| 90 | + baseURL: "https://api.baseten.co", |
| 91 | + }); |
| 92 | + |
| 93 | + const model = "openai/gpt-oss-120b"; |
| 94 | + const messages = [{ role: "user", content: "What is Cloudflare?" }]; |
| 95 | + |
| 96 | + const chatCompletion = await openai.chat.completions.create({ |
| 97 | + model, |
| 98 | + messages, |
| 99 | + }); |
| 100 | + |
| 101 | + return Response.json(chatCompletion); |
| 102 | + }, |
| 103 | +}; |
| 104 | +``` |
| 105 | + |
| 106 | +</TabItem> |
| 107 | +</Tabs> |
| 108 | + |
| 109 | +<Render |
| 110 | + file="chat-completions-providers" |
| 111 | + product="ai-gateway" |
| 112 | + params={{ |
| 113 | + name: "Baseten", |
| 114 | + jsonexample: ` |
| 115 | +{ |
| 116 | + "model": "baseten/{model}" |
| 117 | +}` |
| 118 | + }} |
| 119 | +/> |
| 120 | + |
| 121 | +## Model-specific endpoints |
| 122 | + |
| 123 | +For models that don't use the OpenAI-compatible API, you can access them through their specific model endpoints. |
| 124 | + |
| 125 | +### cURL |
| 126 | + |
| 127 | +```bash title="Example fetch request" |
| 128 | +curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/baseten/model/{model_id} \ |
| 129 | + --header 'Authorization: Bearer {baseten_api_token}' \ |
| 130 | + --header 'Content-Type: application/json' \ |
| 131 | + --data '{ |
| 132 | + "prompt": "What is Cloudflare?", |
| 133 | + "max_tokens": 100 |
| 134 | + }' |
| 135 | +``` |
| 136 | + |
| 137 | +### Use with JavaScript |
| 138 | + |
| 139 | +<Tabs syncKey="sdk"> |
| 140 | +<TabItem label="Javascript" icon="seti:javascript"> |
| 141 | + |
| 142 | +```js title="JavaScript" |
| 143 | +const accountId = "{account_id}"; |
| 144 | +const gatewayId = "{gateway_id}"; |
| 145 | +const basetenApiToken = "{baseten_api_token}"; |
| 146 | +const modelId = "{model_id}"; |
| 147 | +const baseURL = `https://gateway.ai.cloudflare.com/v1/${accountId}/${gatewayId}/baseten`; |
| 148 | + |
| 149 | +const response = await fetch(`${baseURL}/model/${modelId}`, { |
| 150 | + method: "POST", |
| 151 | + headers: { |
| 152 | + "Authorization": `Bearer ${basetenApiToken}`, |
| 153 | + "Content-Type": "application/json", |
| 154 | + }, |
| 155 | + body: JSON.stringify({ |
| 156 | + prompt: "What is Cloudflare?", |
| 157 | + max_tokens: 100, |
| 158 | + }), |
| 159 | +}); |
| 160 | + |
| 161 | +const result = await response.json(); |
| 162 | +console.log(result); |
| 163 | +``` |
| 164 | + |
| 165 | +</TabItem> |
| 166 | +<TabItem label="Javascript (Workers)" icon="seti:javascript"> |
| 167 | + |
| 168 | +```js title="JavaScript (Workers)" |
| 169 | +import { useAIGateway, ProviderAuth } from "@cloudflare/ai-gateway"; |
| 170 | + |
| 171 | +export default { |
| 172 | + async fetch(request, env) { |
| 173 | + useAIGateway({ binding: env.AI, gateway: "test-gateway" }); |
| 174 | + |
| 175 | + const unifiedAuth = ProviderAuth.unifiedBilling(); |
| 176 | + const modelId = "{model_id}"; |
| 177 | + |
| 178 | + const response = await fetch(`https://model-${modelId}.api.baseten.co/production/predict`, { |
| 179 | + method: "POST", |
| 180 | + headers: { |
| 181 | + "Authorization": unifiedAuth, |
| 182 | + "Content-Type": "application/json", |
| 183 | + }, |
| 184 | + body: JSON.stringify({ |
| 185 | + prompt: "What is Cloudflare?", |
| 186 | + max_tokens: 100, |
| 187 | + }), |
| 188 | + }); |
| 189 | + |
| 190 | + const result = await response.json(); |
| 191 | + return Response.json(result); |
| 192 | + }, |
| 193 | +}; |
| 194 | +``` |
| 195 | + |
| 196 | +</TabItem> |
| 197 | +</Tabs> |
0 commit comments