|
| 1 | +function generateCodeSample(fetchedModels, modelName) { |
| 2 | + const model = fetchedModels.find((m) => m.name === modelName); |
| 3 | + if (!model) { |
| 4 | + return; |
| 5 | + } |
| 6 | + const additionalParamsMap = { |
| 7 | + chatAudio: { |
| 8 | + python: `, |
| 9 | + "modalities": ["text", "audio"], |
| 10 | + "audio": {"voice": "alloy", "format": "pcm16"}`, |
| 11 | + js: `, |
| 12 | + modalities: ['text', 'audio'], |
| 13 | + audio: { voice: 'alloy', format: 'pcm16' },`, |
| 14 | + }, |
| 15 | + textToImage: { |
| 16 | + python: `, |
| 17 | + "prompt": "A T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses."`, |
| 18 | + js: `, |
| 19 | + prompt: 'A T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses.',`, |
| 20 | + }, |
| 21 | + imageToImage: { |
| 22 | + python: `, |
| 23 | + "prompt": "Add a crown to the T-rex's head.", |
| 24 | + "${ |
| 25 | + modelName.includes('image-to-image') || modelName === 'triposr' |
| 26 | + ? 'image_url' |
| 27 | + : 'image' |
| 28 | + }": "https://raw.githubusercontent.com/aimlapi/api-docs/main/reference-files/t-rex.png"`, |
| 29 | + js: `, |
| 30 | + prompt: "Add a crown to the T-rex's head.", |
| 31 | + ${ |
| 32 | + modelName.includes('image-to-image') || modelName === 'triposr' |
| 33 | + ? 'image_url' |
| 34 | + : 'image' |
| 35 | + }: 'https://raw.githubusercontent.com/aimlapi/api-docs/main/reference-files/t-rex.png',`, |
| 36 | + }, |
| 37 | + }; |
| 38 | + let additionalParamKey; |
| 39 | + if (modelName.includes('-audio-')) { |
| 40 | + additionalParamKey = 'chatAudio'; |
| 41 | + } else if (model.category === 'image-models') { |
| 42 | + additionalParamKey = model.features?.includes('image-to-image') |
| 43 | + ? 'imageToImage' |
| 44 | + : 'textToImage'; |
| 45 | + } |
| 46 | + if (model.category === 'text-models-llm') { |
| 47 | + return [ |
| 48 | + { |
| 49 | + lang: 'JavaScript', |
| 50 | + source: `async function main() { |
| 51 | + try { |
| 52 | + const response = await fetch('https://api.aimlapi.com/v1/chat/completions', { |
| 53 | + method: 'POST', |
| 54 | + headers: { |
| 55 | + 'Authorization': 'Bearer <YOUR_AIMLAPI_KEY>', |
| 56 | + 'Content-Type': 'application/json', |
| 57 | + }, |
| 58 | + body: JSON.stringify({ |
| 59 | + model: '${modelName}', |
| 60 | + messages:[ |
| 61 | + { |
| 62 | + role:'user', |
| 63 | + content: 'Hello' |
| 64 | + } |
| 65 | + ]${additionalParamKey ? additionalParamsMap[additionalParamKey].js : ''} |
| 66 | + }), |
| 67 | + }); |
| 68 | +
|
| 69 | + if (!response.ok) { |
| 70 | + throw new Error('HTTP error!'); |
| 71 | + } |
| 72 | +
|
| 73 | + const data = await response.json(); |
| 74 | + console.log(JSON.stringify(data, null, 2)); |
| 75 | +
|
| 76 | + } catch (error) { |
| 77 | + console.error('Error', error); |
| 78 | + } |
| 79 | +} |
| 80 | +
|
| 81 | +main();`, |
| 82 | + }, |
| 83 | + { |
| 84 | + lang: 'Python', |
| 85 | + source: `import requests |
| 86 | +
|
| 87 | +response = requests.post( |
| 88 | + "https://api.aimlapi.com/v1/chat/completions", |
| 89 | + headers={ |
| 90 | + "Content-Type":"application/json", |
| 91 | + "Authorization":"Bearer <YOUR_AIMLAPI_KEY>", |
| 92 | + }, |
| 93 | + json={ |
| 94 | + "model":"${modelName}", |
| 95 | + "messages":[ |
| 96 | + { |
| 97 | + "role":"user", |
| 98 | + "content":"Hello" |
| 99 | + } |
| 100 | + ]${ |
| 101 | + additionalParamKey |
| 102 | + ? additionalParamsMap[additionalParamKey].python |
| 103 | + : '' |
| 104 | + } |
| 105 | + } |
| 106 | +) |
| 107 | +
|
| 108 | +data = response.json() |
| 109 | +print(data)`, |
| 110 | + }, |
| 111 | + ]; |
| 112 | + } else if (modelName === 'openai/gpt-image-1') { |
| 113 | + return [ |
| 114 | + { |
| 115 | + lang: 'JavaScript', |
| 116 | + source: `import fs from 'fs'; |
| 117 | +import fetch from 'node-fetch'; |
| 118 | +import FormData from 'form-data'; |
| 119 | +
|
| 120 | +async function main() { |
| 121 | + const formData = new FormData(); |
| 122 | + formData.append('model', 'openai/gpt-image-1'); |
| 123 | + formData.append('prompt', "Add a crown to the T-rex's head."); |
| 124 | + formData.append('image', fs.createReadStream('<YOUR_IMAGE_PATH.png>'), { |
| 125 | + filename: '<IMAGE_NAME>', |
| 126 | + contentType: 'image/png', |
| 127 | + }); |
| 128 | +
|
| 129 | + const response = await fetch('https://api.aimlapi.com/v1/images/edits', { |
| 130 | + method: 'POST', |
| 131 | + headers: { |
| 132 | + Authorization: 'Bearer <YOUR_AIMLAPI_KEY>', |
| 133 | + ...formData.getHeaders(), |
| 134 | + }, |
| 135 | + body: formData, |
| 136 | + }); |
| 137 | +
|
| 138 | + const data = await response.json(); |
| 139 | + console.log(data); |
| 140 | +} |
| 141 | +
|
| 142 | +main();`, |
| 143 | + }, |
| 144 | + { |
| 145 | + lang: 'Python', |
| 146 | + source: `import requests |
| 147 | +with open("<YOUR_IMAGE_PATH.png>", "rb") as file: |
| 148 | + files = {"image": ("<IMAGE_NAME>", file, "image/png")} |
| 149 | + response = requests.post( |
| 150 | + "https://api.aimlapi.com/v1/images/edits", |
| 151 | + headers={ |
| 152 | + "Authorization":"Bearer <YOUR_AIMLAPI_KEY>", |
| 153 | + }, |
| 154 | + data={ |
| 155 | + "model":"${modelName}", |
| 156 | + "prompt": "Add a crown to the T-rex's head." |
| 157 | + }, |
| 158 | + files=files |
| 159 | + ) |
| 160 | +
|
| 161 | + data = response.json() |
| 162 | + print(data)`, |
| 163 | + }, |
| 164 | + ]; |
| 165 | + } else if (model.category === 'image-models') { |
| 166 | + return [ |
| 167 | + { |
| 168 | + lang: 'JavaScript', |
| 169 | + source: `async function main() { |
| 170 | + try { |
| 171 | + const response = await fetch('https://api.aimlapi.com/v1/images/generations', { |
| 172 | + method: 'POST', |
| 173 | + headers: { |
| 174 | + 'Authorization': 'Bearer <YOUR_AIMLAPI_KEY>', |
| 175 | + 'Content-Type': 'application/json', |
| 176 | + }, |
| 177 | + body: JSON.stringify({ |
| 178 | + model: '${modelName}'${ |
| 179 | + additionalParamKey ? additionalParamsMap[additionalParamKey].js : '' |
| 180 | + } |
| 181 | + }), |
| 182 | + }); |
| 183 | +
|
| 184 | + if (!response.ok) { |
| 185 | + throw new Error('HTTP error!'); |
| 186 | + } |
| 187 | +
|
| 188 | + const data = await response.json(); |
| 189 | + console.log(JSON.stringify(data, null, 2)); |
| 190 | +
|
| 191 | + } catch (error) { |
| 192 | + console.error('Error', error); |
| 193 | + } |
| 194 | +} |
| 195 | +
|
| 196 | +main();`, |
| 197 | + }, |
| 198 | + { |
| 199 | + lang: 'Python', |
| 200 | + source: `import requests |
| 201 | +
|
| 202 | +response = requests.post( |
| 203 | + "https://api.aimlapi.com/v1/images/generations", |
| 204 | + headers={ |
| 205 | + "Content-Type":"application/json", |
| 206 | + "Authorization":"Bearer <YOUR_AIMLAPI_KEY>", |
| 207 | + }, |
| 208 | + json={ |
| 209 | + "model":"${modelName}"${ |
| 210 | + additionalParamKey |
| 211 | + ? additionalParamsMap[additionalParamKey].python |
| 212 | + : '' |
| 213 | + } |
| 214 | + } |
| 215 | +) |
| 216 | +
|
| 217 | +data = response.json() |
| 218 | +print(data)`, |
| 219 | + }, |
| 220 | + ]; |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +module.exports = generateCodeSample; |
0 commit comments